package wire_test import ( "bytes" "encoding/base64" "encoding/json" "errors" "fmt" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // designDispatch is a Design step's dispatch as the instance sends it at // protocol 38: Engineering's step kind with the specialist's binding. func designDispatch() wire.DispatchCommand { return wire.DispatchCommand{Person: "owner@example.com", TaskID: "tk1", StepID: "s_1", Attempt: 1, Specialist: wire.SpecialistEngineering, Repo: "amadan.net/oficina/sample", Branch: "equipo/tk1-x", Findings: []wire.Finding{}, SpecialistID: "sp_abc234", SpecialistRevision: 2, SpecialistProject: "p_xyz567", Profile: "design-v1", Capabilities: []string{"branch.edit", "page.render"}} } func sum(c byte) string { return strings.Repeat(string(c), 64) } func designOptions() []wire.DesignOption { return []wire.DesignOption{ {Name: "Quiet editorial", Captures: []wire.DesignCapture{{ID: "1-work-1.png", SHA256: sum('a'), TreeDigest: sum('b')}, {ID: "1-work-2.png", SHA256: sum('c'), TreeDigest: sum('b')}}}, {Name: "Bold grid", Captures: []wire.DesignCapture{{ID: "2-work-1.png", SHA256: sum('d'), TreeDigest: sum('e')}, {ID: "2-diff-1.png", SHA256: sum('f'), TreeDigest: sum('e')}, {ID: "2-base-1.png", SHA256: sum('0'), TreeDigest: sum('1')}}}, } } func renderFile() wire.RenderFile { return wire.RenderFile{TaskID: "tk1", StepID: "s_1", Attempt: 1, CaptureID: "1-work-1.png", Index: 0, Total: 1, Size: 3, SHA256: sum('a'), Data: "YWJj"} } // TestProtocol38Design: protocol 38 is the Design specialist's and the // current one. func TestProtocol38Design(t *testing.T) { if wire.Protocol38 != 38 || wire.ProtocolDesign != wire.Protocol38 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 || wire.ProtocolMacEngineering != 37 { t.Fatalf("design %d, current %d, spoken %d", wire.ProtocolDesign, wire.ProtocolCurrent, wire.Spoken(99)) } } // TestEncodeBelowProtocol38RefusesDesign: at 38 a bound dispatch, a // render lease check, a render_file chunk, a report's options and a // project-scoped proposal go out whole. Below it a dispatch carrying any // part of a specialist binding, a render check, a render_file chunk and // a proposal for one project are refused (an older runner would run a // Design step as plain Engineering, an older instance would grant a // render as a start, or a proposal for the whole team); a report loses // its options, and a proposal for the whole team its empty project. func TestEncodeBelowProtocol38RefusesDesign(t *testing.T) { dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, designDispatch()) lease, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s_1", Attempt: 1, Purpose: wire.LeasePurposeRender}) chunk, _ := wire.NewFrame("f", wire.FrameRenderFile, renderFile()) report, _ := wire.NewFrame("r", wire.FrameStep, wire.StepReport{TaskID: "tk1", StepID: "s_1", Attempt: 1, State: wire.StepDone, Findings: []wire.Finding{}, Options: designOptions()}) scoped, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{TurnID: "t", CallID: "c", Name: wire.ToolProposeSpecialist, Args: []byte(`{"profile":"design-v1","name":"Design","brief":"b","capabilities":[],"tools":[],"project":"p_xyz567"}`)}) team, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{TurnID: "t", CallID: "c", Name: wire.ToolProposeSpecialist, Args: []byte(`{"profile":"research-v1","name":"Research","brief":"b","capabilities":[],"tools":[],"project":""}`)}) for _, f := range []wire.Frame{dispatch, lease, chunk, report, scoped, team} { if same, err := wire.Encode(f, wire.ProtocolDesign); err != nil || !bytes.Equal(same.Data, f.Data) { t.Fatalf("%s changed at 38: %v", f.Type, err) } } if !chunk.Durable() { t.Fatal("render_file is not durable") } for _, f := range []wire.Frame{dispatch, lease, chunk, scoped} { for _, p := range []int{wire.ProtocolMacEngineering, wire.ProtocolLookup, wire.ProtocolChangeDiffs, wire.ProtocolPlaces, wire.ProtocolResearch, wire.Protocol2} { if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { t.Fatalf("%s %s encoded at %d: %v", f.Type, f.Data, p, err) } } } // Each part of the binding alone makes it a Design dispatch. for name, mutate := range map[string]func(*wire.DispatchCommand){ "id": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistID: "sp_abc234"} }, "revision": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistRevision: 1} }, "project": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistProject: "p_1"} }, "profile": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, Profile: "design-v1"} }, "capabilities": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, Capabilities: []string{"page.render"}} }, "explore": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, Explore: true} }, "brief": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistBrief: "Calm and editorial."} }, } { d := designDispatch() mutate(&d) f, _ := wire.NewFrame("d", wire.FrameDispatch, d) if _, err := wire.Encode(f, wire.ProtocolMacEngineering); !errors.Is(err, wire.ErrProtocol) { t.Fatalf("a dispatch with only its %s at 37: %v", name, err) } } // Another lease purpose, and a dispatch without a binding, are as before. plainLease, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s_1", Attempt: 1}) eng := designDispatch() eng.SpecialistID, eng.SpecialistRevision, eng.SpecialistProject, eng.Profile, eng.Capabilities = "", 0, "", "", nil plain, _ := wire.NewFrame("e", wire.FrameDispatch, eng) for _, f := range []wire.Frame{plainLease, plain} { if same, err := wire.Encode(f, wire.ProtocolMacEngineering); err != nil || !bytes.Equal(same.Data, f.Data) { t.Fatalf("%s changed at 37: %s %v", f.Type, same.Data, err) } } got, err := wire.Encode(report, wire.ProtocolMacEngineering) if err != nil || strings.Contains(string(got.Data), `"options"`) || !strings.Contains(string(got.Data), `"state":"done"`) { t.Fatalf("a report with options at 37: %s %v", got.Data, err) } got, err = wire.Encode(team, wire.ProtocolMacEngineering) if err != nil || strings.Contains(string(got.Data), `project`) || !strings.Contains(string(got.Data), `research-v1`) { t.Fatalf("a team proposal at 37: %s %v", got.Data, err) } // A proposal without the key is untouched. bare, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{TurnID: "t", CallID: "c", Name: wire.ToolProposeSpecialist, Args: []byte(`{"profile":"research-v1","name":"Research","brief":"b","capabilities":[],"tools":[]}`)}) if same, err := wire.Encode(bare, wire.ProtocolMacEngineering); err != nil || !bytes.Equal(same.Data, bare.Data) { t.Fatalf("a proposal without project changed at 37: %v", err) } // A project that is not a string is not read as the team's. odd, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{TurnID: "t", CallID: "c", Name: wire.ToolProposeSpecialist, Args: []byte(`{"profile":"research-v1","project":null}`)}) if _, err := wire.Encode(odd, wire.ProtocolMacEngineering); !errors.Is(err, wire.ErrProtocol) { t.Fatalf("a null project at 37: %v", err) } } // TestDesignRoundTrip: each new frame and field survives the wire exactly // (strict decode on the far side), with the names the spec gives them. func TestDesignRoundTrip(t *testing.T) { for _, c := range []struct { kind string body any want []string }{ {wire.FrameDispatch, designDispatch(), []string{`"specialist_id":"sp_abc234"`, `"specialist_revision":2`, `"specialist_project":"p_xyz567"`, `"profile":"design-v1"`, `"capabilities":["branch.edit","page.render"]`}}, {wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s_1", Attempt: 1, Purpose: wire.LeasePurposeRender}, []string{`"purpose":"page_render"`}}, {wire.FrameRenderFile, renderFile(), []string{`"capture_id":"1-work-1.png"`, `"index":0`, `"total":1`, `"size":3`}}, {wire.FrameStep, wire.StepReport{TaskID: "tk1", StepID: "s_1", Attempt: 1, State: wire.StepDone, Findings: []wire.Finding{}, Options: designOptions()}, []string{`"options":[{"name":"Quiet editorial","captures":[{"id":"1-work-1.png","sha256":"` + sum('a') + `","tree_digest":"` + sum('b') + `"}`}}, } { f, err := wire.NewFrame("x", c.kind, c.body) if err != nil { t.Fatal(err) } raw, _ := json.Marshal(f) g, body, err := wire.ParseFrame(raw) if err != nil { t.Fatalf("%s: %v", c.kind, err) } again, _ := json.Marshal(body) if !bytes.Equal(again, g.Data) { t.Fatalf("%s did not round trip:\n%s\n%s", c.kind, again, g.Data) } for _, w := range c.want { if !strings.Contains(string(g.Data), w) { t.Fatalf("%s lacks %s: %s", c.kind, w, g.Data) } } } // Absent, the new fields are not on the wire at all. f, _ := wire.NewFrame("e", wire.FrameDispatch, wire.DispatchCommand{Findings: []wire.Finding{}}) r, _ := wire.NewFrame("r", wire.FrameStep, wire.StepReport{Findings: []wire.Finding{}}) for _, s := range []string{`specialist_`, `"profile"`, `"capabilities"`, `"options"`} { if strings.Contains(string(f.Data), s) || strings.Contains(string(r.Data), s) { t.Fatalf("an empty %s on the wire: %s %s", s, f.Data, r.Data) } } // A specialist's scope: its project, "" for the team, always said. raw, _ := json.Marshal(wire.Specialist{ID: "sp_1", Project: "p_xyz567"}) team, _ := json.Marshal(wire.Specialist{ID: "sp_1"}) if !strings.Contains(string(raw), `"project":"p_xyz567"`) || !strings.Contains(string(team), `"project":""`) { t.Fatalf("specialist scope: %s %s", raw, team) } var back wire.Specialist if err := wire.Strict(raw, &back); err != nil || back.Project != "p_xyz567" { t.Fatalf("specialist scope back: %+v %v", back, err) } // Build's note is the same field as Try again's. var answer wire.AnswerCard if err := wire.Strict([]byte(`{"choice":"build","revision":3,"note":"The bold grid."}`), &answer); err != nil || answer.Note != "The bold grid." || answer.Choice != wire.ChoiceBuild { t.Fatalf("build note: %+v %v", answer, err) } } // TestValidOptions bounds a Design report's options: at most 4, at most // 12 captures across them, each name a short plain label said once, each // capture a render_page file id said once, with SHA-256 and tree digests // of lower-case hex. No options is valid. func TestValidOptions(t *testing.T) { if err := wire.ValidOptions(designOptions()); err != nil { t.Fatalf("refused: %v", err) } if err := wire.ValidOptions(nil); err != nil { t.Fatalf("no options refused: %v", err) } if err := wire.ValidOptions([]wire.DesignOption{{Name: "Café 2 - l'été", Captures: []wire.DesignCapture{}}}); err != nil { t.Fatalf("a plain label without captures refused: %v", err) } if err := wire.ValidOptions([]wire.DesignOption{{Name: strings.Repeat("a", wire.MaxOptionName), Captures: []wire.DesignCapture{{ID: "20-base-6.png", SHA256: sum('a'), TreeDigest: sum('b')}}}}); err != nil { t.Fatalf("the longest name and a high id refused: %v", err) } five := []wire.DesignOption{} for _, n := range []string{"A", "B", "C", "D", "E"} { five = append(five, wire.DesignOption{Name: n, Captures: []wire.DesignCapture{}}) } // Thirteen distinct captures (calls 1 to 3, pages 1 to 6), over two // options: seven and six. thirteen := []wire.DesignOption{{Name: "A"}, {Name: "B"}} for i := range 13 { c := wire.DesignCapture{ID: fmt.Sprintf("%d-work-%d.png", i/6+1, i%6+1), SHA256: sum('a'), TreeDigest: sum('b')} thirteen[i%2].Captures = append(thirteen[i%2].Captures, c) } twelve := []wire.DesignOption{{Name: "A", Captures: thirteen[0].Captures}, {Name: "B", Captures: thirteen[1].Captures[:5]}} if err := wire.ValidOptions(twelve); err != nil { t.Fatalf("twelve captures refused: %v", err) } mutate := func(f func(o []wire.DesignOption)) []wire.DesignOption { o := designOptions() for i := range o { o[i].Captures = append([]wire.DesignCapture{}, o[i].Captures...) } f(o) return o } for name, bad := range map[string][]wire.DesignOption{ "five options": five, "thirteen captures": thirteen, "empty name": mutate(func(o []wire.DesignOption) { o[0].Name = "" }), "long name": mutate(func(o []wire.DesignOption) { o[0].Name = strings.Repeat("a", wire.MaxOptionName+1) }), "leading space": mutate(func(o []wire.DesignOption) { o[0].Name = " Bold" }), "trailing space": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold " }), "two spaces": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold grid" }), "leading hyphen": mutate(func(o []wire.DesignOption) { o[0].Name = "-Bold" }), "control": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold\ngrid" }), "markup": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold" }), "path": mutate(func(o []wire.DesignOption) { o[0].Name = "mockups/bold" }), "invalid utf-8": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold\xff" }), "format character": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold\u202egrid" }), "same name": mutate(func(o []wire.DesignOption) { o[1].Name = "quiet EDITORIAL" }), "same capture": mutate(func(o []wire.DesignOption) { o[1].Captures[0].ID = "1-work-1.png" }), "same capture twice": mutate(func(o []wire.DesignOption) { o[0].Captures[1].ID = "1-work-1.png" }), "id with a path": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "/render/1-work-1.png" }), "id with a dot dot": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "../1-work-1.png" }), "id of another tree": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-head-1.png" }), "id call zero": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "0-work-1.png" }), "id leading zero": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "01-work-1.png" }), "id past the calls": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "100-work-1.png" }), "id page zero": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-work-0.png" }), "id not a png": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-work-1.jpg" }), "id upper case": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-WORK-1.png" }), "empty id": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "" }), "sha256 short": mutate(func(o []wire.DesignOption) { o[0].Captures[0].SHA256 = sum('a')[:63] }), "sha256 upper": mutate(func(o []wire.DesignOption) { o[0].Captures[0].SHA256 = strings.Repeat("A", 64) }), "sha256 empty": mutate(func(o []wire.DesignOption) { o[0].Captures[0].SHA256 = "" }), "tree digest sha1": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = strings.Repeat("b", 40) }), "tree digest not hex": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = strings.Repeat("g", 64) }), "tree digest empty": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = "" }), "tree digest too long": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = sum('b') + "b" }), } { if err := wire.ValidOptions(bad); !errors.Is(err, wire.ErrStrict) { t.Errorf("%s accepted: %v", name, err) } } } // TestValidRenderFile bounds one render_file chunk: its step, a capture // id of the render_page shape, a whole file of 1 byte to MaxCaptureBytes // in exactly as many chunks as its size needs, and this chunk's bytes // exactly its share, in strict base64. func TestValidRenderFile(t *testing.T) { if !wire.ValidRenderFile(renderFile()) { t.Fatal("refused a one-chunk file") } full := make([]byte, wire.RenderFileChunk) big := wire.RenderFile{TaskID: "tk1", StepID: "s_1", Attempt: 1, CaptureID: "3-diff-2.png", Index: 0, Total: wire.MaxRenderFileChunks, Size: wire.MaxCaptureBytes, SHA256: sum('a'), Data: base64.StdEncoding.EncodeToString(full)} if !wire.ValidRenderFile(big) { t.Fatal("refused the first chunk of the largest capture") } last := big last.Index = wire.MaxRenderFileChunks - 1 if !wire.ValidRenderFile(last) { t.Fatal("refused the last chunk of the largest capture") } if wire.MaxRenderFileChunks != 16 || wire.MaxCaptureBytes != 2<<20 || wire.RenderFileChunk != wire.ResearchFileChunk { t.Fatalf("bounds %d %d %d", wire.MaxRenderFileChunks, wire.MaxCaptureBytes, wire.RenderFileChunk) } mutate := func(f func(v *wire.RenderFile)) wire.RenderFile { v := renderFile(); f(&v); return v } for name, bad := range map[string]wire.RenderFile{ "no task": mutate(func(v *wire.RenderFile) { v.TaskID = "" }), "no step": mutate(func(v *wire.RenderFile) { v.StepID = "" }), "long step": mutate(func(v *wire.RenderFile) { v.StepID = strings.Repeat("s", 129) }), "attempt zero": mutate(func(v *wire.RenderFile) { v.Attempt = 0 }), "bad capture": mutate(func(v *wire.RenderFile) { v.CaptureID = "img1" }), "capture path": mutate(func(v *wire.RenderFile) { v.CaptureID = "../1-work-1.png" }), "sha256": mutate(func(v *wire.RenderFile) { v.SHA256 = "abc" }), "empty file": mutate(func(v *wire.RenderFile) { v.Size, v.Data = 0, "" }), "too large": mutate(func(v *wire.RenderFile) { v.Size, v.Total = wire.MaxCaptureBytes+1, wire.MaxRenderFileChunks+1 }), "total wrong": mutate(func(v *wire.RenderFile) { v.Total = 2 }), "total zero": mutate(func(v *wire.RenderFile) { v.Total = 0 }), "index negative": mutate(func(v *wire.RenderFile) { v.Index = -1 }), "index past": mutate(func(v *wire.RenderFile) { v.Index = 1 }), "short data": mutate(func(v *wire.RenderFile) { v.Data = "YWI=" }), "long data": mutate(func(v *wire.RenderFile) { v.Data = "YWJjZA==" }), "not base64": mutate(func(v *wire.RenderFile) { v.Data = "Y*Jj" }), "unpadded": mutate(func(v *wire.RenderFile) { v.Data, v.Size = "YWI", 2 }), "short middle": func() wire.RenderFile { v := big; v.Index = 1; v.Data = "YWJj"; return v }(), "long last": func() wire.RenderFile { v := last v.Data = base64.StdEncoding.EncodeToString(append(full, 0)) return v }(), "oversized encoded": func() wire.RenderFile { v := big v.Data = strings.Repeat("A", base64.StdEncoding.EncodedLen(wire.RenderFileChunk)+4) return v }(), // Each bound alone, everything else consistent with it. "too large, whole": func() wire.RenderFile { v := big v.Size, v.Total = wire.MaxCaptureBytes+1, wire.MaxRenderFileChunks+1 return v }(), "one chunk too many": func() wire.RenderFile { v := big; v.Size, v.Total = wire.RenderFileChunk+3, 3; return v }(), "index before first": func() wire.RenderFile { v := big; v.Index = -1; return v }(), "index after last": func() wire.RenderFile { v := big; v.Index = v.Total; return v }(), } { if wire.ValidRenderFile(bad) { t.Errorf("%s accepted", name) } } } // TestValidSpecialistBinding: a dispatch has no binding at all, or a // whole one: a specialist id, a revision, a project id or "" for the // team, a menu-shaped profile id and 1 to 8 distinct capability ids. func TestValidSpecialistBinding(t *testing.T) { good := designDispatch() if !wire.ValidSpecialistBinding(&good) { t.Fatal("refused a whole binding") } team := designDispatch() team.SpecialistProject = "" none := wire.DispatchCommand{Specialist: wire.SpecialistEngineering} if !wire.ValidSpecialistBinding(&team) || !wire.ValidSpecialistBinding(&none) { t.Fatal("refused a team binding or none") } // An exploration's binding, with the specialist's brief at its bound. full := designDispatch() full.Explore, full.SpecialistBrief = true, strings.Repeat("b", wire.MaxSpecialistBrief) if !wire.ValidSpecialistBinding(&full) { t.Fatal("refused a binding with explore and its brief") } raw, _ := json.Marshal(full) if !strings.Contains(string(raw), `"explore":true`) || !strings.Contains(string(raw), `"specialist_brief":"bbb`) { t.Fatalf("names on the wire: %s", raw) } for name, mutate := range map[string]func(*wire.DispatchCommand){ "no id": func(d *wire.DispatchCommand) { d.SpecialistID = "" }, "id shape": func(d *wire.DispatchCommand) { d.SpecialistID = "abc" }, "id upper": func(d *wire.DispatchCommand) { d.SpecialistID = "sp_ABC" }, "id long": func(d *wire.DispatchCommand) { d.SpecialistID = "sp_" + strings.Repeat("a", 41) }, "revision zero": func(d *wire.DispatchCommand) { d.SpecialistRevision = 0 }, "project shape": func(d *wire.DispatchCommand) { d.SpecialistProject = "patrix" }, "no profile": func(d *wire.DispatchCommand) { d.Profile = "" }, "profile shape": func(d *wire.DispatchCommand) { d.Profile = "Design" }, "no version": func(d *wire.DispatchCommand) { d.Profile = "design" }, "no capabilities": func(d *wire.DispatchCommand) { d.Capabilities = nil }, "capability": func(d *wire.DispatchCommand) { d.Capabilities = []string{"branch.edit", "Page Render"} }, "capability bare": func(d *wire.DispatchCommand) { d.Capabilities = []string{"render"} }, "twice": func(d *wire.DispatchCommand) { d.Capabilities = []string{"page.render", "page.render"} }, "nine": func(d *wire.DispatchCommand) { d.Capabilities = []string{"a.a", "a.b", "a.c", "a.d", "a.e", "a.f", "a.g", "a.h", "a.i"} }, "part only": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Profile: "design-v1"} }, "explore only": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: wire.SpecialistEngineering, Explore: true} }, "brief only": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Specialist: wire.SpecialistEngineering, SpecialistBrief: "b"} }, "brief long": func(d *wire.DispatchCommand) { d.SpecialistBrief = strings.Repeat("b", wire.MaxSpecialistBrief+1) }, "brief not utf8": func(d *wire.DispatchCommand) { d.SpecialistBrief = "\xff" }, "brief nul": func(d *wire.DispatchCommand) { d.SpecialistBrief = "a\x00b" }, } { d := designDispatch() mutate(&d) if wire.ValidSpecialistBinding(&d) { t.Errorf("%s accepted", name) } } } // TestCaptureTree: the tree a capture id names, "" for anything else // (task 19 checks one render's digests by it). func TestCaptureTree(t *testing.T) { for id, want := range map[string]string{"1-work-2.png": "work", "3-base-1.png": "base", "12-diff-6.png": "diff", "0-work-1.png": "", "1-work-1.jpg": "", "../1-work-1.png": "", "": ""} { if got := wire.CaptureTree(id); got != want { t.Errorf("CaptureTree(%q) = %q, want %q", id, got, want) } } } // renderAudits are a Design attempt's render records: a work render, a // compare that ran out of time, and one refused for a secret. func renderAudits() []wire.RenderAudit { return []wire.RenderAudit{ {Call: 1, Attempt: 1, Tree: wire.RenderTreeWork, Pages: 2, Captures: 2, Files: 2, DurationMS: 4100, Outcome: wire.RenderRendered}, {Call: 2, Attempt: 1, Tree: wire.RenderTreeCompare, Pages: 3, Captures: 6, Files: 9, DurationMS: 9000, Outcome: wire.RenderRendered}, {Call: 3, Attempt: 2, Tree: wire.RenderTreeCompare, Pages: 1, Captures: 2, DurationMS: 60001, Outcome: "timeout"}, {Call: 4, Attempt: 2, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, DurationMS: 30, Outcome: "secrets"}, } } // TestRenderAudits (branch review finding 8): a Design report's render // records go out whole at 38 and round trip strictly, are dropped below // it, and are bounded: at most 20, numbered upwards, each of this // attempt or an earlier one, a known tree and outcome, 1 to 6 pages, // captures that are its pages (twice for a compare), files only for a // rendered call (its captures, and a compare's diffs), a duration within // ten minutes. func TestRenderAudits(t *testing.T) { rep := wire.StepReport{TaskID: "tk1", StepID: "s_1", Attempt: 2, State: wire.StepFailed, Reason: "model_failed", Findings: []wire.Finding{}, Renders: renderAudits()} f, _ := wire.NewFrame("r", wire.FrameStep, rep) if same, err := wire.Encode(f, wire.ProtocolDesign); err != nil || !bytes.Equal(same.Data, f.Data) { t.Fatalf("renders changed at 38: %v", err) } raw, _ := json.Marshal(f) _, body, err := wire.ParseFrame(raw) if err != nil { t.Fatal(err) } if got := body.(*wire.StepReport).Renders; len(got) != 4 || got[2] != renderAudits()[2] { t.Fatalf("round trip %+v", got) } for _, w := range []string{`"renders":[{"call":1,"attempt":1,"tree":"work","pages":2,"captures":2,"files":2,"duration_ms":4100,"outcome":"rendered"}`} { if !strings.Contains(string(f.Data), w) { t.Fatalf("lacks %s: %s", w, f.Data) } } got, err := wire.Encode(f, wire.ProtocolMacEngineering) if err != nil || strings.Contains(string(got.Data), `"renders"`) || !strings.Contains(string(got.Data), `"state":"failed"`) { t.Fatalf("renders at 37: %s %v", got.Data, err) } empty, _ := wire.NewFrame("r", wire.FrameStep, wire.StepReport{Findings: []wire.Finding{}}) if strings.Contains(string(empty.Data), `"renders"`) { t.Fatalf("no renders on the wire: %s", empty.Data) } if err := wire.ValidRenderAudits(renderAudits(), 2); err != nil { t.Fatal(err) } if err := wire.ValidRenderAudits(nil, 1); err != nil { t.Fatal(err) } var twenty []wire.RenderAudit for i := 1; i <= wire.MaxRenderAudits; i++ { twenty = append(twenty, wire.RenderAudit{Call: i, Attempt: 1, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, Outcome: "busy"}) } if err := wire.ValidRenderAudits(twenty, 1); err != nil { t.Fatalf("twenty: %v", err) } for name, bad := range map[string]func([]wire.RenderAudit) []wire.RenderAudit{ "twenty-one": func([]wire.RenderAudit) []wire.RenderAudit { return append(slices.Clone(twenty), wire.RenderAudit{Call: 20, Attempt: 1, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, Outcome: "busy"}) }, "call 0": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].Call = 0; return r }, "call 21": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Call = 21; return r }, "call repeated": func(r []wire.RenderAudit) []wire.RenderAudit { r[1].Call = 1; return r }, "call backwards": func(r []wire.RenderAudit) []wire.RenderAudit { r[0], r[1] = r[1], r[0]; return r }, "attempt 0": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].Attempt = 0; return r }, "a later attempt": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Attempt = 3; return r }, "attempt back": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Attempt = 1; return r }, "tree": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].Tree = "both"; return r }, "no pages": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Pages, r[3].Captures = 0, 0; return r }, "seven pages": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Pages, r[3].Captures = 7, 7; return r }, "captures": func(r []wire.RenderAudit) []wire.RenderAudit { r[1].Captures = 3; return r }, "files refused": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Files = 1; return r }, "files short": func(r []wire.RenderAudit) []wire.RenderAudit { r[1].Files = 6; return r }, "duration": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].DurationMS = -1; return r }, "too long": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].DurationMS = wire.MaxRenderAuditMS + 1; return r }, "outcome": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Outcome = "ok"; return r }, } { if err := wire.ValidRenderAudits(bad(renderAudits()), 2); !errors.Is(err, wire.ErrStrict) { t.Errorf("%s: %v", name, err) } } }