| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 10 | ) |
| 11 | |
| 12 | func TestDirectBugQuestionResolution(t *testing.T) { |
| 13 | for _, state := range []string{"pending", "invalid", "delivered"} { |
| 14 | t.Run(state, func(t *testing.T) { |
| 15 | s, r, _, order := cleanupFixture(t) |
| 16 | r.Claim.Assignment.TaskID = "task" |
| 17 | r.Session = provider.Session{AttemptKey: "attempt", ID: "session"} |
| 18 | q := runnerproto.Question{ID: "question", TaskID: "task", AssignmentID: r.Claim.Assignment.ID, Attempt: int64(r.Claim.Handle.Attempt), Fence: r.Claim.Handle.Fence, ServerID: "direct", AgentID: "attempt", SessionID: "session", PermissionID: "request"} |
| 19 | w, err := NewWitness(s.journal) |
| 20 | mustJournal(t, err) |
| 21 | mustJournal(t, w.record(context.Background(), "question", q.ID, q)) |
| 22 | key := runnerproto.EventHash([]any{q.TaskID, q.Attempt, q.Fence, q.ServerID, q.AgentID, q.SessionID, q.PermissionID}) |
| 23 | _, err = s.journal.db.Exec("INSERT INTO journal_answers VALUES(?,'hash',?)", key, state) |
| 24 | mustJournal(t, err) |
| 25 | mustJournal(t, s.resolveDeliveredQuestions(context.Background(), r)) |
| 26 | want := 0 |
| 27 | if state == "delivered" { |
| 28 | want = 1 |
| 29 | } |
| 30 | if len(*order) != want { |
| 31 | t.Fatalf("question resolution trusted transport result instead of journal: %v", *order) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestDirectBugTerminalEOF(t *testing.T) { |
| 38 | for i := 0; i < 100; i++ { |
| 39 | notices := make(chan provider.Event, 1) |
| 40 | done := make(chan error, 1) |
| 41 | notices <- provider.Event{Kind: "turn.completed"} |
| 42 | done <- errDirectUncertain |
| 43 | e, err := nextDirectNotice(context.Background(), notices, done) |
| 44 | if err != nil || e.Kind != "turn.completed" { |
| 45 | t.Fatal("EOF replaced committed terminal event") |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestDirectBugTerminalRequiresCommit(t *testing.T) { |
| 51 | for _, early := range []bool{false, true} { |
| 52 | t.Run(map[bool]string{false: "committed", true: "cancelled-before-commit"}[early], func(t *testing.T) { |
| 53 | j, l, session, p := directFixture(t, "codex") |
| 54 | reader, writer := io.Pipe() |
| 55 | p.out = reader |
| 56 | defer writer.Close() |
| 57 | ctx, cancel := context.WithCancel(context.Background()) |
| 58 | defer cancel() |
| 59 | capture, err := NewCaptureFactory(j).Open(ctx, p, l) |
| 60 | mustJournal(t, err) |
| 61 | mustJournal(t, j.BeginTurn(ctx, session, directTurn())) |
| 62 | mustJournal(t, j.consumeTurn(ctx, session.AttemptKey, session.ID, "hello")) |
| 63 | if early { |
| 64 | cancel() |
| 65 | <-capture.Done |
| 66 | } |
| 67 | pump := EventPump{Journal: j, Contain: func(context.Context) error { return nil }} |
| 68 | template := journalEvent("completion", 1) |
| 69 | template.ActionID = 1 |
| 70 | mustJournal(t, pump.AppendDirect(context.Background(), session, directComplete("turn"), template)) |
| 71 | if !early { |
| 72 | cancel() |
| 73 | <-capture.Done |
| 74 | } |
| 75 | err = j.requireDirectCompletion(context.Background(), session, "turn") |
| 76 | if (err != nil) != early { |
| 77 | t.Fatalf("wrong terminal authority after cancellation: %v", err) |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |