Repository navigation
Expand file tree
/
Copy pathcross-environment.test.ts
More file actions
350 lines (326 loc) · 16.1 KB
/
Copy pathcross-environment.test.ts
File metadata and controls
350 lines (326 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// @ts-nocheck
// Cross-environment conformance suite (#1990).
//
// One definition of "works" that runs IDENTICALLY on ubuntu-latest /
// windows-latest / macos-latest via the `Cross-environment` CI matrix
// (.github/workflows/ci.yml). The same assertions execute on every OS; where a
// branch can only be exercised on one platform, it runs there for real and is
// an EXPLICIT, reasoned skip elsewhere (printed below) — never a silent no-op.
//
// Covers the platform-divergent logic that has actually bitten the packaged
// app:
// - coven shim launch resolution (the #2011 spawn-EINVAL class)
// - sidecar native target mapping (the #2010 sharp-prune class)
// - path / line-ending / env semantics
//
// Neutral defaults and per-OS deltas are documented in docs/cross-environment.md.
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { resolveSidecarTarget } from "./sidecar-target.mjs";
import { covenLaunchCommandForBinary } from "../src/lib/coven-bin.ts";
import { tailnetDiscoveryProof } from "../src/lib/mobile-handoff.ts";
import { openCodeCommand, openCodeLaunch, openCodeNeedsTmpRuntimeDir } from "../src/lib/opencode-bin.ts";
const skips: string[] = [];
function skip(reason: string): void {
skips.push(reason);
console.log(` ↷ skipped: ${reason}`);
}
// ---------------------------------------------------------------------------
// Contract A — sidecar native target resolution (pure; identical on every OS).
// Mirrors scripts/sidecar-bundle.sh's prune, which now consumes this same
// module, so these assertions pin the release prune on all three runners.
// ---------------------------------------------------------------------------
{
assert.deepEqual(resolveSidecarTarget({ platform: "darwin", arch: "arm64" }), {
supported: true,
platform: "darwin",
arch: "arm64",
libc: "",
target: "darwin-arm64",
nextPkg: "@next/swc-darwin-arm64",
sharpPkg: "@img/sharp-darwin-arm64",
sharpVipsPkg: "@img/sharp-libvips-darwin-arm64",
nodePtyPrebuild: "darwin-arm64",
keepFsevents: true,
});
assert.equal(resolveSidecarTarget({ platform: "darwin", arch: "x64" }).sharpPkg, "@img/sharp-darwin-x64");
// linux glibc vs musl diverge in BOTH the sharp and @next/swc package names.
const gnu = resolveSidecarTarget({ platform: "linux", arch: "x64", libc: "gnu" });
assert.equal(gnu.nextPkg, "@next/swc-linux-x64-gnu");
assert.equal(gnu.sharpPkg, "@img/sharp-linux-x64");
assert.equal(gnu.sharpVipsPkg, "@img/sharp-libvips-linux-x64");
assert.equal(gnu.keepFsevents, false);
const musl = resolveSidecarTarget({ platform: "linux", arch: "arm64", libc: "musl" });
assert.equal(musl.nextPkg, "@next/swc-linux-arm64-musl");
assert.equal(musl.sharpPkg, "@img/sharp-linuxmusl-arm64");
assert.equal(musl.sharpVipsPkg, "@img/sharp-libvips-linuxmusl-arm64");
// win32: @next/swc carries the -msvc suffix and sharp bundles libvips inside
// the platform package (no separate @img/sharp-libvips-win32-*).
const win = resolveSidecarTarget({ platform: "win32", arch: "x64" });
assert.equal(win.nextPkg, "@next/swc-win32-x64-msvc");
assert.equal(win.sharpPkg, "@img/sharp-win32-x64");
assert.equal(win.sharpVipsPkg, "");
// Unsupported platforms must report `supported: false` so the prune bails
// (and leaves native packages intact) rather than guessing a target.
assert.equal(resolveSidecarTarget({ platform: "sunos", arch: "x64" }).supported, false);
}
// Host-reality: on the three matrix OSes, the *running* host must resolve to a
// supported target — this is the assertion that genuinely differs per runner.
{
const host = resolveSidecarTarget({
platform: process.platform,
arch: process.arch,
libc: process.platform === "linux" ? "gnu" : "",
});
if (["darwin", "linux", "win32"].includes(process.platform)) {
assert.equal(host.supported, true, `host ${process.platform}/${process.arch} must resolve to a sidecar target`);
assert.ok(host.sharpPkg?.startsWith("@img/sharp-"), "host sharp package is an @img native binary");
} else {
skip(`sidecar host-target assertion: unsupported CI platform ${process.platform} (matrix covers darwin/linux/win32)`);
}
}
// ---------------------------------------------------------------------------
// Contract B — coven launch command resolution (the #2011 .cmd-spawn class).
// The forced-platform table runs identically on every OS; the host branch
// exercises the REAL process.platform path on each runner.
// ---------------------------------------------------------------------------
{
// Forced POSIX: launch the resolved binary directly.
assert.deepEqual(
covenLaunchCommandForBinary("/usr/local/bin/coven", "darwin"),
{ command: "/usr/local/bin/coven", fixedArgs: [] },
"posix launches the coven binary directly",
);
assert.deepEqual(
covenLaunchCommandForBinary("/usr/bin/coven", "linux"),
{ command: "/usr/bin/coven", fixedArgs: [] },
"linux launches the coven binary directly",
);
// Forced win32 with a realistic npm .cmd shim → node + the resolved script.
// This runs on every OS (the platform is forced), proving the shim-parse path
// is not Windows-host-dependent.
const shimDir = mkdtempSync(path.join(os.tmpdir(), "coven-conf-shim-"));
const shimScript = path.join(shimDir, "node_modules", "@opencoven", "cli", "bin", "coven.js");
mkdirSync(path.dirname(shimScript), { recursive: true });
writeFileSync(shimScript, "console.log('coven');\n");
const shim = path.join(shimDir, "coven.cmd");
writeFileSync(
shim,
[
"@ECHO off",
"SETLOCAL",
"CALL :find_dp0",
'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\\node_modules\\@opencoven\\cli\\bin\\coven.js" %*',
"",
].join("\r\n"),
);
const resolvedShim = covenLaunchCommandForBinary(shim, "win32");
assert.equal(
resolvedShim.command,
process.execPath,
"win32 .cmd shims launch through node (never spawned directly — CVE-2024-27980 EINVAL)",
);
assert.deepEqual(
resolvedShim.fixedArgs?.map((target) => statSync(target).ino),
[statSync(shimScript).ino],
"win32 .cmd shims launch their resolved script, including through equivalent macOS /var paths",
);
// Host branch — the genuinely per-OS assertion.
// Coven Code's npm target is extensionless. It must resolve from its own
// shim, never by inferring the Coven CLI's conventional JavaScript path.
const codeShimScript = path.join(shimDir, "node_modules", "@opencoven", "coven-code", "bin", "coven-code");
mkdirSync(path.dirname(codeShimScript), { recursive: true });
writeFileSync(codeShimScript, "console.log('coven-code');\n");
const codeShim = path.join(shimDir, "coven-code.cmd");
writeFileSync(
codeShim,
'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\\node_modules\\@opencoven\\coven-code\\bin\\coven-code" %*\r\n',
);
const resolvedCodeShim = covenLaunchCommandForBinary(codeShim, "win32");
assert.equal(resolvedCodeShim.command, process.execPath, "win32 Coven Code shims launch through node");
assert.deepEqual(
resolvedCodeShim.fixedArgs?.map((target) => statSync(target).ino),
[statSync(codeShimScript).ino],
"win32 npm shims resolve extensionless Coven Code targets from their own package",
);
const missingShim = path.join(shimDir, "missing.cmd");
assert.deepEqual(
covenLaunchCommandForBinary(missingShim, "win32"),
{ command: missingShim, fixedArgs: [], unresolvedWindowsShim: true },
"unreadable Windows shims are explicit unknown targets rather than another package's fallback",
);
if (process.platform === "win32") {
// On a real Windows runner, a .cmd path must resolve to node + script using
// the host's actual filesystem + path semantics.
const real = covenLaunchCommandForBinary(shim);
assert.equal(real.command, process.execPath, "Windows host resolves .cmd shim to node");
assert.deepEqual(real.fixedArgs, [shimScript], "Windows host resolves the shim's target script");
} else {
// On macOS / Linux the resolved binary is launched directly, identity.
assert.deepEqual(
covenLaunchCommandForBinary("/usr/local/bin/coven"),
{ command: "/usr/local/bin/coven", fixedArgs: [] },
"posix host launches the coven binary directly",
);
skip("coven .cmd-shim host resolution: requires a Windows host (matrix runs it on windows-latest)");
}
}
// ---------------------------------------------------------------------------
// Contract B2 — OpenCode direct-launch environment. The executable is shared
// across desktop platforms; only the POSIX XDG runtime-dir setup diverges.
// Keep Windows, Linux/WSL, and macOS decisions executable here even while the
// hosted macOS PR matrix is suspended for Actions-minute capacity.
// ---------------------------------------------------------------------------
{
assert.equal(openCodeCommand(), "opencode", "OpenCode keeps one executable name across desktop platforms");
const hostileArgs = ["run", "--format", "json"];
const hostileInput = `${"x".repeat(40_000)}
review 😀
quotes: "double" 'single'
slashes: C:\\Program Files\\OpenCode\\bin
shell: ; rm -rf ~ | $(evil) && echo "%PATH%" > pwned <'quote\``;
const simulatedShim = "C:\\npm\\opencode.cmd";
const simulatedTarget = "C:\\npm\\node_modules\\opencode-ai\\bin\\opencode.exe";
const windowsLaunch = openCodeLaunch(
hostileArgs,
"win32",
{ Path: "C:\\npm", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
{
statFile: (candidate) => candidate === simulatedShim,
resolveWindowsShim: (candidate) => {
assert.equal(candidate, simulatedShim);
return { command: simulatedTarget, fixedArgs: [] };
},
},
);
assert.equal(
windowsLaunch.command,
simulatedTarget,
"Windows resolves the npm command shim to its native package target",
);
assert.deepEqual(
windowsLaunch.args,
hostileArgs,
"Windows keeps the complete option argv as direct child-process data",
);
assert.doesNotMatch(
windowsLaunch.command,
/\.(?:cmd|bat|ps1)$/i,
"Windows never executes a shell shim between Cave and OpenCode stdin",
);
if (process.platform === "win32") {
// Execute the REAL npm .cmd resolution → direct native process chain. Cave
// reads the shim only to prove its package target, then bypasses every
// shell re-parse boundary while a >40K prompt travels over stdin.
const openCodeDir = mkdtempSync(path.join(os.tmpdir(), "opencode-conf-launch-"));
try {
writeFileSync(
path.join(openCodeDir, "opencode-shim.mjs"),
[
"let input = \"\";",
"process.stdin.setEncoding(\"utf8\");",
"for await (const chunk of process.stdin) input += chunk;",
"console.log(JSON.stringify({ args: process.argv.slice(2), input }));",
].join("\n"),
);
writeFileSync(
path.join(openCodeDir, "opencode.cmd"),
'"%dp0%\\node.exe" "%dp0%\\opencode-shim.mjs" %*\r\n',
);
const pathKey = Object.keys(process.env).find((key) => key.toLowerCase() === "path") ?? "Path";
const inheritedPath = process.env[pathKey] ?? "";
const env = {
...process.env,
[pathKey]: `${openCodeDir};${inheritedPath}`,
};
const realLaunch = openCodeLaunch(hostileArgs, process.platform, env);
const result = spawnSync(realLaunch.command, realLaunch.args, {
env,
encoding: "utf8",
input: hostileInput,
windowsHide: true,
});
assert.equal(
result.status,
0,
`Windows OpenCode launch failed:\n${result.stderr}\n${result.stdout}`,
);
assert.deepEqual(
JSON.parse(result.stdout.trim()),
{ args: hostileArgs, input: hostileInput },
"the resolved direct process preserves option argv and >40K multiline Unicode stdin exactly",
);
} finally {
rmSync(openCodeDir, { recursive: true, force: true });
}
} else {
skip("OpenCode Windows shim resolution: requires a Windows host (matrix runs it on windows-latest)");
}
assert.equal(openCodeNeedsTmpRuntimeDir("win32", {}), false, "Windows does not receive an XDG runtime directory");
assert.equal(openCodeNeedsTmpRuntimeDir("linux", {}), true, "headless Linux receives /tmp for OpenCode runtime files");
assert.equal(openCodeNeedsTmpRuntimeDir("linux", { XDG_RUNTIME_DIR: "/run/user/1000" }), false, "native Linux preserves its XDG runtime directory");
assert.equal(openCodeNeedsTmpRuntimeDir("linux", { WSL_INTEROP: "/run/WSL/1_interop", XDG_RUNTIME_DIR: "/run/user/1000" }), true, "WSL overrides a stale inherited XDG runtime directory");
assert.equal(openCodeNeedsTmpRuntimeDir("darwin", {}), true, "headless macOS receives /tmp for OpenCode runtime files");
assert.equal(openCodeNeedsTmpRuntimeDir("darwin", { XDG_RUNTIME_DIR: "/var/folders/runtime" }), false, "native macOS preserves its XDG runtime directory");
}
// ---------------------------------------------------------------------------
// Contract C — path / line-ending semantics that diverge per OS, asserted for
// real against the running platform.
// ---------------------------------------------------------------------------
{
if (process.platform === "win32") {
assert.equal(path.sep, "\\", "win32 path separator is backslash");
assert.equal(path.delimiter, ";", "win32 PATH delimiter is semicolon");
assert.equal(os.EOL, "\r\n", "win32 line ending is CRLF");
} else {
assert.equal(path.sep, "/", "posix path separator is slash");
assert.equal(path.delimiter, ":", "posix PATH delimiter is colon");
assert.equal(os.EOL, "\n", "posix line ending is LF");
}
// PATH splitting must use the platform delimiter (not a hard-coded ":"), or
// Windows PATH entries collapse into one bogus path — the bug coven-bin.ts
// guards against. Verify the contract end-to-end with a constructed PATH.
const entries = ["a", "b", "c"];
const joined = entries.join(path.delimiter);
assert.deepEqual(joined.split(path.delimiter), entries, "PATH round-trips through the platform delimiter");
}
// ---------------------------------------------------------------------------
// Contract D — live Tailscale/MagicDNS host discovery when the runner has a
// connected Tailscale daemon. CI runners are not joined to a private tailnet, so
// they print an explicit skip; developer/release hosts that have Tailscale
// available prove the real platform network stack instead of silently no-oping.
// ---------------------------------------------------------------------------
{
const tailscale = spawnSync(
process.env.TAILSCALE_BIN || "tailscale",
["status", "--self", "--json"],
{ encoding: "utf8", timeout: 8000 },
);
if (tailscale.error) {
skip(`Tailscale/MagicDNS host discovery: tailscale CLI unavailable (${tailscale.error.message})`);
} else if (tailscale.status !== 0) {
const detail = (tailscale.stderr || tailscale.stdout || `exit ${tailscale.status}`).trim().split(/\r?\n/)[0];
skip(`Tailscale/MagicDNS host discovery: tailscale status unavailable (${detail || `exit ${tailscale.status}`})`);
} else {
let selfStatus: unknown;
try {
selfStatus = JSON.parse(tailscale.stdout);
} catch (err) {
assert.fail(`tailscale status --self --json returned invalid JSON: ${err instanceof Error ? err.message : String(err)}`);
}
const proof = tailnetDiscoveryProof({
selfStatus,
serveStatus: {},
backendUrl: "http://127.0.0.1:3000",
});
assert.equal(proof.ok, true, proof.ok ? undefined : proof.reason);
assert.equal(proof.source, "magicdns-self-status", "live Tailscale proof should derive from status --self MagicDNS");
assert.match(proof.host, /\.ts\.net$/i, "live MagicDNS host must be a Tailscale .ts.net name");
assert.equal(proof.serveUrl, `https://${proof.host}/`, "MagicDNS serve URL is derived from the live host");
console.log(" ✓ Tailscale/MagicDNS host discovery: live status --self proof");
}
}
console.log(`cross-environment.test.ts: ok on ${process.platform}/${process.arch} (${skips.length} explicit skip(s))`);