Open History to see the last commit
1import { assert, describe, it } from "vite-plus/test";
2
3import { getProjectFilePickerMatches, groupMatchRanges } from "./ProjectFilePicker.logic";
4
5function pathsForQuery(entries: Parameters<typeof getProjectFilePickerMatches>[0], query: string) {
6 return getProjectFilePickerMatches(entries, query).map(({ name, path }) => ({ name, path }));
7}
8
9describe("getProjectFilePickerMatches", () => {
10 it("keeps the leading dot of a dotfile query when highlighting", () => {
11 const entries = [{ kind: "file", path: "src/.env.example" }] as const;
12
13 assert.deepEqual(
14 getProjectFilePickerMatches(entries, ".env")[0]?.nameMatchIndices,
15 [0, 1, 2, 3],
16 );
17 assert.deepEqual(
18 getProjectFilePickerMatches(entries, "./.env")[0]?.nameMatchIndices,
19 [0, 1, 2, 3],
20 );
21 });
22
23 it("matches ordered characters while allowing skipped characters", () => {
24 const fuzzyEntries = [
25 { kind: "file", path: "src/TestFlags.tsx" },
26 { kind: "file", path: "src/SubtestFlow.tsx" },
27 { kind: "file", path: "src/useSubtestFlags.ts" },
28 {
29 kind: "file",
30 path: "src/useSubtestFlags/useTabActivity.ts",
31 },
32 ] as const;
33
34 assert.deepEqual(
35 pathsForQuery(fuzzyEntries, "testf").map(({ name }) => name),
36 ["TestFlags.tsx", "SubtestFlow.tsx", "useSubtestFlags.ts", "useTabActivity.ts"],
37 );
38 assert.deepEqual(getProjectFilePickerMatches(fuzzyEntries, "tsfl")[0], {
39 name: "TestFlags.tsx",
40 nameMatchIndices: [0, 2, 4, 5],
41 path: "src/TestFlags.tsx",
42 pathMatchIndices: [4, 6, 8, 9],
43 });
44 });
45
46 it("normalizes path prefixes consistently with server search", () => {
47 assert.deepEqual(
48 getProjectFilePickerMatches([{ kind: "file", path: "src/index.ts" }], "@/src")[0]
49 ?.pathMatchIndices,
50 [0, 1, 2],
51 );
52 });
53
54 it("maps highlights back to original characters when lowercasing expands them", () => {
55 const entries = [{ kind: "file", path: "icons/İ-logo.svg" }] as const;
56 const indices = (query: string) => {
57 const match = getProjectFilePickerMatches(entries, query)[0];
58 return [match?.nameMatchIndices, match?.pathMatchIndices];
59 };
60
61 assert.deepEqual(indices("logo"), [
62 [2, 3, 4, 5],
63 [8, 9, 10, 11],
64 ]);
65 assert.deepEqual(indices("İ"), [[0], [0, 6]]);
66 });
67
68 it("matches emoji by code point without splitting surrogate pairs", () => {
69 const match = getProjectFilePickerMatches([{ kind: "file", path: "notes/📁-🔥.md" }], "🔥")[0];
70
71 assert.deepEqual(match, {
72 name: "📁-🔥.md",
73 nameMatchIndices: [3, 4],
74 path: "notes/📁-🔥.md",
75 pathMatchIndices: [9, 10],
76 });
77 });
78
79 it("maps emoji highlights accurately when lowercasing expands earlier characters", () => {
80 const entries = [{ kind: "file", path: "notes/İ-📁-🔥.md" }] as const;
81
82 assert.deepEqual(getProjectFilePickerMatches(entries, "🔥")[0], {
83 name: "İ-📁-🔥.md",
84 nameMatchIndices: [5, 6],
85 path: "notes/İ-📁-🔥.md",
86 pathMatchIndices: [11, 12],
87 });
88 assert.deepEqual(getProjectFilePickerMatches(entries, "İ🔥m")[0], {
89 name: "İ-📁-🔥.md",
90 nameMatchIndices: [0, 5, 6, 8],
91 path: "notes/İ-📁-🔥.md",
92 pathMatchIndices: [6, 11, 12, 14],
93 });
94 });
95});
96
97describe("groupMatchRanges", () => {
98 it("groups contiguous indices into [start, end) ranges", () => {
99 assert.deepEqual(groupMatchRanges([0, 1]), [[0, 2]]);
100 assert.deepEqual(groupMatchRanges([0, 2, 4, 5]), [
101 [0, 1],
102 [2, 3],
103 [4, 6],
104 ]);
105 assert.deepEqual(groupMatchRanges([]), []);
106 });
107});