| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | // swipeWords is what Paul said on 2026-11-23 (hub oficina/equipo #48, #49). |
| 15 | const swipeWords = "On iOS, the keyboard should be dismissible with a swipe." |
| 16 | |
| 17 | // swipeHello is his runner's reach that day: four namespaces, and |
| 18 | // oficina/equipo read with the names its README and AGENTS.md give it |
| 19 | // ("Create" is a capitalised word that appears twice in them). |
| 20 | func swipeHello() wire.Hello { |
| 21 | return wire.Hello{ |
| 22 | Agent: "claude~paul@keymail.dev", |
| 23 | Repos: []string{"amadan.net/carlos/platform", "amadan.net/oficina/equipo", "amadan.net/oficina/old", "amadan.net/oficina/new", "amadan.net/paulca/threatmin", "amadan.net/woodstar/app"}, |
| 24 | CreateIn: []string{"oficina"}, |
| 25 | Catalog: []wire.RepoInfo{ |
| 26 | {Repo: "amadan.net/oficina/equipo", Names: []string{"equipo", "Create", "Generate", "Owners", "Open"}, Hints: []string{"go"}, Summary: "An Oficina app: a coordinator and a team of AI specialists."}, |
| 27 | {Repo: "amadan.net/carlos/platform", Names: []string{"platform"}, Hints: []string{}}, |
| 28 | {Repo: "amadan.net/paulca/threatmin", Names: []string{"threatmin"}, Hints: []string{}}, |
| 29 | {Repo: "amadan.net/woodstar/app", Names: []string{"app", "Woodstar"}, Hints: []string{}}, |
| 30 | // Read only: a reader grant puts a repository in the catalog, |
| 31 | // never in Repos, so tito is a known namespace. |
| 32 | {Repo: "amadan.net/tito/docs", Names: []string{"docs"}, Hints: []string{}}, |
| 33 | }, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // swipeRig is that conversation's turn, running, with the person's words. |
| 38 | func swipeRig(t *testing.T) (*meteredRig, turnRecord, Lease) { |
| 39 | t.Helper() |
| 40 | r := placementRig(t) |
| 41 | r.s.authority.CurrentRunner = func(*gorm.DB, RunnerPrincipal) error { return nil } |
| 42 | h, _ := json.Marshal(swipeHello()) |
| 43 | if err := r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("hello", string(h)).Error; err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | turn := turnRecord{ID: "tr_swipe", Person: hkOwner, Kind: wire.TurnPerson, State: wire.TurnRunning, Attempt: 1} |
| 47 | for _, row := range []any{&turn, &messageRecord{ID: "m_swipe", Person: hkOwner, TurnID: turn.ID, Role: wire.RolePerson, Text: swipeWords, Origin: originComposer}} { |
| 48 | if err := r.s.db.G.Create(row).Error; err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | } |
| 52 | return r, turn, Lease{Runner: RunnerPrincipal{Person: hkOwner, RunnerID: "RUNNER1"}, Protocol: wire.ProtocolCurrent} |
| 53 | } |
| 54 | |
| 55 | // callTool sends one Coordinator tool call the way the runner does and |
| 56 | // returns the result it is sent back. |
| 57 | func callTool(t *testing.T, r *meteredRig, l Lease, turn turnRecord, id, name string, args any) wire.ToolResult { |
| 58 | t.Helper() |
| 59 | raw, _ := json.Marshal(args) |
| 60 | call := wire.ToolCall{CallID: id, TurnID: turn.ID, Attempt: turn.Attempt, Name: name, Args: raw} |
| 61 | frame, _ := wire.NewFrame("frame-"+id, wire.FrameToolCall, call) |
| 62 | if err := r.s.Apply(context.Background(), l, frame, &call); err != nil { |
| 63 | t.Fatalf("%s: %v", name, err) |
| 64 | } |
| 65 | var cmd commandRecord |
| 66 | if err := r.s.db.G.Last(&cmd, "kind = ?", wire.FrameToolResult).Error; err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | var res wire.ToolResult |
| 70 | if err := json.Unmarshal([]byte(cmd.Body), &res); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if res.CallID != id { |
| 74 | t.Fatalf("%s: result for %q", name, res.CallID) |
| 75 | } |
| 76 | return res |
| 77 | } |
| 78 | |
| 79 | func accessCards(t *testing.T, r *meteredRig) []cardRecord { |
| 80 | t.Helper() |
| 81 | var cards []cardRecord |
| 82 | if err := r.s.db.G.Where("kind = ?", wire.CardAccess).Order("created_at").Find(&cards).Error; err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | return cards |
| 86 | } |
| 87 | |
| 88 | // #48 and #49: the Coordinator looked for "iOS keyboard", then filed the |
| 89 | // task in oficina/equipo. Looking for a feature raises no access card, and |
| 90 | // the project made for the task is called Equipo, not "Create". |
| 91 | func TestSwipeConversationNoFalseAccessCardAndRepoTitle(t *testing.T) { |
| 92 | r, turn, l := swipeRig(t) |
| 93 | found := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": "iOS keyboard"}) |
| 94 | if !found.IsError || found.Code != "not_a_repository" { |
| 95 | t.Fatalf("find of a feature: %+v", found) |
| 96 | } |
| 97 | if cards := accessCards(t, r); len(cards) != 0 { |
| 98 | t.Fatalf("access card for a feature: %+v", cards) |
| 99 | } |
| 100 | recorded := callTool(t, r, l, turn, "call-record", wire.ToolRecordTask, map[string]string{"repo": "amadan.net/oficina/equipo", "title": "Swipe down to dismiss the keyboard on iOS", "brief": "On iOS, the keyboard should be dismissible with a swipe."}) |
| 101 | if recorded.IsError || recorded.Code != "recorded" { |
| 102 | t.Fatalf("record: %+v", recorded) |
| 103 | } |
| 104 | var projects []projectRecord |
| 105 | if err := r.s.db.G.Where("repo = ?", "amadan.net/oficina/equipo").Find(&projects).Error; err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | // Shown from the repository; nothing stored that no reader shows (RN4). |
| 109 | if len(projects) != 1 || projects[0].shownTitle() != "Equipo" || projects[0].Title != "" || projects[0].TitleSet { |
| 110 | t.Fatalf("project: %+v", projects) |
| 111 | } |
| 112 | // A second task for the same repository goes in the same project. |
| 113 | again := callTool(t, r, l, turn, "call-record-2", wire.ToolRecordTask, map[string]any{"repo": "oficina/equipo", "title": "Keep the keyboard toolbar above the swipe area", "brief": "Second task.", "separate": true}) |
| 114 | if again.Code != "recorded" { |
| 115 | t.Fatalf("second record: %+v", again) |
| 116 | } |
| 117 | var n int64 |
| 118 | r.s.db.G.Model(&projectRecord{}).Where("repo = ?", "amadan.net/oficina/equipo").Count(&n) |
| 119 | if n != 1 { |
| 120 | t.Fatalf("projects for one repository: %d", n) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Housekeeping on the runner's hello resolves, as not needed, only an |
| 125 | // open card whose stored target is invalid by find's rules, parsed the way |
| 126 | // find parses: free text, or a bare name (no card is raised for one now). |
| 127 | // An older card whose question was free text but whose name is an |
| 128 | // explicit repository ("the acme/Moonbeam repo") stays open while it is |
| 129 | // unreached; a reached one says it is reachable (review S3). |
| 130 | func TestHelloResolvesAccessCardsThatFailValidation(t *testing.T) { |
| 131 | r, _, _ := swipeRig(t) |
| 132 | cards := []cardRecord{ |
| 133 | {ID: "c_free", Question: "iOS keyboard", Name: "keyboard"}, |
| 134 | {ID: "c_vague", Question: "the login bug", Name: ""}, |
| 135 | {ID: "c_bare", Question: "tito website", Name: "tito"}, |
| 136 | {ID: "c_ns", Question: "tito", Name: "tito"}, |
| 137 | {ID: "c_reached", Question: "oficina/equipo", Name: "oficina/equipo"}, |
| 138 | {ID: "c_real", Question: "tito/website", Name: "tito/website"}, |
| 139 | {ID: "c_older", Question: "the acme/Moonbeam repo", Name: "acme/moonbeam"}, |
| 140 | } |
| 141 | for i := range cards { |
| 142 | c := &cards[i] |
| 143 | c.Person, c.Kind, c.State, c.TurnID, c.Options, c.Choice, c.Revision = hkOwner, wire.CardAccess, wire.CardOpen, "tr_old", `["oficina"]`, "claude~paul@keymail.dev", 1 |
| 144 | if err := r.s.db.G.Create(c).Error; err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | } |
| 148 | for range 2 { |
| 149 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 150 | return tx.resolveAccess(hkOwner, swipeHello()) |
| 151 | }); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | } |
| 155 | want := map[string]string{"c_free": wire.ChoiceNotNeeded, "c_vague": wire.ChoiceNotNeeded, "c_bare": wire.ChoiceNotNeeded, "c_ns": wire.ChoiceNotNeeded, "c_reached": "", "c_real": "open", "c_older": "open"} |
| 156 | for _, c := range accessCards(t, r) { |
| 157 | switch want[c.ID] { |
| 158 | case "open": |
| 159 | if c.State != wire.CardOpen || c.Revision != 1 { |
| 160 | t.Errorf("%s resolved: %+v", c.ID, c) |
| 161 | } |
| 162 | case wire.ChoiceNotNeeded: |
| 163 | if c.State != wire.CardResolved || c.Choice != wire.ChoiceNotNeeded || c.Revision != 2 { |
| 164 | t.Errorf("%s not resolved once as not needed: %+v", c.ID, c) |
| 165 | } |
| 166 | default: |
| 167 | // Reached: resolved, and it says so itself. |
| 168 | if c.State != wire.CardResolved || c.Choice == wire.ChoiceNotNeeded { |
| 169 | t.Errorf("%s: %+v", c.ID, c) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // find raises a card only for an explicit repository in a namespace |
| 176 | // Equipo knows (its agent reaches or reads something there) that it does |
| 177 | // not reach. Single words, free text, invented namespaces and a missing |
| 178 | // repository in a namespace the agent fully holds get no card (review S2). |
| 179 | func TestAccessCardOnlyForUnreachedRepositoryInKnownNamespace(t *testing.T) { |
| 180 | for _, tc := range []struct { |
| 181 | query string |
| 182 | code string |
| 183 | }{ |
| 184 | {"iOS keyboard", "not_a_repository"}, |
| 185 | {"swipe gesture", "not_a_repository"}, |
| 186 | {"ios", "not_a_repository"}, |
| 187 | {"keyboard", "not_a_repository"}, |
| 188 | {"tito", "found"}, |
| 189 | {"ios/keyboard", "unknown_namespace"}, |
| 190 | {"oficina/ios-keyboard", "no_such_repository"}, |
| 191 | {"tito/website", "none"}, |
| 192 | {"amadan.net/tito/website", "none"}, |
| 193 | {"https://amadan.net/tito/website.git", "none"}, |
| 194 | } { |
| 195 | t.Run(tc.query, func(t *testing.T) { |
| 196 | r, turn, l := swipeRig(t) |
| 197 | got := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": tc.query}) |
| 198 | if got.Code != tc.code || got.IsError != (tc.code != "none" && tc.code != "found") { |
| 199 | t.Fatalf("find: %+v", got) |
| 200 | } |
| 201 | cards := accessCards(t, r) |
| 202 | if (len(cards) == 1) != (tc.code == "none") || len(cards) > 1 || len(cards) == 1 && cards[0].Name != "tito/website" { |
| 203 | t.Fatalf("cards: %+v", cards) |
| 204 | } |
| 205 | // The refusal never invites a retry with a shorter name. |
| 206 | if got.IsError && (strings.Contains(got.Text, "name alone") || !strings.Contains(got.Text, "ask the person which repository this is for")) { |
| 207 | t.Fatalf("refusal: %s", got.Text) |
| 208 | } |
| 209 | }) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // request_access refuses only access the agent already has: write work |
| 214 | // needs the repository on the runner's list, so a repository it only reads |
| 215 | // can still be asked for at role agent, after record_task found it not |
| 216 | // allowed (review S1). |
| 217 | func TestRequestAccessAlreadyReachedMatchesRole(t *testing.T) { |
| 218 | r, turn, l := swipeRig(t) |
| 219 | got := callTool(t, r, l, turn, "call-ask", wire.ToolRequestAccess, map[string]string{"target": "oficina/equipo", "reason": "I need to fix the keyboard."}) |
| 220 | if !got.IsError || got.Code != "already_reached" { |
| 221 | t.Fatalf("request_access for a written repository: %+v", got) |
| 222 | } |
| 223 | got = callTool(t, r, l, turn, "call-read", wire.ToolRequestAccess, map[string]string{"target": "tito/docs", "role": "reader", "reason": "I need to read the docs."}) |
| 224 | if !got.IsError || got.Code != "already_reached" { |
| 225 | t.Fatalf("reader access to a read repository: %+v", got) |
| 226 | } |
| 227 | if cards := accessCards(t, r); len(cards) != 0 { |
| 228 | t.Fatalf("cards: %+v", cards) |
| 229 | } |
| 230 | recorded := callTool(t, r, l, turn, "call-record", wire.ToolRecordTask, map[string]string{"repo": "tito/docs", "title": "Fix the docs typo", "brief": "Fix it."}) |
| 231 | if recorded.Code != "not_allowed" { |
| 232 | t.Fatalf("record in a read repository: %+v", recorded) |
| 233 | } |
| 234 | got = callTool(t, r, l, turn, "call-write", wire.ToolRequestAccess, map[string]string{"target": "tito/docs", "reason": "I need to fix the docs typo."}) |
| 235 | if got.IsError || got.Code != "asked" { |
| 236 | t.Fatalf("write access to a read repository: %+v", got) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Fable's sequence (review B2): one turn names a reached and an unreached |
| 241 | // repository. Filing the task in the reached one leaves the other's card |
| 242 | // open; request_access then confirms on that open card; and a card Equipo |
| 243 | // resolved as not needed never takes a confirmation or stops the turn's |
| 244 | // next card. |
| 245 | func TestTwoRepositoryTurnKeepsTheUnreachedCard(t *testing.T) { |
| 246 | r, turn, l := swipeRig(t) |
| 247 | if err := r.s.db.G.Model(&messageRecord{}).Where("id = ?", "m_swipe").Update("text", "Fix the footer in oficina/equipo and the header in tito/website.").Error; err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | if got := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": "tito/website"}); got.Code != "none" { |
| 251 | t.Fatalf("find: %+v", got) |
| 252 | } |
| 253 | if got := callTool(t, r, l, turn, "call-record", wire.ToolRecordTask, map[string]string{"repo": "oficina/equipo", "title": "Fix the footer", "brief": "Fix it."}); got.Code != "recorded" { |
| 254 | t.Fatalf("record: %+v", got) |
| 255 | } |
| 256 | cards := accessCards(t, r) |
| 257 | if len(cards) != 1 || cards[0].State != wire.CardOpen || cards[0].Choice != "claude~paul@keymail.dev" { |
| 258 | t.Fatalf("the unreached card was closed: %+v", cards) |
| 259 | } |
| 260 | if got := callTool(t, r, l, turn, "call-ask", wire.ToolRequestAccess, map[string]string{"target": "tito/website", "reason": "I need to fix the header."}); got.Code != "asked" { |
| 261 | t.Fatalf("request: %+v", got) |
| 262 | } |
| 263 | cards = accessCards(t, r) |
| 264 | if len(cards) != 1 || cards[0].State != wire.CardOpen || cards[0].Access == "" { |
| 265 | t.Fatalf("confirmation not on the open card: %+v", cards) |
| 266 | } |
| 267 | |
| 268 | // A find card Equipo resolved as not needed takes no confirmation and |
| 269 | // does not count as the turn's card. |
| 270 | r, turn, l = swipeRig(t) |
| 271 | done := cardRecord{ID: "c_done", Person: hkOwner, Kind: wire.CardAccess, State: wire.CardResolved, TurnID: turn.ID, Question: "iOS keyboard", Options: "[]", Choice: wire.ChoiceNotNeeded, Name: "keyboard", Revision: 2} |
| 272 | if err := r.s.db.G.Create(&done).Error; err != nil { |
| 273 | t.Fatal(err) |
| 274 | } |
| 275 | if got := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": "tito/website"}); got.Code != "none" { |
| 276 | t.Fatalf("find: %+v", got) |
| 277 | } |
| 278 | // The find raised its own card (review RN1). |
| 279 | if cards := accessCards(t, r); len(cards) != 2 || !slices.ContainsFunc(cards, func(c cardRecord) bool { |
| 280 | return c.ID != done.ID && c.State == wire.CardOpen && c.Name == "tito/website" && c.Access == "" |
| 281 | }) { |
| 282 | t.Fatalf("find after a not-needed card: %+v", cards) |
| 283 | } |
| 284 | if got := callTool(t, r, l, turn, "call-ask", wire.ToolRequestAccess, map[string]string{"target": "tito/website", "reason": "I need to fix the header."}); got.Code != "asked" { |
| 285 | t.Fatalf("request: %+v", got) |
| 286 | } |
| 287 | for _, c := range accessCards(t, r) { |
| 288 | switch { |
| 289 | case c.ID == done.ID && (c.State != wire.CardResolved || c.Access != "" || c.Revision != 2): |
| 290 | t.Fatalf("resolved card rewritten: %+v", c) |
| 291 | case c.ID != done.ID && (c.State != wire.CardOpen || c.Access == "" || c.Name != "tito/website"): |
| 292 | t.Fatalf("new card: %+v", c) |
| 293 | } |
| 294 | } |
| 295 | if n := len(accessCards(t, r)); n != 2 { |
| 296 | t.Fatalf("cards: %d", n) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // The need passes for a card whose own target is the task's repository, |
| 301 | // or its namespace when the agent reaches it, and for no other card. |
| 302 | func TestAccessNeedPassesOnlyForTheTasksRepository(t *testing.T) { |
| 303 | r, turn, l := swipeRig(t) |
| 304 | for _, c := range []cardRecord{ |
| 305 | {ID: "c_repo", Question: "oficina/equipo", Name: "oficina/equipo"}, |
| 306 | {ID: "c_ns", Question: "oficina", Name: "oficina"}, |
| 307 | {ID: "c_other", Question: "tito/website", Name: "tito/website"}, |
| 308 | } { |
| 309 | c.Person, c.Kind, c.State, c.TurnID, c.Options, c.Choice, c.Revision = hkOwner, wire.CardAccess, wire.CardOpen, turn.ID, "[]", "claude~paul@keymail.dev", 1 |
| 310 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 311 | t.Fatal(err) |
| 312 | } |
| 313 | } |
| 314 | if got := callTool(t, r, l, turn, "call-record", wire.ToolRecordTask, map[string]string{"repo": "oficina/equipo", "title": "Fix the footer", "brief": "Fix it."}); got.Code != "recorded" { |
| 315 | t.Fatalf("record: %+v", got) |
| 316 | } |
| 317 | for _, c := range accessCards(t, r) { |
| 318 | if passed := c.State == wire.CardResolved && c.Choice == wire.ChoiceNotNeeded; passed != (c.ID != "c_other") { |
| 319 | t.Errorf("%s: %+v", c.ID, c) |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // A project's title from its repository is the repository's name, never a |
| 325 | // word lifted from its README, and never a bare verb or generic word. |
| 326 | func TestProjectTitleFromRepositoryName(t *testing.T) { |
| 327 | equipo := swipeHello().Catalog[0] |
| 328 | for _, tc := range []struct { |
| 329 | repo string |
| 330 | info *wire.RepoInfo |
| 331 | want string |
| 332 | }{ |
| 333 | {"amadan.net/oficina/equipo", &equipo, "Equipo"}, |
| 334 | {"amadan.net/oficina/equipo", nil, "Equipo"}, |
| 335 | {"amadan.net/paulca/threatmin", &wire.RepoInfo{Names: []string{"threatmin", "Build", "Deploy"}}, "Threatmin"}, |
| 336 | // Review probes (Fable, ae26c16c): the runner's own names and |
| 337 | // summaries for real Oficina repositories. A name the README |
| 338 | // repeats is never the title, even when the summary uses it. |
| 339 | {"amadan.net/oficina/base", &wire.RepoInfo{Names: []string{"base", "Store", "Platform", "Sign", "Settings"}, Summary: "Base is the front door ... you install apps from a Store."}, "Base"}, |
| 340 | {"amadan.net/oficina/alta", &wire.RepoInfo{Names: []string{"alta", "Paul", "Oficina", "Home", "Continue"}, Summary: "Paul's team-provisioning app for the Oficina suite."}, "Alta"}, |
| 341 | {"amadan.net/oficina/sample", &wire.RepoInfo{Names: []string{"sample", "Woodstar"}, Summary: "Woodstar is a reading app."}, "Sample"}, |
| 342 | {"amadan.net/oficina/sample", &wire.RepoInfo{Names: []string{"sample", "Install"}, Summary: "Install with make."}, "Sample"}, |
| 343 | {"amadan.net/paulca/create", nil, "paulca/create"}, |
| 344 | {"amadan.net/woodstar/app", nil, "woodstar/app"}, |
| 345 | {"amadan.net/paulca/new", nil, "paulca/new"}, |
| 346 | } { |
| 347 | if got := ProjectTitle(tc.repo); got != tc.want { |
| 348 | t.Errorf("%s: %q, want %q", tc.repo, got, tc.want) |
| 349 | } |
| 350 | } |
| 351 | for _, title := range []string{"Create", "new", " Project ", "Add", "Fix", "", "Untitled"} { |
| 352 | if UsableProjectTitle(title) { |
| 353 | t.Errorf("%q passed", title) |
| 354 | } |
| 355 | } |
| 356 | for _, title := range []string{"Equipo", "Woodstar", "Create Studio", "paulca/create"} { |
| 357 | if !UsableProjectTitle(title) { |
| 358 | t.Errorf("%q refused", title) |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // sayInTurn replaces the person's words in the rig's turn. |
| 364 | func sayInTurn(t *testing.T, r *meteredRig, words string) { |
| 365 | t.Helper() |
| 366 | if err := r.s.db.G.Model(&messageRecord{}).Where("id = ?", "m_swipe").Update("text", words).Error; err != nil { |
| 367 | t.Fatal(err) |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // setHello replaces the runner's hello and protocol. |
| 372 | func setHello(t *testing.T, r *meteredRig, h wire.Hello, protocol int) { |
| 373 | t.Helper() |
| 374 | raw, _ := json.Marshal(h) |
| 375 | if err := r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Updates(map[string]any{"hello": string(raw), "protocol": protocol}).Error; err != nil { |
| 376 | t.Fatal(err) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Review R1: in a namespace Equipo knows nothing of, a repository the |
| 381 | // person named this turn gets the grant card (also on a runner without |
| 382 | // request_access, and for a new team whose agent reaches nothing); one the |
| 383 | // model made up gets neither a card nor a request_access suggestion, and |
| 384 | // request_access refuses it. |
| 385 | func TestUnknownNamespaceCardOnlyWhenThePersonNamesIt(t *testing.T) { |
| 386 | for _, tc := range []struct { |
| 387 | name, words string |
| 388 | hello wire.Hello |
| 389 | protocol int |
| 390 | }{ |
| 391 | {"named", "Fix the header in acme/website.", swipeHello(), wire.ProtocolCurrent}, |
| 392 | {"named on an old runner", "Fix the header in acme/website.", swipeHello(), wire.ProtocolAccess - 1}, |
| 393 | {"new team", "Please work on https://amadan.net/acme/website", wire.Hello{Agent: "claude~paul@keymail.dev"}, wire.ProtocolCurrent}, |
| 394 | } { |
| 395 | t.Run(tc.name, func(t *testing.T) { |
| 396 | r, turn, l := swipeRig(t) |
| 397 | sayInTurn(t, r, tc.words) |
| 398 | setHello(t, r, tc.hello, tc.protocol) |
| 399 | got := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": "acme/website"}) |
| 400 | if got.IsError || got.Code != "none" || strings.Contains(got.Text, "request_access") != (tc.protocol >= wire.ProtocolAccess) { |
| 401 | t.Fatalf("find: %+v", got) |
| 402 | } |
| 403 | if cards := accessCards(t, r); len(cards) != 1 || cards[0].Name != "acme/website" || cards[0].State != wire.CardOpen { |
| 404 | t.Fatalf("cards: %+v", cards) |
| 405 | } |
| 406 | }) |
| 407 | } |
| 408 | r, turn, l := swipeRig(t) |
| 409 | got := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": "ios/keyboard"}) |
| 410 | if !got.IsError || got.Code != "unknown_namespace" || strings.Contains(got.Text, "request_access") { |
| 411 | t.Fatalf("find of an invented repository: %+v", got) |
| 412 | } |
| 413 | got = callTool(t, r, l, turn, "call-ask", wire.ToolRequestAccess, map[string]string{"target": "ios/keyboard", "reason": "I need to fix the keyboard."}) |
| 414 | if !got.IsError || got.Code != "not_named" { |
| 415 | t.Fatalf("request_access for an invented repository: %+v", got) |
| 416 | } |
| 417 | if cards := accessCards(t, r); len(cards) != 0 { |
| 418 | t.Fatalf("cards: %+v", cards) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Review RN2 and RN3: reading one repository in a namespace is not holding |
| 423 | // the namespace, and a repository the agent reads in a namespace it holds |
| 424 | // exists, so asking to write to it is not "no such repository". |
| 425 | func TestRequestAccessNamespaceReaderAndReadRepository(t *testing.T) { |
| 426 | r, turn, l := swipeRig(t) |
| 427 | sayInTurn(t, r, "Give Equipo read access to the whole tito namespace.") |
| 428 | if got := callTool(t, r, l, turn, "call-ns", wire.ToolRequestAccess, map[string]string{"target": "tito", "role": "reader", "reason": "I need to read everything in tito."}); got.IsError || got.Code != "asked" { |
| 429 | t.Fatalf("namespace reader: %+v", got) |
| 430 | } |
| 431 | r, turn, l = swipeRig(t) |
| 432 | h := swipeHello() |
| 433 | h.Catalog = append(h.Catalog, wire.RepoInfo{Repo: "amadan.net/oficina/readonly", Names: []string{"readonly"}, Hints: []string{}}) |
| 434 | setHello(t, r, h, wire.ProtocolCurrent) |
| 435 | if got := callTool(t, r, l, turn, "call-ro", wire.ToolRequestAccess, map[string]string{"target": "oficina/readonly", "reason": "I need to fix it."}); got.IsError || got.Code != "asked" { |
| 436 | t.Fatalf("write to a read repository in a held namespace: %+v", got) |
| 437 | } |
| 438 | r, turn, l = swipeRig(t) |
| 439 | if got := callTool(t, r, l, turn, "call-find", wire.ToolFind, map[string]string{"query": "oficina/ios-keyboard"}); got.Code != "no_such_repository" || !strings.Contains(got.Text, "sees no repository oficina/ios-keyboard there yet") { |
| 440 | t.Fatalf("find of a missing repository: %+v", got) |
| 441 | } |
| 442 | } |
| 443 | |