oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package wire_test
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13func TestProtocol31ChangeFiles(t *testing.T) {
14 if wire.ProtocolChangeFiles != 31 || wire.ProtocolCurrent != 38 {
15 t.Fatal("protocol not 31")
16 }
17 facts := &wire.ChangeFiles{Base: strings.Repeat("a", 40), SHA: strings.Repeat("b", 40), Files: 1, Additions: 1, Entries: []wire.ChangedFile{{Path: "a.txt", Status: "added", Additions: 1}}}
18 report := wire.StepReport{StepID: "s", Attempt: 1, State: wire.StepDone, ChangeFiles: facts}
19 f, err := wire.NewFrame("r", wire.FrameStep, report)
20 if err != nil {
21 t.Fatal(err)
22 }
23 for p := 1; p <= 31; p++ {
24 got, err := wire.Encode(f, p)
25 if err != nil {
26 t.Fatalf("%d: %v", p, err)
27 }
28 var fields map[string]json.RawMessage
29 if err := json.Unmarshal(got.Data, &fields); err != nil {
30 t.Fatal(err)
31 }
32 if (fields["change_files"] != nil) != (p >= 31) {
33 t.Fatalf("field at %d: %s", p, got.Data)
34 }
35 if p < 31 {
36 frozen13(t, "pre-31 StepReport", got.Data, frozen30StepReport)
37 }
38
39 }
40 if report.ChangeFiles == nil {
41 t.Fatal("Encode mutated input")
42 }
43}
44
45func TestChangeFilesValidation(t *testing.T) {
46 valid := func() *wire.ChangeFiles {
47 return &wire.ChangeFiles{Base: strings.Repeat("a", 40), SHA: strings.Repeat("b", 40), Files: 1, Additions: 1, Entries: []wire.ChangedFile{{Path: "dir/a 日本語.txt", Status: "added", Additions: 1}}}
48 }
49 if !wire.ValidChangeFiles(valid()) {
50 t.Fatal("valid paths refused")
51 }
52 for name, mutate := range map[string]func(*wire.ChangeFiles){
53 "nil-list": func(f *wire.ChangeFiles) { f.Files = 0; f.Additions = 0; f.Entries = nil },
54 "base": func(f *wire.ChangeFiles) { f.Base = "main" },
55 "sha": func(f *wire.ChangeFiles) { f.SHA = strings.Repeat("z", 40) },
56 "negative": func(f *wire.ChangeFiles) { f.Additions = -1 },
57 "totals": func(f *wire.ChangeFiles) { f.Additions = 2 },
58 "truncation": func(f *wire.ChangeFiles) { f.Files = 2 },
59 "status": func(f *wire.ChangeFiles) { f.Entries[0].Status = "copy" },
60 "old": func(f *wire.ChangeFiles) { f.Entries[0].OldPath = "old" },
61 "rename": func(f *wire.ChangeFiles) { f.Entries[0].Status = "renamed" },
62 "binary": func(f *wire.ChangeFiles) { f.Entries[0].Binary = true; f.Binary = 1 },
63 "short-base": func(f *wire.ChangeFiles) { f.Base = strings.Repeat("a", 39) },
64 "short-sha": func(f *wire.ChangeFiles) { f.SHA = strings.Repeat("b", 39) },
65 "excess-binaries": func(f *wire.ChangeFiles) { f.Entries = []wire.ChangedFile{}; f.Truncated = true; f.Binary = 2 },
66 "invalid-utf8": func(f *wire.ChangeFiles) { f.Entries[0].Path = string([]byte{255}) },
67 "path": func(f *wire.ChangeFiles) { f.Entries[0].Path = "../evil" },
68 "control": func(f *wire.ChangeFiles) { f.Entries[0].Path = "a\nb" },
69 "byte-bound": func(f *wire.ChangeFiles) {
70 f.Files = 200
71 f.Additions = 200
72 f.Entries = []wire.ChangedFile{}
73 for i := 0; i < 200; i++ {
74 f.Entries = append(f.Entries, wire.ChangedFile{Path: fmt.Sprintf("%d/%s", i, strings.Repeat("x", 2000)), Status: "added", Additions: 1})
75 }
76 },
77 "duplicate": func(f *wire.ChangeFiles) { f.Files = 2; f.Additions = 2; f.Entries = append(f.Entries, f.Entries[0]) },
78 "bound": func(f *wire.ChangeFiles) {
79 f.Files = 201
80 f.Additions = 201
81 f.Entries = []wire.ChangedFile{}
82 for i := 0; i < 201; i++ {
83 f.Entries = append(f.Entries, wire.ChangedFile{Path: fmt.Sprintf("file-%d", i), Status: "added", Additions: 1})
84 }
85 },
86 } {
87 t.Run(name, func(t *testing.T) {
88 f := valid()
89 mutate(f)
90 if wire.ValidChangeFiles(f) {
91 t.Fatal("invalid accepted")
92 }
93 })
94 }
95}
96
97// Frozen from main 18fb5be4, before protocol 31.
98var frozen30StepReport = keySet("base_checks base_checks_sha failure copy task_id step_id attempt state reason base_sha candidate_sha pushed termination summary headline log_tail family account verdict review_label findings checks gate exit_code output askable merge_sha into strategy usage switched termination_kind have phase apple checks_sandbox change merge_source merge_parents hosted has_preview preview research hold publish")
99
100func TestChangePathSharedCases(t *testing.T) {
101 raw, err := os.ReadFile("testdata/change-paths.json")
102 if err != nil {
103 t.Fatal(err)
104 }
105 var cases []struct {
106 Path string
107 Valid bool
108 }
109 if err := json.Unmarshal(raw, &cases); err != nil {
110 t.Fatal(err)
111 }
112 for _, c := range cases {
113 if got := wire.ValidChangePath(c.Path); got != c.Valid {
114 t.Errorf("path %q: %v, want %v", c.Path, got, c.Valid)
115 }
116 }
117}
118