| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // The three paths agree about a repository the agent reaches: status |
| 12 | // offers it, add_project adds it, and find and the read tools name it. |
| 13 | // Empty (no commits yet) is said as empty, and one the runner has not |
| 14 | // read yet as not read yet; neither is "unreachable", and neither puts |
| 15 | // an access card in the conversation. A repository the agent does not |
| 16 | // reach is still unreachable. |
| 17 | func TestContractEmptyRepository(t *testing.T) { |
| 18 | r := newToolRigAt(t, wire.ProtocolEmptyRepos) |
| 19 | h := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolEmptyRepos) |
| 20 | h.Repos = append(h.Repos, "amadan.net/paulca/threatmin", "amadan.net/paulca/later") |
| 21 | h.Catalog = append(h.Catalog, wire.RepoInfo{Repo: "amadan.net/paulca/threatmin", Names: []string{"threatmin"}, Hints: []string{}, Empty: true}) |
| 22 | if err := r.runner.Send(r.ctx, "h-empty", wire.FrameHello, h); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | status := r.call("e1", wire.ToolStatus, map[string]string{}) |
| 26 | if !strings.Contains(status.Text, `"paulca/threatmin"`) { |
| 27 | t.Fatalf("status: %s", status.Text) |
| 28 | } |
| 29 | if res := r.call("e2", wire.ToolAddProject, map[string]string{"repo": "paulca/threatmin"}); res.IsError || res.Code != "added" { |
| 30 | t.Fatalf("add: %+v", res) |
| 31 | } |
| 32 | for i, c := range []struct { |
| 33 | name string |
| 34 | args map[string]string |
| 35 | }{ |
| 36 | {wire.ToolReadFile, map[string]string{"repo": "paulca/threatmin", "path": "README.md"}}, |
| 37 | {wire.ToolSearchCode, map[string]string{"repo": "amadan.net/paulca/threatmin", "query": "main"}}, |
| 38 | } { |
| 39 | res := r.call("e3-"+itoa(i), c.name, c.args) |
| 40 | if res.IsError || res.Code != "empty" || res.Repo != "amadan.net/paulca/threatmin" || !strings.Contains(res.Text, "paulca/threatmin") || !strings.Contains(res.Text, "no commits yet") || strings.Contains(res.Text, "reach") { |
| 41 | t.Fatalf("%s of an empty repository: %+v", c.name, res) |
| 42 | } |
| 43 | } |
| 44 | for _, q := range []string{"threatmin", "paulca/threatmin", "Threatmin"} { |
| 45 | found := r.call("e4-"+q, wire.ToolFind, map[string]string{"query": q}) |
| 46 | if found.Code != "found" || !strings.Contains(found.Text, "paulca/threatmin") || !strings.Contains(found.Text, "no commits yet") { |
| 47 | t.Fatalf("find %q: %+v", q, found) |
| 48 | } |
| 49 | } |
| 50 | // In the allowlist, not in the catalog: the runner has not read it. |
| 51 | if res := r.call("e5", wire.ToolReadFile, map[string]string{"repo": "paulca/later", "path": "README.md"}); !res.IsError || res.Code != "not_indexed" || !strings.Contains(res.Text, "paulca/later") || strings.Contains(res.Text, "Use find") { |
| 52 | t.Fatalf("read before the index: %+v", res) |
| 53 | } |
| 54 | if found := r.call("e6", wire.ToolFind, map[string]string{"query": "paulca/later"}); found.Code != "found" || !strings.Contains(found.Text, "paulca/later") { |
| 55 | t.Fatalf("find before the index: %+v", found) |
| 56 | } |
| 57 | if snap, _ := r.client.Snapshot(r.ctx); func() int { |
| 58 | n := 0 |
| 59 | for _, k := range snap.Cards { |
| 60 | if k.Kind == wire.CardAccess { |
| 61 | n++ |
| 62 | } |
| 63 | } |
| 64 | return n |
| 65 | }() != 0 { |
| 66 | t.Fatal("an access card for a repository the agent reaches") |
| 67 | } |
| 68 | if res := r.call("e7", wire.ToolReadFile, map[string]string{"repo": "oficina/secret", "path": "README.md"}); !res.IsError || res.Code != "unreachable" { |
| 69 | t.Fatalf("read outside the allowlist: %+v", res) |
| 70 | } |
| 71 | // What the agent reaches is every namespace it is granted, read or |
| 72 | // not yet read: paulca is among them, and moonbeam, which it reads. |
| 73 | if none := r.call("e8", wire.ToolFind, map[string]string{"query": "paulca/moonbeam"}); none.Code != "none" || !strings.Contains(none.Text, "It reaches: oficina, moonbeam, paulca.") { |
| 74 | t.Fatalf("find nothing: %+v", none) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // An access card is for what was looked for only when that names a |
| 79 | // repository as namespace/repository, however it is spelled (#48: never |
| 80 | // "iOS keyboard"; review S2: never a single word such as "keyboard" or |
| 81 | // "Woodstar"; review N1: https://, a trailing slash and .git are the same |
| 82 | // repository). The grant command names the namespace the card names. |
| 83 | func TestAccessTarget(t *testing.T) { |
| 84 | for q, want := range map[string]string{ |
| 85 | "Woodstar": "", "keyboard": "", "the Woodstar iOS app": "", "Moon Beam": "", "¿Qué?": "", "my app": "", "x": "", "ios": "", "iOS keyboard": "", |
| 86 | "paulca/threatmin": "paulca/threatmin", "amadan.net/paulca/threatmin": "paulca/threatmin", "acme/Moonbeam": "acme/moonbeam", "the acme/Moonbeam repo": "", |
| 87 | "https://amadan.net/tito/website": "tito/website", "amadan.net/tito/website/": "tito/website", "tito/website.git": "tito/website", "https://amadan.net/tito/website.git/": "tito/website", |
| 88 | "amadan.net/amadan.net/tito": "", "github.com/tito/website": "", "a/b/c": "", "/threatmin": "", |
| 89 | } { |
| 90 | if got := chat.AccessTarget(q); got != want { |
| 91 | t.Fatalf("AccessTarget(%q) = %q, want %q", q, got, want) |
| 92 | } |
| 93 | } |
| 94 | for name, want := range map[string]string{"woodstar": "", "threatmin": "", "paulca/threatmin": "paulca", "": ""} { |
| 95 | if got := chat.GrantNamespace(name); got != want { |
| 96 | t.Fatalf("GrantNamespace(%q) = %q, want %q", name, got, want) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |