package runner import ( "context" "encoding/json" "errors" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestJournalCancellationDoesNotLatch(t *testing.T) { for _, op := range []string{"event", "replay", "tool", "intent", "question", "pending", "ack"} { t.Run(op, func(t *testing.T) { j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db")) mustJournal(t, err) defer j.Close() ctx, cancel := context.WithCancel(journalCtx) cancel() switch op { case "event": err = j.Append(ctx, journalEvent("event", 1)) case "replay": err = j.BeginReplay(ctx, provider.Agent{ServerID: "server", AgentID: "agent", ProviderSessionID: "session"}) case "tool": err = j.appendTool(ctx, ToolSubmission{RequestID: "tool"}, "hash") case "intent": err = j.RecordIntent(ctx, provider.Intent{Kind: "agent", Key: "intent", RequestHash: "hash", Request: json.RawMessage(`{}`)}) case "question": err = (&QuestionRelay{Journal: j}).Invalidate(ctx, runnerproto.Question{}) case "pending": _, err = j.Pending(ctx, 10) case "ack": err = j.Ack(ctx, "event") } if err == nil { t.Fatal("cancelled operation succeeded") } if err = j.AdmissionAllowed(journalCtx); err != nil { t.Fatal("cancellation latched journal", err) } }) } } func TestJournalInvalidEventDoesNotLatch(t *testing.T) { j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db")) mustJournal(t, err) defer j.Close() pump := EventPump{Journal: j, Contain: func(context.Context) error { return nil }} e := journalEvent("same", 1) mustJournal(t, pump.Append(journalCtx, e, nil)) e.Payload = json.RawMessage(`{"changed":true}`) if err = pump.Append(journalCtx, e, nil); !errors.Is(err, runnerproto.ErrInvalid) { t.Fatal("conflict accepted", err) } if err = j.AdmissionAllowed(journalCtx); err != nil { t.Fatal("conflict latched journal", err) } } type blockedSource struct{ claims int } func (s *blockedSource) Poll(context.Context) ([]runnerproto.Assignment, error) { return []runnerproto.Assignment{{ID: "blocked", Revision: 1}}, nil } func (s *blockedSource) Claim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) { s.claims++ return runnerproto.ClaimResponse{}, ErrClaimRefused } func TestBlockedJournalDoesNotClaim(t *testing.T) { s, _, _, _ := cleanupFixture(t) _, err := s.journal.db.Exec("DELETE FROM journal_supervisor WHERE kind='attempt'") mustJournal(t, err) s.states = map[string]*attemptState{} _ = s.journal.fail() source := &blockedSource{} service := Service{Supervisor: s, Source: source} ctx, cancel := context.WithTimeout(journalCtx, 100*time.Millisecond) defer cancel() _ = service.Run(ctx) if source.claims != 0 { t.Fatal("blocked journal claimed work", source.claims) } } func TestJournalResetRequiresReconciliation(t *testing.T) { j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db")) mustJournal(t, err) defer j.Close() _ = j.fail() if err = j.resetAfterReconcile(journalCtx, func(context.Context) error { return provider.ErrUncertain }); err == nil { t.Fatal("failed reconciliation reset block") } if j.AdmissionAllowed(journalCtx) == nil { t.Fatal("failed reconciliation reopened admission") } mustJournal(t, j.resetAfterReconcile(journalCtx, func(context.Context) error { return nil })) mustJournal(t, j.AdmissionAllowed(journalCtx)) } func TestJournalCancelWhileAppendWaits(t *testing.T) { j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db")) mustJournal(t, err) defer j.Close() conn, err := j.db.Conn(journalCtx) mustJournal(t, err) ctx, cancel := context.WithCancel(journalCtx) done := make(chan error, 1) go func() { done <- j.Append(ctx, journalEvent("waiting", 1)) }() cancel() _ = conn.Close() if err = <-done; !errors.Is(err, context.Canceled) { t.Fatal("cancelled append lost cause", err) } mustJournal(t, j.AdmissionAllowed(journalCtx)) }