| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // Covers the note a resolved @-reference emits per input kind, and both image |
| 12 | // note variants. The capability is pinned because a zero Controller resolves it |
| 13 | // from the ambient config, which would otherwise make the expected note depend |
| 14 | // on the machine running the test. |
| 15 | func TestResolveRefsAttachmentKinds(t *testing.T) { |
| 16 | temp := t.TempDir() |
| 17 | attachmentsDir := filepath.Join(temp, ".reasonix", "attachments") |
| 18 | if err := os.MkdirAll(attachmentsDir, 0o755); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | ymlRef := filepath.ToSlash(".reasonix/attachments/config.yml") |
| 22 | zipRef := filepath.ToSlash(".reasonix/attachments/archive.zip") |
| 23 | pngRef := filepath.ToSlash(".reasonix/attachments/shot.png") |
| 24 | if err := os.WriteFile(filepath.Join(temp, filepath.FromSlash(ymlRef)), []byte("name: reasonix\n"), 0o644); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := os.WriteFile(filepath.Join(temp, filepath.FromSlash(zipRef)), []byte{'P', 'K', 0x03, 0x04, 0x00}, 0o644); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := os.WriteFile(filepath.Join(temp, filepath.FromSlash(pngRef)), []byte("\x89PNG\r\n\x1a\n"), 0o644); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | |
| 34 | oldCwd, err := os.Getwd() |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := os.Chdir(temp); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | t.Cleanup(func() { |
| 42 | if err := os.Chdir(oldCwd); err != nil { |
| 43 | t.Error(err) |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | line := "check @" + ymlRef + " @" + zipRef + " @" + pngRef |
| 48 | for _, tc := range []struct { |
| 49 | name string |
| 50 | vision bool |
| 51 | note string |
| 52 | }{ |
| 53 | {name: "text_only", note: "image bytes are not inlined"}, |
| 54 | {name: "vision", vision: true, note: "attached as visual input"}, |
| 55 | } { |
| 56 | t.Run(tc.name, func(t *testing.T) { |
| 57 | vision := tc.vision |
| 58 | block, errs := (&Controller{frozenImageInput: &vision}).ResolveRefs(context.Background(), line) |
| 59 | if len(errs) != 0 { |
| 60 | t.Fatalf("ResolveRefs errors = %v", errs) |
| 61 | } |
| 62 | if !strings.Contains(block, `<file path="`+ymlRef+`">`) || !strings.Contains(block, "name: reasonix") { |
| 63 | t.Fatalf("expected yml attachment to resolve as file content, got: %s", block) |
| 64 | } |
| 65 | if !strings.Contains(block, `<file path="`+zipRef+`">`) || !strings.Contains(block, "[binary file "+zipRef) { |
| 66 | t.Fatalf("expected zip attachment to resolve as binary file note, got: %s", block) |
| 67 | } |
| 68 | if !strings.Contains(block, `<image path="`+pngRef+`">`) { |
| 69 | t.Fatalf("expected png attachment to resolve as image block, got: %s", block) |
| 70 | } |
| 71 | if !strings.Contains(block, tc.note) { |
| 72 | t.Fatalf("expected image note %q, got: %s", tc.note, block) |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 |