| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 5 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestIdlePreflightStorageBounded(t *testing.T) { |
| 13 | j, l, _, p := directFixture(t, "codex") |
| 14 | ctx := context.Background() |
| 15 | _, err := j.db.Exec("DELETE FROM direct_attempts") |
| 16 | mustJournal(t, err) |
| 17 | for i := 0; i < 12; i++ { |
| 18 | l.AttemptKey = fmt.Sprintf("codex-preflight-%d", i) |
| 19 | l.Selection.ConnectionID = "" |
| 20 | p.record.AttemptKey = l.AttemptKey |
| 21 | p.record.ID = fmt.Sprintf("%032x", i+1) |
| 22 | mustJournal(t, j.recordDirectLaunch(ctx, l, p.record)) |
| 23 | mustJournal(t, j.AppendFrame(ctx, l.AttemptKey, p.record.ID, 1, []byte(`{"id":1,"result":{}}`))) |
| 24 | _, err = j.db.Exec("UPDATE direct_attempts SET contained=1 WHERE attempt=?", l.AttemptKey) |
| 25 | mustJournal(t, err) |
| 26 | mustJournal(t, j.releaseDirectFrames(ctx)) |
| 27 | mustJournal(t, j.prunePreflights(ctx, func(context.Context, containment.Record) error { return nil })) |
| 28 | var frames, attempts int |
| 29 | mustJournal(t, j.db.QueryRow("SELECT count(*) FROM direct_frames").Scan(&frames)) |
| 30 | mustJournal(t, j.db.QueryRow("SELECT count(*) FROM direct_attempts").Scan(&attempts)) |
| 31 | if frames != 0 || attempts != 0 { |
| 32 | t.Fatalf("idle poll %d retained %d frames and %d attempts", i, frames, attempts) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | func TestDirectBudgetDrainsWithoutReset(t *testing.T) { |
| 37 | j, l, _, p := directFixture(t, "codex") |
| 38 | ctx := context.Background() |
| 39 | j.maxBytes = 20 |
| 40 | if err := j.AppendFrame(ctx, l.AttemptKey, p.ID(), 1, []byte(`{"message":"`+strings.Repeat("x", 30)+`"}`)); err == nil { |
| 41 | t.Fatal("overrun accepted") |
| 42 | } |
| 43 | j.maxBytes = 1024 |
| 44 | if err := j.AdmissionAllowed(ctx); err != nil { |
| 45 | t.Fatalf("budget overrun latched journal: %v", err) |
| 46 | } |
| 47 | mustJournal(t, j.AppendFrame(ctx, l.AttemptKey, p.ID(), 1, []byte(`{"ok":true}`))) |
| 48 | } |
| 49 | func TestDirectPreflightCache(t *testing.T) { |
| 50 | r := &Runtime{} |
| 51 | calls := 0 |
| 52 | probe := func() error { calls++; return nil } |
| 53 | for i := 0; i < 12; i++ { |
| 54 | mustJournal(t, r.cachedPreflight(context.Background(), "binding", "same", probe)) |
| 55 | } |
| 56 | if calls != 1 { |
| 57 | t.Fatalf("idle polls/claim reran preflight %d times", calls) |
| 58 | } |
| 59 | for _, key := range []string{"account-changed", "policy-changed", "binary-changed", "home-changed"} { |
| 60 | mustJournal(t, r.cachedPreflight(context.Background(), "binding", key, probe)) |
| 61 | } |
| 62 | if calls != 5 { |
| 63 | t.Fatal("changed input did not invalidate cache") |
| 64 | } |
| 65 | if err := r.cachedPreflight(context.Background(), "binding", "bad", func() error { return provider.ErrPolicy }); err == nil { |
| 66 | t.Fatal("failed preflight cached") |
| 67 | } |
| 68 | mustJournal(t, r.cachedPreflight(context.Background(), "binding", "bad", probe)) |
| 69 | if calls != 6 { |
| 70 | t.Fatal("failure retained") |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestPendingBudgetsDoNotLatch(t *testing.T) { |
| 75 | for _, kind := range []string{"event", "direct-event", "tool"} { |
| 76 | t.Run(kind, func(t *testing.T) { |
| 77 | j, _, s, _ := directFixture(t, "codex") |
| 78 | ctx := context.Background() |
| 79 | j.maxBytes = 1 |
| 80 | var err error |
| 81 | switch kind { |
| 82 | case "event": |
| 83 | err = j.Append(ctx, journalEvent("budget", 1)) |
| 84 | case "direct-event": |
| 85 | _, err = j.ObserveDirect(ctx, s, provider.Event{Kind: "timeline", SessionID: s.ID, Payload: []byte(`{"message":"hello"}`)}) |
| 86 | case "tool": |
| 87 | err = j.appendTool(ctx, ToolSubmission{RequestID: "budget"}, "hash") |
| 88 | } |
| 89 | if err == nil { |
| 90 | t.Fatal("overrun accepted") |
| 91 | } |
| 92 | j.maxBytes = MaxJournalBytes |
| 93 | if err = j.AdmissionAllowed(ctx); err != nil { |
| 94 | t.Fatal("budget refusal latched storage", err) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |