| 1 | import { assert, describe, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { getProjectFilePickerMatches } from "./ProjectFilePicker.logic"; |
| 4 | |
| 5 | function pathsForQuery(entries: Parameters<typeof getProjectFilePickerMatches>[0], query: string) { |
| 6 | return getProjectFilePickerMatches(entries, query).map(({ name, path }) => ({ name, path })); |
| 7 | } |
| 8 | |
| 9 | const entries = [ |
| 10 | { kind: "directory", path: "apps/web/src" }, |
| 11 | { kind: "file", path: "apps/web/src/index.ts" }, |
| 12 | { kind: "file", path: "packages/shared/src/index.ts" }, |
| 13 | { kind: "file", path: "README.md" }, |
| 14 | { kind: "file", path: ".gitignore" }, |
| 15 | ] as const; |
| 16 | |
| 17 | describe("getProjectFilePickerMatches", () => { |
| 18 | it("returns files only and preserves index order for an empty query", () => { |
| 19 | assert.deepEqual(pathsForQuery(entries, ""), [ |
| 20 | { name: "index.ts", path: "apps/web/src/index.ts" }, |
| 21 | { name: "index.ts", path: "packages/shared/src/index.ts" }, |
| 22 | { name: "README.md", path: "README.md" }, |
| 23 | { name: ".gitignore", path: ".gitignore" }, |
| 24 | ]); |
| 25 | }); |
| 26 | |
| 27 | it("preserves the server result order", () => { |
| 28 | assert.deepEqual(pathsForQuery(entries, "index"), [ |
| 29 | { name: "index.ts", path: "apps/web/src/index.ts" }, |
| 30 | { name: "index.ts", path: "packages/shared/src/index.ts" }, |
| 31 | { name: "README.md", path: "README.md" }, |
| 32 | { name: ".gitignore", path: ".gitignore" }, |
| 33 | ]); |
| 34 | }); |
| 35 | |
| 36 | it("supports space-separated path tokens and a result limit", () => { |
| 37 | assert.deepEqual( |
| 38 | getProjectFilePickerMatches(entries, "src index", 1).map(({ name, path }) => ({ |
| 39 | name, |
| 40 | path, |
| 41 | })), |
| 42 | [{ name: "index.ts", path: "apps/web/src/index.ts" }], |
| 43 | ); |
| 44 | }); |
| 45 | |
| 46 | it("matches ordered characters while allowing skipped characters", () => { |
| 47 | const fuzzyEntries = [ |
| 48 | { kind: "file", path: "src/TestFlags.tsx" }, |
| 49 | { kind: "file", path: "src/SubtestFlow.tsx" }, |
| 50 | { kind: "file", path: "src/useSubtestFlags.ts" }, |
| 51 | { |
| 52 | kind: "file", |
| 53 | path: "src/useSubtestFlags/useTabActivity.ts", |
| 54 | }, |
| 55 | ] as const; |
| 56 | |
| 57 | assert.deepEqual( |
| 58 | pathsForQuery(fuzzyEntries, "testf").map(({ name }) => name), |
| 59 | ["TestFlags.tsx", "SubtestFlow.tsx", "useSubtestFlags.ts", "useTabActivity.ts"], |
| 60 | ); |
| 61 | assert.deepEqual(getProjectFilePickerMatches(fuzzyEntries, "tsfl")[0], { |
| 62 | name: "TestFlags.tsx", |
| 63 | nameMatchIndices: [0, 2, 4, 5], |
| 64 | path: "src/TestFlags.tsx", |
| 65 | pathMatchIndices: [4, 6, 8, 9], |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | it("uses the first ordered subsequence for highlighting", () => { |
| 70 | assert.deepEqual( |
| 71 | getProjectFilePickerMatches([{ kind: "file", path: "aabba" }], "aba")[0]?.nameMatchIndices, |
| 72 | [0, 2, 4], |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | it("normalizes path prefixes consistently with server search", () => { |
| 77 | assert.deepEqual( |
| 78 | getProjectFilePickerMatches([{ kind: "file", path: "src/index.ts" }], "@/src")[0] |
| 79 | ?.pathMatchIndices, |
| 80 | [0, 1, 2], |
| 81 | ); |
| 82 | }); |
| 83 | }); |