| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestSelfHealWaitIsLiveAndRetries(t *testing.T) { |
| 9 | f, x, p, _ := coordinatorFixture(t) |
| 10 | ownerReports(t, f) |
| 11 | queueCoordinator(t, f, "hello", "self-heal-wait") |
| 12 | before := f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system'") |
| 13 | f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false'))") |
| 14 | offers, err := x.PollCoordinator(ctx, p) |
| 15 | must(t, err) |
| 16 | if len(offers) != 0 { |
| 17 | t.Fatal("unavailable admitted") |
| 18 | } |
| 19 | page, err := f.Store.Conversation(ctx, owner, "", 50) |
| 20 | must(t, err) |
| 21 | thinking := false |
| 22 | for _, card := range page.Cards { |
| 23 | thinking = thinking || card.Thinking |
| 24 | } |
| 25 | if !thinking { |
| 26 | t.Fatal("temporary wait has no thinking bubble") |
| 27 | } |
| 28 | if f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system'") != before { |
| 29 | t.Fatal("temporary wait added system note") |
| 30 | } |
| 31 | f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('true')) WHERE id='claude'") |
| 32 | offers, err = x.PollCoordinator(ctx, p) |
| 33 | must(t, err) |
| 34 | if len(offers) != 1 { |
| 35 | t.Fatal("recovered connection did not retry") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestSelfHealFailoverDoesNotMoveActiveTurn(t *testing.T) { |
| 40 | f, x, p, b := coordinatorFixture(t) |
| 41 | ownerReports(t, f) |
| 42 | turn := queueCoordinator(t, f, "hello", "active") |
| 43 | claim, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) |
| 44 | must(t, err) |
| 45 | before, err := x.Core.GetJob(ctx, claim.Scope) |
| 46 | must(t, err) |
| 47 | f.exec(t, `UPDATE connections SET reports_json=json_set(reports_json,'$[0].OwnerMode',json('true'),'$[0].LauncherVersion','direct-v3','$[0].PolicyVersion','direct-v1') WHERE id='codex'`) |
| 48 | f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false')) WHERE id='claude'") |
| 49 | _, err = x.PollCoordinator(ctx, p) |
| 50 | must(t, err) |
| 51 | job, err := x.Core.GetJob(ctx, claim.Scope) |
| 52 | must(t, err) |
| 53 | if job.State != before.State { |
| 54 | t.Fatal("automatic fallback cancelled active job") |
| 55 | } |
| 56 | after, err := f.Store.Coordinator(ctx, owner) |
| 57 | must(t, err) |
| 58 | if after.ConnectionID != b.ConnectionID || after.Generation != b.Generation { |
| 59 | t.Fatal("active turn moved") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestSelfHealBusyConnectionFailsOver(t *testing.T) { |
| 64 | f, x, p, _ := coordinatorFixture(t) |
| 65 | ownerReports(t, f) |
| 66 | first := queueCoordinator(t, f, "first", "busy-first") |
| 67 | c, err := x.ClaimCoordinator(ctx, p, first.ID, first.Revision) |
| 68 | must(t, err) |
| 69 | f.exec(t, `UPDATE coordinator_turns SET state='delivered',attempt_id=0,job_id=0 WHERE id=?`, first.ID) |
| 70 | f.exec(t, `INSERT INTO runner_assignments(id,task_id,person,state,attempt_id,selection) VALUES('busy','busy-task',?,'claimed',?,?)`, owner.Address, c.Handle.Attempt, `{"ConnectionID":"claude"}`) |
| 71 | f.exec(t, `UPDATE connections SET reports_json=json_set(reports_json,'$[0].OwnerMode',json('true'),'$[0].LauncherVersion','direct-v3','$[0].PolicyVersion','direct-v1') WHERE id='codex'`) |
| 72 | queueCoordinator(t, f, "second", "busy-second") |
| 73 | turns, err := x.PollCoordinator(ctx, p) |
| 74 | must(t, err) |
| 75 | if len(turns) != 1 { |
| 76 | t.Fatal("idle connection was not offered", turns) |
| 77 | } |
| 78 | b, err := f.Store.Coordinator(ctx, owner) |
| 79 | must(t, err) |
| 80 | if b.ConnectionID != "codex" { |
| 81 | t.Fatal("busy connection retained", b.ConnectionID) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestSelfHealProjectCardThenWorkAdmitted(t *testing.T) { |
| 86 | f, x, p, c := claimTools(t) |
| 87 | f.exec(t, "INSERT INTO runner_repositories(runner_id,repository) VALUES(?,?)", p.RunnerID, "amadan.net/oficina/self-heal-app") |
| 88 | proposal := toolCall(t, x, p, c, "setup", "propose_project", map[string]any{"repository": "amadan.net/oficina/self-heal-app", "why": "Requested"}) |
| 89 | must(t, f.Store.AnswerProjectProposal(ctx, owner, proposal.ResourceID, true, equipo.Mutation{RequestID: "accept", Revision: proposal.Revision})) |
| 90 | var project int64 |
| 91 | must(t, f.DB.Writer().QueryRow("SELECT project_id FROM project_proposals WHERE id=?", proposal.ResourceID).Scan(&project)) |
| 92 | budgets, err := f.Store.Budgets(ctx) |
| 93 | must(t, err) |
| 94 | budgets.Projects = nil |
| 95 | must(t, f.Store.SaveBudgets(ctx, owner, budgets)) |
| 96 | task, err := f.Store.CreateBug(ctx, owner, project, "Fix this project", "new-project-work") |
| 97 | must(t, err) |
| 98 | if task.ProjectID != project { |
| 99 | t.Fatal("work lost project") |
| 100 | } |
| 101 | } |
| 102 | |