package thintest import ( "strings" "testing" "amadan.net/oficina/equipo/internal/equipo/chat" "amadan.net/oficina/equipo/internal/wire" ) // The three paths agree about a repository the agent reaches: status // offers it, add_project adds it, and find and the read tools name it. // Empty (no commits yet) is said as empty, and one the runner has not // read yet as not read yet; neither is "unreachable", and neither puts // an access card in the conversation. A repository the agent does not // reach is still unreachable. func TestContractEmptyRepository(t *testing.T) { r := newToolRigAt(t, wire.ProtocolEmptyRepos) h := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolEmptyRepos) h.Repos = append(h.Repos, "amadan.net/paulca/threatmin", "amadan.net/paulca/later") h.Catalog = append(h.Catalog, wire.RepoInfo{Repo: "amadan.net/paulca/threatmin", Names: []string{"threatmin"}, Hints: []string{}, Empty: true}) if err := r.runner.Send(r.ctx, "h-empty", wire.FrameHello, h); err != nil { t.Fatal(err) } status := r.call("e1", wire.ToolStatus, map[string]string{}) if !strings.Contains(status.Text, `"paulca/threatmin"`) { t.Fatalf("status: %s", status.Text) } if res := r.call("e2", wire.ToolAddProject, map[string]string{"repo": "paulca/threatmin"}); res.IsError || res.Code != "added" { t.Fatalf("add: %+v", res) } for i, c := range []struct { name string args map[string]string }{ {wire.ToolReadFile, map[string]string{"repo": "paulca/threatmin", "path": "README.md"}}, {wire.ToolSearchCode, map[string]string{"repo": "amadan.net/paulca/threatmin", "query": "main"}}, } { res := r.call("e3-"+itoa(i), c.name, c.args) 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") { t.Fatalf("%s of an empty repository: %+v", c.name, res) } } for _, q := range []string{"threatmin", "paulca/threatmin", "Threatmin"} { found := r.call("e4-"+q, wire.ToolFind, map[string]string{"query": q}) if found.Code != "found" || !strings.Contains(found.Text, "paulca/threatmin") || !strings.Contains(found.Text, "no commits yet") { t.Fatalf("find %q: %+v", q, found) } } // In the allowlist, not in the catalog: the runner has not read it. 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") { t.Fatalf("read before the index: %+v", res) } if found := r.call("e6", wire.ToolFind, map[string]string{"query": "paulca/later"}); found.Code != "found" || !strings.Contains(found.Text, "paulca/later") { t.Fatalf("find before the index: %+v", found) } if snap, _ := r.client.Snapshot(r.ctx); func() int { n := 0 for _, k := range snap.Cards { if k.Kind == wire.CardAccess { n++ } } return n }() != 0 { t.Fatal("an access card for a repository the agent reaches") } if res := r.call("e7", wire.ToolReadFile, map[string]string{"repo": "oficina/secret", "path": "README.md"}); !res.IsError || res.Code != "unreachable" { t.Fatalf("read outside the allowlist: %+v", res) } // What the agent reaches is every namespace it is granted, read or // not yet read: paulca is among them, and moonbeam, which it reads. 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.") { t.Fatalf("find nothing: %+v", none) } } // An access card is for what was looked for only when that names a // repository as namespace/repository, however it is spelled (#48: never // "iOS keyboard"; review S2: never a single word such as "keyboard" or // "Woodstar"; review N1: https://, a trailing slash and .git are the same // repository). The grant command names the namespace the card names. func TestAccessTarget(t *testing.T) { for q, want := range map[string]string{ "Woodstar": "", "keyboard": "", "the Woodstar iOS app": "", "Moon Beam": "", "¿Qué?": "", "my app": "", "x": "", "ios": "", "iOS keyboard": "", "paulca/threatmin": "paulca/threatmin", "amadan.net/paulca/threatmin": "paulca/threatmin", "acme/Moonbeam": "acme/moonbeam", "the acme/Moonbeam repo": "", "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", "amadan.net/amadan.net/tito": "", "github.com/tito/website": "", "a/b/c": "", "/threatmin": "", } { if got := chat.AccessTarget(q); got != want { t.Fatalf("AccessTarget(%q) = %q, want %q", q, got, want) } } for name, want := range map[string]string{"woodstar": "", "threatmin": "", "paulca/threatmin": "paulca", "": ""} { if got := chat.GrantNamespace(name); got != want { t.Fatalf("GrantNamespace(%q) = %q, want %q", name, got, want) } } }