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

shell.casing.test.ts

101 lines4.5 KB

1// @effect-diagnostics nodeBuiltinImport:off -- The spawn resolver reads through node:fs.
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { it as effectIt } from "@effect/vitest";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Option from "effect/Option";
8import * as Path from "effect/Path";
9import * as NodeFS from "node:fs";
10import { expect, vi } from "vite-plus/test";
11​
12import {
13 CommandResolutionCache,
14 makePathDirectoryListings,
15 PathDirectoryListings,
16 resolveCommandPath,
17 SpawnExecutableResolution,
18} from "./shell.ts";
19​
20const KELVIN_KIRO = "\u212Airo.Cmd";
21​
22// Each case is a case-sensitive Windows PATH directory: only the listed spellings exist. The spawn
23// resolver keeps listings for the process, so every case gets a directory of its own.
24const cases = [
25 { command: "code", entries: { "code.Cmd": "File" }, expected: "code.Cmd" },
26 { command: "code", entries: { "code.Cmd": "File", "code.cmd": "File" }, expected: "code.cmd" },
27 { command: "kiro", entries: { [KELVIN_KIRO]: "File" }, expected: null },
28 { command: "My Tool", entries: { "My Tool.Cmd": "File" }, expected: "My Tool.Cmd" },
29 {
30 command: "Claude (Work)",
31 entries: { "Claude (Work).Cmd": "File" },
32 expected: "Claude (Work).Cmd",
33 },
34] as const;
35// A fixed mtime keeps the listing current for the whole lookup.
36// @effect-diagnostics-next-line globalDate:off
37const MTIME = Option.some(new Date(0));
38const directoryFor = (testCase: (typeof cases)[number]) => `C:\\cased-${cases.indexOf(testCase)}`;
39​
40effectIt.layer(NodeServices.layer)("case-sensitive Windows PATH directories", (it) => {
41 it.effect.each(cases)("resolveCommandPath finds $expected for $command", (testCase) =>
42 Effect.gen(function* () {
43 const path = yield* Path.Path;
44 const directory = directoryFor(testCase);
45 const entries: Record<string, "File" | "Directory"> = testCase.entries;
46 const missing = FileSystem.makeNoop({});
47 const fileSystem = FileSystem.makeNoop({
48 stat: (filePath) => {
49 const type =
50 filePath === directory ? "Directory" : entries[filePath.slice(directory.length + 1)];
51 return type === undefined
52 ? missing.stat(filePath)
53 : Effect.succeed({ type, mtime: MTIME } as FileSystem.File.Info);
54 },
55 readDirectory: () => Effect.succeed(Object.keys(entries)),
56 });
57 const resolved = yield* resolveCommandPath(testCase.command, {
58 env: { PATH: directory, PATHEXT: ".EXE;.CMD" },
59 }).pipe(
60 Effect.provideService(HostProcess.Platform, "win32"),
61 Effect.provideService(CommandResolutionCache, new Map()),
62 Effect.provideServiceEffect(PathDirectoryListings, makePathDirectoryListings),
63 Effect.provideService(FileSystem.FileSystem, fileSystem),
64 );
65 expect(resolved).toBe(
66 testCase.expected === null ? null : path.join(directory, testCase.expected),
67 );
68 }),
69 );
70​
71 it.effect.each(cases)("the spawn resolver finds $expected for $command", (testCase) =>
72 Effect.gen(function* () {
73 const directory = directoryFor(testCase);
74 const entries: Record<string, "File" | "Directory"> = testCase.entries;
75 const directoryStat = yield* Effect.promise(() => NodeFS.promises.stat(".."));
76 const fileStat = yield* Effect.promise(() => NodeFS.promises.stat(import.meta.filename));
77 using _stat = vi.spyOn(NodeFS.promises, "stat").mockImplementation(async (candidate) => {
78 const type =
79 candidate === directory
80 ? "Directory"
81 : entries[String(candidate).slice(directory.length + 1)];
82 if (type === undefined) throw Object.assign(new Error("not found"), { code: "ENOENT" });
83 return type === "File" ? fileStat : directoryStat;
84 });
85 using _readdir = vi
86 .spyOn(NodeFS.promises, "readdir")
87 .mockResolvedValue(Object.keys(entries) as never);
88 const resolve = SpawnExecutableResolution.defaultValue();
89 const explicit = yield* Effect.promise(async () =>
90 resolve(`${directory}\\${testCase.command}`, "win32", { PATHEXT: ".EXE;.CMD" }),
91 );
92 const found = yield* Effect.promise(async () =>
93 resolve(testCase.command, "win32", { PATH: directory, PATHEXT: ".EXE;.CMD" }),
94 );
95 expect(found).toBe(
96 testCase.expected === null ? undefined : `${directory}\\${testCase.expected}`,
97 );
98 expect(explicit).toBe(found);
99 }),
100 );
101});