The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { FileFinder } from "@ff-labs/fff-node"; |
| 5 | import { it, afterEach, describe, expect } from "@effect/vitest"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as PlatformError from "effect/PlatformError"; |
| 11 | import * as TestClock from "effect/testing/TestClock"; |
| 12 | import { vi } from "vite-plus/test"; |
| 13 | |
| 14 | import * as ServerConfig from "../config.ts"; |
| 15 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 16 | import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 17 | import * as WorkspaceEntries from "./WorkspaceEntries.ts"; |
| 18 | import * as WorkspacePaths from "./WorkspacePaths.ts"; |
| 19 | |
| 20 | vi.mock("node:fs/promises", async (importOriginal) => { |
| 21 | const actual = await importOriginal<typeof import("node:fs/promises")>(); |
| 22 | return { ...actual, readdir: vi.fn(actual.readdir) }; |
| 23 | }); |
| 24 | |
| 25 | const layerTest = Layer.empty.pipe( |
| 26 | Layer.provideMerge(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))), |
| 27 | Layer.provideMerge(WorkspacePaths.layer), |
| 28 | Layer.provideMerge(VcsProcess.layer), |
| 29 | Layer.provide( |
| 30 | ServerConfig.ServerConfig.layerTest(process.cwd(), { |
| 31 | prefix: "t3-workspace-entries-test-", |
| 32 | }), |
| 33 | ), |
| 34 | Layer.provideMerge(NodeServices.layer), |
| 35 | ); |
| 36 | |
| 37 | const makeTempDir = Effect.fn(function* (opts?: { prefix?: string; git?: boolean }) { |
| 38 | const fileSystem = yield* FileSystem.FileSystem; |
| 39 | const dir = yield* fileSystem.makeTempDirectoryScoped({ |
| 40 | prefix: opts?.prefix ?? "t3code-workspace-entries-", |
| 41 | }); |
| 42 | if (opts?.git) { |
| 43 | yield* git(dir, ["init"]); |
| 44 | } |
| 45 | return dir; |
| 46 | }); |
| 47 | |
| 48 | function writeTextFile( |
| 49 | cwd: string, |
| 50 | relativePath: string, |
| 51 | contents = "", |
| 52 | ): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> { |
| 53 | return Effect.gen(function* () { |
| 54 | const fileSystem = yield* FileSystem.FileSystem; |
| 55 | const path = yield* Path.Path; |
| 56 | const absolutePath = path.join(cwd, relativePath); |
| 57 | yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true }); |
| 58 | yield* fileSystem.writeFileString(absolutePath, contents); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | const git = (cwd: string, args: ReadonlyArray<string>, env?: NodeJS.ProcessEnv) => |
| 63 | Effect.gen(function* () { |
| 64 | const process = yield* VcsProcess.VcsProcess; |
| 65 | const result = yield* process.run({ |
| 66 | operation: "WorkspaceEntries.test.git", |
| 67 | command: "git", |
| 68 | cwd, |
| 69 | args, |
| 70 | ...(env ? { env } : {}), |
| 71 | timeoutMs: 10_000, |
| 72 | }); |
| 73 | return result.stdout.trim(); |
| 74 | }); |
| 75 | |
| 76 | const searchWorkspaceEntries = (input: { |
| 77 | cwd: string; |
| 78 | query: string; |
| 79 | limit: number; |
| 80 | kind?: "file" | "directory"; |
| 81 | }) => |
| 82 | Effect.gen(function* () { |
| 83 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 84 | return yield* workspaceEntries.search(input); |
| 85 | }); |
| 86 | |
| 87 | const appendSeparator = (input: string) => |
| 88 | Effect.map(HostProcess.Platform, (platform) => |
| 89 | input.endsWith("/") || input.endsWith("\\") |
| 90 | ? input |
| 91 | : `${input}${platform === "win32" ? "\\" : "/"}`, |
| 92 | ); |
| 93 | |
| 94 | it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { |
| 95 | afterEach(() => { |
| 96 | vi.restoreAllMocks(); |
| 97 | }); |
| 98 | |
| 99 | describe("list", () => { |
| 100 | it.effect("lists immediate children including ignored and empty directories", () => |
| 101 | Effect.gen(function* () { |
| 102 | const cwd = yield* makeTempDir({ git: true }); |
| 103 | const fileSystem = yield* FileSystem.FileSystem; |
| 104 | const path = yield* Path.Path; |
| 105 | yield* writeTextFile(cwd, "tracked.txt"); |
| 106 | yield* git(cwd, ["add", "tracked.txt"]); |
| 107 | yield* writeTextFile(cwd, ".gitignore", "node_modules/\n.env\ntracked.txt\n"); |
| 108 | yield* writeTextFile(cwd, ".env", "secret=value"); |
| 109 | yield* writeTextFile(cwd, "node_modules/pkg/index.js"); |
| 110 | yield* writeTextFile(cwd, "src/index.ts"); |
| 111 | yield* fileSystem.makeDirectory(path.join(cwd, "empty")); |
| 112 | yield* git(cwd, ["init", "sub"]); |
| 113 | yield* writeTextFile(cwd, "sub/.gitignore", "dist/\n"); |
| 114 | yield* writeTextFile(cwd, "sub/dist/out.js"); |
| 115 | const gitlink = yield* git(cwd, ["hash-object", "tracked.txt"]); |
| 116 | yield* git(cwd, ["update-index", "--add", "--cacheinfo", `160000,${gitlink},sub`]); |
| 117 | // A nested clone that isn't a submodule keeps the outer repository's ignore rules. |
| 118 | yield* git(cwd, ["init", "node_modules/pkg"]); |
| 119 | yield* git(cwd, ["init", "sub/dist"]); |
| 120 | |
| 121 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 122 | const root = yield* workspaceEntries.list({ cwd, directoryPath: "" }); |
| 123 | expect(root.entries).toEqual( |
| 124 | expect.arrayContaining([ |
| 125 | { path: ".env", kind: "file", ignored: true }, |
| 126 | { path: "node_modules", kind: "directory", ignored: true }, |
| 127 | { path: "src", kind: "directory" }, |
| 128 | { path: "empty", kind: "directory" }, |
| 129 | { path: "tracked.txt", kind: "file" }, |
| 130 | ]), |
| 131 | ); |
| 132 | expect(root.entries.some((entry) => entry.path.includes("/"))).toBe(false); |
| 133 | expect(root.entries.some((entry) => entry.path === ".git")).toBe(false); |
| 134 | expect(root.truncated).toBe(false); |
| 135 | expect(yield* workspaceEntries.list({ cwd, directoryPath: "node_modules/pkg" })).toEqual({ |
| 136 | entries: [{ path: "node_modules/pkg/index.js", kind: "file", ignored: true }], |
| 137 | truncated: false, |
| 138 | }); |
| 139 | // Git refuses pathspecs inside a submodule from the outer repository. |
| 140 | expect((yield* workspaceEntries.list({ cwd, directoryPath: "sub" })).entries).toEqual( |
| 141 | expect.arrayContaining([ |
| 142 | { path: "sub/.gitignore", kind: "file" }, |
| 143 | { path: "sub/dist", kind: "directory", ignored: true }, |
| 144 | ]), |
| 145 | ); |
| 146 | expect(yield* workspaceEntries.list({ cwd, directoryPath: "sub/dist" })).toEqual({ |
| 147 | entries: [{ path: "sub/dist/out.js", kind: "file", ignored: true }], |
| 148 | truncated: false, |
| 149 | }); |
| 150 | expect(yield* workspaceEntries.list({ cwd, directoryPath: "empty" })).toEqual({ |
| 151 | entries: [], |
| 152 | truncated: false, |
| 153 | }); |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | it.effect("lists symlinks as what they point to, hiding ones that cannot be opened", () => |
| 158 | Effect.gen(function* () { |
| 159 | const platform = yield* HostProcess.Platform; |
| 160 | if (platform === "win32") return; |
| 161 | const cwd = yield* makeTempDir({ git: true }); |
| 162 | const outside = yield* makeTempDir(); |
| 163 | const fileSystem = yield* FileSystem.FileSystem; |
| 164 | const path = yield* Path.Path; |
| 165 | yield* writeTextFile(cwd, "real.txt"); |
| 166 | yield* writeTextFile(cwd, "realdir/nested.txt"); |
| 167 | yield* writeTextFile(outside, "outside.txt"); |
| 168 | yield* fileSystem.symlink("real.txt", path.join(cwd, "link.txt")); |
| 169 | yield* fileSystem.symlink("realdir", path.join(cwd, "linkdir")); |
| 170 | yield* fileSystem.symlink(outside, path.join(cwd, "outside-link")); |
| 171 | yield* fileSystem.symlink(".git", path.join(cwd, "git-link")); |
| 172 | yield* fileSystem.symlink("missing", path.join(cwd, "dangling")); |
| 173 | |
| 174 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 175 | const root = yield* workspaceEntries.list({ cwd, directoryPath: "" }); |
| 176 | expect(root.entries.toSorted((a, b) => a.path.localeCompare(b.path))).toEqual([ |
| 177 | { path: "link.txt", kind: "file" }, |
| 178 | { path: "linkdir", kind: "directory" }, |
| 179 | { path: "real.txt", kind: "file" }, |
| 180 | { path: "realdir", kind: "directory" }, |
| 181 | ]); |
| 182 | expect(yield* workspaceEntries.list({ cwd, directoryPath: "linkdir" })).toEqual({ |
| 183 | entries: [{ path: "linkdir/nested.txt", kind: "file" }], |
| 184 | truncated: false, |
| 185 | }); |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | it.effect("hides folder links that lead back to a folder on the listed path", () => |
| 190 | // "Expand all folders" loads every folder it expands, so a link that loops would |
| 191 | // keep nesting the same entries without end. |
| 192 | Effect.gen(function* () { |
| 193 | const platform = yield* HostProcess.Platform; |
| 194 | if (platform === "win32") return; |
| 195 | const cwd = yield* makeTempDir(); |
| 196 | const fileSystem = yield* FileSystem.FileSystem; |
| 197 | const path = yield* Path.Path; |
| 198 | yield* writeTextFile(cwd, "a/file.txt"); |
| 199 | yield* writeTextFile(cwd, "b/file.txt"); |
| 200 | yield* fileSystem.symlink(".", path.join(cwd, "self")); |
| 201 | yield* fileSystem.symlink("..", path.join(cwd, "a/up")); |
| 202 | yield* fileSystem.symlink("../b", path.join(cwd, "a/to-b")); |
| 203 | yield* fileSystem.symlink("../a", path.join(cwd, "b/to-a")); |
| 204 | |
| 205 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 206 | const paths = (directoryPath: string) => |
| 207 | workspaceEntries |
| 208 | .list({ cwd, directoryPath }) |
| 209 | .pipe(Effect.map(({ entries }) => entries.map((entry) => entry.path).toSorted())); |
| 210 | expect(yield* paths("")).toEqual(["a", "b"]); |
| 211 | // A link into a sibling folder is fine on its own... |
| 212 | expect(yield* paths("a")).toEqual(["a/file.txt", "a/to-b"]); |
| 213 | // ...but once followed, the sibling's link back is hidden. |
| 214 | expect(yield* paths("a/to-b")).toEqual(["a/to-b/file.txt"]); |
| 215 | }), |
| 216 | ); |
| 217 | |
| 218 | it.effect("resolves a folder of many symlinks in bounded batches", () => |
| 219 | Effect.gen(function* () { |
| 220 | const platform = yield* HostProcess.Platform; |
| 221 | if (platform === "win32") return; |
| 222 | const cwd = yield* makeTempDir(); |
| 223 | const fileSystem = yield* FileSystem.FileSystem; |
| 224 | const path = yield* Path.Path; |
| 225 | // Shaped like a pnpm `node_modules`: one link per package into `.pnpm`. |
| 226 | const directoryPath = [...Array.from({ length: 80 }, () => "nested"), "node_modules"].join( |
| 227 | "/", |
| 228 | ); |
| 229 | const packages = Array.from({ length: 200 }, (_, index) => `pkg-${index}`); |
| 230 | yield* Effect.forEach( |
| 231 | packages, |
| 232 | (name) => |
| 233 | Effect.gen(function* () { |
| 234 | yield* writeTextFile(cwd, `${directoryPath}/.pnpm/${name}/index.js`); |
| 235 | yield* fileSystem.symlink(`.pnpm/${name}`, path.join(cwd, directoryPath, name)); |
| 236 | }), |
| 237 | { concurrency: 32, discard: true }, |
| 238 | ); |
| 239 | const realpath = NodeFSP.realpath; |
| 240 | let inFlight = 0; |
| 241 | let maxInFlight = 0; |
| 242 | vi.spyOn(NodeFSP, "realpath").mockImplementation((async (target: string) => { |
| 243 | maxInFlight = Math.max(maxInFlight, ++inFlight); |
| 244 | try { |
| 245 | return await realpath(target); |
| 246 | } finally { |
| 247 | inFlight--; |
| 248 | } |
| 249 | }) as typeof NodeFSP.realpath); |
| 250 | |
| 251 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 252 | const { entries } = yield* workspaceEntries.list({ cwd, directoryPath }); |
| 253 | expect(entries.map((entry) => entry.path).toSorted()).toEqual( |
| 254 | [ |
| 255 | `${directoryPath}/.pnpm`, |
| 256 | ...packages.map((name) => `${directoryPath}/${name}`), |
| 257 | ].toSorted(), |
| 258 | ); |
| 259 | expect(entries.every((entry) => entry.kind === "directory")).toBe(true); |
| 260 | expect(maxInFlight).toBeLessThanOrEqual(64); |
| 261 | }), |
| 262 | ); |
| 263 | |
| 264 | it.effect("labels the children of wide folders exactly as git does for each child", () => |
| 265 | Effect.gen(function* () { |
| 266 | // Above 1,000 children, listing first asks whether the whole folder is ignored. |
| 267 | const cwd = yield* makeTempDir({ git: true }); |
| 268 | const fileSystem = yield* FileSystem.FileSystem; |
| 269 | const path = yield* Path.Path; |
| 270 | const platform = yield* HostProcess.Platform; |
| 271 | yield* writeTextFile( |
| 272 | cwd, |
| 273 | ".gitignore", |
| 274 | "node_modules/\ndist/\n\\[id\\]/\n*.map\nfoo\n!foo/\n", |
| 275 | ); |
| 276 | yield* Effect.forEach( |
| 277 | [ |
| 278 | "node_modules", |
| 279 | "dist", |
| 280 | "[id]", |
| 281 | "src", |
| 282 | ...(platform !== "win32" ? [":foo"] : []), |
| 283 | ].flatMap((directory) => |
| 284 | Array.from({ length: 1001 }, (_, index) => `${directory}/pkg-${index}.js`), |
| 285 | ), |
| 286 | (relativePath) => writeTextFile(cwd, relativePath), |
| 287 | { concurrency: 32, discard: true }, |
| 288 | ); |
| 289 | for (const relativePath of [ |
| 290 | "dist/kept.js", |
| 291 | "dist/vendored/index.js", |
| 292 | "i/page.tsx", |
| 293 | "[id]/page.tsx", |
| 294 | "src/bundle.map", |
| 295 | ]) { |
| 296 | yield* writeTextFile(cwd, relativePath); |
| 297 | } |
| 298 | yield* git(cwd, ["add", "-f", "dist/kept.js", "dist/vendored/index.js", "i/page.tsx"]); |
| 299 | if (platform !== "win32") { |
| 300 | yield* fileSystem.symlink(path.join(cwd, "src"), path.join(cwd, "node_modules/link")); |
| 301 | } |
| 302 | |
| 303 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 304 | for (const { directoryPath, ignored, except } of [ |
| 305 | { directoryPath: "node_modules", ignored: true, except: [] }, |
| 306 | { directoryPath: "dist", ignored: true, except: ["dist/kept.js", "dist/vendored"] }, |
| 307 | // git reads `[id]/page.tsx` as a pathspec, which matches the tracked `i/page.tsx`. |
| 308 | { directoryPath: "[id]", ignored: true, except: ["[id]/page.tsx"] }, |
| 309 | { directoryPath: "src", ignored: false, except: ["src/bundle.map"] }, |
| 310 | ...(platform !== "win32" |
| 311 | ? [ |
| 312 | // git refuses to classify paths beyond a symlink, so nothing there is labelled. |
| 313 | { directoryPath: "node_modules/link", ignored: false, except: [] }, |
| 314 | // Leading : is pathspec magic; Git sees foo/pkg-0.js, unignored by !foo/. |
| 315 | { directoryPath: ":foo", ignored: false, except: [] }, |
| 316 | ] |
| 317 | : []), |
| 318 | ]) { |
| 319 | const { entries } = yield* workspaceEntries.list({ cwd, directoryPath }); |
| 320 | expect(entries.length, directoryPath).toBeGreaterThan(1000); |
| 321 | expect( |
| 322 | entries |
| 323 | .filter((entry) => (entry.ignored === true) !== ignored) |
| 324 | .map((entry) => entry.path) |
| 325 | .toSorted(), |
| 326 | directoryPath, |
| 327 | ).toEqual(except); |
| 328 | } |
| 329 | }), |
| 330 | ); |
| 331 | |
| 332 | it.effect("lists a host folder and its parent, each from its own root", () => |
| 333 | Effect.gen(function* () { |
| 334 | const parent = yield* makeTempDir(); |
| 335 | const path = yield* Path.Path; |
| 336 | yield* writeTextFile(parent, "linked/notes.md"); |
| 337 | yield* writeTextFile(parent, "sibling/readme.md"); |
| 338 | const linked = path.join(parent, "linked"); |
| 339 | |
| 340 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 341 | expect(yield* workspaceEntries.list({ cwd: linked, directoryPath: "" })).toEqual({ |
| 342 | entries: [{ path: "notes.md", kind: "file" }], |
| 343 | truncated: false, |
| 344 | }); |
| 345 | const up = yield* workspaceEntries.list({ cwd: parent, directoryPath: "" }); |
| 346 | expect(up.entries.map((entry) => entry.path).toSorted()).toEqual(["linked", "sibling"]); |
| 347 | const escape = yield* workspaceEntries |
| 348 | .list({ cwd: linked, directoryPath: "../sibling" }) |
| 349 | .pipe(Effect.flip); |
| 350 | expect(escape._tag).toBe("WorkspaceEntriesReadDirectoryError"); |
| 351 | }), |
| 352 | ); |
| 353 | |
| 354 | it.effect( |
| 355 | "rejects directory traversal, git internals, and symlinks outside the workspace", |
| 356 | () => |
| 357 | Effect.gen(function* () { |
| 358 | const cwd = yield* makeTempDir(); |
| 359 | const outside = yield* makeTempDir(); |
| 360 | const fileSystem = yield* FileSystem.FileSystem; |
| 361 | const path = yield* Path.Path; |
| 362 | yield* writeTextFile(cwd, ".git/HEAD"); |
| 363 | const platform = yield* HostProcess.Platform; |
| 364 | if (platform !== "win32") yield* fileSystem.symlink(outside, path.join(cwd, "external")); |
| 365 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 366 | for (const directoryPath of [ |
| 367 | "../", |
| 368 | outside, |
| 369 | ".git", |
| 370 | "missing", |
| 371 | ...(platform !== "win32" ? ["external"] : []), |
| 372 | ]) { |
| 373 | const error = yield* workspaceEntries.list({ cwd, directoryPath }).pipe(Effect.flip); |
| 374 | expect(error._tag).toBe("WorkspaceEntriesReadDirectoryError"); |
| 375 | } |
| 376 | }), |
| 377 | ); |
| 378 | |
| 379 | it.effect("includes images and other binary files in non-git workspace indexes", () => |
| 380 | Effect.gen(function* () { |
| 381 | const cwd = yield* makeTempDir(); |
| 382 | const imagePaths = ["photo.png", "photo.jpg", "photo.jpeg"]; |
| 383 | const binaryPaths = [...imagePaths, "document.pdf", "archive.zip", "data.bin"]; |
| 384 | for (const filePath of binaryPaths) { |
| 385 | yield* writeTextFile(cwd, filePath, "\0binary contents\n"); |
| 386 | } |
| 387 | yield* writeTextFile(cwd, "notes.txt", "plain text\n"); |
| 388 | |
| 389 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 390 | const expectedPaths = [...binaryPaths, "notes.txt"].toSorted(); |
| 391 | const listed = yield* workspaceEntries.list({ cwd }); |
| 392 | expect(listed.entries.map((entry) => entry.path).toSorted()).toEqual(expectedPaths); |
| 393 | expect(listed.truncated).toBe(false); |
| 394 | |
| 395 | const searched = yield* workspaceEntries.search({ |
| 396 | cwd, |
| 397 | query: "", |
| 398 | limit: 100, |
| 399 | kind: "file", |
| 400 | }); |
| 401 | expect(searched.entries.map((entry) => entry.path).toSorted()).toEqual(expectedPaths); |
| 402 | expect(searched.truncated).toBe(false); |
| 403 | |
| 404 | const images = yield* workspaceEntries.search({ |
| 405 | cwd, |
| 406 | query: "", |
| 407 | limit: 100, |
| 408 | imageOnly: true, |
| 409 | }); |
| 410 | expect(images.entries.map((entry) => entry.path).toSorted()).toEqual(imagePaths.toSorted()); |
| 411 | expect(images.truncated).toBe(false); |
| 412 | }), |
| 413 | ); |
| 414 | }); |
| 415 | |
| 416 | describe("listIgnoredFiles", () => { |
| 417 | it.effect("lists ignored files with sizes and skips wholly ignored folders", () => |
| 418 | Effect.gen(function* () { |
| 419 | const cwd = yield* makeTempDir({ git: true }); |
| 420 | yield* writeTextFile(cwd, ".gitignore", "node_modules/\n.env*\n"); |
| 421 | yield* writeTextFile(cwd, ".env", "A=1"); |
| 422 | yield* writeTextFile(cwd, "apps/web/index.ts", "y"); |
| 423 | yield* git(cwd, ["add", "apps/web/index.ts"]); |
| 424 | yield* writeTextFile(cwd, "apps/web/.env.local", "B=22"); |
| 425 | yield* writeTextFile(cwd, "node_modules/pkg/index.js", "x"); |
| 426 | |
| 427 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 428 | expect(yield* workspaceEntries.listIgnoredFiles({ cwd })).toEqual({ |
| 429 | files: [ |
| 430 | { path: ".env", byteLength: 3 }, |
| 431 | { path: "apps/web/.env.local", byteLength: 4 }, |
| 432 | ], |
| 433 | truncated: false, |
| 434 | }); |
| 435 | }), |
| 436 | ); |
| 437 | |
| 438 | it.effect("finds ignored files that sort past the listing limit", () => |
| 439 | Effect.gen(function* () { |
| 440 | const cwd = yield* makeTempDir({ git: true }); |
| 441 | yield* writeTextFile(cwd, ".gitignore", "*.secret\n"); |
| 442 | for (let index = 1; index <= 205; index += 1) { |
| 443 | yield* writeTextFile(cwd, `file-${String(index).padStart(3, "0")}.secret`, "s"); |
| 444 | } |
| 445 | |
| 446 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 447 | const listed = yield* workspaceEntries.listIgnoredFiles({ cwd, query: "file-205" }); |
| 448 | expect(listed.files).toContainEqual({ path: "file-205.secret", byteLength: 1 }); |
| 449 | }), |
| 450 | ); |
| 451 | }); |
| 452 | |
| 453 | describe("search", () => { |
| 454 | it.effect("applies the file filter before limiting search results", () => |
| 455 | Effect.gen(function* () { |
| 456 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-file-limit-" }); |
| 457 | yield* writeTextFile(cwd, "src/index.ts"); |
| 458 | yield* writeTextFile(cwd, "src/internal.ts"); |
| 459 | |
| 460 | const result = yield* searchWorkspaceEntries({ |
| 461 | cwd, |
| 462 | query: "src", |
| 463 | limit: 1, |
| 464 | kind: "file", |
| 465 | }); |
| 466 | |
| 467 | // Native ranking can put either matching file first. |
| 468 | expect(result.entries).toHaveLength(1); |
| 469 | expect(result.entries[0]?.kind).toBe("file"); |
| 470 | expect(["src/index.ts", "src/internal.ts"]).toContain(result.entries[0]?.path); |
| 471 | expect(result.truncated).toBe(true); |
| 472 | }), |
| 473 | ); |
| 474 | |
| 475 | it.effect("matches glob metacharacters in the query literally", () => |
| 476 | Effect.gen(function* () { |
| 477 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-brackets-" }); |
| 478 | yield* writeTextFile(cwd, "src/app/game/[id]/index.tsx"); |
| 479 | yield* writeTextFile(cwd, "src/foo{bar}.ts"); |
| 480 | yield* writeTextFile(cwd, "src/main.ts"); |
| 481 | yield* writeTextFile(cwd, "src/notes.md"); |
| 482 | |
| 483 | const search = (query: string) => |
| 484 | searchWorkspaceEntries({ cwd, query, limit: 10 }).pipe( |
| 485 | Effect.map((result) => result.entries.map((entry) => entry.path)), |
| 486 | ); |
| 487 | |
| 488 | for (const query of ["game/[i", "game/[id]"]) { |
| 489 | expect(yield* search(query)).toContain("src/app/game/[id]/index.tsx"); |
| 490 | } |
| 491 | expect(yield* search("foo{bar}.ts")).toContain("src/foo{bar}.ts"); |
| 492 | expect((yield* search("*.{ts,tsx}")).toSorted()).toEqual([ |
| 493 | "src/app/game/[id]/index.tsx", |
| 494 | "src/foo{bar}.ts", |
| 495 | "src/main.ts", |
| 496 | ]); |
| 497 | expect((yield* search("*.{ts}")).toSorted()).toEqual(["src/foo{bar}.ts", "src/main.ts"]); |
| 498 | expect(yield* search("src/{main}.ts")).toEqual(["src/main.ts"]); |
| 499 | expect(yield* search("src/{foo{bar},notes}.md")).toEqual(["src/notes.md"]); |
| 500 | }), |
| 501 | ); |
| 502 | |
| 503 | it.effect("excludes gitignored paths for git repositories", () => |
| 504 | Effect.gen(function* () { |
| 505 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-gitignore-", git: true }); |
| 506 | yield* writeTextFile(cwd, ".gitignore", ".convex/\nconvex/\nignored.txt\n"); |
| 507 | yield* writeTextFile(cwd, "src/keep.ts", "export {};"); |
| 508 | yield* writeTextFile(cwd, "ignored.txt", "ignore me"); |
| 509 | yield* writeTextFile(cwd, ".convex/local-storage/data.json", "{}"); |
| 510 | yield* writeTextFile(cwd, "convex/UOoS-l/convex_local_storage/modules/data.json", "{}"); |
| 511 | yield* writeTextFile(cwd, ".git/info/exclude", "info-excluded.txt\n"); |
| 512 | yield* writeTextFile(cwd, "info-excluded.txt", "ignore me"); |
| 513 | yield* writeTextFile(cwd, ".git/configured-excludes", "configured-excluded.txt\n"); |
| 514 | yield* git(cwd, ["config", "core.excludesFile", ".git/configured-excludes"]); |
| 515 | yield* writeTextFile(cwd, "configured-excluded.txt", "ignore me"); |
| 516 | |
| 517 | const result = yield* searchWorkspaceEntries({ cwd, query: "", limit: 100 }); |
| 518 | const paths = result.entries.map((entry) => entry.path); |
| 519 | |
| 520 | expect(paths).toContain("src"); |
| 521 | expect(paths).toContain("src/keep.ts"); |
| 522 | expect(paths).not.toContain("ignored.txt"); |
| 523 | expect(paths).not.toContain("info-excluded.txt"); |
| 524 | expect(paths).not.toContain("configured-excluded.txt"); |
| 525 | expect(paths.some((entryPath) => entryPath.startsWith(".convex/"))).toBe(false); |
| 526 | expect(paths.some((entryPath) => entryPath.startsWith("convex/"))).toBe(false); |
| 527 | }), |
| 528 | ); |
| 529 | |
| 530 | it.effect("excludes tracked paths that match ignore rules", () => |
| 531 | Effect.gen(function* () { |
| 532 | const cwd = yield* makeTempDir({ |
| 533 | prefix: "t3code-workspace-tracked-gitignore-", |
| 534 | git: true, |
| 535 | }); |
| 536 | yield* writeTextFile(cwd, ".convex/local-storage/data.json", "{}"); |
| 537 | yield* writeTextFile(cwd, "src/keep.ts", "export {};"); |
| 538 | yield* git(cwd, ["add", ".convex/local-storage/data.json", "src/keep.ts"]); |
| 539 | yield* writeTextFile(cwd, ".gitignore", ".convex/\n"); |
| 540 | |
| 541 | const result = yield* searchWorkspaceEntries({ cwd, query: "", limit: 100 }); |
| 542 | const paths = result.entries.map((entry) => entry.path); |
| 543 | |
| 544 | expect(paths).toContain("src"); |
| 545 | expect(paths).toContain("src/keep.ts"); |
| 546 | expect(paths.some((entryPath) => entryPath.startsWith(".convex/"))).toBe(false); |
| 547 | }), |
| 548 | ); |
| 549 | |
| 550 | it.effect("indexes symlinked files and directories inside the workspace", () => |
| 551 | Effect.gen(function* () { |
| 552 | if ((yield* HostProcess.Platform) === "win32") return; |
| 553 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-symlinks-" }); |
| 554 | const fileSystem = yield* FileSystem.FileSystem; |
| 555 | const path = yield* Path.Path; |
| 556 | yield* writeTextFile(cwd, "src/index.ts", "export {};"); |
| 557 | yield* fileSystem.symlink("src", path.join(cwd, "src-link")); |
| 558 | yield* fileSystem.symlink("src/index.ts", path.join(cwd, "index-link.ts")); |
| 559 | const symlinkEntries = [ |
| 560 | { kind: "directory", path: "src-link" }, |
| 561 | { kind: "file", path: "index-link.ts" }, |
| 562 | ]; |
| 563 | |
| 564 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 565 | const searched = yield* searchWorkspaceEntries({ cwd, query: "link", limit: 100 }); |
| 566 | const listed = yield* workspaceEntries.list({ cwd }); |
| 567 | |
| 568 | for (const entry of symlinkEntries) { |
| 569 | expect(searched.entries).toContainEqual(expect.objectContaining(entry)); |
| 570 | expect(listed.entries).toContainEqual(expect.objectContaining(entry)); |
| 571 | } |
| 572 | }), |
| 573 | ); |
| 574 | it.effect("skips symlinks that ignore rules hide or that leave the workspace", () => |
| 575 | Effect.gen(function* () { |
| 576 | if ((yield* HostProcess.Platform) === "win32") return; |
| 577 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-symlinks-", git: true }); |
| 578 | const outside = yield* makeTempDir({ prefix: "t3code-workspace-symlinks-outside-" }); |
| 579 | const fileSystem = yield* FileSystem.FileSystem; |
| 580 | const path = yield* Path.Path; |
| 581 | yield* writeTextFile(cwd, "src/index.ts", "export {};"); |
| 582 | yield* writeTextFile(cwd, ".ignore", "custom-link\n"); |
| 583 | yield* fileSystem.symlink("src", path.join(cwd, "custom-link")); |
| 584 | yield* fileSystem.symlink("src/index.ts", path.join(cwd, "tracked-link.ts")); |
| 585 | yield* fileSystem.symlink("src", path.join(cwd, "ignored-link")); |
| 586 | yield* fileSystem.symlink("src", path.join(cwd, "kept-link")); |
| 587 | yield* fileSystem.symlink(outside, path.join(cwd, "outside-link")); |
| 588 | yield* git(cwd, ["add", "tracked-link.ts"]); |
| 589 | yield* writeTextFile(cwd, ".gitignore", "tracked-link.ts\nignored-link\n"); |
| 590 | // A submodule path makes `git check-ignore` refuse the whole batch without --no-index. |
| 591 | const submodule = path.join(cwd, "vendor/sub"); |
| 592 | yield* writeTextFile(submodule, "a.ts", "export {};"); |
| 593 | yield* git(submodule, ["init"]); |
| 594 | yield* git(submodule, ["add", "a.ts"]); |
| 595 | yield* git(submodule, [ |
| 596 | "-c", |
| 597 | "user.name=t", |
| 598 | "-c", |
| 599 | "user.email=t@t", |
| 600 | "commit", |
| 601 | "-m", |
| 602 | "init", |
| 603 | ]); |
| 604 | yield* git(cwd, ["add", "vendor/sub"]); |
| 605 | yield* fileSystem.symlink("a.ts", path.join(submodule, "sublink.ts")); |
| 606 | |
| 607 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 608 | const searched = yield* searchWorkspaceEntries({ cwd, query: "link", limit: 100 }); |
| 609 | const listed = yield* workspaceEntries.list({ cwd }); |
| 610 | |
| 611 | for (const entries of [searched.entries, listed.entries]) { |
| 612 | const paths = entries.map((entry) => entry.path); |
| 613 | expect(paths).toContain("kept-link"); |
| 614 | expect(paths).toContain("vendor/sub/sublink.ts"); |
| 615 | expect(paths).not.toContain("custom-link"); |
| 616 | expect(paths).not.toContain("tracked-link.ts"); |
| 617 | expect(paths).not.toContain("ignored-link"); |
| 618 | expect(paths).not.toContain("outside-link"); |
| 619 | } |
| 620 | }), |
| 621 | ); |
| 622 | it.effect("lets the nearest .ignore rule decide a symlink, ahead of git's rules", () => |
| 623 | Effect.gen(function* () { |
| 624 | if ((yield* HostProcess.Platform) === "win32") return; |
| 625 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-symlinks-", git: true }); |
| 626 | const fileSystem = yield* FileSystem.FileSystem; |
| 627 | const path = yield* Path.Path; |
| 628 | yield* writeTextFile(cwd, ".gitignore", "*keep*\n"); |
| 629 | yield* writeTextFile(cwd, ".ignore", "!*keep*\n*hide*\n"); |
| 630 | yield* writeTextFile(cwd, "sub/.ignore", "!*hide*\n"); |
| 631 | yield* writeTextFile(cwd, "keep-real.ts", "export {};"); |
| 632 | yield* writeTextFile(cwd, "sub/hide-real.ts", "export {};"); |
| 633 | yield* fileSystem.symlink("keep-real.ts", path.join(cwd, "keep-link.ts")); |
| 634 | yield* fileSystem.symlink("hide-real.ts", path.join(cwd, "sub/hide-link.ts")); |
| 635 | |
| 636 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 637 | const searched = yield* searchWorkspaceEntries({ cwd, query: "link", limit: 100 }); |
| 638 | const listed = yield* workspaceEntries.list({ cwd }); |
| 639 | |
| 640 | for (const entries of [searched.entries, listed.entries]) { |
| 641 | const paths = entries.map((entry) => entry.path); |
| 642 | expect(paths).toContain("sub/hide-link.ts"); |
| 643 | expect(paths).toContain("keep-link.ts"); |
| 644 | } |
| 645 | // The real files are shown or hidden by the same rules. |
| 646 | const listedPaths = listed.entries.map((entry) => entry.path); |
| 647 | expect(listedPaths).toContain("sub/hide-real.ts"); |
| 648 | expect(listedPaths).toContain("keep-real.ts"); |
| 649 | }), |
| 650 | ); |
| 651 | it.effect("matches .ignore rules against whole names and treats folder links as links", () => |
| 652 | Effect.gen(function* () { |
| 653 | if ((yield* HostProcess.Platform) === "win32") return; |
| 654 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-symlinks-", git: true }); |
| 655 | const fileSystem = yield* FileSystem.FileSystem; |
| 656 | const path = yield* Path.Path; |
| 657 | yield* writeTextFile(cwd, "src/index.ts", "export {};"); |
| 658 | yield* writeTextFile(cwd, ".gitignore", "blink\n"); |
| 659 | // `!link` must not re-include `blink`, and `folder-link/` does not apply to a link. |
| 660 | yield* writeTextFile(cwd, ".ignore", "!link\nfolder-link/\n"); |
| 661 | yield* fileSystem.symlink("src", path.join(cwd, "blink")); |
| 662 | yield* fileSystem.symlink("src", path.join(cwd, "folder-link")); |
| 663 | yield* writeTextFile(cwd, "sub/blink", "regular"); |
| 664 | |
| 665 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 666 | const searched = yield* searchWorkspaceEntries({ cwd, query: "link", limit: 100 }); |
| 667 | const listed = yield* workspaceEntries.list({ cwd }); |
| 668 | |
| 669 | for (const entries of [searched.entries, listed.entries]) { |
| 670 | const paths = entries.map((entry) => entry.path); |
| 671 | expect(paths).not.toContain("blink"); |
| 672 | expect(paths).not.toContain("sub/blink"); |
| 673 | expect(paths).toContain("folder-link"); |
| 674 | } |
| 675 | }), |
| 676 | ); |
| 677 | it.effect("finds a path inside a gitignored nested git worktree only by its exact path", () => |
| 678 | Effect.gen(function* () { |
| 679 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-worktree-", git: true }); |
| 680 | const env = { |
| 681 | ...process.env, |
| 682 | GIT_AUTHOR_NAME: "Test", |
| 683 | GIT_AUTHOR_EMAIL: "test@example.com", |
| 684 | GIT_COMMITTER_NAME: "Test", |
| 685 | GIT_COMMITTER_EMAIL: "test@example.com", |
| 686 | }; |
| 687 | yield* writeTextFile(cwd, ".gitignore", ".worktrees/\n"); |
| 688 | yield* git(cwd, ["add", ".gitignore"]); |
| 689 | yield* git(cwd, ["commit", "-m", "init"], env); |
| 690 | yield* git(cwd, ["worktree", "add", "-b", "x", ".worktrees/x"]); |
| 691 | yield* writeTextFile(cwd, ".worktrees/x/src/foo.py", "print(1)"); |
| 692 | const expected = [".worktrees/x/src/foo.py"]; |
| 693 | // Windows file names cannot contain a newline. |
| 694 | if ((yield* HostProcess.Platform) !== "win32") { |
| 695 | yield* git(cwd, ["worktree", "add", "--detach", ".worktrees/line\nname"]); |
| 696 | yield* writeTextFile(cwd, ".worktrees/line\nname/src/foo.py", "print(2)"); |
| 697 | expected.unshift(".worktrees/line\nname/src/foo.py"); |
| 698 | } |
| 699 | |
| 700 | const exact = yield* searchWorkspaceEntries({ |
| 701 | cwd, |
| 702 | query: "src/foo.py", |
| 703 | limit: 25, |
| 704 | kind: "file", |
| 705 | }); |
| 706 | expect(exact.entries.toSorted((a, b) => (a.path < b.path ? -1 : 1))).toEqual( |
| 707 | expected.map((path) => ({ path, kind: "file" })), |
| 708 | ); |
| 709 | |
| 710 | const partial = yield* searchWorkspaceEntries({ cwd, query: "src/fo", limit: 25 }); |
| 711 | for (const path of expected) { |
| 712 | expect(partial.entries.map((entry) => entry.path)).not.toContain(path); |
| 713 | } |
| 714 | }), |
| 715 | ); |
| 716 | |
| 717 | it.effect("releases an index nobody searched within the idle timeout on refresh", () => |
| 718 | Effect.gen(function* () { |
| 719 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-idle-refresh-" }); |
| 720 | yield* writeTextFile(cwd, "src/index.ts"); |
| 721 | let rescans = 0; |
| 722 | let destroyed = 0; |
| 723 | const createFinder = FileFinder.create.bind(FileFinder); |
| 724 | vi.spyOn(FileFinder, "create").mockImplementation((options) => { |
| 725 | const created = createFinder(options); |
| 726 | if (created.ok) { |
| 727 | const finder = created.value; |
| 728 | const scanFilesAsync = finder.scanFilesAsync.bind(finder); |
| 729 | finder.scanFilesAsync = () => { |
| 730 | rescans += 1; |
| 731 | return scanFilesAsync(); |
| 732 | }; |
| 733 | const destroy = finder.destroy.bind(finder); |
| 734 | finder.destroy = () => { |
| 735 | destroyed += 1; |
| 736 | destroy(); |
| 737 | }; |
| 738 | } |
| 739 | return created; |
| 740 | }); |
| 741 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 742 | yield* TestClock.adjust("1 hour"); |
| 743 | yield* searchWorkspaceEntries({ cwd, query: "index", limit: 10 }); |
| 744 | |
| 745 | for (let run = 0; run < 3; run++) { |
| 746 | yield* TestClock.adjust("6 minutes"); |
| 747 | yield* workspaceEntries.refresh(cwd); |
| 748 | } |
| 749 | |
| 750 | expect({ rescans, destroyed }).toEqual({ rescans: 2, destroyed: 1 }); |
| 751 | }).pipe(Effect.provide(TestClock.layer())), |
| 752 | ); |
| 753 | }); |
| 754 | |
| 755 | describe("searchContents", () => { |
| 756 | it.effect("returns content matches with file paths, line numbers, and ranges", () => |
| 757 | Effect.gen(function* () { |
| 758 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-search-" }); |
| 759 | yield* writeTextFile( |
| 760 | cwd, |
| 761 | "src/shapes.ts", |
| 762 | "export const square = 4;\nexport const Square = 16;\nexport const squareSize = 8;\n", |
| 763 | ); |
| 764 | yield* writeTextFile(cwd, "src/other.ts", "const circle = true;\n"); |
| 765 | |
| 766 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 767 | const result = yield* workspaceEntries.searchContents({ |
| 768 | cwd, |
| 769 | query: "Square", |
| 770 | limit: 100, |
| 771 | caseSensitive: false, |
| 772 | wholeWord: true, |
| 773 | useRegex: false, |
| 774 | }); |
| 775 | |
| 776 | expect(result.matches.map((match) => [match.path, match.lineNumber])).toEqual([ |
| 777 | ["src/shapes.ts", 1], |
| 778 | ["src/shapes.ts", 2], |
| 779 | ]); |
| 780 | expect(result.matches[0]?.matchRanges).toEqual([{ start: 13, end: 19 }]); |
| 781 | expect(result.truncated).toBe(false); |
| 782 | }), |
| 783 | ); |
| 784 | |
| 785 | it.effect("honors case sensitivity and gitignore rules", () => |
| 786 | Effect.gen(function* () { |
| 787 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-ignore-", git: true }); |
| 788 | yield* writeTextFile(cwd, ".gitignore", "ignored.txt\n"); |
| 789 | yield* writeTextFile(cwd, "src/keep.ts", "square\nSquare\n"); |
| 790 | yield* writeTextFile(cwd, "ignored.txt", "Square\n"); |
| 791 | yield* writeTextFile(cwd, ".git/info/exclude", "info-excluded.txt\n"); |
| 792 | yield* writeTextFile(cwd, "info-excluded.txt", "Square\n"); |
| 793 | |
| 794 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 795 | const result = yield* workspaceEntries.searchContents({ |
| 796 | cwd, |
| 797 | query: "Square", |
| 798 | limit: 100, |
| 799 | caseSensitive: true, |
| 800 | wholeWord: false, |
| 801 | useRegex: false, |
| 802 | }); |
| 803 | |
| 804 | expect(result.matches).toHaveLength(1); |
| 805 | expect(result.matches[0]).toMatchObject({ path: "src/keep.ts", lineNumber: 2 }); |
| 806 | }), |
| 807 | ); |
| 808 | |
| 809 | it.effect("finds later whole-word matches in a file after rejected raw matches", () => |
| 810 | Effect.gen(function* () { |
| 811 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-late-whole-word-" }); |
| 812 | yield* writeTextFile(cwd, "src/words.ts", `${"afoo\n".repeat(10)}foo\n`); |
| 813 | |
| 814 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 815 | const result = yield* workspaceEntries.searchContents({ |
| 816 | cwd, |
| 817 | query: "foo", |
| 818 | limit: 1, |
| 819 | caseSensitive: true, |
| 820 | wholeWord: true, |
| 821 | useRegex: false, |
| 822 | }); |
| 823 | |
| 824 | expect(result.matches).toEqual([ |
| 825 | expect.objectContaining({ |
| 826 | path: "src/words.ts", |
| 827 | lineNumber: 11, |
| 828 | matchRanges: [{ start: 0, end: 3 }], |
| 829 | }), |
| 830 | ]); |
| 831 | }), |
| 832 | ); |
| 833 | |
| 834 | it.effect("treats astral-plane letters as whole word characters", () => |
| 835 | Effect.gen(function* () { |
| 836 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-astral-word-" }); |
| 837 | yield* writeTextFile(cwd, "src/words.ts", "𐐀foo foo foo𐐀\n"); |
| 838 | |
| 839 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 840 | const result = yield* workspaceEntries.searchContents({ |
| 841 | cwd, |
| 842 | query: "foo", |
| 843 | limit: 100, |
| 844 | caseSensitive: true, |
| 845 | wholeWord: true, |
| 846 | useRegex: false, |
| 847 | }); |
| 848 | |
| 849 | expect(result.matches).toEqual([ |
| 850 | expect.objectContaining({ |
| 851 | path: "src/words.ts", |
| 852 | lineNumber: 1, |
| 853 | matchRanges: [{ start: 6, end: 9 }], |
| 854 | }), |
| 855 | ]); |
| 856 | }), |
| 857 | ); |
| 858 | |
| 859 | it.effect("matches punctuation-edged whole-word queries including adjacent occurrences", () => |
| 860 | Effect.gen(function* () { |
| 861 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-punctuation-" }); |
| 862 | yield* writeTextFile(cwd, "src/words.ts", "-foo- -foo- -foo-\n"); |
| 863 | |
| 864 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 865 | const result = yield* workspaceEntries.searchContents({ |
| 866 | cwd, |
| 867 | query: "-foo-", |
| 868 | limit: 100, |
| 869 | caseSensitive: true, |
| 870 | wholeWord: true, |
| 871 | useRegex: false, |
| 872 | }); |
| 873 | |
| 874 | expect(result.matches).toHaveLength(1); |
| 875 | expect(result.matches[0]).toMatchObject({ |
| 876 | path: "src/words.ts", |
| 877 | lineNumber: 1, |
| 878 | matchRanges: [ |
| 879 | { start: 0, end: 5 }, |
| 880 | { start: 6, end: 11 }, |
| 881 | { start: 12, end: 17 }, |
| 882 | ], |
| 883 | }); |
| 884 | }), |
| 885 | ); |
| 886 | |
| 887 | it.effect("caps matches per file so one dense file cannot fill the page", () => |
| 888 | Effect.gen(function* () { |
| 889 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-per-file-cap-" }); |
| 890 | yield* writeTextFile(cwd, "src/dense.ts", "needle\n".repeat(300)); |
| 891 | yield* writeTextFile(cwd, "src/other.ts", "needle\n"); |
| 892 | |
| 893 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 894 | const result = yield* workspaceEntries.searchContents({ |
| 895 | cwd, |
| 896 | query: "needle", |
| 897 | limit: 500, |
| 898 | caseSensitive: true, |
| 899 | wholeWord: false, |
| 900 | useRegex: false, |
| 901 | }); |
| 902 | |
| 903 | const byPath = new Map<string, number>(); |
| 904 | for (const match of result.matches) { |
| 905 | byPath.set(match.path, (byPath.get(match.path) ?? 0) + 1); |
| 906 | } |
| 907 | expect(byPath.get("src/dense.ts")).toBe(100); |
| 908 | expect(byPath.get("src/other.ts")).toBe(1); |
| 909 | }), |
| 910 | ); |
| 911 | |
| 912 | it.effect("preserves regex escapes during case-insensitive searches", () => |
| 913 | Effect.gen(function* () { |
| 914 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-regex-" }); |
| 915 | yield* writeTextFile(cwd, "src/shapes.ts", "Square\nsquare\n"); |
| 916 | |
| 917 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 918 | const result = yield* workspaceEntries.searchContents({ |
| 919 | cwd, |
| 920 | query: "\\SQUARE", |
| 921 | limit: 100, |
| 922 | caseSensitive: false, |
| 923 | wholeWord: false, |
| 924 | useRegex: true, |
| 925 | }); |
| 926 | |
| 927 | expect(result.matches.map((match) => match.lineNumber)).toEqual([1, 2]); |
| 928 | }), |
| 929 | ); |
| 930 | |
| 931 | it.effect("searches an invalid regex literally without matching case", () => |
| 932 | Effect.gen(function* () { |
| 933 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-invalid-regex-" }); |
| 934 | yield* writeTextFile(cwd, "src/calls.ts", "call(Foo(bar));\n"); |
| 935 | |
| 936 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 937 | const result = yield* workspaceEntries.searchContents({ |
| 938 | cwd, |
| 939 | query: "foo(", |
| 940 | limit: 100, |
| 941 | caseSensitive: false, |
| 942 | wholeWord: false, |
| 943 | useRegex: true, |
| 944 | }); |
| 945 | |
| 946 | expect(result.regexFallbackError).toBeDefined(); |
| 947 | expect(result.matches).toEqual([ |
| 948 | expect.objectContaining({ |
| 949 | path: "src/calls.ts", |
| 950 | lineNumber: 1, |
| 951 | matchRanges: [{ start: 5, end: 9 }], |
| 952 | }), |
| 953 | ]); |
| 954 | }), |
| 955 | ); |
| 956 | |
| 957 | it.effect("maps multi-byte lines to string-indexed ranges", () => |
| 958 | Effect.gen(function* () { |
| 959 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-multibyte-" }); |
| 960 | yield* writeTextFile(cwd, "src/notes.ts", 'const label = "héllo wörld";\n'); |
| 961 | |
| 962 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 963 | const result = yield* workspaceEntries.searchContents({ |
| 964 | cwd, |
| 965 | query: "wörld", |
| 966 | limit: 100, |
| 967 | caseSensitive: true, |
| 968 | wholeWord: false, |
| 969 | useRegex: false, |
| 970 | }); |
| 971 | |
| 972 | expect(result.matches).toHaveLength(1); |
| 973 | const match = result.matches[0]!; |
| 974 | const range = match.matchRanges[0]!; |
| 975 | expect(match.lineContent.slice(range.start, range.end)).toBe("wörld"); |
| 976 | }), |
| 977 | ); |
| 978 | |
| 979 | it.effect("keeps matches past the truncated part of a long line", () => |
| 980 | Effect.gen(function* () { |
| 981 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-long-line-" }); |
| 982 | yield* writeTextFile(cwd, "dist/app.min.js", `${"x".repeat(600)} needleSuffix\n`); |
| 983 | |
| 984 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 985 | const search = (wholeWord: boolean) => |
| 986 | workspaceEntries.searchContents({ |
| 987 | cwd, |
| 988 | query: "needle", |
| 989 | limit: 100, |
| 990 | caseSensitive: true, |
| 991 | wholeWord, |
| 992 | useRegex: false, |
| 993 | }); |
| 994 | |
| 995 | expect((yield* search(false)).matches).toEqual([ |
| 996 | expect.objectContaining({ path: "dist/app.min.js", lineNumber: 1 }), |
| 997 | ]); |
| 998 | expect((yield* search(true)).matches).toEqual([]); |
| 999 | }), |
| 1000 | ); |
| 1001 | |
| 1002 | it.effect("keeps answering from the live index when a refresh outlasts the scan wait", () => |
| 1003 | Effect.gen(function* () { |
| 1004 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-refresh-" }); |
| 1005 | yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n"); |
| 1006 | let scansOutlastWait = false; |
| 1007 | let finders = 0; |
| 1008 | let rescans = 0; |
| 1009 | const createFinder = FileFinder.create.bind(FileFinder); |
| 1010 | vi.spyOn(FileFinder, "create").mockImplementation((options) => { |
| 1011 | const created = createFinder(options); |
| 1012 | if (created.ok) { |
| 1013 | const finder = created.value; |
| 1014 | finders += 1; |
| 1015 | // The synchronous rescan and grep wait on fff's picker lock on the event loop. |
| 1016 | finder.scanFiles = () => { |
| 1017 | throw new Error("refresh must not rescan on the JS thread"); |
| 1018 | }; |
| 1019 | finder.grep = () => { |
| 1020 | throw new Error("search must not grep on the JS thread"); |
| 1021 | }; |
| 1022 | const scanFilesAsync = finder.scanFilesAsync.bind(finder); |
| 1023 | finder.scanFilesAsync = () => { |
| 1024 | rescans += 1; |
| 1025 | return scanFilesAsync(); |
| 1026 | }; |
| 1027 | const waitForIndexReady = finder.waitForIndexReady.bind(finder); |
| 1028 | finder.waitForIndexReady = (timeoutMs) => |
| 1029 | scansOutlastWait |
| 1030 | ? Promise.resolve({ ok: true, value: false }) |
| 1031 | : waitForIndexReady(timeoutMs); |
| 1032 | } |
| 1033 | return created; |
| 1034 | }); |
| 1035 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 1036 | const searchNeedle = workspaceEntries.searchContents({ |
| 1037 | cwd, |
| 1038 | query: "needle", |
| 1039 | limit: 100, |
| 1040 | caseSensitive: true, |
| 1041 | wholeWord: false, |
| 1042 | useRegex: false, |
| 1043 | }); |
| 1044 | yield* searchNeedle; |
| 1045 | |
| 1046 | scansOutlastWait = true; |
| 1047 | yield* workspaceEntries.refresh(cwd); |
| 1048 | const result = yield* searchNeedle; |
| 1049 | |
| 1050 | expect(result.matches.map((match) => match.path)).toEqual(["src/notes.ts"]); |
| 1051 | // The second search ran on the rescanned live finder, not a rebuild. |
| 1052 | expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 }); |
| 1053 | }), |
| 1054 | ); |
| 1055 | |
| 1056 | it.effect("serves content search once a first scan that outlasted the wait finishes", () => |
| 1057 | Effect.gen(function* () { |
| 1058 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-slow-scan-" }); |
| 1059 | yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n"); |
| 1060 | // Real finders; only the initial wait and the progress it reads report a scan still running. |
| 1061 | let scanning = true; |
| 1062 | let finders = 0; |
| 1063 | let settle = (): Promise<unknown> => Promise.resolve(); |
| 1064 | const createFinder = FileFinder.create.bind(FileFinder); |
| 1065 | vi.spyOn(FileFinder, "create").mockImplementation((options) => { |
| 1066 | const created = createFinder(options); |
| 1067 | if (!created.ok) return created; |
| 1068 | const finder = created.value; |
| 1069 | finders += 1; |
| 1070 | const waitForIndexReady = finder.waitForIndexReady.bind(finder); |
| 1071 | settle = () => waitForIndexReady(15_000); |
| 1072 | finder.waitForIndexReady = (timeoutMs) => |
| 1073 | scanning ? Promise.resolve({ ok: true, value: false }) : waitForIndexReady(timeoutMs); |
| 1074 | const getScanProgress = finder.getScanProgressAsync.bind(finder); |
| 1075 | finder.getScanProgressAsync = async () => { |
| 1076 | const progress = await getScanProgress(); |
| 1077 | return scanning && progress.ok |
| 1078 | ? { |
| 1079 | ok: true, |
| 1080 | value: { ...progress.value, isScanning: true, isWarmupComplete: false }, |
| 1081 | } |
| 1082 | : progress; |
| 1083 | }; |
| 1084 | return created; |
| 1085 | }); |
| 1086 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 1087 | const searchNeedle = workspaceEntries |
| 1088 | .searchContents({ |
| 1089 | cwd, |
| 1090 | query: "needle", |
| 1091 | limit: 100, |
| 1092 | caseSensitive: true, |
| 1093 | wholeWord: false, |
| 1094 | useRegex: false, |
| 1095 | }) |
| 1096 | .pipe( |
| 1097 | Effect.map((result): unknown => result.matches.map((match) => match.path)), |
| 1098 | Effect.catchTags({ |
| 1099 | WorkspaceSearchIndexScanTimedOut: (error) => Effect.succeed<unknown>(error._tag), |
| 1100 | }), |
| 1101 | ); |
| 1102 | |
| 1103 | const whileScanning = yield* Effect.all([searchNeedle, searchNeedle], { |
| 1104 | concurrency: "unbounded", |
| 1105 | }); |
| 1106 | scanning = false; |
| 1107 | yield* Effect.promise(() => settle()); |
| 1108 | const afterScan = yield* searchNeedle; |
| 1109 | |
| 1110 | expect({ whileScanning, afterScan, finders }).toEqual({ |
| 1111 | whileScanning: ["WorkspaceSearchIndexScanTimedOut", "WorkspaceSearchIndexScanTimedOut"], |
| 1112 | afterScan: ["src/notes.ts"], |
| 1113 | finders: 1, |
| 1114 | }); |
| 1115 | }), |
| 1116 | ); |
| 1117 | }); |
| 1118 | |
| 1119 | describe("browse", () => { |
| 1120 | it.effect("shows dot directories in directory mode and hidden-prefix mode", () => |
| 1121 | Effect.gen(function* () { |
| 1122 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 1123 | const path = yield* Path.Path; |
| 1124 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-browse-hidden-" }); |
| 1125 | yield* writeTextFile(cwd, ".config/settings.json", "{}"); |
| 1126 | yield* writeTextFile(cwd, "config/settings.json", "{}"); |
| 1127 | const cwdWithSeparator = yield* appendSeparator(cwd); |
| 1128 | |
| 1129 | const directoryResult = yield* workspaceEntries.browse({ |
| 1130 | partialPath: cwdWithSeparator, |
| 1131 | }); |
| 1132 | const hiddenPrefixResult = yield* workspaceEntries.browse({ |
| 1133 | partialPath: `${cwdWithSeparator}.c`, |
| 1134 | }); |
| 1135 | |
| 1136 | expect(directoryResult.entries.map((entry) => entry.name)).toEqual([".config", "config"]); |
| 1137 | expect(hiddenPrefixResult).toEqual({ |
| 1138 | parentPath: cwd, |
| 1139 | entries: [{ name: ".config", fullPath: path.join(cwd, ".config") }], |
| 1140 | }); |
| 1141 | }), |
| 1142 | ); |
| 1143 | |
| 1144 | it.effect("marks only a missing directory as missing when browsing fails", () => |
| 1145 | Effect.gen(function* () { |
| 1146 | const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; |
| 1147 | const path = yield* Path.Path; |
| 1148 | const cwd = yield* makeTempDir({ prefix: "t3code-workspace-browse-missing-" }); |
| 1149 | yield* writeTextFile(cwd, "file.txt", ""); |
| 1150 | |
| 1151 | const missing = yield* workspaceEntries |
| 1152 | .browse({ partialPath: `${path.join(cwd, "absent", "nested")}${path.sep}` }) |
| 1153 | .pipe(Effect.flip); |
| 1154 | const underFile = yield* workspaceEntries |
| 1155 | .browse({ partialPath: `${path.join(cwd, "file.txt")}${path.sep}` }) |
| 1156 | .pipe(Effect.flip); |
| 1157 | |
| 1158 | expect(missing).toMatchObject({ |
| 1159 | _tag: "WorkspaceEntriesReadDirectoryError", |
| 1160 | pathMissing: true, |
| 1161 | }); |
| 1162 | expect(underFile).toMatchObject({ |
| 1163 | _tag: "WorkspaceEntriesReadDirectoryError", |
| 1164 | pathMissing: false, |
| 1165 | }); |
| 1166 | }), |
| 1167 | ); |
| 1168 | }); |
| 1169 | }); |