| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { applyDiffRangesToTokens, computeWordAltDiffRanges } from "./reviewWordDiffs"; |
| 4 | |
| 5 | describe("computeWordAltDiffRanges", () => { |
| 6 | it("joins adjacent word replacements across a single shared separator", () => { |
| 7 | const ranges = computeWordAltDiffRanges({ |
| 8 | deletionLine: "old old", |
| 9 | additionLine: "new new", |
| 10 | }); |
| 11 | |
| 12 | expect(ranges.deletion).toEqual([{ start: 0, end: "old old".length }]); |
| 13 | expect(ranges.addition).toEqual([{ start: 0, end: "new new".length }]); |
| 14 | }); |
| 15 | |
| 16 | it("skips inline word diffs for long lines", () => { |
| 17 | const longDeletion = `const before = "${"a".repeat(1_001)}";`; |
| 18 | const longAddition = `const after = "${"b".repeat(1_001)}";`; |
| 19 | |
| 20 | const ranges = computeWordAltDiffRanges({ |
| 21 | deletionLine: longDeletion, |
| 22 | additionLine: longAddition, |
| 23 | }); |
| 24 | |
| 25 | expect(ranges.deletion).toEqual([]); |
| 26 | expect(ranges.addition).toEqual([]); |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | describe("applyDiffRangesToTokens", () => { |
| 31 | it("splits highlighted fragments out of syntax tokens", () => { |
| 32 | const tokens = [ |
| 33 | { |
| 34 | content: "const before = 1;", |
| 35 | color: "#fff", |
| 36 | fontStyle: null, |
| 37 | }, |
| 38 | ]; |
| 39 | const nextTokens = applyDiffRangesToTokens(tokens, [{ start: 6, end: 12 }]); |
| 40 | |
| 41 | expect(nextTokens).toEqual([ |
| 42 | { |
| 43 | content: "const ", |
| 44 | color: "#fff", |
| 45 | fontStyle: null, |
| 46 | diffHighlight: false, |
| 47 | }, |
| 48 | { |
| 49 | content: "before", |
| 50 | color: "#fff", |
| 51 | fontStyle: null, |
| 52 | diffHighlight: true, |
| 53 | }, |
| 54 | { |
| 55 | content: " = 1;", |
| 56 | color: "#fff", |
| 57 | fontStyle: null, |
| 58 | diffHighlight: false, |
| 59 | }, |
| 60 | ]); |
| 61 | }); |
| 62 | |
| 63 | it("skips exhausted ranges when later tokens start after the last range", () => { |
| 64 | const tokens = [ |
| 65 | { |
| 66 | content: "a", |
| 67 | color: "#fff", |
| 68 | fontStyle: null, |
| 69 | }, |
| 70 | { |
| 71 | content: "b", |
| 72 | color: "#fff", |
| 73 | fontStyle: null, |
| 74 | }, |
| 75 | ]; |
| 76 | |
| 77 | const nextTokens = applyDiffRangesToTokens(tokens, [{ start: 0, end: 1 }]); |
| 78 | |
| 79 | expect(nextTokens).toEqual([ |
| 80 | { |
| 81 | content: "a", |
| 82 | color: "#fff", |
| 83 | fontStyle: null, |
| 84 | diffHighlight: true, |
| 85 | }, |
| 86 | { |
| 87 | content: "b", |
| 88 | color: "#fff", |
| 89 | fontStyle: null, |
| 90 | }, |
| 91 | ]); |
| 92 | }); |
| 93 | }); |