| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func ownerReports(t *testing.T, f *fixture) { |
| 12 | t.Helper() |
| 13 | var raw string |
| 14 | must(t, f.DB.Writer().QueryRow("SELECT reports_json FROM connections WHERE id='claude'").Scan(&raw)) |
| 15 | var reports []equipo.ConnectionReport |
| 16 | must(t, json.Unmarshal([]byte(raw), &reports)) |
| 17 | reports[0].OwnerMode = true |
| 18 | reports[0].LauncherVersion = "direct-v3" |
| 19 | reports[0].PolicyVersion = "direct-v1" |
| 20 | reports[0].IsolationConfigHash = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" |
| 21 | rawBytes, err := json.Marshal(reports) |
| 22 | must(t, err) |
| 23 | f.exec(t, "UPDATE connections SET reports_json=? WHERE id='claude'", string(rawBytes)) |
| 24 | } |
| 25 | |
| 26 | func RunOwnerModeSelectionAndDefaults(t *testing.T) { |
| 27 | f, x, p, _ := coordinatorFixture(t) |
| 28 | ownerReports(t, f) |
| 29 | f.exec(t, "DELETE FROM coordinator_limits") |
| 30 | f.exec(t, "DELETE FROM budget_records") |
| 31 | f.exec(t, "DELETE FROM period_budgets") |
| 32 | turn := queueCoordinator(t, f, "hello", "owner-mode") |
| 33 | offers, err := x.PollCoordinator(ctx, p) |
| 34 | must(t, err) |
| 35 | if len(offers) != 1 { |
| 36 | t.Fatalf("owner mode did not offer turn: %+v", offers) |
| 37 | } |
| 38 | claim, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) |
| 39 | must(t, err) |
| 40 | if claim.Brief.Selection.Isolation.Mode != "owner mode" { |
| 41 | t.Fatal("claim lost mode") |
| 42 | } |
| 43 | if f.count(t, "SELECT count(*) FROM coordinator_limits") != 1 || f.count(t, "SELECT count(*) FROM budget_records") != 1 { |
| 44 | t.Fatal("defaults not saved") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func RunOwnerModeForeignRunnerDenied(t *testing.T) { |
| 49 | f, x, p, _ := coordinatorFixture(t) |
| 50 | ownerReports(t, f) |
| 51 | queueCoordinator(t, f, "hello", "foreign-owner-mode") |
| 52 | f.exec(t, "UPDATE runners SET person=? WHERE id=?", peer.Address, p.RunnerID) |
| 53 | offers, err := x.PollCoordinator(ctx, p) |
| 54 | if err == nil && len(offers) > 0 { |
| 55 | t.Fatal("foreign runner admitted owner work") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func RunOwnerModeFallback(t *testing.T) { |
| 60 | f, x, p, b := coordinatorFixture(t) |
| 61 | ownerReports(t, f) |
| 62 | queueCoordinator(t, f, "hello", "fallback-owner") |
| 63 | // The current binding points at a broken profile; claude remains available. |
| 64 | f.exec(t, "UPDATE coordinator_bindings SET connection_id='codex' WHERE id=?", b.ID) |
| 65 | f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false')) WHERE id='codex'") |
| 66 | offers, err := x.PollCoordinator(ctx, p) |
| 67 | must(t, err) |
| 68 | if len(offers) != 1 { |
| 69 | t.Fatal("fallback did not release queued turn", offers) |
| 70 | } |
| 71 | if f.count(t, "SELECT count(*) FROM coordinator_bindings WHERE id=? AND connection_id='claude' AND generation=2", b.ID) != 1 { |
| 72 | t.Fatal("fallback binding not saved") |
| 73 | } |
| 74 | if f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system' AND body LIKE '%coordinator%'") != 0 { |
| 75 | t.Fatal("automatic fallback added system note") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func RunOwnerModeDenialNote(t *testing.T) { |
| 80 | f, x, p, _ := coordinatorFixture(t) |
| 81 | ownerReports(t, f) |
| 82 | queueCoordinator(t, f, "hello", "denial-owner") |
| 83 | f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false'))") |
| 84 | offers, err := x.PollCoordinator(ctx, p) |
| 85 | must(t, err) |
| 86 | if len(offers) != 0 { |
| 87 | t.Fatal("unavailable admitted") |
| 88 | } |
| 89 | page, err := f.Store.Conversation(ctx, owner, "", 50) |
| 90 | must(t, err) |
| 91 | if len(page.Cards) == 0 { |
| 92 | t.Fatal("blocked turn missing conversation card") |
| 93 | } |
| 94 | if f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system' AND body LIKE '%connection%'") != 0 { |
| 95 | t.Fatal("temporary connection denial added system note") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func RunOwnerModeConfiguredLimitsStay(t *testing.T) { |
| 100 | f, x, p, _ := coordinatorFixture(t) |
| 101 | ownerReports(t, f) |
| 102 | before, revision, err := x.Core.CoordinatorLimits(ctx, team.AccountID(f.TeamID), owner.Address) |
| 103 | must(t, err) |
| 104 | queueCoordinator(t, f, "hello", "limits-stay") |
| 105 | _, err = x.PollCoordinator(ctx, p) |
| 106 | must(t, err) |
| 107 | after, next, err := x.Core.CoordinatorLimits(ctx, team.AccountID(f.TeamID), owner.Address) |
| 108 | must(t, err) |
| 109 | if before != after || revision != next { |
| 110 | t.Fatal("configured limits overwritten") |
| 111 | } |
| 112 | } |
| 113 | func RunOwnerModePausedNote(t *testing.T) { |
| 114 | f, x, p, _ := coordinatorFixture(t) |
| 115 | ownerReports(t, f) |
| 116 | queueCoordinator(t, f, "hello", "paused-note") |
| 117 | f.exec(t, "UPDATE service SET admission_paused=1 WHERE id=1") |
| 118 | offers, err := x.PollCoordinator(ctx, p) |
| 119 | must(t, err) |
| 120 | if len(offers) > 0 || f.count(t, "SELECT count(*) FROM coordinator_turns WHERE state='admission_denied'") != 1 { |
| 121 | t.Fatal("paused admission not visible") |
| 122 | } |
| 123 | if f.count(t, "SELECT count(*) FROM conversation_messages WHERE author_kind='system' AND body LIKE '%cannot start%'") != 0 { |
| 124 | t.Fatal("admission wait added system note") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func RunOwnerModeStaleBindingNote(t *testing.T) { |
| 129 | f, x, p, _ := coordinatorFixture(t) |
| 130 | ownerReports(t, f) |
| 131 | queueCoordinator(t, f, "hello", "stale-note") |
| 132 | f.exec(t, "UPDATE coordinator_bindings SET connection_revision=99") |
| 133 | f.exec(t, "UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false'))") |
| 134 | offers, err := x.PollCoordinator(ctx, p) |
| 135 | if !errors.Is(err, equipo.ErrForbidden) { |
| 136 | t.Fatal("authority denial lost", err) |
| 137 | } |
| 138 | if len(offers) > 0 || f.count(t, "SELECT count(*) FROM coordinator_turns WHERE state='admission_denied'") != 1 { |
| 139 | t.Fatal("stale binding denial hidden") |
| 140 | } |
| 141 | } |
| 142 | |