Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceSearchIndex.native.test.ts

114 lines4.9 KB

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