|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import type { NativeReviewDiffRow } from "./nativeReviewDiffSurface"; |
| 4 | +import type { NativeReviewDiffFile } from "./nativeReviewDiffTypes"; |
| 5 | +import { highlightNativeReviewDiffVisibleRows } from "./nativeReviewDiffHighlighter"; |
| 6 | + |
| 7 | +const TYPESCRIPT_FILE: NativeReviewDiffFile = { |
| 8 | + id: "file-1", |
| 9 | + path: "example.ts", |
| 10 | + language: "typescript", |
| 11 | + additions: 0, |
| 12 | + deletions: 0, |
| 13 | +}; |
| 14 | + |
| 15 | +function makeLine( |
| 16 | + input: Pick<NativeReviewDiffRow, "id" | "content" | "change" | "oldLineNumber" | "newLineNumber">, |
| 17 | +): NativeReviewDiffRow { |
| 18 | + return { |
| 19 | + kind: "line", |
| 20 | + fileId: TYPESCRIPT_FILE.id, |
| 21 | + ...input, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +function makeHunk(id: string): NativeReviewDiffRow { |
| 26 | + return { |
| 27 | + kind: "hunk", |
| 28 | + id, |
| 29 | + fileId: TYPESCRIPT_FILE.id, |
| 30 | + text: "@@", |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function highlight( |
| 35 | + rows: ReadonlyArray<NativeReviewDiffRow>, |
| 36 | + alreadyHighlightedRowIds?: ReadonlySet<string>, |
| 37 | +) { |
| 38 | + return highlightNativeReviewDiffVisibleRows({ |
| 39 | + rows, |
| 40 | + files: [TYPESCRIPT_FILE], |
| 41 | + scheme: "dark", |
| 42 | + engine: "javascript", |
| 43 | + firstRowIndex: 0, |
| 44 | + lastRowIndex: rows.length - 1, |
| 45 | + overscanRows: 0, |
| 46 | + maxRows: 100, |
| 47 | + alreadyHighlightedRowIds, |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +describe("highlightNativeReviewDiffVisibleRows", () => { |
| 52 | + it("does not carry grammar state across hunk boundaries", async () => { |
| 53 | + const exportRow = makeLine({ |
| 54 | + id: "export-row", |
| 55 | + content: "export async function run() {}", |
| 56 | + change: "add", |
| 57 | + oldLineNumber: null, |
| 58 | + newLineNumber: 100, |
| 59 | + }); |
| 60 | + const rows = [ |
| 61 | + makeHunk("hunk-1"), |
| 62 | + makeLine({ |
| 63 | + id: "import-open", |
| 64 | + content: "import {", |
| 65 | + change: "context", |
| 66 | + oldLineNumber: 1, |
| 67 | + newLineNumber: 1, |
| 68 | + }), |
| 69 | + makeLine({ |
| 70 | + id: "import-entry", |
| 71 | + content: " Model,", |
| 72 | + change: "context", |
| 73 | + oldLineNumber: 2, |
| 74 | + newLineNumber: 2, |
| 75 | + }), |
| 76 | + makeHunk("hunk-2"), |
| 77 | + exportRow, |
| 78 | + ]; |
| 79 | + |
| 80 | + const [highlighted, standalone] = await Promise.all([ |
| 81 | + highlight(rows), |
| 82 | + highlight([makeHunk("standalone-hunk"), exportRow]), |
| 83 | + ]); |
| 84 | + |
| 85 | + expect(highlighted.tokensByRowId[exportRow.id]).toEqual(standalone.tokensByRowId[exportRow.id]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("keeps grammar state across inline comment rows", async () => { |
| 89 | + const openingRow = makeLine({ |
| 90 | + id: "template-open", |
| 91 | + content: "const message = `open", |
| 92 | + change: "add", |
| 93 | + oldLineNumber: null, |
| 94 | + newLineNumber: 1, |
| 95 | + }); |
| 96 | + const closingRow = makeLine({ |
| 97 | + id: "template-close", |
| 98 | + content: "closed`;", |
| 99 | + change: "add", |
| 100 | + oldLineNumber: null, |
| 101 | + newLineNumber: 2, |
| 102 | + }); |
| 103 | + const trailingRow = makeLine({ |
| 104 | + id: "trailing-row", |
| 105 | + content: "export const answer = 42;", |
| 106 | + change: "add", |
| 107 | + oldLineNumber: null, |
| 108 | + newLineNumber: 3, |
| 109 | + }); |
| 110 | + const commentRow: NativeReviewDiffRow = { |
| 111 | + kind: "comment", |
| 112 | + id: "comment-1", |
| 113 | + fileId: TYPESCRIPT_FILE.id, |
| 114 | + commentText: "Review note", |
| 115 | + }; |
| 116 | + |
| 117 | + const [withComment, contiguous] = await Promise.all([ |
| 118 | + highlight([openingRow, commentRow, closingRow, trailingRow]), |
| 119 | + highlight([openingRow, closingRow, trailingRow]), |
| 120 | + ]); |
| 121 | + |
| 122 | + expect(withComment.tokensByRowId).toEqual(contiguous.tokensByRowId); |
| 123 | + }); |
| 124 | + |
| 125 | + it("does not join unhighlighted rows across cached gaps", async () => { |
| 126 | + const trailingRow = makeLine({ |
| 127 | + id: "trailing-row", |
| 128 | + content: "export const answer = 42;", |
| 129 | + change: "add", |
| 130 | + oldLineNumber: null, |
| 131 | + newLineNumber: 3, |
| 132 | + }); |
| 133 | + const rows = [ |
| 134 | + makeLine({ |
| 135 | + id: "template-open", |
| 136 | + content: "const message = `open", |
| 137 | + change: "add", |
| 138 | + oldLineNumber: null, |
| 139 | + newLineNumber: 1, |
| 140 | + }), |
| 141 | + makeLine({ |
| 142 | + id: "template-close", |
| 143 | + content: "closed`;", |
| 144 | + change: "add", |
| 145 | + oldLineNumber: null, |
| 146 | + newLineNumber: 2, |
| 147 | + }), |
| 148 | + trailingRow, |
| 149 | + ]; |
| 150 | + |
| 151 | + const [highlighted, standalone] = await Promise.all([ |
| 152 | + highlight(rows, new Set(["template-close"])), |
| 153 | + highlight([trailingRow]), |
| 154 | + ]); |
| 155 | + |
| 156 | + expect(highlighted.tokensByRowId[trailingRow.id]).toEqual( |
| 157 | + standalone.tokensByRowId[trailingRow.id], |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it("keeps deletion grammar state out of addition rows", async () => { |
| 162 | + const additionRow = makeLine({ |
| 163 | + id: "addition-row", |
| 164 | + content: "export const answer = 42;", |
| 165 | + change: "add", |
| 166 | + oldLineNumber: null, |
| 167 | + newLineNumber: 1, |
| 168 | + }); |
| 169 | + const rows = [ |
| 170 | + makeLine({ |
| 171 | + id: "deletion-row", |
| 172 | + content: "const removed = `open", |
| 173 | + change: "delete", |
| 174 | + oldLineNumber: 1, |
| 175 | + newLineNumber: null, |
| 176 | + }), |
| 177 | + additionRow, |
| 178 | + ]; |
| 179 | + |
| 180 | + const [highlighted, standalone] = await Promise.all([ |
| 181 | + highlight(rows), |
| 182 | + highlight([additionRow]), |
| 183 | + ]); |
| 184 | + |
| 185 | + expect(highlighted.tokensByRowId[additionRow.id]).toEqual( |
| 186 | + standalone.tokensByRowId[additionRow.id], |
| 187 | + ); |
| 188 | + }); |
| 189 | +}); |
0 commit comments