| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestProjectsReviewPrivateNamesAndSnapshot(t *testing.T) { |
| 11 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 12 | p, err := r.client.AddProject(r.ctx, "oficina/equipo") |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+p.ID+"/star", wire.SetProjectStar{Starred: true}, nil); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | snap, err := r.client.Snapshot(r.ctx) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if len(snap.Projects) != 1 || !snap.Projects[0].Starred { |
| 24 | t.Fatal("snapshot lost star", snap.Projects) |
| 25 | } |
| 26 | for _, person := range []string{Owner, "teammate@example.com"} { |
| 27 | if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_project_names(project_id,person,name) VALUES(?,?,?)", p.ID, person, "my portal"); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | page := r.get(r.inst.Cookie(), "/projects/"+p.ID, 200) |
| 32 | if !strings.Contains(page, "my portal") || !strings.Contains(page, "Remove this name") { |
| 33 | t.Fatal("no private name removal form") |
| 34 | } |
| 35 | if err := r.client.do(r.ctx, "DELETE", "/api/v1/projects/"+p.ID+"/names", map[string]string{"name": "my portal"}, nil); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := r.client.do(r.ctx, "DELETE", "/api/v1/projects/"+p.ID+"/names", map[string]string{"name": "my portal"}, nil); err != nil { |
| 39 | t.Fatal("delete not idempotent", err) |
| 40 | } |
| 41 | var n int |
| 42 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_project_names WHERE person = ?", "teammate@example.com").Scan(&n); err != nil || n != 1 { |
| 43 | t.Fatal("other person's name removed", n, err) |
| 44 | } |
| 45 | if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_project_names(project_id,person,name) VALUES(?,?,?)", p.ID, Owner, "web portal"); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | status, _ := r.post(r.inst.Cookie(), "/projects/"+p.ID+"/names/remove", url.Values{"name": {"web portal"}}) |
| 49 | if status != 303 { |
| 50 | t.Fatal("web remove", status) |
| 51 | } |
| 52 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_project_names WHERE person = ?", Owner).Scan(&n); err != nil || n != 0 { |
| 53 | t.Fatal("own name remains", n, err) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestProjectsReviewQueuedSelection(t *testing.T) { |
| 58 | for _, removed := range []bool{true, false} { |
| 59 | t.Run(map[bool]string{true: "removed", false: "unreachable"}[removed], func(t *testing.T) { |
| 60 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 61 | p, err := r.client.AddProject(r.ctx, "oficina/equipo") |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | var selected wire.Sent |
| 66 | if err := r.client.do(r.ctx, "POST", "/api/v1/messages", wire.SendMessage{Text: "Fix it", ProjectID: p.ID, IdempotencyKey: "selected-queue"}, &selected); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if removed { |
| 70 | if _, err := r.client.RemoveProject(r.ctx, p.ID); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | } else { |
| 74 | if _, err := r.inst.DB.Writer().Exec("UPDATE runner_sessions SET hello = ?", `{"repos":[]}`); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | } |
| 78 | if err := r.runner.Send(r.ctx, "reply-before-selected", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Previous reply retained."}); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | _, body := r.expect(wire.FrameMessage) |
| 82 | cmd := body.(*wire.MessageCommand) |
| 83 | if cmd.TurnID != selected.Turn.ID || !strings.Contains(cmd.Text, "no longer available") { |
| 84 | t.Fatalf("selected queue stalled: %+v", cmd) |
| 85 | } |
| 86 | snap, err := r.client.Snapshot(r.ctx) |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | found := false |
| 91 | for _, m := range snap.Messages { |
| 92 | if m.Text == "Previous reply retained." { |
| 93 | found = true |
| 94 | } |
| 95 | } |
| 96 | if !found { |
| 97 | t.Fatal("previous reply lost") |
| 98 | } |
| 99 | // A frame from the now unbound turn still produces a tool result. |
| 100 | r.turn = selected.Turn.ID |
| 101 | got := r.call("unknown-after-stale", wire.ToolStartTask, map[string]any{"task": "unknown"}) |
| 102 | if !got.IsError { |
| 103 | t.Fatal("unknown task did not refuse", got) |
| 104 | } |
| 105 | follow, err := r.client.Send(r.ctx, "Continue", "after-stale") |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if err := r.runner.Send(r.ctx, "reply-after-selected", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Continued."}); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | _, body = r.expect(wire.FrameMessage) |
| 113 | if body.(*wire.MessageCommand).TurnID != follow.Turn.ID { |
| 114 | t.Fatal("lane remained poisoned") |
| 115 | } |
| 116 | }) |
| 117 | } |
| 118 | } |
| 119 | |