|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - Executes the emitted Node launcher with a local spawn fixture. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | +import { assert, it } from "@effect/vitest"; |
| 5 | +import { |
| 6 | + HostProcessExecutablePath, |
| 7 | + HostProcessIsExecutable, |
| 8 | + HostProcessPlatform, |
| 9 | +} from "@t3tools/shared/hostProcess"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as FileSystem from "effect/FileSystem"; |
| 12 | +import * as Path from "effect/Path"; |
| 13 | + |
| 14 | +import { ensureAgentDeviceShim } from "./AgentDeviceShim.ts"; |
| 15 | + |
| 16 | +it.effect( |
| 17 | + "sets windowsHide and forwards arguments and exit status in the emitted device launcher", |
| 18 | + () => |
| 19 | + Effect.gen(function* () { |
| 20 | + const fs = yield* FileSystem.FileSystem; |
| 21 | + const path = yield* Path.Path; |
| 22 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-shim-" }); |
| 23 | + const entryPath = path.join(root, "entry with spaces.mjs"); |
| 24 | + const shimDir = yield* ensureAgentDeviceShim({ entryPath, stateDir: root }); |
| 25 | + const preload = path.join(root, "spawn-fixture.cjs"); |
| 26 | + yield* fs.writeFileString( |
| 27 | + preload, |
| 28 | + ` |
| 29 | +const { EventEmitter } = require("node:events"); |
| 30 | +require("node:child_process").spawn = (command, args, options) => { |
| 31 | + console.log(JSON.stringify({ command, args, windowsHide: options.windowsHide, stdio: options.stdio, |
| 32 | + inheritedConfig: options.env.AGENT_DEVICE_CONFIG })); |
| 33 | + const child = new EventEmitter(); |
| 34 | + queueMicrotask(() => child.emit("exit", 23)); |
| 35 | + return child; |
| 36 | +}; |
| 37 | +require("node:module").syncBuiltinESMExports(); |
| 38 | +`, |
| 39 | + ); |
| 40 | + const result = NodeChildProcess.spawnSync( |
| 41 | + process.execPath, |
| 42 | + ["--require", preload, path.join(shimDir, "agent-device-launcher.mjs"), "--help"], |
| 43 | + { |
| 44 | + encoding: "utf8", |
| 45 | + windowsHide: true, |
| 46 | + env: { ...process.env, AGENT_DEVICE_CONFIG: "fixture-config" }, |
| 47 | + }, |
| 48 | + ); |
| 49 | + assert.isUndefined(result.error); |
| 50 | + assert.equal(result.status, 23); |
| 51 | + assert.deepEqual(JSON.parse(result.stdout), { |
| 52 | + command: process.execPath, |
| 53 | + args: [entryPath, "--help"], |
| 54 | + windowsHide: true, |
| 55 | + stdio: "inherit", |
| 56 | + }); |
| 57 | + assert.include( |
| 58 | + yield* fs.readFileString(path.join(shimDir, "agent-device.cmd")), |
| 59 | + "agent-device-launcher.mjs", |
| 60 | + ); |
| 61 | + }).pipe( |
| 62 | + Effect.scoped, |
| 63 | + Effect.provideService(HostProcessPlatform, "win32"), |
| 64 | + Effect.provideService(HostProcessExecutablePath, process.execPath), |
| 65 | + Effect.provideService(HostProcessIsExecutable, false), |
| 66 | + Effect.provide(NodeServices.layer), |
| 67 | + ), |
| 68 | +); |
0 commit comments