Open History to see the last commit
1import * as NodeModule from "node:module";
2import type { Result } from "@ff-labs/fff-node";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8
9// Use the same require export as WorkspaceSearchIndex, with the actual native library.
10const requireForFff = NodeModule.createRequire(import.meta.url);
11const { FileFinder } = requireForFff("@ff-labs/fff-node") as typeof import("@ff-labs/fff-node");
12
13function valueOf<T>(result: Result<T>): T {
14 if (!result.ok) throw new Error(result.error);
15 return result.value;
16}
17
18it.effect("decodes native search and grep strings and keeps scores only through 200 results", () =>
19 Effect.gen(function* () {
20 const fileSystem = yield* FileSystem.FileSystem;
21 const path = yield* Path.Path;
22 const basePath = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fff-native-" });
23 const names = [
24 ...Array.from({ length: 205 }, (_, index) => `file-${String(index).padStart(3, "0")}`),
25 "日本語",
26 "café",
27 "emoji-🦊",
28 ];
29 const relativePaths = names.map((name) => `${name}/${name}.ts`);
30 const content = "prefix marker-日本語🦊\n";
31 yield* Effect.forEach(
32 names,
33 (name) =>
34 Effect.gen(function* () {
35 yield* fileSystem.makeDirectory(path.join(basePath, name));
36 yield* fileSystem.writeFileString(path.join(basePath, name, `${name}.ts`), content);
37 }),
38 { concurrency: "unbounded" },
39 );
40 expect(requireForFff.resolve("@ff-labs/fff-node").replaceAll("\\", "/")).toMatch(
41 /\/dist\/index\.js$/u,
42 );
43 const finder = yield* Effect.acquireRelease(
44 Effect.sync(() =>
45 valueOf(FileFinder.create({ basePath, disableWatch: true, disableMmapCache: true })),
46 ),
47 (finder) => Effect.sync(() => finder.destroy()),
48 );
49 expect(valueOf(yield* Effect.promise(() => finder.waitForIndexReady(10_000)))).toBe(true);
50
51 const files200 = valueOf(finder.fileSearch("", { pageSize: 200 }));
52 const files201 = valueOf(finder.fileSearch("", { pageSize: 201 }));
53 expect(files200.items).toHaveLength(200);
54 expect(files200.scores).toHaveLength(200);
55 expect(files201.items).toHaveLength(201);
56 expect(files201.scores).toEqual([]);
57 expect(files201.items.slice(0, 200)).toEqual(files200.items);
58 const files = valueOf(finder.fileSearch("", { pageSize: 1000 }));
59 expect(files.items.map((item) => item.relativePath.normalize("NFC")).toSorted()).toEqual(
60 relativePaths.toSorted(),
61 );
62 for (const item of files.items) {
63 expect(item.fileName.normalize("NFC")).toBe(
64 path.basename(item.relativePath).normalize("NFC"),
65 );
66 expect(item.size).toBe(Buffer.byteLength(content));
67 expect(Number.isFinite(item.modified)).toBe(true);
68 }
69 const exact = valueOf(finder.fileSearch("日本語.ts", { pageSize: 1 }));
70 expect(exact.items[0]?.fileName.normalize("NFC")).toBe("日本語.ts");
71 expect(exact.scores[0]).toMatchObject({ exactMatch: true, matchType: "exact_filename" });
72
73 const dirs200 = valueOf(finder.directorySearch("", { pageSize: 200 }));
74 const dirs201 = valueOf(finder.directorySearch("", { pageSize: 201 }));
75 expect(dirs200.scores).toHaveLength(200);
76 expect(dirs201.items).toHaveLength(201);
77 expect(dirs201.scores).toEqual([]);
78 expect(dirs201.items.slice(0, 200)).toEqual(dirs200.items);
79 const directories = valueOf(finder.directorySearch("", { pageSize: 1000 }));
80 expect(
81 directories.items
82 .map((item) => item.relativePath.normalize("NFC"))
83 .filter(Boolean)
84 .toSorted(),
85 ).toEqual(names.map((name) => `${name}/`).toSorted());
86
87 const mixed200 = valueOf(finder.mixedSearch("", { pageSize: 200 }));
88 const mixed201 = valueOf(finder.mixedSearch("", { pageSize: 201 }));
89 expect(mixed200.scores).toHaveLength(200);
90 expect(mixed201.items).toHaveLength(201);
91 expect(mixed201.scores).toEqual([]);
92 expect(mixed201.items.slice(0, 200)).toEqual(mixed200.items);
93 const mixed = valueOf(finder.mixedSearch("", { pageSize: 1000 }));
94 expect(mixed.items.filter((item) => item.type === "file").map(({ item }) => item)).toEqual(
95 files.items,
96 );
97 expect(mixed.items.filter((item) => item.type === "directory").map(({ item }) => item)).toEqual(
98 directories.items,
99 );
100
101 const grep = valueOf(finder.grep("marker", { mode: "plain", pageSize: 1000 }));
102 expect(grep.items.map((item) => item.relativePath.normalize("NFC")).toSorted()).toEqual(
103 relativePaths.toSorted(),
104 );
105 for (const item of grep.items) {
106 expect(item.fileName.normalize("NFC")).toBe(
107 path.basename(item.relativePath).normalize("NFC"),
108 );
109 expect(item.lineContent).toBe(content.trimEnd());
110 expect(item.lineNumber).toBe(1);
111 expect(item.col).toBe(7);
112 expect(item.matchRanges).toEqual([[7, 13]]);
113 }
114 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
115);