| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestProtocol35ChangeDiffs(t *testing.T) { |
| 14 | if wire.ProtocolChangeDiffs != 35 || wire.ProtocolCurrent != 38 { |
| 15 | t.Fatal("protocol") |
| 16 | } |
| 17 | f, err := wire.NewFrame("diff", wire.FrameChangeDiff, wire.ChangeDiffUpload{}) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | for p := 1; p < 35; p++ { |
| 22 | if _, err := wire.Encode(f, p); !errors.Is(err, wire.ErrProtocol) { |
| 23 | t.Fatalf("upload at %d", p) |
| 24 | } |
| 25 | } |
| 26 | for p := 35; p <= wire.ProtocolCurrent; p++ { |
| 27 | if _, err := wire.Encode(f, p); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | report := wire.StepReport{ChangeDiffs: "collection"} |
| 32 | f, _ = wire.NewFrame("done", wire.FrameStep, report) |
| 33 | for p := 1; p <= wire.ProtocolCurrent; p++ { |
| 34 | got, err := wire.Encode(f, p) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | var fields map[string]json.RawMessage |
| 39 | _ = json.Unmarshal(got.Data, &fields) |
| 40 | if (fields["change_diffs"] != nil) != (p >= 35) { |
| 41 | t.Fatalf("report at %d", p) |
| 42 | } |
| 43 | } |
| 44 | if report.ChangeDiffs != "collection" { |
| 45 | t.Fatal("mutated caller") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestDisplayPatchBounds(t *testing.T) { |
| 50 | good := wire.FileDiff{Path: "space 日本語.txt", Patch: "@@ -1 +1 @@\n-old\n+new\n"} |
| 51 | if !wire.ValidFileDiff(good) { |
| 52 | t.Fatal("valid") |
| 53 | } |
| 54 | for name, mutate := range map[string]func(*wire.FileDiff){ |
| 55 | "partial-line": func(d *wire.FileDiff) { d.Patch = "+partial" }, |
| 56 | "path": func(d *wire.FileDiff) { d.Path = "../x" }, |
| 57 | "bytes": func(d *wire.FileDiff) { d.Patch = strings.Repeat("x", wire.MaxFileDiffBytes) + "\n" }, |
| 58 | "lines": func(d *wire.FileDiff) { d.Patch = strings.Repeat("+\n", wire.MaxFileDiffLines+1) }, |
| 59 | "utf8": func(d *wire.FileDiff) { d.Patch = string([]byte{255}) + "\n" }, |
| 60 | "nul": func(d *wire.FileDiff) { d.Patch = "+\x00\n" }, |
| 61 | "reason": func(d *wire.FileDiff) { d.Reason = "unknown" }, |
| 62 | "withheld-bytes": func(d *wire.FileDiff) { d.Reason = wire.DiffWithheld }, |
| 63 | "withheld-truncated": func(d *wire.FileDiff) { d.Patch = ""; d.Reason = wire.DiffWithheld; d.Truncated = true }, |
| 64 | } { |
| 65 | t.Run(name, func(t *testing.T) { |
| 66 | d := good |
| 67 | mutate(&d) |
| 68 | if wire.ValidFileDiff(d) { |
| 69 | t.Fatal("accepted") |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | for _, reason := range []string{wire.DiffBinary, wire.DiffTooLarge, wire.DiffWithheld, wire.DiffUnavailable, wire.DiffExpired} { |
| 74 | if !wire.ValidFileDiff(wire.FileDiff{Path: "x", Reason: reason}) { |
| 75 | t.Fatal(reason) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestDisplayBoundsContract(t *testing.T) { |
| 81 | if wire.MaxChangeEntries != 200 || wire.MaxDiffBlobBytes != 1<<20 || wire.MaxDiffInputBytes != 8<<20 || wire.MaxFileDiffBytes != 64<<10 || wire.MaxFileDiffLines != 2000 || wire.MaxCandidateDiffBytes != 2<<20 || wire.MaxDiffResponseBytes != 128<<10 { |
| 82 | t.Fatal("approved display limits changed") |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Unicode 17 Cf and Default_Ignorable_Code_Point ranges are shared with native. |
| 87 | func TestPatchHiddenCharacters(t *testing.T) { |
| 88 | data, err := os.ReadFile("testdata/hidden-codepoints.json") |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | var cases []struct { |
| 93 | First, Last, Kind, Prefix, Suffix string |
| 94 | Hidden bool |
| 95 | } |
| 96 | if err = json.Unmarshal(data, &cases); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | for _, c := range cases { |
| 100 | first, err := strconv.ParseInt(c.First, 16, 32) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | last, err := strconv.ParseInt(c.Last, 16, 32) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | for r := rune(first); r <= rune(last); r++ { |
| 109 | if got := wire.PatchHasHiddenCharacters("+safe" + c.Prefix + string(r) + c.Suffix + "code\n"); got != c.Hidden { |
| 110 | t.Errorf("%s U+%04X: hidden = %v, want %v", c.Kind, r, got, c.Hidden) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // StepReport keys at 32, 33 (Assistant files) and 34 (places) are identical: |
| 117 | // those protocols changed other frames, not this report. Diff references must |
| 118 | // reach none of them. |
| 119 | var frozen32StepReport = 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 change_files") |
| 120 | |
| 121 | func TestDiffDowngradeFrozen32To34(t *testing.T) { |
| 122 | for _, protocol := range []int{32, 33, 34} { |
| 123 | report := wire.StepReport{TaskID: "task", StepID: "step", State: wire.StepDone, Reason: "step_timeout", CandidateSHA: strings.Repeat("a", 40), Pushed: true, ChangeDiffs: "collection"} |
| 124 | frame, _ := wire.NewFrame("report", wire.FrameStep, report) |
| 125 | encoded, err := wire.Encode(frame, protocol) |
| 126 | if err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | frozen13(t, "32-34 report", encoded.Data, frozen32StepReport) |
| 130 | var decoded wire.StepReport |
| 131 | if err = wire.Strict(encoded.Data, &decoded); err != nil || decoded.Reason != "step_timeout" || decoded.State != wire.StepDone || !decoded.Pushed || decoded.ChangeDiffs != "" { |
| 132 | t.Fatalf("downgrade %d: %+v %v", protocol, decoded, err) |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |