| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // env is one opened store with its fakes. Every test builds its own in a |
| 13 | // scratch directory; nothing reaches a network, an env var or a login. |
| 14 | type env struct { |
| 15 | t *testing.T |
| 16 | ctx context.Context |
| 17 | dir string |
| 18 | path string |
| 19 | clock *FakeClock |
| 20 | auth *FakeAuthority |
| 21 | impl *FakeProvider |
| 22 | reviewer *FakeProvider |
| 23 | forge *FakeForge |
| 24 | tracker *FakeTracker |
| 25 | store *Store |
| 26 | } |
| 27 | |
| 28 | const ( |
| 29 | acctA = AccountID("acct-a") |
| 30 | acctB = AccountID("acct-b") |
| 31 | grant = "grant-a" |
| 32 | ) |
| 33 | |
| 34 | func newEnv(t *testing.T) *env { |
| 35 | t.Helper() |
| 36 | e := &env{t: t, ctx: context.Background(), dir: t.TempDir()} |
| 37 | e.path = filepath.Join(e.dir, "team.db") |
| 38 | e.clock = NewFakeClock(time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)) |
| 39 | e.auth = NewFakeAuthority(e.clock, 5*time.Second) |
| 40 | e.auth.Issue(grant, Grant{Account: acctA, Operations: map[Operation]bool{ |
| 41 | OpProviderCall: true, OpForgePublish: true, OpTrackerPublish: true, OpJobContinue: true, OpAttemptContinue: true}}) |
| 42 | e.impl = NewFakeProvider("fake-a", "model-a", map[string]string{"implement": FixtureFixedSource, "reconcile": ""}) |
| 43 | e.reviewer = NewFakeProvider("fake-b", "model-b", map[string]string{"review": "approve: candidate fixes the symptom and nothing else"}) |
| 44 | e.forge = NewFakeForge() |
| 45 | e.tracker = NewFakeTracker() |
| 46 | e.open() |
| 47 | t.Cleanup(func() { |
| 48 | if e.store != nil { |
| 49 | e.store.Close() |
| 50 | } |
| 51 | }) |
| 52 | return e |
| 53 | } |
| 54 | |
| 55 | func (e *env) options() Options { |
| 56 | return Options{Clock: e.clock, Authority: e.auth, Adapters: map[Operation]Adapter{ |
| 57 | OpProviderCall: providerMux{e.impl, e.reviewer}, OpForgePublish: e.forge, OpTrackerPublish: e.tracker}} |
| 58 | } |
| 59 | |
| 60 | // open opens (or reopens) the store at e.path and resumes it. |
| 61 | func (e *env) open() { |
| 62 | e.t.Helper() |
| 63 | s, err := Open(e.ctx, e.path, e.options()) |
| 64 | if err != nil { |
| 65 | e.t.Fatalf("open: %v", err) |
| 66 | } |
| 67 | e.store = s |
| 68 | if _, err := s.Resume(e.ctx); err != nil { |
| 69 | e.t.Fatalf("resume: %v", err) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // providerMux routes provider calls by the payload's task so one adapter |
| 74 | // slot serves both the implementer and the independent reviewer. |
| 75 | type providerMux struct{ impl, review *FakeProvider } |
| 76 | |
| 77 | func (m providerMux) pick(d Dispatch) *FakeProvider { |
| 78 | var in providerPayload |
| 79 | _ = strictJSON(d.Payload, &in) |
| 80 | if in.Task == "review" { |
| 81 | return m.review |
| 82 | } |
| 83 | return m.impl |
| 84 | } |
| 85 | func (m providerMux) Classify(op Operation) (ReplayClass, error) { return m.impl.Classify(op) } |
| 86 | func (m providerMux) Execute(ctx context.Context, d Dispatch) (Receipt, error) { |
| 87 | return m.pick(d).Execute(ctx, d) |
| 88 | } |
| 89 | func (m providerMux) Reconcile(ctx context.Context, d Dispatch) (ReconcileResult, error) { |
| 90 | return m.pick(d).Reconcile(ctx, d) |
| 91 | } |
| 92 | |
| 93 | func testRates() Rates { |
| 94 | return Rates{Version: 1, InputMicrosPer: 3, OutputMicrosPer: 15, CallMicrosPer: 1000, ForgeMicrosPer: 500, TrackerMicrosPer: 100} |
| 95 | } |
| 96 | |
| 97 | func testPolicy() Policy { return DefaultPolicy(5_000_000, testRates()) } |
| 98 | |
| 99 | func softwareSpec(steps []StepSpec) JobSpec { |
| 100 | return JobSpec{ |
| 101 | Account: acctA, Requester: "paul", GrantRef: grant, |
| 102 | Target: TargetBinding{Kind: TargetRepository, Repository: "example/shop", Ref: "team/fix-sum", DefaultRef: "main", BaseRevision: hashBytes([]byte(FixtureBaseSource))}, |
| 103 | Evidence: EvidenceRequirement{Predicate: PredicateSoftwareFix, ExpectedSymptom: FixtureSymptom, CheckDefinitionHash: CheckDefinitionHash}, |
| 104 | Policy: testPolicy(), |
| 105 | Steps: steps, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func (e *env) createJob(spec JobSpec) (Job, Scope) { |
| 110 | e.t.Helper() |
| 111 | if spec.Policy.Version == 2 { |
| 112 | if spec.Target.Kind != TargetConversation && spec.Project == 0 { |
| 113 | spec.Project = 1 |
| 114 | } |
| 115 | if _, err := e.store.ConfiguredBudgets(e.ctx, spec.Account); errors.Is(err, ErrBudget) { |
| 116 | b := periodSet() |
| 117 | l := PeriodLimits{Calls: 10000, InputTokens: 100000000, OutputTokens: 100000000, Seconds: 100000000} |
| 118 | b.Team = l |
| 119 | for id := range b.Projects { |
| 120 | b.Projects[id] = l |
| 121 | } |
| 122 | must(e.t, e.store.ConfigureBudgets(e.ctx, spec.Account, 0, b)) |
| 123 | } else { |
| 124 | must(e.t, err) |
| 125 | } |
| 126 | } |
| 127 | j, err := e.store.CreateJob(e.ctx, spec) |
| 128 | if err != nil { |
| 129 | e.t.Fatalf("create job: %v", err) |
| 130 | } |
| 131 | return j, Scope{Account: j.Account, Job: j.ID} |
| 132 | } |
| 133 | |
| 134 | func (e *env) workflow(scope Scope) *Workflow { |
| 135 | return &Workflow{Store: e.store, Scope: scope, Implementer: e.impl, Reviewer: e.reviewer, Forge: e.forge, Tracker: e.tracker, Supervisor: "supervisor", Log: e.t.Logf} |
| 136 | } |
| 137 | |
| 138 | func wantCode(t *testing.T, err error, code Code) { |
| 139 | t.Helper() |
| 140 | if err == nil { |
| 141 | t.Fatalf("want %s, got nil", code) |
| 142 | } |
| 143 | if CodeOf(err) != code { |
| 144 | t.Fatalf("want %s, got %v", code, err) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func must(t *testing.T, err error) { |
| 149 | t.Helper() |
| 150 | if err != nil { |
| 151 | t.Fatalf("unexpected: %v", err) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func (e *env) job(scope Scope) Job { |
| 156 | e.t.Helper() |
| 157 | j, err := e.store.GetJob(e.ctx, scope) |
| 158 | must(e.t, err) |
| 159 | return j |
| 160 | } |
| 161 | |
| 162 | func (e *env) claim(scope Scope, step, runner, ws string) Handle { |
| 163 | e.t.Helper() |
| 164 | h, err := e.store.Claim(e.ctx, scope, step, runner, ws) |
| 165 | must(e.t, err) |
| 166 | return h |
| 167 | } |
| 168 | |
| 169 | func (e *env) hasEvent(scope Scope, typ string) bool { |
| 170 | e.t.Helper() |
| 171 | evs, err := e.store.Events(e.ctx, scope, 0) |
| 172 | must(e.t, err) |
| 173 | for _, ev := range evs { |
| 174 | if ev.Type == typ { |
| 175 | return true |
| 176 | } |
| 177 | } |
| 178 | return false |
| 179 | } |
| 180 | |
| 181 | var _ = errors.Is |
| 182 | |
| 183 | func dropRecoverySchema(t *testing.T, db *sql.DB) { |
| 184 | t.Helper() |
| 185 | dropRunnerPolicySchema(t, db) |
| 186 | for _, q := range []string{`DROP TABLE recovery_events`, `DROP TABLE recovery_charges`, `DROP TABLE grant_fences`, `ALTER TABLE service DROP COLUMN release_required`, `ALTER TABLE service DROP COLUMN recovery_revision`, `ALTER TABLE service DROP COLUMN reconciled_revision`, `ALTER TABLE service DROP COLUMN clock_high_water`, `ALTER TABLE jobs DROP COLUMN grant_stale`, `ALTER TABLE time_reservations DROP COLUMN elapsed_unknown`} { |
| 187 | _, err := db.Exec(q) |
| 188 | must(t, err) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func dropRunnerPolicySchema(t *testing.T, db *sql.DB) { |
| 193 | t.Helper() |
| 194 | for _, q := range []string{"DROP TABLE admission_tickets", "DROP INDEX jobs_request", "ALTER TABLE jobs DROP COLUMN request_key", "ALTER TABLE jobs DROP COLUMN request_hash"} { |
| 195 | _, err := db.Exec(q) |
| 196 | must(t, err) |
| 197 | } |
| 198 | for _, q := range []string{"DROP INDEX reservations_period_account", "DROP INDEX reservations_period_project", "DROP INDEX time_period_account", "DROP INDEX time_period_project", "DROP INDEX recovery_charges_scope", "ALTER TABLE reservations DROP COLUMN project_id", "ALTER TABLE reservations DROP COLUMN period", "ALTER TABLE reservations DROP COLUMN cost_unavailable", "ALTER TABLE reservations DROP COLUMN s_cached", "ALTER TABLE period_budgets DROP COLUMN content_hash"} { |
| 199 | _, err := db.Exec(q) |
| 200 | must(t, err) |
| 201 | } |
| 202 | } |
| 203 | |