// @effect-diagnostics nodeBuiltinImport:off - the Windows launch smoke tests use filesystem notifications to observe real PowerShell launches.
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Deferred from "effect/Deferred";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as TestClock from "effect/testing/TestClock";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Option from "effect/Option";
import * as PlatformError from "effect/PlatformError";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
import { EDITORS } from "@t3tools/contracts";
import { resolveEditorCommand } from "@t3tools/shared/editor";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { SpawnExecutableResolution } from "@t3tools/shared/shell";
import * as ExternalLauncher from "./externalLauncher.ts";
// Windows drive letters break mocked POSIX PATH splitting; these tests also require /bin/sh stubs.
const windowsHost = HostProcess.Platform.defaultValue() === "win32";
interface MockSpawnResult {
readonly spawnError?: PlatformError.PlatformError;
readonly exitCode?: number;
readonly stdout?: string;
readonly stall?: boolean;
}
function makeMockDetachedHandle(input: MockSpawnResult & { readonly onUnref?: () => void } = {}) {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: input.stall
? Effect.never
: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
isRunning: Effect.succeed(true),
kill: () => Effect.void,
unref: Effect.sync(() => {
input.onUnref?.();
return Effect.void;
}),
stdin: Sink.drain,
stdout:
input.stdout === undefined
? Stream.empty
: Stream.make(new TextEncoder().encode(input.stdout)),
stderr: Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
});
}
const layerTest = (input: {
readonly platform: NodeJS.Platform;
readonly env?: Record<string, string>;
readonly resolveExecutable?: (command: string) => string | undefined;
readonly onSpawn?: (command: ChildProcess.StandardCommand) => void;
readonly onUnref?: () => void;
readonly spawnResult?: (command: ChildProcess.StandardCommand) => MockSpawnResult | undefined;
}) => {
const layerSpawner = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) =>
Effect.gen(function* () {
assert.equal(ChildProcess.isStandardCommand(command), true);
if (!ChildProcess.isStandardCommand(command)) {
throw new Error("Expected a standard command");
}
input.onSpawn?.(command);
const result = input.spawnResult?.(command);
if (result?.spawnError) return yield* result.spawnError;
return makeMockDetachedHandle({
...(input.onUnref === undefined ? {} : { onUnref: input.onUnref }),
...result,
});
}),
),
);
return Layer.mergeAll(
ExternalLauncher.layer.pipe(Layer.provide(Layer.merge(NodeServices.layer, layerSpawner))),
Layer.succeed(HostProcess.Platform, input.platform),
Layer.succeed(
SpawnExecutableResolution,
(command) => input.resolveExecutable?.(command) ?? command,
),
ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.env ?? {} })),
);
};
it.effect.skipIf(windowsHost)(
"launched browsers and editors do not inherit ELECTRON_RUN_AS_NODE",
() =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
const codePath = path.join(binDir, "code");
yield* fileSystem.writeFileString(codePath, "#!/bin/sh\n");
yield* fileSystem.chmod(codePath, 0o755);
const spawned: ChildProcess.StandardCommand[] = [];
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.launchBrowser("https://example.com");
yield* launcher.launchEditor({ editor: "vscode", cwd: "/workspace" });
}).pipe(
Effect.provide(
layerTest({
platform: "linux",
env: { PATH: binDir, DISPLAY: ":0" },
onSpawn: (command) => spawned.push(command),
}),
),
Effect.provideService(HostProcess.Environment, {
PATH: binDir,
DISPLAY: ":0",
ELECTRON_RUN_AS_NODE: "1",
}),
);
assert.deepEqual(
spawned.map((command) => ({
command: command.command,
env: command.options.env,
extendEnv: command.options.extendEnv,
})),
["xdg-open", "code"].map((command) => ({
command,
env: { PATH: binDir, DISPLAY: ":0" },
extendEnv: false,
})),
);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
// Windows drive letters cannot resolve the POSIX editor stub.
it.effect.skipIf(windowsHost)(
"a Linux editor launch without a display takes the desktop session's display from the systemd user manager",
() =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
const codiumPath = path.join(binDir, "codium");
yield* fileSystem.writeFileString(codiumPath, "#!/bin/sh\n");
yield* fileSystem.chmod(codiumPath, 0o755);
const launchEnv = (sessionManager: MockSpawnResult) =>
Effect.gen(function* () {
const spawned: ChildProcess.StandardCommand[] = [];
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.launchEditor({ editor: "vscodium", cwd: "/workspace" });
yield* launcher.launchBrowser("https://example.com");
}).pipe(
Effect.provide(
layerTest({
platform: "linux",
env: { PATH: binDir },
onSpawn: (command) => spawned.push(command),
spawnResult: (command) =>
command.command === "busctl" ? sessionManager : undefined,
}),
),
Effect.provideService(HostProcess.Environment, { PATH: binDir }),
);
// Only editors borrow the session display; a browser must not open on that screen.
assert.deepEqual(spawned.find((command) => command.command === "xdg-open")?.options.env, {
PATH: binDir,
});
return spawned.find((command) => command.command === "codium")?.options.env;
});
assert.deepEqual(
yield* launchEnv({
stdout: JSON.stringify({
type: "as",
data: [
"DISPLAY=:0",
"HOME=/home/user",
"WAYLAND_DISPLAY=wayland-1",
"XAUTHORITY=/home/user/auth files/cookie",
],
}),
}),
{
PATH: binDir,
DISPLAY: ":0",
WAYLAND_DISPLAY: "wayland-1",
// Raw, not the