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

WorkspaceSearchIndex.test.ts

159 lines5.4 KB

1import { FileFinder } from "@ff-labs/fff-node";
2import { afterEach, expect, it } from "@effect/vitest";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Exit from "effect/Exit";
6import { vi } from "vite-plus/test";
7​
8import * as WorkspaceSearchIndex from "./WorkspaceSearchIndex.ts";
9​
10afterEach(() => {
11 vi.restoreAllMocks();
12});
13​
14it.effect("preserves unexpected FileFinder creation failures", () =>
15 Effect.gen(function* () {
16 const cause = new Error("native initialization failed");
17 vi.spyOn(FileFinder, "create").mockImplementationOnce(() => {
18 throw cause;
19 });
20​
21 const error = yield* Effect.flip(
22 Effect.scoped(WorkspaceSearchIndex.make("/workspace/project")),
23 );
24​
25 expect(error).toMatchObject({
26 _tag: "WorkspaceSearchIndexCreateFailed",
27 cwd: "/workspace/project",
28 reason: "FileFinder.create threw unexpectedly.",
29 cause,
30 });
31 }),
32);
33​
34it.effect("keeps returned FileFinder creation diagnostics out of the cause chain", () =>
35 Effect.gen(function* () {
36 vi.spyOn(FileFinder, "create").mockReturnValueOnce({
37 ok: false,
38 error: "native index rejected the directory",
39 });
40​
41 const error = yield* Effect.flip(
42 Effect.scoped(WorkspaceSearchIndex.make("/workspace/project")),
43 );
44​
45 expect(error).toMatchObject({
46 _tag: "WorkspaceSearchIndexCreateFailed",
47 cwd: "/workspace/project",
48 reason: "native index rejected the directory",
49 });
50 expect(error.cause).toBeUndefined();
51 }),
52);
53​
54it.effect("preserves FileFinder destroy failures as structured defects", () =>
55 Effect.gen(function* () {
56 const cause = new Error("native destroy failed");
57 const finder = {
58 destroy: vi.fn(() => {
59 throw cause;
60 }),
61 isScanning: vi.fn(() => false),
62 } as unknown as FileFinder;
63 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
64​
65 const exit = yield* Effect.scoped(WorkspaceSearchIndex.make("/workspace/project")).pipe(
66 Effect.exit,
67 );
68​
69 expect(Exit.isFailure(exit)).toBe(true);
70 if (Exit.isFailure(exit)) {
71 expect(Cause.hasDies(exit.cause)).toBe(true);
72 const error = Cause.squash(exit.cause);
73 expect(error).toBeInstanceOf(WorkspaceSearchIndex.WorkspaceSearchIndexDestroyFailed);
74 expect(error).toMatchObject({
75 _tag: "WorkspaceSearchIndexDestroyFailed",
76 cwd: "/workspace/project",
77 cause,
78 });
79 }
80 }),
81);
82​
83it.effect("preserves search and refresh failures with operation context", () =>
84 Effect.scoped(
85 Effect.gen(function* () {
86 const searchCause = new Error("native search failed");
87 const refreshCause = new Error("native scan failed");
88 const finder = {
89 destroy: vi.fn(),
90 isScanning: vi.fn(() => false),
91 mixedSearch: vi.fn(() => {
92 throw searchCause;
93 }),
94 scanFiles: vi.fn(() => {
95 throw refreshCause;
96 }),
97 } as unknown as FileFinder;
98 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
99​
100 const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
101 const query = "authorization: Bearer secret-token";
102 const searchError = yield* Effect.flip(searchIndex.search(query, 3));
103 const refreshError = yield* Effect.flip(searchIndex.refresh());
104​
105 expect(searchError).toMatchObject({
106 _tag: "WorkspaceSearchIndexSearchFailed",
107 cwd: "/workspace/project",
108 queryLength: query.length,
109 pageSize: 4,
110 reason: "FileFinder.mixedSearch threw unexpectedly.",
111 cause: searchCause,
112 });
113 expect(searchError).not.toHaveProperty("query");
114 expect(searchError.message).not.toMatch(/Bearer|secret-token/);
115 expect(refreshError).toMatchObject({
116 _tag: "WorkspaceSearchIndexRefreshFailed",
117 cwd: "/workspace/project",
118 reason: "FileFinder.scanFiles threw unexpectedly.",
119 cause: refreshCause,
120 });
121 }),
122 ),
123);
124​
125it.effect("keeps returned search diagnostics out of the cause chain", () =>
126 Effect.scoped(
127 Effect.gen(function* () {
128 const finder = {
129 destroy: vi.fn(),
130 isScanning: vi.fn(() => false),
131 mixedSearch: vi.fn(() => ({ ok: false, error: "native query rejected" })),
132 scanFiles: vi.fn(() => ({ ok: false, error: "native refresh rejected" })),
133 } as unknown as FileFinder;
134 vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
135​
136 const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
137 const query = "authorization: Bearer secret-token";
138 const searchError = yield* Effect.flip(searchIndex.search(query, 3));
139 const refreshError = yield* Effect.flip(searchIndex.refresh());
140​
141 expect(searchError).toMatchObject({
142 _tag: "WorkspaceSearchIndexSearchFailed",
143 cwd: "/workspace/project",
144 queryLength: query.length,
145 pageSize: 4,
146 reason: "native query rejected",
147 });
148 expect(searchError).not.toHaveProperty("query");
149 expect(searchError.message).not.toMatch(/Bearer|secret-token/);
150 expect(searchError.cause).toBeUndefined();
151 expect(refreshError).toMatchObject({
152 _tag: "WorkspaceSearchIndexRefreshFailed",
153 cwd: "/workspace/project",
154 reason: "native refresh rejected",
155 });
156 expect(refreshError.cause).toBeUndefined();
157 }),
158 ),
159);