package plan3test import ( "amadan.net/oficina/equipo/internal/equipo" "testing" ) func TestSelfHealWaitIsLiveAndRetries(t *testing.T) { f, x, p, _ := coordinatorFixture(t) ownerReports(t, f) queueCoordinator(t, f, "hello", "self-heal-wait") before := f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system'") f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false'))") offers, err := x.PollCoordinator(ctx, p) must(t, err) if len(offers) != 0 { t.Fatal("unavailable admitted") } page, err := f.Store.Conversation(ctx, owner, "", 50) must(t, err) thinking := false for _, card := range page.Cards { thinking = thinking || card.Thinking } if !thinking { t.Fatal("temporary wait has no thinking bubble") } if f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system'") != before { t.Fatal("temporary wait added system note") } f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('true')) WHERE id='claude'") offers, err = x.PollCoordinator(ctx, p) must(t, err) if len(offers) != 1 { t.Fatal("recovered connection did not retry") } } func TestSelfHealFailoverDoesNotMoveActiveTurn(t *testing.T) { f, x, p, b := coordinatorFixture(t) ownerReports(t, f) turn := queueCoordinator(t, f, "hello", "active") claim, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) must(t, err) before, err := x.Core.GetJob(ctx, claim.Scope) must(t, err) 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'`) f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false')) WHERE id='claude'") _, err = x.PollCoordinator(ctx, p) must(t, err) job, err := x.Core.GetJob(ctx, claim.Scope) must(t, err) if job.State != before.State { t.Fatal("automatic fallback cancelled active job") } after, err := f.Store.Coordinator(ctx, owner) must(t, err) if after.ConnectionID != b.ConnectionID || after.Generation != b.Generation { t.Fatal("active turn moved") } } func TestSelfHealBusyConnectionFailsOver(t *testing.T) { f, x, p, _ := coordinatorFixture(t) ownerReports(t, f) first := queueCoordinator(t, f, "first", "busy-first") c, err := x.ClaimCoordinator(ctx, p, first.ID, first.Revision) must(t, err) f.exec(t, `UPDATE coordinator_turns SET state='delivered',attempt_id=0,job_id=0 WHERE id=?`, first.ID) 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"}`) 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'`) queueCoordinator(t, f, "second", "busy-second") turns, err := x.PollCoordinator(ctx, p) must(t, err) if len(turns) != 1 { t.Fatal("idle connection was not offered", turns) } b, err := f.Store.Coordinator(ctx, owner) must(t, err) if b.ConnectionID != "codex" { t.Fatal("busy connection retained", b.ConnectionID) } } func TestSelfHealProjectCardThenWorkAdmitted(t *testing.T) { f, x, p, c := claimTools(t) f.exec(t, "INSERT INTO runner_repositories(runner_id,repository) VALUES(?,?)", p.RunnerID, "amadan.net/oficina/self-heal-app") proposal := toolCall(t, x, p, c, "setup", "propose_project", map[string]any{"repository": "amadan.net/oficina/self-heal-app", "why": "Requested"}) must(t, f.Store.AnswerProjectProposal(ctx, owner, proposal.ResourceID, true, equipo.Mutation{RequestID: "accept", Revision: proposal.Revision})) var project int64 must(t, f.DB.Writer().QueryRow("SELECT project_id FROM project_proposals WHERE id=?", proposal.ResourceID).Scan(&project)) budgets, err := f.Store.Budgets(ctx) must(t, err) budgets.Projects = nil must(t, f.Store.SaveBudgets(ctx, owner, budgets)) task, err := f.Store.CreateBug(ctx, owner, project, "Fix this project", "new-project-work") must(t, err) if task.ProjectID != project { t.Fatal("work lost project") } }