package thintest
import (
"html"
"strings"
"testing"
"amadan.net/oficina/equipo/internal/wire"
)
// macMade runs w's Engineering attempt d to done with a summary, and the
// primary publishes it.
func (r *runnersRig) macMade(w *peer, d *wire.DispatchCommand, cand string) {
r.t.Helper()
if !w.lease(d.StepID, d.Attempt) {
r.t.Fatal("start refused")
}
w.send(wire.FrameStep, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: d.BaseSHA, Findings: []wire.Finding{}})
w.send(wire.FrameStep, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, Findings: []wire.Finding{}})
_, body := w.expect(wire.FrameSendPack)
h := body.(*wire.SendPackCommand).Transfer
w.send(wire.FrameStep, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, CandidateSHA: cand, Summary: "Swipe back added.", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}})
w.send(wire.FrameBlob, chunk(h, []byte("PACK commit")))
r.fromPrimary(wire.FrameBlob)
pub := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Pushed: true})
}
// TestContractStepSaysMadeOn: the instance names the worker an
// Engineering attempt ran on (never before it starts, never the main
// runner) with what it got of Xcode, and the task page says "Made on"
// the Mac and, without Xcode access, that the Mac's checks build it.
// While the base is prepared the step says which Mac it waits for.
func TestContractStepSaysMadeOn(t *testing.T) {
r := newMacRig(t)
w := r.newMacEngineer("Tito Studio", wire.EngineeringReady)
task := r.macTask("iOS made")
if s := r.newestEngineering(task); s.Placement != wire.PlacementBase || s.RunnerLabel != "Tito Studio" || s.MadeOn != "" {
t.Fatalf("waiting for its base %+v", s)
}
r.based(r.prepared(task), sha(1))
_, body := w.expect(wire.FrameDispatch)
d := body.(*wire.DispatchCommand)
r.engineeringTransfer(w, d)
if s := r.newestEngineering(task); s.MadeOn != "" {
t.Fatalf("named before it started %+v", s)
}
r.macMade(w, d, sha(0xc1))
s := r.newestEngineering(task)
if s.MadeOn != "Tito Studio" || s.XcodeAccess != "" {
t.Fatalf("made on %+v", s)
}
page := html.UnescapeString(r.get(r.inst.Cookie(), "/tasks/"+task, 200))
for _, want := range []string{"Made on Tito Studio", "Built and tested by the Mac's checks, not while making the change"} {
if !strings.Contains(page, want) {
t.Fatalf("task page lacks %q:\n%s", want, page)
}
}
// F5 denied: sandboxed Xcode access, so no such line.
r = newMacRig(t)
w = r.newMacEngineerAt("Tito Studio", wire.EngineeringReady, wire.ProtocolMacEngineering, true)
task = r.macTask("iOS xcode")
r.based(r.prepared(task), sha(2))
_, body = w.expect(wire.FrameDispatch)
d = body.(*wire.DispatchCommand)
r.engineeringTransfer(w, d)
r.macMade(w, d, sha(0xc2))
if s := r.newestEngineering(task); s.MadeOn != "Tito Studio" || s.XcodeAccess != wire.XcodeAccessSandboxed {
t.Fatalf("with Xcode access %+v", s)
}
if page := html.UnescapeString(r.get(r.inst.Cookie(), "/tasks/"+task, 200)); !strings.Contains(page, "Made on Tito Studio") || strings.Contains(page, "not while making the change") {
t.Fatalf("with Xcode access, the page:\n%s", page)
}
}
// Engineering on the main runner names no machine.
func TestContractMainRunnerNotNamed(t *testing.T) {
r := newRunnersRig(t)
task, _ := r.candidate("Linux change", false)
for _, s := range r.task(task).Steps {
if s.Specialist == wire.SpecialistEngineering && (s.MadeOn != "" || s.StartedAt == nil) {
t.Fatalf("the main runner named, or the rig did not start it: %+v", s)
}
}
}
// TestContractAddClaudeOnConnectionWait: a Mac Engineering attempt that
// placement put waiting for a Mac's Claude connection links its creator
// straight to adding Claude to that Mac, by the Mac's id, while the Mac
// has no connection or is signed out; not while what it lacks is
// something else (its home folder open).
func TestContractAddClaudeOnConnectionWait(t *testing.T) {
r := newMacRig(t)
w := r.newMacEngineer("Tito Studio", wire.EngineeringNoConnection)
wid := w.fr.Welcome.RunnerID
task := r.macTask("iOS needs Claude")
if s := r.newestEngineering(task); s.Placement != wire.PlacementMacConnection || s.RunnerID != "" || s.WaitingFor != wid {
t.Fatalf("waiting %+v", s)
}
link := `href="/conversation/settings/connections#machine-` + wid + `" data-add-claude>Add Claude to Tito Studio`
for _, state := range []string{wire.EngineeringNoConnection, wire.EngineeringSignedOut} {
r.macState(w, state, false)
if page := r.get(r.inst.Cookie(), "/tasks/"+task, 200); !strings.Contains(page, link) {
t.Fatalf("%s: no link to add Claude to the Mac:\n%s", state, page)
}
}
r.macState(w, wire.EngineeringHomeOpen, false)
if page := r.get(r.inst.Cookie(), "/tasks/"+task, 200); strings.Contains(page, "data-add-claude") {
t.Fatal("home open: offered to add Claude")
}
}
// TestContractMadeOnAfterPrimaryReplaced: whether an attempt was made on
// a worker is what its dispatch recorded, not a comparison with today's
// main runner. A change the old Linux primary made says neither "Made on"
// nor the no-Xcode line once another machine is the main runner.
func TestContractMadeOnAfterPrimaryReplaced(t *testing.T) {
r := newRunnersRig(t)
task, _ := r.candidate("Linux change", false)
old := r.primary.fr.Welcome.RunnerID
r.remove(old, true)
h := primaryHello(0)
h.Label = "docs-t2"
p, err := DialRunnerHello(r.ctx, r.inst.URL, r.inst.EnrolRunner(t).Token, h)
if err != nil || p.Welcome.Role != wire.RolePrimary || p.Welcome.RunnerID == old {
t.Fatalf("replacement %+v %v", p, err)
}
t.Cleanup(p.Close)
for _, s := range r.task(task).Steps {
if s.Specialist == wire.SpecialistEngineering && (s.MadeOn != "" || s.StartedAt == nil) {
t.Fatalf("the old primary's change named as made on a worker: %+v", s)
}
}
if page := html.UnescapeString(r.get(r.inst.Cookie(), "/tasks/"+task, 200)); strings.Contains(page, "Made on") || strings.Contains(page, "not while making the change") {
t.Fatalf("task page:\n%s", page)
}
}