| 💾 Saved. d722711 k33g 4h ago | 1 | package mention |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // A workspace with a file, a nested file and a directory, to mention. |
| 11 | func workspace(t *testing.T) string { |
| 12 | t.Helper() |
| 13 | dir := t.TempDir() |
| 14 | for _, p := range []string{"main.go", "internal/acp/acp.go"} { |
| 15 | if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0o755); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | if err := os.WriteFile(filepath.Join(dir, p), []byte("package x"), 0o644); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | } |
| 22 | return dir |
| 23 | } |
| 24 | |
| 25 | // The text is kept as typed and each existing path becomes one attachment |
| 26 | // line, in order, once — the shape an ACP resource_link takes. |
| 27 | func TestExpandAttachesExistingPathsOnce(t *testing.T) { |
| 28 | dir := workspace(t) |
| 29 | in := "compare @main.go with @internal/acp/acp.go, then @main.go again; and @internal too" |
| 30 | got, att := Expand(in, dir) |
| 31 | |
| 32 | want := in + |
| 33 | "\n[attached file: " + filepath.Join(dir, "main.go") + "]" + |
| 34 | "\n[attached file: " + filepath.Join(dir, "internal/acp/acp.go") + "]" + |
| 35 | "\n[attached directory: " + filepath.Join(dir, "internal") + "]" |
| 36 | if got != want { |
| 37 | t.Errorf("Expand:\n got %q\nwant %q", got, want) |
| 38 | } |
| 39 | if len(att) != 3 || att[0].Mention != "main.go" || att[2].Dir != true || att[1].Dir { |
| 40 | t.Errorf("attachments %+v", att) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // What is NOT a mention: a path that does not exist (a name, a handle), an |
| 45 | // e-mail address (@ glued to a word), a lone @, and the input untouched when |
| 46 | // nothing matches. |
| 47 | func TestExpandLeavesNonPathsAlone(t *testing.T) { |
| 48 | dir := workspace(t) |
| 49 | for _, in := range []string{ |
| 50 | "ask @bob about it", |
| 51 | "mail k33g@example.com", |
| 52 | "a lone @ sign", |
| 53 | "@does/not/exist.go", |
| 54 | "", |
| 55 | } { |
| 56 | got, att := Expand(in, dir) |
| 57 | if got != in || len(att) != 0 { |
| 58 | t.Errorf("Expand(%q) = %q, %+v; want the input unchanged and no attachment", in, got, att) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Absolute paths, ~ paths, and mentions wrapped in brackets or quotes or |
| 64 | // glued to punctuation all resolve to the same clean absolute path. |
| 65 | func TestExpandResolvesAbsoluteHomeAndPunctuated(t *testing.T) { |
| 66 | dir := workspace(t) |
| 67 | t.Setenv("HOME", dir) |
| 68 | abs := filepath.Join(dir, "main.go") |
| 69 | for _, in := range []string{ |
| 70 | "see @" + abs + ".", |
| 71 | "see @~/main.go!", |
| 72 | "see (@main.go)", |
| 73 | "see \"@./main.go\"", |
| 74 | "see @" + dir + "/internal/../main.go", |
| 75 | "@main.go", |
| 76 | } { |
| 77 | _, att := Expand(in, dir) |
| 78 | if len(att) != 1 || att[0].Path != abs { |
| 79 | t.Errorf("Expand(%q) attachments = %+v, want one at %s", in, att, abs) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // A mention is resolved against cwd, not the process directory: the ACP |
| 85 | // front end passes the session's cwd, the terminal the directory mm runs in. |
| 86 | func TestExpandIsRelativeToCwd(t *testing.T) { |
| 87 | dir := workspace(t) |
| 88 | _, att := Expand("@acp.go", filepath.Join(dir, "internal", "acp")) |
| 89 | if len(att) != 1 || !strings.HasSuffix(att[0].Path, filepath.Join("internal", "acp", "acp.go")) { |
| 90 | t.Errorf("attachments %+v", att) |
| 91 | } |
| 92 | if _, att := Expand("@acp.go", dir); len(att) != 0 { |
| 93 | t.Errorf("acp.go does not exist at the root, yet %+v", att) |
| 94 | } |
| 95 | } |