| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/attachment" |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/imageinput" |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | func writeImageGateConfig(t *testing.T, root string) { |
| 16 | t.Helper() |
| 17 | cfg := config.Default() |
| 18 | cfg.DefaultModel = "custom/mystery" |
| 19 | cfg.Providers = []config.ProviderEntry{ |
| 20 | {Name: "custom", Kind: "openai", BaseURL: "https://example.invalid/v1", Models: []string{"mystery"}}, |
| 21 | {Name: "declared", Kind: "openai", BaseURL: "https://example.invalid/v1", Models: []string{"text-only", "vision-pro"}, VisionModels: []string{"vision-pro"}}, |
| 22 | } |
| 23 | if err := cfg.SaveTo(filepath.Join(root, "reasonix.toml")); err != nil { |
| 24 | t.Fatalf("save config: %v", err) |
| 25 | } |
| 26 | approveWorkspace(t, root) |
| 27 | } |
| 28 | |
| 29 | func TestImageInputIsBlockedOnlyWhenTheModelIsDeclaredTextOnly(t *testing.T) { |
| 30 | root := t.TempDir() |
| 31 | t.Chdir(root) |
| 32 | writeImageGateConfig(t, root) |
| 33 | cases := []struct { |
| 34 | name, ref, fallback string |
| 35 | want bool |
| 36 | }{ |
| 37 | {"undeclared model passes images to the provider", "custom/mystery", "", true}, |
| 38 | {"undeclared model keeps the configured fallback", "custom/mystery", "auto", false}, |
| 39 | {"declared text-only model stays blocked", "declared/text-only", "", false}, |
| 40 | {"declared vision model reads images", "declared/vision-pro", "", true}, |
| 41 | {"declared vision model reads images despite a fallback", "declared/vision-pro", "auto", true}, |
| 42 | } |
| 43 | for _, tc := range cases { |
| 44 | t.Run(tc.name, func(t *testing.T) { |
| 45 | c := &Controller{workspaceRoot: root, selection: modelSelection{ref: tc.ref}, visionModel: tc.fallback} |
| 46 | if got := c.imageInputEnabled(); got != tc.want { |
| 47 | t.Fatalf("imageInputEnabled(%s, fallback %q) = %v, want %v", tc.ref, tc.fallback, got, tc.want) |
| 48 | } |
| 49 | resolved := &Controller{workspaceRoot: root, selection: modelSelection{ref: tc.ref}, visionModel: tc.fallback, |
| 50 | modelCapabilityResolver: config.NewTransientModelCapabilityResolver().Resolve} |
| 51 | if got := resolved.imageInputEnabled(); got != tc.want { |
| 52 | t.Fatalf("resolver imageInputEnabled(%s, fallback %q) = %v, want %v", tc.ref, tc.fallback, got, tc.want) |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestTextOnlyModelWithoutFallbackDoesNotFailOnToolOrHistoricalImages(t *testing.T) { |
| 59 | root := t.TempDir() |
| 60 | c := newOwnedTestController(t, Options{WorkspaceRoot: root, SessionPath: filepath.Join(root, "session.jsonl"), ImageRouteConfig: config.Default()}) |
| 61 | img := []attachment.ImageInput{{Kind: attachment.KindURL, URL: "https://example.invalid/shot.png"}} |
| 62 | messages := []provider.Message{ |
| 63 | {ID: "old", Role: provider.RoleUser, Content: "old turn", ImageInputs: img}, |
| 64 | {ID: "now", Role: provider.RoleUser, Content: "plain text follow-up"}, |
| 65 | {ID: "tool", Role: provider.RoleTool, Content: "screenshot taken", ImageInputs: img}, |
| 66 | } |
| 67 | got, err := c.ResolveRequestImagesForModel(t.Context(), messages, "declared/text-only", false) |
| 68 | if err != nil { |
| 69 | t.Fatalf("a plain-text turn failed because of earlier images: %v", err) |
| 70 | } |
| 71 | for _, i := range []int{0, 2} { |
| 72 | if len(got[i].ImageInputs) != 0 || len(got[i].Images) != 0 { |
| 73 | t.Fatalf("message %d still carries images: %+v", i, got[i]) |
| 74 | } |
| 75 | if !strings.Contains(got[i].Content, "cannot read images") { |
| 76 | t.Fatalf("message %d has no host-owned reason: %q", i, got[i].Content) |
| 77 | } |
| 78 | } |
| 79 | if len(messages[2].ImageInputs) != 1 { |
| 80 | t.Fatal("canonical history was rewritten") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestTextOnlyModelStillRejectsTheCurrentUserImageWithATypedReason(t *testing.T) { |
| 85 | root := t.TempDir() |
| 86 | c := newOwnedTestController(t, Options{WorkspaceRoot: root, SessionPath: filepath.Join(root, "session.jsonl"), ImageRouteConfig: config.Default()}) |
| 87 | messages := []provider.Message{{ID: "now", Role: provider.RoleUser, Content: "look", ImageInputs: []attachment.ImageInput{{Kind: attachment.KindURL, URL: "https://example.invalid/shot.png"}}}} |
| 88 | _, err := c.ResolveRequestImagesForModel(t.Context(), messages, "declared/text-only", false) |
| 89 | if !errors.Is(err, imageinput.ErrNoModel) { |
| 90 | t.Fatalf("err = %v, want imageinput.ErrNoModel", err) |
| 91 | } |
| 92 | } |
| 93 |