260 lines
11 KiB
TypeScript
260 lines
11 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { afterEach, beforeEach, expect, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Option from "effect/Option";
|
|
import * as Path from "effect/Path";
|
|
import * as Sink from "effect/Sink";
|
|
import * as Stream from "effect/Stream";
|
|
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
|
|
|
|
import * as DesktopCliCommand from "./DesktopCliCommand.ts";
|
|
import * as DesktopCliShim from "./DesktopCliShim.ts";
|
|
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
|
|
|
|
const environmentFor = (
|
|
path: Path.Path,
|
|
input: { home: string; baseDir: string; platform?: NodeJS.Platform; isPackaged?: boolean },
|
|
) =>
|
|
DesktopEnvironment.DesktopEnvironment.of({
|
|
path,
|
|
platform: input.platform ?? "linux",
|
|
isPackaged: input.isPackaged ?? true,
|
|
homeDirectory: input.home,
|
|
baseDir: input.baseDir,
|
|
stateDir: path.join(input.baseDir, "userdata"),
|
|
serverRoot: "/opt/T3 Code/resources/app.asar",
|
|
appImagePath: Option.none(),
|
|
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
|
|
|
|
/** The service for a packaged app; the launcher is written on Install. */
|
|
const commandIn = (
|
|
input: { home: string; baseDir?: string; platform?: NodeJS.Platform; isPackaged?: boolean },
|
|
spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"],
|
|
) =>
|
|
Effect.gen(function* () {
|
|
const path = yield* Path.Path;
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const baseDir = input.baseDir ?? path.join(input.home, ".t3");
|
|
yield* fs.makeDirectory(path.join(baseDir, "userdata"), { recursive: true });
|
|
const make = DesktopCliCommand.make.pipe(
|
|
Effect.provideService(
|
|
DesktopEnvironment.DesktopEnvironment,
|
|
environmentFor(path, { ...input, baseDir }),
|
|
),
|
|
);
|
|
return yield* spawner
|
|
? make.pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner))
|
|
: make;
|
|
});
|
|
|
|
/** A PowerShell that keeps the user's PATH in memory and can be told to fail reads. */
|
|
const fakePowerShell = (initial: string) => {
|
|
const registry = { path: initial, failReads: false, writes: 0 };
|
|
const spawner = ChildProcessSpawner.make((command) =>
|
|
Effect.sync(() => {
|
|
const { options } = command as unknown as {
|
|
readonly options: { readonly env?: Record<string, string> };
|
|
};
|
|
const env = options.env ?? {};
|
|
let exitCode = 0;
|
|
let stdout = "";
|
|
if (env.T3_SET === "1") {
|
|
registry.path = env.T3_PATH ?? "";
|
|
registry.writes += 1;
|
|
} else if (registry.failReads) {
|
|
exitCode = 1;
|
|
} else {
|
|
stdout = Buffer.from(registry.path, "utf8").toString("base64");
|
|
}
|
|
return ChildProcessSpawner.makeHandle({
|
|
pid: ChildProcessSpawner.ProcessId(1),
|
|
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
|
|
isRunning: Effect.succeed(false),
|
|
kill: () => Effect.void,
|
|
unref: Effect.succeed(Effect.void),
|
|
stdin: Sink.drain,
|
|
stdout: Stream.make(new TextEncoder().encode(stdout)),
|
|
stderr: Stream.empty,
|
|
all: Stream.empty,
|
|
getInputFd: () => Sink.drain,
|
|
getOutputFd: () => Stream.empty,
|
|
});
|
|
}),
|
|
);
|
|
return { registry, spawner };
|
|
};
|
|
|
|
// The machine's own PATH may already hold a t3; each test sets the PATH it means.
|
|
const machinePath = process.env.PATH;
|
|
beforeEach(() => {
|
|
process.env.PATH = "";
|
|
});
|
|
afterEach(() => {
|
|
process.env.PATH = machinePath;
|
|
});
|
|
|
|
it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
|
|
it.effect("links the launcher onto PATH and removes only that link", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
const command = yield* commandIn({ home });
|
|
const link = path.join(home, ".local", "bin", "t3");
|
|
|
|
expect(yield* command.state).toEqual({ supported: true, installedPath: null, onPath: false });
|
|
// Install writes the launcher itself, even when no local backend ever did.
|
|
const installed = yield* command.install;
|
|
expect(installed.installedPath).toBe(link);
|
|
expect(yield* fs.readLink(link)).toBe(path.join(home, ".t3", "bin", "t3"));
|
|
expect((yield* command.install).installedPath).toBe(link);
|
|
|
|
expect((yield* command.uninstall).installedPath).toBeNull();
|
|
expect(yield* fs.exists(link)).toBe(false);
|
|
// The launcher itself stays for setup commands.
|
|
expect(yield* fs.exists(path.join(home, ".t3", "bin", "t3"))).toBe(true);
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("never replaces or removes a t3 it did not create", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
const command = yield* commandIn({ home });
|
|
const theirs = path.join(home, ".local", "bin", "t3");
|
|
yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
|
|
yield* fs.writeFileString(theirs, "npm's t3\n");
|
|
// Even a broken link in the next folder is someone else's.
|
|
yield* fs.makeDirectory(path.join(home, "bin"), { recursive: true });
|
|
yield* fs.symlink(path.join(home, "gone"), path.join(home, "bin", "t3"));
|
|
|
|
const error = yield* Effect.flip(command.install);
|
|
expect(error.message).toContain("Another t3 command is already installed");
|
|
yield* command.uninstall;
|
|
expect(yield* fs.readFileString(theirs)).toBe("npm's t3\n");
|
|
expect(yield* fs.readLink(path.join(home, "bin", "t3"))).toBe(path.join(home, "gone"));
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("finds and removes a link left by a previous T3 home", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
const before = yield* commandIn({ home, baseDir: path.join(home, "old-t3") });
|
|
const link = (yield* before.install).installedPath;
|
|
|
|
const after = yield* commandIn({ home, baseDir: path.join(home, "new-t3") });
|
|
expect((yield* after.state).installedPath).toBe(link);
|
|
// Installing again points the link at this home's launcher.
|
|
expect((yield* after.install).installedPath).toBe(link);
|
|
expect(yield* fs.readLink(link!)).toBe(path.join(home, "new-t3", "bin", "t3"));
|
|
yield* after.uninstall;
|
|
expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
|
|
expect(yield* fs.exists(path.join(home, "bin", "t3"))).toBe(false);
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("does not read a large binary another t3 links to", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
// A big executable that happens to contain the marker text is still not ours.
|
|
const binary = path.join(home, "native-t3");
|
|
yield* fs.writeFileString(binary, `${"\0".repeat(64 * 1024)}${DesktopCliShim.MARKER}`);
|
|
yield* fs.makeDirectory(path.join(home, ".local", "bin"), { recursive: true });
|
|
yield* fs.symlink(binary, path.join(home, ".local", "bin", "t3"));
|
|
const command = yield* commandIn({ home });
|
|
expect((yield* command.state).installedPath).toBeNull();
|
|
yield* command.uninstall;
|
|
expect(yield* fs.readLink(path.join(home, ".local", "bin", "t3"))).toBe(binary);
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("refuses to install behind another t3 that runs first", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
const shadow = path.join(home, "shadow");
|
|
yield* fs.makeDirectory(shadow);
|
|
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
|
|
process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
|
|
|
|
const command = yield* commandIn({ home });
|
|
const theirs = path.join(shadow, "t3");
|
|
expect((yield* command.state).shadowedBy).toBe(theirs);
|
|
// A link behind it would never run, so nothing is created.
|
|
const error = yield* Effect.flip(command.install);
|
|
expect(error.message).toContain(theirs);
|
|
expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
|
|
|
|
yield* fs.remove(theirs);
|
|
const installed = yield* command.install;
|
|
expect(installed).toMatchObject({
|
|
onPath: true,
|
|
installedPath: path.join(home, ".local", "bin", "t3"),
|
|
});
|
|
expect(installed.shadowedBy).toBeUndefined();
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("leaves the Windows PATH alone when it cannot be read", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
const userPath = "C:\\Users\\Me\\npm;%USERPROFILE%\\tools";
|
|
const { registry, spawner } = fakePowerShell(userPath);
|
|
const command = yield* commandIn({ home, platform: "win32" }, spawner);
|
|
|
|
registry.failReads = true;
|
|
const error = yield* Effect.flip(command.install);
|
|
expect(error.message).toContain("left unchanged");
|
|
expect(registry).toMatchObject({ path: userPath, writes: 0 });
|
|
|
|
registry.failReads = false;
|
|
const launcherDir = DesktopCliShim.launcherPath(
|
|
environmentFor(yield* Path.Path, { home, baseDir: `${home}/.t3`, platform: "win32" }),
|
|
).replace(/[\\/]t3\.cmd$/, "");
|
|
yield* command.install;
|
|
expect(registry.path).toBe(`${userPath};${launcherDir}`);
|
|
yield* command.uninstall;
|
|
expect(registry.path).toBe(userPath);
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("does not claim or remove a Windows PATH entry the user added", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const home = yield* fs.makeTempDirectoryScoped();
|
|
const launcherDir = path.dirname(
|
|
DesktopCliShim.launcherPath(
|
|
environmentFor(path, { home, baseDir: path.join(home, ".t3"), platform: "win32" }),
|
|
),
|
|
);
|
|
const userPath = `C:\\Tools;${launcherDir}`;
|
|
const { registry, spawner } = fakePowerShell(userPath);
|
|
const command = yield* commandIn({ home, platform: "win32" }, spawner);
|
|
|
|
expect((yield* command.state).installedPath).toBeNull();
|
|
yield* command.uninstall;
|
|
expect(registry.path).toBe(userPath);
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
|
|
it.effect("offers nothing for a development build", () =>
|
|
Effect.gen(function* () {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const command = yield* commandIn({
|
|
home: yield* fs.makeTempDirectoryScoped(),
|
|
isPackaged: false,
|
|
});
|
|
expect((yield* command.state).supported).toBe(false);
|
|
}).pipe(Effect.scoped),
|
|
);
|
|
});
|