|
| 1 | +import type { CodeViewItem } from "@pierre/diffs"; |
| 2 | +import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles"; |
| 3 | +import { describe, expect, it } from "vite-plus/test"; |
| 4 | + |
| 5 | +// Find-in-diff lives in our @pierre/diffs patch and is not a package export. |
| 6 | +const searchUrl = new URL("./components/CodeViewSearch.js", import.meta.resolve("@pierre/diffs")); |
| 7 | +const { collectMatches } = (await import(/* @vite-ignore */ searchUrl.href)) as { |
| 8 | + collectMatches( |
| 9 | + items: ReadonlyArray<CodeViewItem<undefined>>, |
| 10 | + params: { text: string; caseSensitive: boolean; wholeWord: boolean; regex: boolean }, |
| 11 | + ): Array<{ id: string; side: string; lineNumber: number; start: number; end: number }>; |
| 12 | +}; |
| 13 | + |
| 14 | +const PATCH = `diff --git a/src/a.ts b/src/a.ts |
| 15 | +index 1111111..2222222 100644 |
| 16 | +--- a/src/a.ts |
| 17 | ++++ b/src/a.ts |
| 18 | +@@ -1,4 +1,5 @@ |
| 19 | + const keep = 1; |
| 20 | +-const oldName = 2; |
| 21 | ++const newName = 2; |
| 22 | ++const added = oldName; |
| 23 | + const tail = 4; |
| 24 | +@@ -20,3 +21,3 @@ function x() { |
| 25 | + ctx20 name |
| 26 | +-del21 Name |
| 27 | ++add22 names |
| 28 | + ctx22 |
| 29 | +diff --git a/src/b.ts b/src/b.ts |
| 30 | +index 3333333..4444444 100644 |
| 31 | +--- a/src/b.ts |
| 32 | ++++ b/src/b.ts |
| 33 | +@@ -1,1 +1,1 @@ |
| 34 | +-nil |
| 35 | ++: nil |
| 36 | +`; |
| 37 | + |
| 38 | +const items: CodeViewItem<undefined>[] = parsePatchFiles(PATCH)[0]!.files.map( |
| 39 | + (fileDiff, index) => ({ id: `file-${index}`, type: "diff", fileDiff, collapsed: index === 1 }), |
| 40 | +); |
| 41 | + |
| 42 | +function find( |
| 43 | + text: string, |
| 44 | + flags: { caseSensitive?: boolean; wholeWord?: boolean; regex?: boolean } = {}, |
| 45 | +) { |
| 46 | + return collectMatches(items, { |
| 47 | + text, |
| 48 | + caseSensitive: flags.caseSensitive ?? false, |
| 49 | + wholeWord: flags.wholeWord ?? false, |
| 50 | + regex: flags.regex ?? false, |
| 51 | + }).map(({ id, side, lineNumber, start, end }) => `${id}:${side}:${lineNumber}:${start}-${end}`); |
| 52 | +} |
| 53 | + |
| 54 | +describe("collectMatches", () => { |
| 55 | + it("finds removed, added, and context lines in render order, folded files included", () => { |
| 56 | + expect(find("oldName")).toEqual(["file-0:deletions:2:6-13", "file-0:additions:3:14-21"]); |
| 57 | + expect(find("nil")).toEqual(["file-1:deletions:1:0-3", "file-1:additions:1:2-5"]); |
| 58 | + }); |
| 59 | + |
| 60 | + it("honours case, whole-word, and regex toggles", () => { |
| 61 | + expect(find("Name", { caseSensitive: true })).toEqual([ |
| 62 | + "file-0:deletions:2:9-13", |
| 63 | + "file-0:additions:2:9-13", |
| 64 | + "file-0:additions:3:17-21", |
| 65 | + "file-0:deletions:21:6-10", |
| 66 | + ]); |
| 67 | + expect(find("name", { wholeWord: true })).toEqual([ |
| 68 | + "file-0:additions:21:6-10", |
| 69 | + "file-0:deletions:21:6-10", |
| 70 | + ]); |
| 71 | + expect(find("n[a-z]+e", { regex: true })).toHaveLength(6); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns nothing for an empty query or an invalid pattern", () => { |
| 75 | + expect(find("")).toEqual([]); |
| 76 | + expect(find("(", { regex: true })).toEqual([]); |
| 77 | + }); |
| 78 | +}); |
0 commit comments