oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "path/filepath"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/runner/provider"
12 "amadan.net/oficina/equipo/internal/runnerproto"
13)
14
15func TestJournalCancellationDoesNotLatch(t *testing.T) {
16 for _, op := range []string{"event", "replay", "tool", "intent", "question", "pending", "ack"} {
17 t.Run(op, func(t *testing.T) {
18 j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db"))
19 mustJournal(t, err)
20 defer j.Close()
21 ctx, cancel := context.WithCancel(journalCtx)
22 cancel()
23 switch op {
24 case "event":
25 err = j.Append(ctx, journalEvent("event", 1))
26 case "replay":
27 err = j.BeginReplay(ctx, provider.Agent{ServerID: "server", AgentID: "agent", ProviderSessionID: "session"})
28 case "tool":
29 err = j.appendTool(ctx, ToolSubmission{RequestID: "tool"}, "hash")
30 case "intent":
31 err = j.RecordIntent(ctx, provider.Intent{Kind: "agent", Key: "intent", RequestHash: "hash", Request: json.RawMessage(`{}`)})
32 case "question":
33 err = (&QuestionRelay{Journal: j}).Invalidate(ctx, runnerproto.Question{})
34 case "pending":
35 _, err = j.Pending(ctx, 10)
36 case "ack":
37 err = j.Ack(ctx, "event")
38 }
39 if err == nil {
40 t.Fatal("cancelled operation succeeded")
41 }
42 if err = j.AdmissionAllowed(journalCtx); err != nil {
43 t.Fatal("cancellation latched journal", err)
44 }
45 })
46 }
47}
48func TestJournalInvalidEventDoesNotLatch(t *testing.T) {
49 j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db"))
50 mustJournal(t, err)
51 defer j.Close()
52 pump := EventPump{Journal: j, Contain: func(context.Context) error { return nil }}
53 e := journalEvent("same", 1)
54 mustJournal(t, pump.Append(journalCtx, e, nil))
55 e.Payload = json.RawMessage(`{"changed":true}`)
56 if err = pump.Append(journalCtx, e, nil); !errors.Is(err, runnerproto.ErrInvalid) {
57 t.Fatal("conflict accepted", err)
58 }
59 if err = j.AdmissionAllowed(journalCtx); err != nil {
60 t.Fatal("conflict latched journal", err)
61 }
62}
63
64type blockedSource struct{ claims int }
65
66func (s *blockedSource) Poll(context.Context) ([]runnerproto.Assignment, error) {
67 return []runnerproto.Assignment{{ID: "blocked", Revision: 1}}, nil
68}
69func (s *blockedSource) Claim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) {
70 s.claims++
71 return runnerproto.ClaimResponse{}, ErrClaimRefused
72}
73func TestBlockedJournalDoesNotClaim(t *testing.T) {
74 s, _, _, _ := cleanupFixture(t)
75 _, err := s.journal.db.Exec("DELETE FROM journal_supervisor WHERE kind='attempt'")
76 mustJournal(t, err)
77 s.states = map[string]*attemptState{}
78 _ = s.journal.fail()
79 source := &blockedSource{}
80 service := Service{Supervisor: s, Source: source}
81 ctx, cancel := context.WithTimeout(journalCtx, 100*time.Millisecond)
82 defer cancel()
83 _ = service.Run(ctx)
84 if source.claims != 0 {
85 t.Fatal("blocked journal claimed work", source.claims)
86 }
87}
88func TestJournalResetRequiresReconciliation(t *testing.T) {
89 j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db"))
90 mustJournal(t, err)
91 defer j.Close()
92 _ = j.fail()
93 if err = j.resetAfterReconcile(journalCtx, func(context.Context) error { return provider.ErrUncertain }); err == nil {
94 t.Fatal("failed reconciliation reset block")
95 }
96 if j.AdmissionAllowed(journalCtx) == nil {
97 t.Fatal("failed reconciliation reopened admission")
98 }
99 mustJournal(t, j.resetAfterReconcile(journalCtx, func(context.Context) error { return nil }))
100 mustJournal(t, j.AdmissionAllowed(journalCtx))
101}
102
103func TestJournalCancelWhileAppendWaits(t *testing.T) {
104 j, err := OpenJournal(journalCtx, filepath.Join(t.TempDir(), "private/j.db"))
105 mustJournal(t, err)
106 defer j.Close()
107 conn, err := j.db.Conn(journalCtx)
108 mustJournal(t, err)
109 ctx, cancel := context.WithCancel(journalCtx)
110 done := make(chan error, 1)
111 go func() { done <- j.Append(ctx, journalEvent("waiting", 1)) }()
112 cancel()
113 _ = conn.Close()
114 if err = <-done; !errors.Is(err, context.Canceled) {
115 t.Fatal("cancelled append lost cause", err)
116 }
117 mustJournal(t, j.AdmissionAllowed(journalCtx))
118}
119