| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | // designDispatch is a Design step's dispatch as the instance sends it at |
| 17 | // protocol 38: Engineering's step kind with the specialist's binding. |
| 18 | func designDispatch() wire.DispatchCommand { |
| 19 | 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{}, |
| 20 | SpecialistID: "sp_abc234", SpecialistRevision: 2, SpecialistProject: "p_xyz567", Profile: "design-v1", Capabilities: []string{"branch.edit", "page.render"}} |
| 21 | } |
| 22 | |
| 23 | func sum(c byte) string { return strings.Repeat(string(c), 64) } |
| 24 | |
| 25 | func designOptions() []wire.DesignOption { |
| 26 | return []wire.DesignOption{ |
| 27 | {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')}}}, |
| 28 | {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')}}}, |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func renderFile() wire.RenderFile { |
| 33 | 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"} |
| 34 | } |
| 35 | |
| 36 | // TestProtocol38Design: protocol 38 is the Design specialist's and the |
| 37 | // current one. |
| 38 | func TestProtocol38Design(t *testing.T) { |
| 39 | if wire.Protocol38 != 38 || wire.ProtocolDesign != wire.Protocol38 || wire.ProtocolCurrent != wire.ProtocolDesign || wire.Spoken(99) != 38 || wire.ProtocolMacEngineering != 37 { |
| 40 | t.Fatalf("design %d, current %d, spoken %d", wire.ProtocolDesign, wire.ProtocolCurrent, wire.Spoken(99)) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // TestEncodeBelowProtocol38RefusesDesign: at 38 a bound dispatch, a |
| 45 | // render lease check, a render_file chunk, a report's options and a |
| 46 | // project-scoped proposal go out whole. Below it a dispatch carrying any |
| 47 | // part of a specialist binding, a render check, a render_file chunk and |
| 48 | // a proposal for one project are refused (an older runner would run a |
| 49 | // Design step as plain Engineering, an older instance would grant a |
| 50 | // render as a start, or a proposal for the whole team); a report loses |
| 51 | // its options, and a proposal for the whole team its empty project. |
| 52 | func TestEncodeBelowProtocol38RefusesDesign(t *testing.T) { |
| 53 | dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, designDispatch()) |
| 54 | lease, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s_1", Attempt: 1, Purpose: wire.LeasePurposeRender}) |
| 55 | chunk, _ := wire.NewFrame("f", wire.FrameRenderFile, renderFile()) |
| 56 | report, _ := wire.NewFrame("r", wire.FrameStep, wire.StepReport{TaskID: "tk1", StepID: "s_1", Attempt: 1, State: wire.StepDone, Findings: []wire.Finding{}, Options: designOptions()}) |
| 57 | 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"}`)}) |
| 58 | 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":""}`)}) |
| 59 | for _, f := range []wire.Frame{dispatch, lease, chunk, report, scoped, team} { |
| 60 | if same, err := wire.Encode(f, wire.ProtocolDesign); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 61 | t.Fatalf("%s changed at 38: %v", f.Type, err) |
| 62 | } |
| 63 | } |
| 64 | if !chunk.Durable() { |
| 65 | t.Fatal("render_file is not durable") |
| 66 | } |
| 67 | for _, f := range []wire.Frame{dispatch, lease, chunk, scoped} { |
| 68 | for _, p := range []int{wire.ProtocolMacEngineering, wire.ProtocolLookup, wire.ProtocolChangeDiffs, wire.ProtocolPlaces, wire.ProtocolResearch, wire.Protocol2} { |
| 69 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 70 | t.Fatalf("%s %s encoded at %d: %v", f.Type, f.Data, p, err) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | // Each part of the binding alone makes it a Design dispatch. |
| 75 | for name, mutate := range map[string]func(*wire.DispatchCommand){ |
| 76 | "id": func(d *wire.DispatchCommand) { |
| 77 | *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistID: "sp_abc234"} |
| 78 | }, |
| 79 | "revision": func(d *wire.DispatchCommand) { |
| 80 | *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistRevision: 1} |
| 81 | }, |
| 82 | "project": func(d *wire.DispatchCommand) { |
| 83 | *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistProject: "p_1"} |
| 84 | }, |
| 85 | "profile": func(d *wire.DispatchCommand) { |
| 86 | *d = wire.DispatchCommand{Specialist: d.Specialist, Profile: "design-v1"} |
| 87 | }, |
| 88 | "capabilities": func(d *wire.DispatchCommand) { |
| 89 | *d = wire.DispatchCommand{Specialist: d.Specialist, Capabilities: []string{"page.render"}} |
| 90 | }, |
| 91 | "explore": func(d *wire.DispatchCommand) { |
| 92 | *d = wire.DispatchCommand{Specialist: d.Specialist, Explore: true} |
| 93 | }, |
| 94 | "brief": func(d *wire.DispatchCommand) { |
| 95 | *d = wire.DispatchCommand{Specialist: d.Specialist, SpecialistBrief: "Calm and editorial."} |
| 96 | }, |
| 97 | } { |
| 98 | d := designDispatch() |
| 99 | mutate(&d) |
| 100 | f, _ := wire.NewFrame("d", wire.FrameDispatch, d) |
| 101 | if _, err := wire.Encode(f, wire.ProtocolMacEngineering); !errors.Is(err, wire.ErrProtocol) { |
| 102 | t.Fatalf("a dispatch with only its %s at 37: %v", name, err) |
| 103 | } |
| 104 | } |
| 105 | // Another lease purpose, and a dispatch without a binding, are as before. |
| 106 | plainLease, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s_1", Attempt: 1}) |
| 107 | eng := designDispatch() |
| 108 | eng.SpecialistID, eng.SpecialistRevision, eng.SpecialistProject, eng.Profile, eng.Capabilities = "", 0, "", "", nil |
| 109 | plain, _ := wire.NewFrame("e", wire.FrameDispatch, eng) |
| 110 | for _, f := range []wire.Frame{plainLease, plain} { |
| 111 | if same, err := wire.Encode(f, wire.ProtocolMacEngineering); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 112 | t.Fatalf("%s changed at 37: %s %v", f.Type, same.Data, err) |
| 113 | } |
| 114 | } |
| 115 | got, err := wire.Encode(report, wire.ProtocolMacEngineering) |
| 116 | if err != nil || strings.Contains(string(got.Data), `"options"`) || !strings.Contains(string(got.Data), `"state":"done"`) { |
| 117 | t.Fatalf("a report with options at 37: %s %v", got.Data, err) |
| 118 | } |
| 119 | got, err = wire.Encode(team, wire.ProtocolMacEngineering) |
| 120 | if err != nil || strings.Contains(string(got.Data), `project`) || !strings.Contains(string(got.Data), `research-v1`) { |
| 121 | t.Fatalf("a team proposal at 37: %s %v", got.Data, err) |
| 122 | } |
| 123 | // A proposal without the key is untouched. |
| 124 | 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":[]}`)}) |
| 125 | if same, err := wire.Encode(bare, wire.ProtocolMacEngineering); err != nil || !bytes.Equal(same.Data, bare.Data) { |
| 126 | t.Fatalf("a proposal without project changed at 37: %v", err) |
| 127 | } |
| 128 | // A project that is not a string is not read as the team's. |
| 129 | odd, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{TurnID: "t", CallID: "c", Name: wire.ToolProposeSpecialist, Args: []byte(`{"profile":"research-v1","project":null}`)}) |
| 130 | if _, err := wire.Encode(odd, wire.ProtocolMacEngineering); !errors.Is(err, wire.ErrProtocol) { |
| 131 | t.Fatalf("a null project at 37: %v", err) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // TestDesignRoundTrip: each new frame and field survives the wire exactly |
| 136 | // (strict decode on the far side), with the names the spec gives them. |
| 137 | func TestDesignRoundTrip(t *testing.T) { |
| 138 | for _, c := range []struct { |
| 139 | kind string |
| 140 | body any |
| 141 | want []string |
| 142 | }{ |
| 143 | {wire.FrameDispatch, designDispatch(), []string{`"specialist_id":"sp_abc234"`, `"specialist_revision":2`, `"specialist_project":"p_xyz567"`, `"profile":"design-v1"`, `"capabilities":["branch.edit","page.render"]`}}, |
| 144 | {wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "s_1", Attempt: 1, Purpose: wire.LeasePurposeRender}, []string{`"purpose":"page_render"`}}, |
| 145 | {wire.FrameRenderFile, renderFile(), []string{`"capture_id":"1-work-1.png"`, `"index":0`, `"total":1`, `"size":3`}}, |
| 146 | {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') + `"}`}}, |
| 147 | } { |
| 148 | f, err := wire.NewFrame("x", c.kind, c.body) |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | raw, _ := json.Marshal(f) |
| 153 | g, body, err := wire.ParseFrame(raw) |
| 154 | if err != nil { |
| 155 | t.Fatalf("%s: %v", c.kind, err) |
| 156 | } |
| 157 | again, _ := json.Marshal(body) |
| 158 | if !bytes.Equal(again, g.Data) { |
| 159 | t.Fatalf("%s did not round trip:\n%s\n%s", c.kind, again, g.Data) |
| 160 | } |
| 161 | for _, w := range c.want { |
| 162 | if !strings.Contains(string(g.Data), w) { |
| 163 | t.Fatalf("%s lacks %s: %s", c.kind, w, g.Data) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | // Absent, the new fields are not on the wire at all. |
| 168 | f, _ := wire.NewFrame("e", wire.FrameDispatch, wire.DispatchCommand{Findings: []wire.Finding{}}) |
| 169 | r, _ := wire.NewFrame("r", wire.FrameStep, wire.StepReport{Findings: []wire.Finding{}}) |
| 170 | for _, s := range []string{`specialist_`, `"profile"`, `"capabilities"`, `"options"`} { |
| 171 | if strings.Contains(string(f.Data), s) || strings.Contains(string(r.Data), s) { |
| 172 | t.Fatalf("an empty %s on the wire: %s %s", s, f.Data, r.Data) |
| 173 | } |
| 174 | } |
| 175 | // A specialist's scope: its project, "" for the team, always said. |
| 176 | raw, _ := json.Marshal(wire.Specialist{ID: "sp_1", Project: "p_xyz567"}) |
| 177 | team, _ := json.Marshal(wire.Specialist{ID: "sp_1"}) |
| 178 | if !strings.Contains(string(raw), `"project":"p_xyz567"`) || !strings.Contains(string(team), `"project":""`) { |
| 179 | t.Fatalf("specialist scope: %s %s", raw, team) |
| 180 | } |
| 181 | var back wire.Specialist |
| 182 | if err := wire.Strict(raw, &back); err != nil || back.Project != "p_xyz567" { |
| 183 | t.Fatalf("specialist scope back: %+v %v", back, err) |
| 184 | } |
| 185 | // Build's note is the same field as Try again's. |
| 186 | var answer wire.AnswerCard |
| 187 | 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 { |
| 188 | t.Fatalf("build note: %+v %v", answer, err) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // TestValidOptions bounds a Design report's options: at most 4, at most |
| 193 | // 12 captures across them, each name a short plain label said once, each |
| 194 | // capture a render_page file id said once, with SHA-256 and tree digests |
| 195 | // of lower-case hex. No options is valid. |
| 196 | func TestValidOptions(t *testing.T) { |
| 197 | if err := wire.ValidOptions(designOptions()); err != nil { |
| 198 | t.Fatalf("refused: %v", err) |
| 199 | } |
| 200 | if err := wire.ValidOptions(nil); err != nil { |
| 201 | t.Fatalf("no options refused: %v", err) |
| 202 | } |
| 203 | if err := wire.ValidOptions([]wire.DesignOption{{Name: "Café 2 - l'été", Captures: []wire.DesignCapture{}}}); err != nil { |
| 204 | t.Fatalf("a plain label without captures refused: %v", err) |
| 205 | } |
| 206 | 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 { |
| 207 | t.Fatalf("the longest name and a high id refused: %v", err) |
| 208 | } |
| 209 | five := []wire.DesignOption{} |
| 210 | for _, n := range []string{"A", "B", "C", "D", "E"} { |
| 211 | five = append(five, wire.DesignOption{Name: n, Captures: []wire.DesignCapture{}}) |
| 212 | } |
| 213 | // Thirteen distinct captures (calls 1 to 3, pages 1 to 6), over two |
| 214 | // options: seven and six. |
| 215 | thirteen := []wire.DesignOption{{Name: "A"}, {Name: "B"}} |
| 216 | for i := range 13 { |
| 217 | c := wire.DesignCapture{ID: fmt.Sprintf("%d-work-%d.png", i/6+1, i%6+1), SHA256: sum('a'), TreeDigest: sum('b')} |
| 218 | thirteen[i%2].Captures = append(thirteen[i%2].Captures, c) |
| 219 | } |
| 220 | twelve := []wire.DesignOption{{Name: "A", Captures: thirteen[0].Captures}, {Name: "B", Captures: thirteen[1].Captures[:5]}} |
| 221 | if err := wire.ValidOptions(twelve); err != nil { |
| 222 | t.Fatalf("twelve captures refused: %v", err) |
| 223 | } |
| 224 | mutate := func(f func(o []wire.DesignOption)) []wire.DesignOption { |
| 225 | o := designOptions() |
| 226 | for i := range o { |
| 227 | o[i].Captures = append([]wire.DesignCapture{}, o[i].Captures...) |
| 228 | } |
| 229 | f(o) |
| 230 | return o |
| 231 | } |
| 232 | for name, bad := range map[string][]wire.DesignOption{ |
| 233 | "five options": five, |
| 234 | "thirteen captures": thirteen, |
| 235 | "empty name": mutate(func(o []wire.DesignOption) { o[0].Name = "" }), |
| 236 | "long name": mutate(func(o []wire.DesignOption) { o[0].Name = strings.Repeat("a", wire.MaxOptionName+1) }), |
| 237 | "leading space": mutate(func(o []wire.DesignOption) { o[0].Name = " Bold" }), |
| 238 | "trailing space": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold " }), |
| 239 | "two spaces": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold grid" }), |
| 240 | "leading hyphen": mutate(func(o []wire.DesignOption) { o[0].Name = "-Bold" }), |
| 241 | "control": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold\ngrid" }), |
| 242 | "markup": mutate(func(o []wire.DesignOption) { o[0].Name = "<b>Bold</b>" }), |
| 243 | "path": mutate(func(o []wire.DesignOption) { o[0].Name = "mockups/bold" }), |
| 244 | "invalid utf-8": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold\xff" }), |
| 245 | "format character": mutate(func(o []wire.DesignOption) { o[0].Name = "Bold\u202egrid" }), |
| 246 | "same name": mutate(func(o []wire.DesignOption) { o[1].Name = "quiet EDITORIAL" }), |
| 247 | "same capture": mutate(func(o []wire.DesignOption) { o[1].Captures[0].ID = "1-work-1.png" }), |
| 248 | "same capture twice": mutate(func(o []wire.DesignOption) { o[0].Captures[1].ID = "1-work-1.png" }), |
| 249 | "id with a path": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "/render/1-work-1.png" }), |
| 250 | "id with a dot dot": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "../1-work-1.png" }), |
| 251 | "id of another tree": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-head-1.png" }), |
| 252 | "id call zero": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "0-work-1.png" }), |
| 253 | "id leading zero": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "01-work-1.png" }), |
| 254 | "id past the calls": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "100-work-1.png" }), |
| 255 | "id page zero": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-work-0.png" }), |
| 256 | "id not a png": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-work-1.jpg" }), |
| 257 | "id upper case": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "1-WORK-1.png" }), |
| 258 | "empty id": mutate(func(o []wire.DesignOption) { o[0].Captures[0].ID = "" }), |
| 259 | "sha256 short": mutate(func(o []wire.DesignOption) { o[0].Captures[0].SHA256 = sum('a')[:63] }), |
| 260 | "sha256 upper": mutate(func(o []wire.DesignOption) { o[0].Captures[0].SHA256 = strings.Repeat("A", 64) }), |
| 261 | "sha256 empty": mutate(func(o []wire.DesignOption) { o[0].Captures[0].SHA256 = "" }), |
| 262 | "tree digest sha1": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = strings.Repeat("b", 40) }), |
| 263 | "tree digest not hex": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = strings.Repeat("g", 64) }), |
| 264 | "tree digest empty": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = "" }), |
| 265 | "tree digest too long": mutate(func(o []wire.DesignOption) { o[0].Captures[0].TreeDigest = sum('b') + "b" }), |
| 266 | } { |
| 267 | if err := wire.ValidOptions(bad); !errors.Is(err, wire.ErrStrict) { |
| 268 | t.Errorf("%s accepted: %v", name, err) |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // TestValidRenderFile bounds one render_file chunk: its step, a capture |
| 274 | // id of the render_page shape, a whole file of 1 byte to MaxCaptureBytes |
| 275 | // in exactly as many chunks as its size needs, and this chunk's bytes |
| 276 | // exactly its share, in strict base64. |
| 277 | func TestValidRenderFile(t *testing.T) { |
| 278 | if !wire.ValidRenderFile(renderFile()) { |
| 279 | t.Fatal("refused a one-chunk file") |
| 280 | } |
| 281 | full := make([]byte, wire.RenderFileChunk) |
| 282 | 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)} |
| 283 | if !wire.ValidRenderFile(big) { |
| 284 | t.Fatal("refused the first chunk of the largest capture") |
| 285 | } |
| 286 | last := big |
| 287 | last.Index = wire.MaxRenderFileChunks - 1 |
| 288 | if !wire.ValidRenderFile(last) { |
| 289 | t.Fatal("refused the last chunk of the largest capture") |
| 290 | } |
| 291 | if wire.MaxRenderFileChunks != 16 || wire.MaxCaptureBytes != 2<<20 || wire.RenderFileChunk != wire.ResearchFileChunk { |
| 292 | t.Fatalf("bounds %d %d %d", wire.MaxRenderFileChunks, wire.MaxCaptureBytes, wire.RenderFileChunk) |
| 293 | } |
| 294 | mutate := func(f func(v *wire.RenderFile)) wire.RenderFile { v := renderFile(); f(&v); return v } |
| 295 | for name, bad := range map[string]wire.RenderFile{ |
| 296 | "no task": mutate(func(v *wire.RenderFile) { v.TaskID = "" }), |
| 297 | "no step": mutate(func(v *wire.RenderFile) { v.StepID = "" }), |
| 298 | "long step": mutate(func(v *wire.RenderFile) { v.StepID = strings.Repeat("s", 129) }), |
| 299 | "attempt zero": mutate(func(v *wire.RenderFile) { v.Attempt = 0 }), |
| 300 | "bad capture": mutate(func(v *wire.RenderFile) { v.CaptureID = "img1" }), |
| 301 | "capture path": mutate(func(v *wire.RenderFile) { v.CaptureID = "../1-work-1.png" }), |
| 302 | "sha256": mutate(func(v *wire.RenderFile) { v.SHA256 = "abc" }), |
| 303 | "empty file": mutate(func(v *wire.RenderFile) { v.Size, v.Data = 0, "" }), |
| 304 | "too large": mutate(func(v *wire.RenderFile) { v.Size, v.Total = wire.MaxCaptureBytes+1, wire.MaxRenderFileChunks+1 }), |
| 305 | "total wrong": mutate(func(v *wire.RenderFile) { v.Total = 2 }), |
| 306 | "total zero": mutate(func(v *wire.RenderFile) { v.Total = 0 }), |
| 307 | "index negative": mutate(func(v *wire.RenderFile) { v.Index = -1 }), |
| 308 | "index past": mutate(func(v *wire.RenderFile) { v.Index = 1 }), |
| 309 | "short data": mutate(func(v *wire.RenderFile) { v.Data = "YWI=" }), |
| 310 | "long data": mutate(func(v *wire.RenderFile) { v.Data = "YWJjZA==" }), |
| 311 | "not base64": mutate(func(v *wire.RenderFile) { v.Data = "Y*Jj" }), |
| 312 | "unpadded": mutate(func(v *wire.RenderFile) { v.Data, v.Size = "YWI", 2 }), |
| 313 | "short middle": func() wire.RenderFile { v := big; v.Index = 1; v.Data = "YWJj"; return v }(), |
| 314 | "long last": func() wire.RenderFile { |
| 315 | v := last |
| 316 | v.Data = base64.StdEncoding.EncodeToString(append(full, 0)) |
| 317 | return v |
| 318 | }(), |
| 319 | "oversized encoded": func() wire.RenderFile { |
| 320 | v := big |
| 321 | v.Data = strings.Repeat("A", base64.StdEncoding.EncodedLen(wire.RenderFileChunk)+4) |
| 322 | return v |
| 323 | }(), |
| 324 | // Each bound alone, everything else consistent with it. |
| 325 | "too large, whole": func() wire.RenderFile { |
| 326 | v := big |
| 327 | v.Size, v.Total = wire.MaxCaptureBytes+1, wire.MaxRenderFileChunks+1 |
| 328 | return v |
| 329 | }(), |
| 330 | "one chunk too many": func() wire.RenderFile { v := big; v.Size, v.Total = wire.RenderFileChunk+3, 3; return v }(), |
| 331 | "index before first": func() wire.RenderFile { v := big; v.Index = -1; return v }(), |
| 332 | "index after last": func() wire.RenderFile { v := big; v.Index = v.Total; return v }(), |
| 333 | } { |
| 334 | if wire.ValidRenderFile(bad) { |
| 335 | t.Errorf("%s accepted", name) |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // TestValidSpecialistBinding: a dispatch has no binding at all, or a |
| 341 | // whole one: a specialist id, a revision, a project id or "" for the |
| 342 | // team, a menu-shaped profile id and 1 to 8 distinct capability ids. |
| 343 | func TestValidSpecialistBinding(t *testing.T) { |
| 344 | good := designDispatch() |
| 345 | if !wire.ValidSpecialistBinding(&good) { |
| 346 | t.Fatal("refused a whole binding") |
| 347 | } |
| 348 | team := designDispatch() |
| 349 | team.SpecialistProject = "" |
| 350 | none := wire.DispatchCommand{Specialist: wire.SpecialistEngineering} |
| 351 | if !wire.ValidSpecialistBinding(&team) || !wire.ValidSpecialistBinding(&none) { |
| 352 | t.Fatal("refused a team binding or none") |
| 353 | } |
| 354 | // An exploration's binding, with the specialist's brief at its bound. |
| 355 | full := designDispatch() |
| 356 | full.Explore, full.SpecialistBrief = true, strings.Repeat("b", wire.MaxSpecialistBrief) |
| 357 | if !wire.ValidSpecialistBinding(&full) { |
| 358 | t.Fatal("refused a binding with explore and its brief") |
| 359 | } |
| 360 | raw, _ := json.Marshal(full) |
| 361 | if !strings.Contains(string(raw), `"explore":true`) || !strings.Contains(string(raw), `"specialist_brief":"bbb`) { |
| 362 | t.Fatalf("names on the wire: %s", raw) |
| 363 | } |
| 364 | for name, mutate := range map[string]func(*wire.DispatchCommand){ |
| 365 | "no id": func(d *wire.DispatchCommand) { d.SpecialistID = "" }, |
| 366 | "id shape": func(d *wire.DispatchCommand) { d.SpecialistID = "abc" }, |
| 367 | "id upper": func(d *wire.DispatchCommand) { d.SpecialistID = "sp_ABC" }, |
| 368 | "id long": func(d *wire.DispatchCommand) { d.SpecialistID = "sp_" + strings.Repeat("a", 41) }, |
| 369 | "revision zero": func(d *wire.DispatchCommand) { d.SpecialistRevision = 0 }, |
| 370 | "project shape": func(d *wire.DispatchCommand) { d.SpecialistProject = "patrix" }, |
| 371 | "no profile": func(d *wire.DispatchCommand) { d.Profile = "" }, |
| 372 | "profile shape": func(d *wire.DispatchCommand) { d.Profile = "Design" }, |
| 373 | "no version": func(d *wire.DispatchCommand) { d.Profile = "design" }, |
| 374 | "no capabilities": func(d *wire.DispatchCommand) { d.Capabilities = nil }, |
| 375 | "capability": func(d *wire.DispatchCommand) { d.Capabilities = []string{"branch.edit", "Page Render"} }, |
| 376 | "capability bare": func(d *wire.DispatchCommand) { d.Capabilities = []string{"render"} }, |
| 377 | "twice": func(d *wire.DispatchCommand) { d.Capabilities = []string{"page.render", "page.render"} }, |
| 378 | "nine": func(d *wire.DispatchCommand) { |
| 379 | d.Capabilities = []string{"a.a", "a.b", "a.c", "a.d", "a.e", "a.f", "a.g", "a.h", "a.i"} |
| 380 | }, |
| 381 | "part only": func(d *wire.DispatchCommand) { *d = wire.DispatchCommand{Profile: "design-v1"} }, |
| 382 | "explore only": func(d *wire.DispatchCommand) { |
| 383 | *d = wire.DispatchCommand{Specialist: wire.SpecialistEngineering, Explore: true} |
| 384 | }, |
| 385 | "brief only": func(d *wire.DispatchCommand) { |
| 386 | *d = wire.DispatchCommand{Specialist: wire.SpecialistEngineering, SpecialistBrief: "b"} |
| 387 | }, |
| 388 | "brief long": func(d *wire.DispatchCommand) { d.SpecialistBrief = strings.Repeat("b", wire.MaxSpecialistBrief+1) }, |
| 389 | "brief not utf8": func(d *wire.DispatchCommand) { d.SpecialistBrief = "\xff" }, |
| 390 | "brief nul": func(d *wire.DispatchCommand) { d.SpecialistBrief = "a\x00b" }, |
| 391 | } { |
| 392 | d := designDispatch() |
| 393 | mutate(&d) |
| 394 | if wire.ValidSpecialistBinding(&d) { |
| 395 | t.Errorf("%s accepted", name) |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // TestCaptureTree: the tree a capture id names, "" for anything else |
| 401 | // (task 19 checks one render's digests by it). |
| 402 | func TestCaptureTree(t *testing.T) { |
| 403 | 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": "", "": ""} { |
| 404 | if got := wire.CaptureTree(id); got != want { |
| 405 | t.Errorf("CaptureTree(%q) = %q, want %q", id, got, want) |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // renderAudits are a Design attempt's render records: a work render, a |
| 411 | // compare that ran out of time, and one refused for a secret. |
| 412 | func renderAudits() []wire.RenderAudit { |
| 413 | return []wire.RenderAudit{ |
| 414 | {Call: 1, Attempt: 1, Tree: wire.RenderTreeWork, Pages: 2, Captures: 2, Files: 2, DurationMS: 4100, Outcome: wire.RenderRendered}, |
| 415 | {Call: 2, Attempt: 1, Tree: wire.RenderTreeCompare, Pages: 3, Captures: 6, Files: 9, DurationMS: 9000, Outcome: wire.RenderRendered}, |
| 416 | {Call: 3, Attempt: 2, Tree: wire.RenderTreeCompare, Pages: 1, Captures: 2, DurationMS: 60001, Outcome: "timeout"}, |
| 417 | {Call: 4, Attempt: 2, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, DurationMS: 30, Outcome: "secrets"}, |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // TestRenderAudits (branch review finding 8): a Design report's render |
| 422 | // records go out whole at 38 and round trip strictly, are dropped below |
| 423 | // it, and are bounded: at most 20, numbered upwards, each of this |
| 424 | // attempt or an earlier one, a known tree and outcome, 1 to 6 pages, |
| 425 | // captures that are its pages (twice for a compare), files only for a |
| 426 | // rendered call (its captures, and a compare's diffs), a duration within |
| 427 | // ten minutes. |
| 428 | func TestRenderAudits(t *testing.T) { |
| 429 | rep := wire.StepReport{TaskID: "tk1", StepID: "s_1", Attempt: 2, State: wire.StepFailed, Reason: "model_failed", Findings: []wire.Finding{}, Renders: renderAudits()} |
| 430 | f, _ := wire.NewFrame("r", wire.FrameStep, rep) |
| 431 | if same, err := wire.Encode(f, wire.ProtocolDesign); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 432 | t.Fatalf("renders changed at 38: %v", err) |
| 433 | } |
| 434 | raw, _ := json.Marshal(f) |
| 435 | _, body, err := wire.ParseFrame(raw) |
| 436 | if err != nil { |
| 437 | t.Fatal(err) |
| 438 | } |
| 439 | if got := body.(*wire.StepReport).Renders; len(got) != 4 || got[2] != renderAudits()[2] { |
| 440 | t.Fatalf("round trip %+v", got) |
| 441 | } |
| 442 | for _, w := range []string{`"renders":[{"call":1,"attempt":1,"tree":"work","pages":2,"captures":2,"files":2,"duration_ms":4100,"outcome":"rendered"}`} { |
| 443 | if !strings.Contains(string(f.Data), w) { |
| 444 | t.Fatalf("lacks %s: %s", w, f.Data) |
| 445 | } |
| 446 | } |
| 447 | got, err := wire.Encode(f, wire.ProtocolMacEngineering) |
| 448 | if err != nil || strings.Contains(string(got.Data), `"renders"`) || !strings.Contains(string(got.Data), `"state":"failed"`) { |
| 449 | t.Fatalf("renders at 37: %s %v", got.Data, err) |
| 450 | } |
| 451 | empty, _ := wire.NewFrame("r", wire.FrameStep, wire.StepReport{Findings: []wire.Finding{}}) |
| 452 | if strings.Contains(string(empty.Data), `"renders"`) { |
| 453 | t.Fatalf("no renders on the wire: %s", empty.Data) |
| 454 | } |
| 455 | if err := wire.ValidRenderAudits(renderAudits(), 2); err != nil { |
| 456 | t.Fatal(err) |
| 457 | } |
| 458 | if err := wire.ValidRenderAudits(nil, 1); err != nil { |
| 459 | t.Fatal(err) |
| 460 | } |
| 461 | var twenty []wire.RenderAudit |
| 462 | for i := 1; i <= wire.MaxRenderAudits; i++ { |
| 463 | twenty = append(twenty, wire.RenderAudit{Call: i, Attempt: 1, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, Outcome: "busy"}) |
| 464 | } |
| 465 | if err := wire.ValidRenderAudits(twenty, 1); err != nil { |
| 466 | t.Fatalf("twenty: %v", err) |
| 467 | } |
| 468 | for name, bad := range map[string]func([]wire.RenderAudit) []wire.RenderAudit{ |
| 469 | "twenty-one": func([]wire.RenderAudit) []wire.RenderAudit { |
| 470 | return append(slices.Clone(twenty), wire.RenderAudit{Call: 20, Attempt: 1, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, Outcome: "busy"}) |
| 471 | }, |
| 472 | "call 0": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].Call = 0; return r }, |
| 473 | "call 21": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Call = 21; return r }, |
| 474 | "call repeated": func(r []wire.RenderAudit) []wire.RenderAudit { r[1].Call = 1; return r }, |
| 475 | "call backwards": func(r []wire.RenderAudit) []wire.RenderAudit { r[0], r[1] = r[1], r[0]; return r }, |
| 476 | "attempt 0": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].Attempt = 0; return r }, |
| 477 | "a later attempt": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Attempt = 3; return r }, |
| 478 | "attempt back": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Attempt = 1; return r }, |
| 479 | "tree": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].Tree = "both"; return r }, |
| 480 | "no pages": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Pages, r[3].Captures = 0, 0; return r }, |
| 481 | "seven pages": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Pages, r[3].Captures = 7, 7; return r }, |
| 482 | "captures": func(r []wire.RenderAudit) []wire.RenderAudit { r[1].Captures = 3; return r }, |
| 483 | "files refused": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Files = 1; return r }, |
| 484 | "files short": func(r []wire.RenderAudit) []wire.RenderAudit { r[1].Files = 6; return r }, |
| 485 | "duration": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].DurationMS = -1; return r }, |
| 486 | "too long": func(r []wire.RenderAudit) []wire.RenderAudit { r[0].DurationMS = wire.MaxRenderAuditMS + 1; return r }, |
| 487 | "outcome": func(r []wire.RenderAudit) []wire.RenderAudit { r[3].Outcome = "ok"; return r }, |
| 488 | } { |
| 489 | if err := wire.ValidRenderAudits(bad(renderAudits()), 2); !errors.Is(err, wire.ErrStrict) { |
| 490 | t.Errorf("%s: %v", name, err) |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |