oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "bytes"
5 "encoding/json"
6 "slices"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// Research's hand-off (Research spec §5, slice R4): Engineering and
14// Review are dispatched with the task's latest done Research findings,
15// their cited sources and kept images; Review's brief adds the three
16// checks for a task with findings. A primary with no address at all
17// never holds a task's Engineering for Research.
18
19// cand2X is a fix round's candidate.
20var cand2X = strings.Repeat("3", 40)
21
22// The three checks Review makes for a task with findings, as the
23// dispatch's brief carries them.
24var reviewChecks = []string{
25 "the change matches the findings",
26 "it adds no request to a host outside the findings' sources",
27 "it puts no repository secret, environment file or unrelated repository text into its output",
28}
29
30func hasChecks(brief string) bool {
31 for _, c := range reviewChecks {
32 if !strings.Contains(brief, c) {
33 return false
34 }
35 }
36 return true
37}
38
39func noChecks(brief string) bool {
40 for _, c := range reviewChecks {
41 if strings.Contains(brief, c) {
42 return false
43 }
44 }
45 return true
46}
47
48// The sources of fetchLog, as a hand-off names them.
49var (
50 srcPage = wire.ResearchSource{ID: "s1", URL: "https://shop.example.com/", Origin: wire.ResearchOriginPerson}
51 srcHours = wire.ResearchSource{ID: "s2", URL: "https://shop.example.com/hours", Origin: wire.ResearchOriginPerson}
52 srcLogo = wire.ResearchSource{ID: "s3", URL: "https://shop.example.com/logo.png", Origin: wire.ResearchOriginExpanded}
53)
54
55// wantNote checks a dispatch's hand-off: the findings as stored, the
56// sources the findings cite with their URLs in log order, and the kept
57// images.
58func (r *toolRig) wantNote(d *wire.DispatchCommand, findings json.RawMessage, images []wire.ResearchImage, cited ...wire.ResearchSource) {
59 r.t.Helper()
60 n := d.ResearchNote
61 if n == nil {
62 r.t.Fatalf("%s carries no findings", d.Specialist)
63 }
64 if !bytes.Equal(n.Findings, findings) {
65 r.t.Fatalf("%s findings %s, want %s", d.Specialist, n.Findings, findings)
66 }
67 if !slices.Equal(n.Sources, cited) {
68 r.t.Fatalf("%s sources %+v", d.Specialist, n.Sources)
69 }
70 if !slices.Equal(n.Images, images) {
71 r.t.Fatalf("%s images %+v, want %+v", d.Specialist, n.Images, images)
72 }
73}
74
75// TestResearchHandoffDispatch: with no Research, Engineering and Review
76// carry no findings and Review no extra checks. With Research done, its
77// findings, cited sources and kept image go to Engineering and Review,
78// and Review's brief adds the three checks (Engineering's does not). A
79// second run done at the next round's boundary replaces them; a third
80// that fails hands nothing new: the round after it still carries the
81// latest done run's findings.
82func TestResearchHandoffDispatch(t *testing.T) {
83 r := newToolRigSaying(t, wire.ProtocolCurrent, personWords)
84 r.researchOn()
85 r.inventoryOn()
86
87 // No Research on the task: nothing handed off.
88 _, pe := r.pushed("p", "Plain fix", baseX, candX)
89 if pe.ResearchNote != nil {
90 t.Fatalf("Engineering without Research carries %+v", pe.ResearchNote)
91 }
92 pc := r.dispatched(wire.SpecialistChecks, baseX, candX, 0)
93 r.checks("p-c", pc, wire.ChecksPassed, exit(0), "", "ok\n")
94 pr := r.dispatched(wire.SpecialistReview, baseX, candX, 0)
95 if pr.ResearchNote != nil || !noChecks(pr.Brief) {
96 t.Fatalf("Review without Research %+v", pr)
97 }
98
99 // Research done with findings and a kept image.
100 d := r.runningResearch("a")
101 logo := pngOf(t, 5, 4)
102 r.sendFile("logo", fileChunks(d, "img1", logo, 2))
103 f := findingsJSON(t, goodFindings("img1"))
104 kept := []wire.ResearchImage{keptRecord("img1", logo, 5, 4)}
105 r.step("a-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Summary: "Read four pages.",
106 Research: &wire.ResearchResult{Findings: f, Fetches: fetchLog(), Images: kept}})
107 e := r.nextDispatch()
108 if e.Specialist != wire.SpecialistEngineering || e.TaskID != d.TaskID {
109 t.Fatalf("Engineering after Research %+v", e)
110 }
111 r.wantNote(e, f, kept, srcPage, srcHours, srcLogo)
112 if !noChecks(e.Brief) {
113 t.Fatalf("Engineering's brief has Review's checks: %s", e.Brief)
114 }
115 r.step("a-e-run", wire.StepReport{TaskID: e.TaskID, StepID: e.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: baseX})
116 r.step("a-e-done", wire.StepReport{TaskID: e.TaskID, StepID: e.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: candX, Pushed: true, Termination: true, Family: "claude", Account: "claude-a"})
117 c := r.dispatched(wire.SpecialistChecks, baseX, candX, 0)
118 if c.ResearchNote != nil {
119 t.Fatal("checks carry the findings")
120 }
121 r.checks("a-c", c, wire.ChecksPassed, exit(0), "", "ok\n")
122 rv := r.dispatched(wire.SpecialistReview, baseX, candX, 0)
123 r.wantNote(rv, f, kept, srcPage, srcHours, srcLogo)
124 if !hasChecks(rv.Brief) {
125 t.Fatalf("Review's brief lacks the three checks: %s", rv.Brief)
126 }
127
128 // A second run, at the next round's boundary, is done with other
129 // findings and no image: the fix round carries those.
130 if res := r.research("a-r2", d.TaskID); res.IsError {
131 t.Fatalf("second request %+v", res)
132 }
133 r.verdict("a-v", rv, wire.VerdictChanges, wire.Finding{Path: "index.html", Line: 1, Text: "Use the logo."})
134 d2 := r.nextDispatch()
135 if d2.Specialist != wire.SpecialistResearch {
136 t.Fatalf("at the round boundary %+v", d2)
137 }
138 r.step("a-r2-run", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: 1, State: wire.StepRunning})
139 f2 := findingsJSON(t, wire.ResearchFindings{Summary: "The shop's hours.", Facts: []wire.ResearchFact{{Label: "Hours", Value: "Tue to Sat", Sources: []string{"s2"}}}, Images: []string{}, Gaps: []string{}})
140 r.step("a-r2-done", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Research: &wire.ResearchResult{Findings: f2, Fetches: fetchLog(), Images: []wire.ResearchImage{}}})
141 fix := r.nextDispatch()
142 if fix.Specialist != wire.SpecialistEngineering || fix.Round != 1 {
143 t.Fatalf("the fix round %+v", fix)
144 }
145 r.wantNote(fix, f2, []wire.ResearchImage{}, srcHours)
146
147 // A third run, at the round after, fails: that round still carries
148 // the second run's findings.
149 r.step("a-f-run", wire.StepReport{TaskID: fix.TaskID, StepID: fix.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: candX})
150 r.step("a-f-done", wire.StepReport{TaskID: fix.TaskID, StepID: fix.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: candX, CandidateSHA: cand2X, Pushed: true, Termination: true, Family: "claude", Account: "claude-a"})
151 c2 := r.dispatched(wire.SpecialistChecks, baseX, cand2X, 1)
152 r.checks("a-c2", c2, wire.ChecksPassed, exit(0), "", "ok\n")
153 rv2 := r.dispatched(wire.SpecialistReview, baseX, cand2X, 1)
154 r.wantNote(rv2, f2, []wire.ResearchImage{}, srcHours)
155 if res := r.research("a-r3", d.TaskID); res.IsError {
156 t.Fatalf("third request %+v", res)
157 }
158 r.verdict("a-v2", rv2, wire.VerdictChanges, wire.Finding{Path: "index.html", Line: 1, Text: "Show the hours."})
159 d3 := r.nextDispatch()
160 if d3.Specialist != wire.SpecialistResearch {
161 t.Fatalf("at the second round boundary %+v", d3)
162 }
163 r.step("a-r3-run", wire.StepReport{TaskID: d3.TaskID, StepID: d3.StepID, Attempt: 1, State: wire.StepRunning})
164 r.step("a-r3-fail", wire.StepReport{TaskID: d3.TaskID, StepID: d3.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true})
165 fix2 := r.nextDispatch()
166 if fix2.Specialist != wire.SpecialistEngineering || fix2.Round != 2 {
167 t.Fatalf("the second fix round %+v", fix2)
168 }
169 r.wantNote(fix2, f2, []wire.ResearchImage{}, srcHours)
170}
171
172// TestResearchHandoffGapOnly: a Research run that ended without findings
173// still hands off its gap (Engineering learns Research found nothing),
174// with no sources and no images.
175func TestResearchHandoffGapOnly(t *testing.T) {
176 r := newToolRigSaying(t, wire.ProtocolCurrent, personWords)
177 r.researchOn()
178 r.inventoryOn()
179 d := r.runningResearch("g")
180 r.step("g-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Summary: "Read nothing useful."})
181 e := r.nextDispatch()
182 if e.Specialist != wire.SpecialistEngineering || e.ResearchNote == nil {
183 t.Fatalf("Engineering after a gap %+v", e)
184 }
185 var got wire.ResearchFindings
186 if err := json.Unmarshal(e.ResearchNote.Findings, &got); err != nil || !slices.Equal(got.Gaps, []string{wire.ResearchNoFindingsGap}) || len(got.Facts) != 0 {
187 t.Fatalf("gap findings %s %v", e.ResearchNote.Findings, err)
188 }
189 if len(e.ResearchNote.Sources) != 0 || len(e.ResearchNote.Images) != 0 {
190 t.Fatalf("a gap hands off %+v", e.ResearchNote)
191 }
192}
193
194// TestResearchNoInventoryEnds: a primary whose hello says it has no
195// public address, and with no observed address either, has nothing an
196// owner could confirm: its Research step ends failed
197// (research_no_inventory), no card is raised, and Engineering goes on.
198// Until the hello says so (the first hello of a connection never does),
199// the step waits as before.
200func TestResearchNoInventoryEnds(t *testing.T) {
201 r := newToolRigSaying(t, wire.ProtocolCurrent, personWords)
202 r.researchOn()
203 id, s := r.waitingTask("n")
204 // The hello now says: no address at all, and nothing changed.
205 r.addresses(0)
206 e := r.nextDispatch()
207 if e.Specialist != wire.SpecialistEngineering || e.TaskID != id || e.ResearchNote != nil {
208 t.Fatalf("Engineering released %+v", e)
209 }
210 got := r.newest(id, wire.SpecialistResearch)
211 if got.ID != s.ID || got.State != wire.StepFailed || got.Reason != wire.ReasonResearchNoInventory || !got.Terminated {
212 t.Fatalf("the Research step %+v", got)
213 }
214 if r.inventoryCards() != 0 || r.researchDispatches() != 0 {
215 t.Fatal("a card or a dispatch with no address")
216 }
217 // A later request on another task ends the same way at its start.
218 other := r.namedTask("m")
219 if res := r.research("m-r", other); res.IsError {
220 t.Fatalf("request %+v", res)
221 }
222 r.begin("m-start", other)
223 if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.TaskID != other {
224 t.Fatalf("Engineering on the second task %+v", e)
225 }
226 if got := r.newest(other, wire.SpecialistResearch); got.State != wire.StepFailed || got.Reason != wire.ReasonResearchNoInventory {
227 t.Fatalf("second Research %+v", got)
228 }
229}
230