package wire_test import ( "encoding/json" "fmt" "os" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) func TestProtocol31ChangeFiles(t *testing.T) { if wire.ProtocolChangeFiles != 31 || wire.ProtocolCurrent != 38 { t.Fatal("protocol not 31") } 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}}} report := wire.StepReport{StepID: "s", Attempt: 1, State: wire.StepDone, ChangeFiles: facts} f, err := wire.NewFrame("r", wire.FrameStep, report) if err != nil { t.Fatal(err) } for p := 1; p <= 31; p++ { got, err := wire.Encode(f, p) if err != nil { t.Fatalf("%d: %v", p, err) } var fields map[string]json.RawMessage if err := json.Unmarshal(got.Data, &fields); err != nil { t.Fatal(err) } if (fields["change_files"] != nil) != (p >= 31) { t.Fatalf("field at %d: %s", p, got.Data) } if p < 31 { frozen13(t, "pre-31 StepReport", got.Data, frozen30StepReport) } } if report.ChangeFiles == nil { t.Fatal("Encode mutated input") } } func TestChangeFilesValidation(t *testing.T) { valid := func() *wire.ChangeFiles { 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}}} } if !wire.ValidChangeFiles(valid()) { t.Fatal("valid paths refused") } for name, mutate := range map[string]func(*wire.ChangeFiles){ "nil-list": func(f *wire.ChangeFiles) { f.Files = 0; f.Additions = 0; f.Entries = nil }, "base": func(f *wire.ChangeFiles) { f.Base = "main" }, "sha": func(f *wire.ChangeFiles) { f.SHA = strings.Repeat("z", 40) }, "negative": func(f *wire.ChangeFiles) { f.Additions = -1 }, "totals": func(f *wire.ChangeFiles) { f.Additions = 2 }, "truncation": func(f *wire.ChangeFiles) { f.Files = 2 }, "status": func(f *wire.ChangeFiles) { f.Entries[0].Status = "copy" }, "old": func(f *wire.ChangeFiles) { f.Entries[0].OldPath = "old" }, "rename": func(f *wire.ChangeFiles) { f.Entries[0].Status = "renamed" }, "binary": func(f *wire.ChangeFiles) { f.Entries[0].Binary = true; f.Binary = 1 }, "short-base": func(f *wire.ChangeFiles) { f.Base = strings.Repeat("a", 39) }, "short-sha": func(f *wire.ChangeFiles) { f.SHA = strings.Repeat("b", 39) }, "excess-binaries": func(f *wire.ChangeFiles) { f.Entries = []wire.ChangedFile{}; f.Truncated = true; f.Binary = 2 }, "invalid-utf8": func(f *wire.ChangeFiles) { f.Entries[0].Path = string([]byte{255}) }, "path": func(f *wire.ChangeFiles) { f.Entries[0].Path = "../evil" }, "control": func(f *wire.ChangeFiles) { f.Entries[0].Path = "a\nb" }, "byte-bound": func(f *wire.ChangeFiles) { f.Files = 200 f.Additions = 200 f.Entries = []wire.ChangedFile{} for i := 0; i < 200; i++ { f.Entries = append(f.Entries, wire.ChangedFile{Path: fmt.Sprintf("%d/%s", i, strings.Repeat("x", 2000)), Status: "added", Additions: 1}) } }, "duplicate": func(f *wire.ChangeFiles) { f.Files = 2; f.Additions = 2; f.Entries = append(f.Entries, f.Entries[0]) }, "bound": func(f *wire.ChangeFiles) { f.Files = 201 f.Additions = 201 f.Entries = []wire.ChangedFile{} for i := 0; i < 201; i++ { f.Entries = append(f.Entries, wire.ChangedFile{Path: fmt.Sprintf("file-%d", i), Status: "added", Additions: 1}) } }, } { t.Run(name, func(t *testing.T) { f := valid() mutate(f) if wire.ValidChangeFiles(f) { t.Fatal("invalid accepted") } }) } } // Frozen from main 18fb5be4, before protocol 31. var 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") func TestChangePathSharedCases(t *testing.T) { raw, err := os.ReadFile("testdata/change-paths.json") if err != nil { t.Fatal(err) } var cases []struct { Path string Valid bool } if err := json.Unmarshal(raw, &cases); err != nil { t.Fatal(err) } for _, c := range cases { if got := wire.ValidChangePath(c.Path); got != c.Valid { t.Errorf("path %q: %v, want %v", c.Path, got, c.Valid) } } }