| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // moveInRig is a rig with a fake runner that said hello, and no turn yet. |
| 13 | type moveInRig struct { |
| 14 | *toolRig |
| 15 | } |
| 16 | |
| 17 | func newMoveInRig(t *testing.T) *moveInRig { |
| 18 | r := newRig(t) |
| 19 | runner := r.dial(0) |
| 20 | hello := wire.Hello{Generation: runner.Welcome.Generation, Repos: []string{"amadan.net/oficina/sample"}, CreateIn: []string{"oficina"}, Agent: "equipo-test", Catalog: []wire.RepoInfo{{Repo: "amadan.net/oficina/sample", DefaultBranch: "main", Summary: "Woodstar is a reading app.", Names: []string{"sample", "Woodstar"}, Hints: []string{"go"}}}, Connections: []wire.HeldConnection{}, Versions: map[string]string{}} |
| 21 | if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) |
| 25 | return &moveInRig{&toolRig{rig: r, runner: runner}} |
| 26 | } |
| 27 | |
| 28 | // moveIn asks for a turn of kind and returns its message command, acked. |
| 29 | func (r *moveInRig) moveIn(frame, kind string) *wire.MessageCommand { |
| 30 | r.t.Helper() |
| 31 | if err := r.runner.Send(r.ctx, frame, wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: kind}); err != nil { |
| 32 | r.t.Fatal(err) |
| 33 | } |
| 34 | return r.dispatched(kind) |
| 35 | } |
| 36 | |
| 37 | // dispatched reads frames until a message command of kind arrives, acks |
| 38 | // it and waits for its turn to run. |
| 39 | func (r *moveInRig) dispatched(kind string) *wire.MessageCommand { |
| 40 | r.t.Helper() |
| 41 | for { |
| 42 | f, body, err := r.runner.Next(r.ctx) |
| 43 | if err != nil { |
| 44 | r.t.Fatal(err) |
| 45 | } |
| 46 | cmd, ok := body.(*wire.MessageCommand) |
| 47 | if !ok { |
| 48 | continue |
| 49 | } |
| 50 | if cmd.Kind != kind { |
| 51 | r.t.Fatalf("dispatched kind %q, want %q: %+v", cmd.Kind, kind, cmd) |
| 52 | } |
| 53 | if err = r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { |
| 54 | r.t.Fatal(err) |
| 55 | } |
| 56 | r.wait("running", func(c *Conversation) bool { return c.Turns[cmd.TurnID].State == wire.TurnRunning }) |
| 57 | r.turn = cmd.TurnID |
| 58 | return cmd |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func (r *moveInRig) reply(turn, text string) { |
| 63 | r.t.Helper() |
| 64 | if err := r.runner.Send(r.ctx, "r-reply-"+turn, wire.FrameReply, wire.Reply{TurnID: turn, Attempt: 1, Text: text}); err != nil { |
| 65 | r.t.Fatal(err) |
| 66 | } |
| 67 | r.wait("replied", func(c *Conversation) bool { return c.Turns[turn].State == wire.TurnReplied }) |
| 68 | } |
| 69 | |
| 70 | // The runner asks for a move-in; the instance starts one move_in turn, |
| 71 | // shows the coordinator moving in, scopes its tools to looking around |
| 72 | // and taking notes, and posts its reply as the one introduction. |
| 73 | func TestContractMoveIn(t *testing.T) { |
| 74 | r := newMoveInRig(t) |
| 75 | if err := r.runner.Send(r.ctx, "r-mi-1", wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: wire.TurnMoveIn}); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | shape(t, "frames/move_in.json", r.runner.Sent[len(r.runner.Sent)-1]) |
| 79 | cmd := r.dispatched(wire.TurnMoveIn) |
| 80 | shape(t, "frames/message.json", r.runner.Received[len(r.runner.Received)-1]) |
| 81 | if withoutProjectContext(t, cmd.Text) != "" || cmd.Person != Owner { |
| 82 | t.Fatalf("move-in command %+v", cmd) |
| 83 | } |
| 84 | r.wait("moving in", func(c *Conversation) bool { |
| 85 | return c.Coordinator.State == wire.CoordinatorMovingIn && c.Coordinator.TurnID == cmd.TurnID |
| 86 | }) |
| 87 | snap, err := r.client.Snapshot(r.ctx) |
| 88 | if err != nil || snap.Coordinator.State != wire.CoordinatorMovingIn || snap.Coordinator.ID != Owner { |
| 89 | t.Fatalf("snapshot coordinator %+v %v", snap.Coordinator, err) |
| 90 | } |
| 91 | shape(t, "api/snapshot.json", snap) |
| 92 | // A second request while one is in flight starts nothing. |
| 93 | if err := r.runner.Send(r.ctx, "r-mi-2", wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: wire.TurnMoveIn}); err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | // Another person's move-in is not this runner's to ask for. |
| 97 | if err := r.runner.Send(r.ctx, "r-mi-3", wire.FrameMoveIn, wire.MoveIn{Person: "someone@example.com", Kind: wire.TurnMoveIn}); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if err := r.runner.Send(r.ctx, "r-mi-4", wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: "party"}); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | |
| 104 | // Activity names the repository it reads, as moving in. |
| 105 | if err := r.runner.Send(r.ctx, "r-a-mi", wire.FrameActivity, wire.ActivityReport{TurnID: cmd.TurnID, Attempt: 1, Kind: wire.ActivityTool, Tool: wire.ToolReadFile, Repo: "amadan.net/oficina/sample"}); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | shape(t, "frames/activity.json", r.runner.Sent[len(r.runner.Sent)-1]) |
| 109 | // A repository outside the catalog is never named. |
| 110 | if err := r.runner.Send(r.ctx, "r-a-mi2", wire.FrameActivity, wire.ActivityReport{TurnID: cmd.TurnID, Attempt: 1, Kind: wire.ActivityTool, Tool: wire.ToolReadFile, Repo: "amadan.net/secret/thing"}); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | var acts []wire.Activity |
| 114 | r.wait("activity", func(c *Conversation) bool { acts = c.Activity[cmd.TurnID]; return len(acts) == 2 }) |
| 115 | if acts[0].Label != "Moving in… reading oficina/sample" || acts[0].Repo != "oficina/sample" { |
| 116 | t.Fatalf("move-in activity %+v", acts[0]) |
| 117 | } |
| 118 | if acts[1].Repo != "" || strings.Contains(acts[1].Label, "secret") { |
| 119 | t.Fatalf("unknown repository named: %+v", acts[1]) |
| 120 | } |
| 121 | |
| 122 | // Only looking around and taking notes. |
| 123 | if res := r.call("m1", wire.ToolStatus, map[string]string{}); res.Code != "ok" || !strings.Contains(res.Text, `"coordinator":"moving_in"`) { |
| 124 | t.Fatalf("status while moving in: %+v", res) |
| 125 | } |
| 126 | for _, c := range []struct { |
| 127 | name string |
| 128 | args any |
| 129 | }{ |
| 130 | {wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}}, |
| 131 | {wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/site"}}, |
| 132 | {wire.ToolAskPerson, map[string]any{"question": "Which repository?", "options": []string{"a", "b"}}}, |
| 133 | {wire.ToolSetProjectHome, map[string]string{"namespace": "oficina"}}, |
| 134 | } { |
| 135 | if res := r.call("m-"+c.name, c.name, c.args); !res.IsError || res.Code != "moving_in" { |
| 136 | t.Fatalf("%s while moving in: %+v", c.name, res) |
| 137 | } |
| 138 | } |
| 139 | if res := r.call("n1", wire.ToolWriteNote, map[string]any{"entries": 3, "bytes": 120}); res.Code != "note" || res.IsError { |
| 140 | t.Fatalf("write_note: %+v", res) |
| 141 | } |
| 142 | for i, bad := range []map[string]any{{"entries": wire.MaxNoteEntries + 1, "bytes": 1}, {"entries": 1, "bytes": wire.MaxNotebookBytes + 1}, {"entries": -1, "bytes": 1}, {"entries": 1, "bytes": 1, "text": "smuggled"}, {"name": "notebook.md", "bytes": 1}} { |
| 143 | if res := r.call("nb"+itoa(i), wire.ToolWriteNote, bad); !res.IsError || res.Code != "invalid" { |
| 144 | t.Fatalf("write_note %v: %+v", bad, res) |
| 145 | } |
| 146 | } |
| 147 | if res := r.call("n2", wire.ToolReadNotes, map[string]string{}); res.Code != "notes" || res.IsError { |
| 148 | t.Fatalf("read_notes: %+v", res) |
| 149 | } |
| 150 | |
| 151 | r.reply(cmd.TurnID, "Hi, I'm your coordinator. I can see oficina/sample (Woodstar).") |
| 152 | r.wait("ready", func(c *Conversation) bool { |
| 153 | return c.Coordinator.State == wire.CoordinatorReady && c.Coordinator.MovedInAt != nil && c.Coordinator.TurnID == "" |
| 154 | }) |
| 155 | var moveIns int |
| 156 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_turns WHERE kind = ?", wire.TurnMoveIn).Scan(&moveIns); err != nil || moveIns != 1 { |
| 157 | t.Fatalf("move-in turns %d %v; the repeated request must start nothing", moveIns, err) |
| 158 | } |
| 159 | var intro []wire.Message |
| 160 | r.conv.Locked(func(c *Conversation) { intro = c.Replies() }) |
| 161 | if len(intro) != 1 || intro[0].TurnID != cmd.TurnID { |
| 162 | t.Fatalf("introduction %+v", intro) |
| 163 | } |
| 164 | turn, err := r.client.Turn(r.ctx, cmd.TurnID) |
| 165 | if err != nil || turn.Kind != wire.TurnMoveIn || len(turn.MessageIDs) != 0 { |
| 166 | t.Fatalf("move-in turn %+v %v", turn, err) |
| 167 | } |
| 168 | // The coordinator event matches its fixture. |
| 169 | s, err := r.client.Open(r.ctx, 0) |
| 170 | if err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | defer s.Close() |
| 174 | for e := range s.Events { |
| 175 | if e.Type == wire.EventCoordinator { |
| 176 | raw, _ := json.Marshal(e) |
| 177 | shape(t, "api/event-coordinator.json", raw) |
| 178 | break |
| 179 | } |
| 180 | } |
| 181 | // Another person's move-in is never started by this runner. |
| 182 | if err := r.runner.Send(r.ctx, "r-mi-5", wire.FrameMoveIn, wire.MoveIn{Person: "someone@example.com", Kind: wire.TurnMoveIn}); err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | for { |
| 186 | _, body, err := r.runner.Next(r.ctx) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | if a, ok := body.(*wire.Ack); ok && a.ID == "r-mi-5" { |
| 191 | break |
| 192 | } |
| 193 | } |
| 194 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_turns WHERE kind = ?", wire.TurnMoveIn).Scan(&moveIns); err != nil || moveIns != 1 { |
| 195 | t.Fatalf("move-in turns %d %v after another person's request", moveIns, err) |
| 196 | } |
| 197 | // Notes and moving-in tools need a running turn like any other. |
| 198 | if res := r.call("n3", wire.ToolWriteNote, map[string]any{"entries": 1, "bytes": 1}); res.Code != "turn_ended" { |
| 199 | t.Fatalf("write_note after the turn: %+v", res) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // A person's message never waits behind a move-in: the running move-in |
| 204 | // gives way (cancelled, reason yielded), the person is answered, then the |
| 205 | // move-in continues in a new turn. A queued move-in goes after them too. |
| 206 | func TestMoveInYieldsToThePerson(t *testing.T) { |
| 207 | r := newMoveInRig(t) |
| 208 | first := r.moveIn("r-mi-1", wire.TurnMoveIn) |
| 209 | sent, err := r.client.Send(r.ctx, "hello", "k-y1") |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | var cancel *wire.CancelCommand |
| 214 | for cancel == nil { |
| 215 | f, body, err := r.runner.Next(r.ctx) |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | switch v := body.(type) { |
| 220 | case *wire.CancelCommand: |
| 221 | cancel = v |
| 222 | _ = r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) |
| 223 | case *wire.MessageCommand: |
| 224 | t.Fatalf("dispatched before the move-in gave way: %+v", v) |
| 225 | } |
| 226 | } |
| 227 | if cancel.TurnID != first.TurnID || cancel.Attempt != 1 { |
| 228 | t.Fatalf("cancel %+v", cancel) |
| 229 | } |
| 230 | if err = r.runner.Send(r.ctx, "r-t-y", wire.FrameTurn, wire.TurnReport{TurnID: first.TurnID, Attempt: 1, State: wire.TurnCancelled, Reason: "cancelled"}); err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | r.wait("yielded", func(c *Conversation) bool { |
| 234 | return c.Turns[first.TurnID].State == wire.TurnCancelled && c.Turns[first.TurnID].Reason == wire.ReasonYielded |
| 235 | }) |
| 236 | person := r.dispatched(wire.TurnPerson) |
| 237 | if person.TurnID != sent.Turn.ID || withoutProjectContext(t, person.Text) != "hello" { |
| 238 | t.Fatalf("person turn %+v", person) |
| 239 | } |
| 240 | var cont string |
| 241 | r.wait("still moving in", func(c *Conversation) bool { |
| 242 | cont = c.Coordinator.TurnID |
| 243 | return c.Coordinator.State == wire.CoordinatorMovingIn && cont != "" && cont != first.TurnID |
| 244 | }) |
| 245 | r.reply(person.TurnID, "Hello.") |
| 246 | next := r.dispatched(wire.TurnMoveIn) |
| 247 | if next.TurnID != cont { |
| 248 | t.Fatalf("continued in %s, want %s", next.TurnID, cont) |
| 249 | } |
| 250 | r.reply(next.TurnID, "Hi, I'm your coordinator.") |
| 251 | r.wait("ready", func(c *Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) |
| 252 | // After an access change the runner asks to explore the difference. |
| 253 | if err := r.runner.Send(r.ctx, "r-ex-1", wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: wire.TurnExplore}); err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | explore := r.dispatched(wire.TurnExplore) |
| 257 | r.reply(explore.TurnID, "") |
| 258 | var replies []wire.Message |
| 259 | r.conv.Locked(func(c *Conversation) { replies = c.Replies() }) |
| 260 | if len(replies) != 2 { |
| 261 | t.Fatalf("an empty explore reply posts nothing; replies %+v", replies) |
| 262 | } |
| 263 | // A queued move-in waits for a person turn sent after it. |
| 264 | held, err := r.client.Send(r.ctx, "sleep:1 first", "k-y2") |
| 265 | if err != nil { |
| 266 | t.Fatal(err) |
| 267 | } |
| 268 | busy := r.dispatched(wire.TurnPerson) |
| 269 | if err := r.runner.Send(r.ctx, "r-ex-2", wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: wire.TurnExplore}); err != nil { |
| 270 | t.Fatal(err) |
| 271 | } |
| 272 | later, err := r.client.Send(r.ctx, "second", "k-y3") |
| 273 | if err != nil { |
| 274 | t.Fatal(err) |
| 275 | } |
| 276 | r.reply(busy.TurnID, "Ok.") |
| 277 | if again := r.dispatched(wire.TurnPerson); again.TurnID != later.Turn.ID || held.Turn.ID != busy.TurnID { |
| 278 | t.Fatalf("person turn after a queued explore: %+v", again) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Astra 2b review 3: the instance's coordinator state is the authority on |
| 283 | // introductions. Once ready, a move-in request (a runner that lost its |
| 284 | // desk record) becomes an explore turn: it may look around again, but |
| 285 | // never introduces itself twice. |
| 286 | func TestMoveInAfterReadyNeverIntroducesTwice(t *testing.T) { |
| 287 | r := newMoveInRig(t) |
| 288 | first := r.moveIn("r-mi-1", wire.TurnMoveIn) |
| 289 | r.reply(first.TurnID, "Hi, I'm your coordinator.") |
| 290 | r.wait("ready", func(c *Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) |
| 291 | if err := r.runner.Send(r.ctx, "r-mi-2", wire.FrameMoveIn, wire.MoveIn{Person: Owner, Kind: wire.TurnMoveIn}); err != nil { |
| 292 | t.Fatal(err) |
| 293 | } |
| 294 | again := r.dispatched(wire.TurnExplore) |
| 295 | r.reply(again.TurnID, "Hi again, I'm your coordinator.") |
| 296 | var intros, replies int |
| 297 | r.conv.Locked(func(c *Conversation) { |
| 298 | for _, m := range c.Replies() { |
| 299 | replies++ |
| 300 | if c.Turns[m.TurnID].Kind == wire.TurnMoveIn { |
| 301 | intros++ |
| 302 | } |
| 303 | } |
| 304 | }) |
| 305 | if intros != 1 { |
| 306 | t.Fatalf("introductions %d", intros) |
| 307 | } |
| 308 | r.conv.Locked(func(c *Conversation) { |
| 309 | if c.Coordinator.State != wire.CoordinatorReady { |
| 310 | t.Fatalf("coordinator %+v", c.Coordinator) |
| 311 | } |
| 312 | }) |
| 313 | } |
| 314 | |
| 315 | // Astra 2b review 1: the owner sees the notebook with each entry's |
| 316 | // source, as the runner reports it; Reset is refused while a turn runs, |
| 317 | // then clears it, tells the runner (which empties the desk and forgets |
| 318 | // the session) and lets the coordinator move in again. |
| 319 | func TestContractNotebook(t *testing.T) { |
| 320 | r := newMoveInRig(t) |
| 321 | empty, err := r.client.Notebook(r.ctx) |
| 322 | if err != nil || len(empty.Entries) != 0 || empty.ID != Owner { |
| 323 | t.Fatalf("empty notebook %+v %v", empty, err) |
| 324 | } |
| 325 | report := wire.NotebookReport{Person: Owner, Entries: []wire.NotebookEntry{ |
| 326 | {ID: "n1", Source: wire.NotePerson, Text: "I like short replies.", At: time.Now().UTC()}, |
| 327 | {ID: "n2", Source: wire.NoteRepo, Repo: "oficina/equipo", Path: "README.md", Text: "Equipo runs a team's coordinator.", At: time.Now().UTC()}, |
| 328 | }} |
| 329 | if err := r.runner.Send(r.ctx, "r-nb-1", wire.FrameNotebook, report); err != nil { |
| 330 | t.Fatal(err) |
| 331 | } |
| 332 | shape(t, "frames/notebook.json", r.runner.Sent[len(r.runner.Sent)-1]) |
| 333 | // Another person's notebook is not this runner's to report. |
| 334 | if err := r.runner.Send(r.ctx, "r-nb-2", wire.FrameNotebook, wire.NotebookReport{Person: "someone@example.com", Entries: []wire.NotebookEntry{{ID: "x", Source: wire.NoteOwn, Text: "planted"}}}); err != nil { |
| 335 | t.Fatal(err) |
| 336 | } |
| 337 | for { |
| 338 | _, body, err := r.runner.Next(r.ctx) |
| 339 | if err != nil { |
| 340 | t.Fatal(err) |
| 341 | } |
| 342 | if a, ok := body.(*wire.Ack); ok && a.ID == "r-nb-2" { |
| 343 | break |
| 344 | } |
| 345 | } |
| 346 | got, err := r.client.Notebook(r.ctx) |
| 347 | if err != nil || len(got.Entries) != 2 || got.Entries[1].Repo != "oficina/equipo" || got.Entries[0].Source != wire.NoteOwn { |
| 348 | t.Fatalf("notebook %+v %v", got, err) |
| 349 | } |
| 350 | shape(t, "api/notebook.json", got) |
| 351 | |
| 352 | // Refused while a turn is in flight. |
| 353 | first := r.moveIn("r-mi-1", wire.TurnMoveIn) |
| 354 | if _, err := r.client.ResetNotebook(r.ctx); Status(err) != 409 { |
| 355 | t.Fatalf("reset during a turn: %v", err) |
| 356 | } |
| 357 | r.reply(first.TurnID, "Hi, I'm your coordinator.") |
| 358 | r.wait("ready", func(c *Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) |
| 359 | c, err := r.client.ResetNotebook(r.ctx) |
| 360 | if err != nil || c.State != wire.CoordinatorNew { |
| 361 | t.Fatalf("reset %+v %v", c, err) |
| 362 | } |
| 363 | f, body, err := r.runner.Expect(r.ctx, wire.FrameReset) |
| 364 | if err != nil || body.(*wire.ResetCommand).Person != Owner { |
| 365 | t.Fatalf("reset frame %v %v", body, err) |
| 366 | } |
| 367 | shape(t, "frames/reset.json", r.runner.Received[len(r.runner.Received)-1]) |
| 368 | _ = r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) |
| 369 | if got, _ := r.client.Notebook(r.ctx); len(got.Entries) != 0 { |
| 370 | t.Fatalf("notebook after reset %+v", got) |
| 371 | } |
| 372 | // Moving in again introduces itself again: the person asked for it. |
| 373 | again := r.moveIn("r-mi-2", wire.TurnMoveIn) |
| 374 | r.reply(again.TurnID, "Hi again.") |
| 375 | r.wait("ready again", func(c *Conversation) bool { return c.Coordinator.State == wire.CoordinatorReady }) |
| 376 | } |
| 377 | |