| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | id1 = "0123456789abcdef0123456789abcdef" |
| 12 | id2 = "fedcba9876543210fedcba9876543210" |
| 13 | sha1a = "git-sha1:1111111111111111111111111111111111111111" |
| 14 | sha1b = "git-sha1:2222222222222222222222222222222222222222" |
| 15 | sha1c = "git-sha1:3333333333333333333333333333333333333333" |
| 16 | digest = "abcdef4444444444444444444444444444444444444444444444444444444444" |
| 17 | ) |
| 18 | |
| 19 | // text is a well-formed review that judges a version, as text, so each |
| 20 | // case below can change one thing about it. |
| 21 | func text() string { |
| 22 | return `{ |
| 23 | "schema": "the-valley/review/v1", |
| 24 | "review_id": "` + id1 + `", |
| 25 | "change_id": "` + changeID + `", |
| 26 | "target": "main", |
| 27 | "review": "docs-style", |
| 28 | "reviewer": "klaus-env", |
| 29 | "verdict": "request-changes", |
| 30 | "withdraws": null, |
| 31 | "binding": { |
| 32 | "record": "` + sha1a + `", |
| 33 | "version": 2, |
| 34 | "head": "` + sha1b + `", |
| 35 | "target_tip": "` + sha1c + `", |
| 36 | "base": "` + sha1a + `", |
| 37 | "delta": "` + digest + `" |
| 38 | }, |
| 39 | "summary": "Two sentences need splitting.\nSee the notes.", |
| 40 | "notes": [ |
| 41 | {"side": "after", "path": "docs/guide.md", "blob": "` + sha1b + `", "lines": {"start": 3, "end": 4}, "body": "Split this sentence."}, |
| 42 | {"side": "before", "path": "docs/old file.md", "blob": "` + sha1c + `", "lines": null, "body": "Why was this removed?"} |
| 43 | ], |
| 44 | "provenance": { |
| 45 | "harness": "klaus 0.9.1", |
| 46 | "model": "claude-opus-5-5", |
| 47 | "settings": "` + digest + `", |
| 48 | "inputs": "` + digest + `", |
| 49 | "input_tokens": 1200, |
| 50 | "output_tokens": null |
| 51 | } |
| 52 | } |
| 53 | ` |
| 54 | } |
| 55 | |
| 56 | // withdrawalText is a well-formed withdrawal. |
| 57 | func withdrawalText() string { |
| 58 | return `{"schema": "the-valley/review/v1", "review_id": "` + id2 + `", "change_id": "` + changeID + `", |
| 59 | "target": "main", "review": "docs-style", "reviewer": "klaus-env", "verdict": "withdraw", |
| 60 | "withdraws": "` + id1 + `", "binding": null, "summary": "Judged in error.", "notes": [], "provenance": null}` |
| 61 | } |
| 62 | |
| 63 | func TestAWellFormedReviewReadsWhole(t *testing.T) { |
| 64 | r, err := Parse([]byte(text())) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | tokens := int64(1200) |
| 69 | want := &Record{ |
| 70 | Schema: Schema, ReviewID: id1, ChangeID: changeID, Target: "main", Review: "docs-style", |
| 71 | Reviewer: "klaus-env", Verdict: RequestChanges, |
| 72 | Binding: &Binding{Record: sha1a, Version: 2, Head: sha1b, TargetTip: sha1c, Base: sha1a, Delta: digest}, |
| 73 | Summary: "Two sentences need splitting.\nSee the notes.", |
| 74 | Notes: []Note{ |
| 75 | {Side: After, Path: "docs/guide.md", Blob: sha1b, Lines: &Lines{Start: 3, End: 4}, Body: "Split this sentence."}, |
| 76 | {Side: Before, Path: "docs/old file.md", Blob: sha1c, Body: "Why was this removed?"}, |
| 77 | }, |
| 78 | Provenance: &Provenance{Harness: "klaus 0.9.1", Model: "claude-opus-5-5", Settings: digest, Inputs: digest, InputTokens: &tokens}, |
| 79 | } |
| 80 | if !reflect.DeepEqual(r, want) { |
| 81 | t.Fatalf("Parse = %+v, want %+v", r, want) |
| 82 | } |
| 83 | for _, data := range []string{text(), withdrawalText()} { |
| 84 | r, err := Parse([]byte(data)) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | encoded, err := Encode(r) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | again, err := Parse(encoded) |
| 93 | if err != nil || !reflect.DeepEqual(again, r) { |
| 94 | t.Fatalf("an encoded review read back as %+v, %v", again, err) |
| 95 | } |
| 96 | } |
| 97 | w, err := Parse([]byte(withdrawalText())) |
| 98 | if err != nil || w.Judges() || *w.Withdraws != id1 { |
| 99 | t.Fatalf("the withdrawal reads as %+v, %v", w, err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Each case changes one thing about a well-formed review, and the parser |
| 104 | // refuses it, saying what is wrong. |
| 105 | func TestMalformedReviewsAreRefused(t *testing.T) { |
| 106 | cases := []struct { |
| 107 | name, old, new, says string |
| 108 | withdrawal bool |
| 109 | }{ |
| 110 | {"a key given twice", `"verdict": "request-changes",`, `"verdict": "request-changes", "verdict": "approve",`, `names the key "verdict" twice`, false}, |
| 111 | {"a key given twice in the binding", `"version": 2,`, `"version": 2, "version": 3,`, `names the key "version" twice`, false}, |
| 112 | {"a field the schema does not have", `"target": "main",`, `"target": "main", "instance": "qinling",`, `the field "instance"`, false}, |
| 113 | {"a field in another case", `"verdict": "request-changes",`, `"Verdict": "request-changes",`, `the field "Verdict"`, false}, |
| 114 | {"a field missing", `"review": "docs-style",`, ``, `no field "review"`, false}, |
| 115 | {"a field in a note the schema does not have", `"body": "Split this sentence."`, `"body": "Split this sentence.", "line": 3`, `notes[0] holds the field "line"`, false}, |
| 116 | {"a field in the provenance the schema does not have", `"output_tokens": null`, `"output_tokens": null, "cost": 3`, `the field "cost"`, false}, |
| 117 | {"a version as a string", `"version": 2,`, `"version": "2",`, "binding.version is a string", false}, |
| 118 | {"a version as a fraction", `"version": 2,`, `"version": 2.0,`, "binding.version is 2.0", false}, |
| 119 | {"a version with an exponent", `"version": 2,`, `"version": 2e0,`, "binding.version is 2e0", false}, |
| 120 | {"a version of 0", `"version": 2,`, `"version": 0,`, "outside 1 to 1000", false}, |
| 121 | {"a verdict of null", `"verdict": "request-changes",`, `"verdict": null,`, "verdict is a literal", false}, |
| 122 | {"a verdict the schema does not have", `"verdict": "request-changes",`, `"verdict": "reject",`, `verdict is "reject"`, false}, |
| 123 | {"an approval that withdraws", `"withdraws": null,`, `"withdraws": "` + id2 + `",`, "only a withdrawal names a review", false}, |
| 124 | {"a judgment with no binding", `"binding": {`, `"binding": null, "x": {`, `the field "x"`, false}, |
| 125 | {"a review ID in capitals", `"review_id": "` + id1 + `"`, `"review_id": "` + strings.ToUpper(id1) + `"`, "is not a review ID", false}, |
| 126 | {"a review ID too short", `"review_id": "` + id1 + `"`, `"review_id": "` + id1[:31] + `"`, "is not a review ID", false}, |
| 127 | {"a change ID that is not one", `"change_id": "` + changeID + `"`, `"change_id": "abc"`, "is not a change ID", false}, |
| 128 | {"a target that is no branch", `"target": "main"`, `"target": "ma..in"`, "is not a branch name", false}, |
| 129 | {"a review name in capitals", `"review": "docs-style"`, `"review": "Docs-style"`, "is not a review's name", false}, |
| 130 | {"a review name too long", `"review": "docs-style"`, `"review": "` + strings.Repeat("d", 65) + `"`, "is not a review's name", false}, |
| 131 | {"a reviewer that is no principal", `"reviewer": "klaus-env"`, `"reviewer": "-klaus"`, "is not a principal name", false}, |
| 132 | {"a record named by another hash", `"record": "` + sha1a + `"`, `"record": "git-sha256:` + digest + `"`, "binding.record", false}, |
| 133 | {"a delta in capitals", `"delta": "` + digest + `"`, `"delta": "` + strings.ToUpper(digest) + `"`, "binding.delta", false}, |
| 134 | {"a side the schema does not have", `"side": "after"`, `"side": "middle"`, `side is "middle"`, false}, |
| 135 | {"a path that climbs", `"path": "docs/guide.md"`, `"path": "../guide.md"`, "has the component ..", false}, |
| 136 | {"a path into .git", `"path": "docs/guide.md"`, `"path": ".Git/config"`, "git keeps for itself", false}, |
| 137 | {"a path with an empty component", `"path": "docs/guide.md"`, `"path": "docs//guide.md"`, "an empty component", false}, |
| 138 | {"a path holding a newline", `"path": "docs/guide.md"`, `"path": "docs/guide\n.md"`, "holds a control character", false}, |
| 139 | {"a blob that is no object", `"blob": "` + sha1b + `"`, `"blob": "docs/guide.md"`, "notes[0].blob", false}, |
| 140 | {"lines that run backwards", `"start": 3, "end": 4`, `"start": 4, "end": 3`, "run from 4 to 3", false}, |
| 141 | {"a line of 0", `"start": 3, "end": 4`, `"start": 0, "end": 4`, "outside 1 to", false}, |
| 142 | {"a line past the last a note may name", `"start": 3, "end": 4`, fmt.Sprintf(`"start": 3, "end": %d`, MaxLine+1), "outside 1 to", false}, |
| 143 | {"an empty summary", `"summary": "Two sentences need splitting.\nSee the notes."`, `"summary": ""`, "summary is not 1 to", false}, |
| 144 | {"a summary with a bell", `"summary": "Two sentences`, `"summary": "\u0007Two sentences`, "summary is not 1 to", false}, |
| 145 | {"an empty note", `"body": "Split this sentence."`, `"body": ""`, "notes[0].body", false}, |
| 146 | {"settings that are no digest", `"settings": "` + digest + `"`, `"settings": "sha256"`, "provenance.settings", false}, |
| 147 | {"negative tokens", `"input_tokens": 1200`, `"input_tokens": -1`, "input_tokens is -1", false}, |
| 148 | {"more tokens than any run", `"input_tokens": 1200`, `"input_tokens": 1000000000001`, "outside 0 to", false}, |
| 149 | {"a model holding a newline", `"model": "claude-opus-5-5"`, `"model": "claude\nopus"`, "provenance.model", false}, |
| 150 | {"a value after the review", "\n}\n", "\n}\n{}", "more after the value", false}, |
| 151 | {"a lone surrogate", `"Split this sentence."`, `"Split \ud800 this sentence."`, "surrogate", false}, |
| 152 | {"a withdrawal of nothing", `"withdraws": "` + id1 + `"`, `"withdraws": null`, "withdraws is not a review ID", true}, |
| 153 | {"a withdrawal of itself", `"withdraws": "` + id1 + `"`, `"withdraws": "` + id2 + `"`, "withdrawal of itself", true}, |
| 154 | {"a withdrawal with a binding", `"binding": null`, `"binding": {"record": "` + sha1a + `", "version": 1, "head": "` + sha1a + `", "target_tip": "` + sha1a + `", "base": "` + sha1a + `", "delta": "` + digest + `"}`, "has no binding", true}, |
| 155 | {"a withdrawal with notes", `"notes": []`, `"notes": [{"side": "after", "path": "a", "blob": "` + sha1a + `", "lines": null, "body": "b"}]`, "has no notes", true}, |
| 156 | } |
| 157 | for _, c := range cases { |
| 158 | t.Run(c.name, func(t *testing.T) { |
| 159 | base := text() |
| 160 | if c.withdrawal { |
| 161 | base = withdrawalText() |
| 162 | } |
| 163 | if !strings.Contains(base, c.old) { |
| 164 | t.Fatalf("the review does not hold %q", c.old) |
| 165 | } |
| 166 | _, err := Parse([]byte(strings.Replace(base, c.old, c.new, 1))) |
| 167 | if err == nil { |
| 168 | t.Fatal("it parsed") |
| 169 | } |
| 170 | if !strings.Contains(err.Error(), c.says) { |
| 171 | t.Fatalf("the refusal does not say %q: %v", c.says, err) |
| 172 | } |
| 173 | }) |
| 174 | } |
| 175 | if _, err := Parse(append([]byte(text()[:20]), 0xff)); err == nil || !strings.Contains(err.Error(), "UTF-8") { |
| 176 | t.Fatalf("bytes that are not UTF-8: %v", err) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // A review is at most MaxRecord bytes, read or written, and its lists and |
| 181 | // strings are held to their own bounds. |
| 182 | func TestAReviewIsBounded(t *testing.T) { |
| 183 | r, err := Parse([]byte(text())) |
| 184 | if err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | padded := text() + strings.Repeat(" ", MaxRecord) |
| 188 | if _, err := Parse([]byte(padded)); err == nil || !strings.Contains(err.Error(), "more than the 65536") { |
| 189 | t.Fatalf("a review of more than 64 KiB: %v", err) |
| 190 | } |
| 191 | |
| 192 | note := r.Notes[0] |
| 193 | note.Body = strings.Repeat("n", MaxNoteBody) |
| 194 | r.Notes = []Note{note} |
| 195 | if _, err := Encode(r); err != nil { |
| 196 | t.Fatalf("a note of %d bytes: %v", MaxNoteBody, err) |
| 197 | } |
| 198 | r.Notes[0].Body += "n" |
| 199 | if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "notes[0].body") { |
| 200 | t.Fatalf("a note of %d bytes: %v", MaxNoteBody+1, err) |
| 201 | } |
| 202 | |
| 203 | note.Body = "short" |
| 204 | r.Notes = nil |
| 205 | for range MaxNotes { |
| 206 | r.Notes = append(r.Notes, note) |
| 207 | } |
| 208 | data, err := Encode(r) |
| 209 | if err != nil { |
| 210 | t.Fatalf("%d notes: %v", MaxNotes, err) |
| 211 | } |
| 212 | if _, err := Parse(data); err != nil { |
| 213 | t.Fatalf("%d notes read back: %v", MaxNotes, err) |
| 214 | } |
| 215 | r.Notes = append(r.Notes, note) |
| 216 | if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "more than the 256") { |
| 217 | t.Fatalf("%d notes: %v", MaxNotes+1, err) |
| 218 | } |
| 219 | refused := strings.Replace(string(data), `"notes": [`, `"notes": [`+strings.Repeat(`{"side": "after", "path": "a", "blob": "`+sha1a+`", "lines": null, "body": "b"},`, 1), 1) |
| 220 | if _, err := Parse([]byte(refused)); err == nil || !strings.Contains(err.Error(), "257 notes") { |
| 221 | t.Fatalf("%d notes read: %v", MaxNotes+1, err) |
| 222 | } |
| 223 | |
| 224 | // Each note within its bounds, and the whole past 64 KiB. |
| 225 | note.Body = strings.Repeat("n", 4096) |
| 226 | r.Notes = nil |
| 227 | for range 20 { |
| 228 | r.Notes = append(r.Notes, note) |
| 229 | } |
| 230 | if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "more than the 65536") { |
| 231 | t.Fatalf("a review of 20 notes of 4 KiB: %v", err) |
| 232 | } |
| 233 | |
| 234 | r.Notes = nil |
| 235 | r.Summary = strings.Repeat("s", MaxSummary+1) |
| 236 | if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "summary") { |
| 237 | t.Fatalf("a summary of %d bytes: %v", MaxSummary+1, err) |
| 238 | } |
| 239 | r.Summary = "s" |
| 240 | r.Notes = []Note{{Side: After, Path: strings.Repeat("p/", MaxPath/2+1) + "x", Blob: sha1a, Body: "b"}} |
| 241 | if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "is not 1 to 4095 bytes") { |
| 242 | t.Fatalf("a path of %d bytes: %v", len(r.Notes[0].Path), err) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func TestReviewIDsAndRefs(t *testing.T) { |
| 247 | a, err := NewID() |
| 248 | if err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | b, _ := NewID() |
| 252 | if !ValidID(a) || !ValidID(b) || a == b { |
| 253 | t.Fatalf("NewID gave %q and %q", a, b) |
| 254 | } |
| 255 | ref := Ref("main", changeID) |
| 256 | if ref != "refs/the-valley/reviews/main/"+changeID { |
| 257 | t.Fatalf("Ref = %s", ref) |
| 258 | } |
| 259 | if target, id, ok := ParseRef(ref); !ok || target != "main" || id != changeID { |
| 260 | t.Fatalf("ParseRef(%s) = %s, %s, %v", ref, target, id, ok) |
| 261 | } |
| 262 | if target, _, ok := ParseRef(Ref("release/1.0", changeID)); !ok || target != "release/1.0" { |
| 263 | t.Fatalf("a target with a slash: %s, %v", target, ok) |
| 264 | } |
| 265 | for _, bad := range []string{ |
| 266 | "refs/the-valley/changes/main/" + changeID, |
| 267 | RefPrefix + changeID, |
| 268 | RefPrefix + "main/" + id1, |
| 269 | RefPrefix + "ma..in/" + changeID, |
| 270 | } { |
| 271 | if _, _, ok := ParseRef(bad); ok { |
| 272 | t.Errorf("ParseRef took %s", bad) |
| 273 | } |
| 274 | } |
| 275 | } |