codewhale 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,7 +84,7 @@ test("install failure hint explains release base override for blocked GitHub dow
84
84
  try {
85
85
  const error = Object.assign(
86
86
  new Error(
87
- "fetch https://github.com/Hmbown/CodeWhale/releases/download/v0.8.19/codewhale-artifacts-sha256.txt failed after 5 attempts:\ngetaddrinfo ENOTFOUND github.com",
87
+ "fetch https://github.com/codewhale-hq/CodeWhale/releases/download/v0.8.19/codewhale-artifacts-sha256.txt failed after 5 attempts:\ngetaddrinfo ENOTFOUND github.com",
88
88
  ),
89
89
  { code: "ENOTFOUND" },
90
90
  );
@@ -129,13 +129,13 @@ test("install failure hint checks configured release base when override is alrea
129
129
  test("glibc preflight message is Codewhale-branded and actionable", () => {
130
130
  const message = glibcInternal.glibcCompatibilityMessage([2, 39, 0], [2, 35, 0]);
131
131
 
132
- assert.match(message, /Prebuilt Codewhale Linux binaries require GLIBC_2\.39/);
132
+ assert.match(message, /This Codewhale binary requires GLIBC_2\.39/);
133
133
  assert.match(message, /this system has glibc 2\.35/);
134
134
  assert.match(message, /cargo install codewhale-cli --locked/);
135
135
  assert.match(message, /ln -sf .*codewhale.*codew/);
136
136
  assert.doesNotMatch(message, /cargo install codewhale-tui/);
137
- assert.match(message, /Linux x64 release asset is a static \(musl\) build/);
138
- assert.match(message, /Linux arm64 asset is a GNU libc build/);
137
+ assert.match(message, /Linux release assets \(x64 and arm64\) are static musl builds/);
138
+ assert.doesNotMatch(message, /GNU libc/);
139
139
  assert.match(message, /CODEWHALE_SKIP_GLIBC_CHECK=1/);
140
140
  });
141
141
 
@@ -184,7 +184,7 @@ test("ensureBinary adopts a manually placed target binary after checksum validat
184
184
  await fs.promises.writeFile(`${target}.version`, "0.8.24", "utf8");
185
185
 
186
186
  const result = await withoutForcedDownload(() =>
187
- _internal.ensureBinary(target, assetName, version, "Hmbown/CodeWhale", async () => {
187
+ _internal.ensureBinary(target, assetName, version, "codewhale-hq/CodeWhale", async () => {
188
188
  checksumLoads += 1;
189
189
  return new Map([[assetName, sha256(content)]]);
190
190
  }),
@@ -209,7 +209,7 @@ test("ensureBinary adopts an official release-named binary placed in downloads",
209
209
  await fs.promises.writeFile(assetPath, content);
210
210
 
211
211
  const result = await withoutForcedDownload(() =>
212
- _internal.ensureBinary(target, assetName, version, "Hmbown/CodeWhale", async () =>
212
+ _internal.ensureBinary(target, assetName, version, "codewhale-hq/CodeWhale", async () =>
213
213
  new Map([[assetName, sha256(content)]]),
214
214
  ),
215
215
  );
@@ -346,3 +346,109 @@ test("httpRequest handles invalid URL parsing errors", async () => {
346
346
  assert.match(err.message, /Invalid URL: not-a-valid-url/);
347
347
  }
348
348
  });
349
+
350
+ // A server that sends headers and part of a body, then either
351
+ // goes silent or keeps trickling bytes, and never ends the response.
352
+ async function withStallingServer(t, { trickleMs = 0 } = {}) {
353
+ const http = require("node:http");
354
+ const timers = [];
355
+ const server = http.createServer((req, res) => {
356
+ res.writeHead(200, { "Content-Type": "application/octet-stream" });
357
+ res.write("partial body");
358
+ if (trickleMs > 0) {
359
+ timers.push(setInterval(() => res.write("."), trickleMs));
360
+ }
361
+ });
362
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
363
+ t.after(() => {
364
+ for (const timer of timers) clearInterval(timer);
365
+ server.closeAllConnections();
366
+ return new Promise((resolve) => server.close(resolve));
367
+ });
368
+ // Keep a developer's proxy variables from routing the loopback request.
369
+ const noProxy = process.env.NO_PROXY;
370
+ process.env.NO_PROXY = "*";
371
+ t.after(() => {
372
+ if (noProxy === undefined) delete process.env.NO_PROXY;
373
+ else process.env.NO_PROXY = noProxy;
374
+ });
375
+ return `http://127.0.0.1:${server.address().port}/asset`;
376
+ }
377
+
378
+ // Consume a handed-off body the way `downloadText` and `streamToFile` do:
379
+ // resolve on `end`, reject on `error`.
380
+ function consumeBody(response) {
381
+ return new Promise((resolve, reject) => {
382
+ response.on("data", () => {});
383
+ response.on("end", resolve);
384
+ response.on("error", reject);
385
+ response.resume();
386
+ });
387
+ }
388
+
389
+ test("a body that stalls after the headers fails with the stall timeout", { timeout: 5000 }, async (t) => {
390
+ const url = await withStallingServer(t);
391
+ const { response } = await _internal.httpRequest(url, {
392
+ stallMs: 150,
393
+ totalTimeoutMs: 30000,
394
+ });
395
+ await assert.rejects(consumeBody(response), (error) => {
396
+ assert.equal(error.code, "EDOWNLOADTIMEOUT");
397
+ assert.match(error.message, /stalled/);
398
+ return true;
399
+ });
400
+ });
401
+
402
+ test("a body that trickles past the total budget fails with the total timeout", { timeout: 5000 }, async (t) => {
403
+ const url = await withStallingServer(t, { trickleMs: 40 });
404
+ const { response } = await _internal.httpRequest(url, {
405
+ stallMs: 1000,
406
+ totalTimeoutMs: 400,
407
+ });
408
+ await assert.rejects(consumeBody(response), (error) => {
409
+ assert.equal(error.code, "EDOWNLOADTIMEOUT");
410
+ assert.match(error.message, /total timeout/);
411
+ return true;
412
+ });
413
+ });
414
+
415
+ test("a body paused by a slow consumer is not reported as a stall", { timeout: 5000 }, async (t) => {
416
+ const http = require("node:http");
417
+ const server = http.createServer((req, res) => {
418
+ res.writeHead(200, { "Content-Type": "application/octet-stream" });
419
+ res.write("first chunk ");
420
+ setTimeout(() => res.end("rest of the body"), 50);
421
+ });
422
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
423
+ t.after(() => {
424
+ server.closeAllConnections();
425
+ return new Promise((resolve) => server.close(resolve));
426
+ });
427
+ const noProxy = process.env.NO_PROXY;
428
+ process.env.NO_PROXY = "*";
429
+ t.after(() => {
430
+ if (noProxy === undefined) delete process.env.NO_PROXY;
431
+ else process.env.NO_PROXY = noProxy;
432
+ });
433
+ const { response } = await _internal.httpRequest(
434
+ `http://127.0.0.1:${server.address().port}/asset`,
435
+ { stallMs: 150, totalTimeoutMs: 30000 },
436
+ );
437
+ // Hold the stream paused for several stall budgets, as `pipe` does while a
438
+ // slow disk drains, then let it flow again.
439
+ let body = "";
440
+ const done = new Promise((resolve, reject) => {
441
+ response.on("data", (chunk) => {
442
+ body += chunk;
443
+ });
444
+ response.on("end", resolve);
445
+ response.on("error", reject);
446
+ });
447
+ response.resume();
448
+ response.once("data", () => {
449
+ response.pause();
450
+ setTimeout(() => response.resume(), 600);
451
+ });
452
+ await done;
453
+ assert.equal(body, "first chunk rest of the body");
454
+ });
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+ const { test } = require('node:test');
3
+ const assert = require('node:assert/strict');
4
+ const fs = require('node:fs');
5
+ const path = require('node:path');
6
+ const os = require('node:os');
7
+ const crypto = require('node:crypto');
8
+ const { spawnSync } = require('node:child_process');
9
+ const script = path.resolve(__dirname, '../../../scripts/compiled-host-native-receipt.py');
10
+ const identity = 'extension_host::tests::compiled_native_host_cannot_read_secrets_or_write_outside_its_data_dir';
11
+ const memoryIdentity = 'extension_host::tests::compiled_native_host_memory_cap_is_enforced';
12
+ const digest = (bytes) => crypto.createHash('sha256').update(bytes).digest('hex');
13
+ const escape = (text) => text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
14
+ const marker = `compiled-native-containment=passed platform=${{darwin:'macos',win32:'windows',linux:'linux'}[process.platform]} arch=${{x64:'x86_64',arm64:'aarch64'}[process.arch]}`;
15
+ const memoryMarker = marker.replace('compiled-native-containment=', 'compiled-native-memory=');
16
+ const testcase = (name, body = '') => `<testcase name="${name}" time="0.2">${body}</testcase>`;
17
+ function fixture(t, body = `<system-err>${escape(marker)}\n</system-err>`) {
18
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'native-receipt-fixture-'));
19
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
20
+ const host = path.join(root, 'codewhale-extension-host' + (process.platform === 'win32' ? '.exe' : ''));
21
+ const bundle = path.join(root, 'bundle.mjs'), bun = path.join(root, 'bun-runtime'), junit = path.join(root, 'junit.xml');
22
+ fs.writeFileSync(host, 'synthetic image; no executable or runtime proof');
23
+ fs.writeFileSync(bundle, 'synthetic canonical bundle'); fs.writeFileSync(bun, 'synthetic compiler');
24
+ fs.writeFileSync(junit, `<testsuites><testsuite>${testcase(identity, body)}${testcase(memoryIdentity, `<system-err>${escape(memoryMarker)}\n</system-err>`)}</testsuite></testsuites>`);
25
+ const output = path.join(root, 'receipt');
26
+ const args = [script, '--junit', junit, '--compiled-image', host, '--bundle', bundle, '--bun', bun,
27
+ '--source-sha', '1'.repeat(40), '--image-platform', process.platform, '--image-arch', process.arch, '--host-sha256', digest(fs.readFileSync(host)),
28
+ '--bundle-sha256', digest(fs.readFileSync(bundle)), '--runtime-sha256', digest(fs.readFileSync(bun)), '--output', output];
29
+ return { root, host, bundle, bun, junit, output, args,
30
+ run: () => spawnSync('python3', args, { encoding: 'utf8' }) };
31
+ }
32
+ function windowsCases(omit = '') {
33
+ const names = ['windows_native_lpac_node_owner_boundary', 'windows_native_lpac_bun_owner_boundary',
34
+ 'windows_native_lpac_compiled_owner_boundary', 'windows_native_profiles_are_distinct_and_acl_grant_refuses_junctions',
35
+ 'windows_argv_and_environment_keep_exact_values_and_reject_nul',
36
+ 'windows_profile_retirement_removes_only_its_grants_and_inherited_data_on_restarts',
37
+ 'windows_profile_directory_budget_and_recorded_identity_refuse_before_overwrite'];
38
+ return names.filter(name => name !== omit).map(name => testcase(`extension_host::windows::tests::${name}`)).join('');
39
+ }
40
+ function includeWindows(f) {
41
+ if (process.platform === 'win32') fs.writeFileSync(f.junit,
42
+ fs.readFileSync(f.junit, 'utf8').replace('</testsuite>', windowsCases() + '</testsuite>'));
43
+ }
44
+ test('receipt exports exact bytes and real report fields; synthetic fixture is not runtime proof', t => {
45
+ const f = fixture(t); includeWindows(f);
46
+ const result = f.run(); assert.equal(result.status, 0, result.stderr);
47
+ const receipt = JSON.parse(fs.readFileSync(path.join(f.output, 'native-receipt.json'), 'utf8'));
48
+ assert.equal(receipt.scope, 'native-compiled-host'); assert.equal(receipt.platform, process.platform); assert.equal(receipt.arch, process.arch);
49
+ assert.equal(receipt.passed, process.platform === 'win32' ? 9 : 2); assert.equal(receipt.failed, 0); assert.equal(receipt.skipped, 0);
50
+ assert.deepEqual(fs.readFileSync(path.join(f.output, path.basename(f.host))), fs.readFileSync(f.host));
51
+ assert.equal(receipt.host_sha256, digest(fs.readFileSync(f.host)));
52
+ assert.equal(receipt.log_sha256, digest(fs.readFileSync(path.join(f.output, receipt.log))));
53
+ if (process.platform === 'linux') assert.equal(receipt.libc.family, 'glibc');
54
+ });
55
+ test('receipt refuses missing Native testcase', t => {
56
+ const f = fixture(t); fs.writeFileSync(f.junit, '<testsuites/>');
57
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /required Native testcase is missing/); assert.ok(!fs.existsSync(f.output));
58
+ });
59
+ test('receipt refuses an ordinary successful early return without complete Native marker', t => {
60
+ const f = fixture(t, ''); includeWindows(f);
61
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /completion marker is missing/);
62
+ });
63
+ for (const element of ['failure', 'error', 'skipped', 'flakyFailure']) {
64
+ test(`receipt refuses ${element} rather than upgrading it to a pass`, t => {
65
+ const f = fixture(t, `<${element}/><system-err>${escape(marker)}</system-err>`); includeWindows(f);
66
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /did not pass once/);
67
+ });
68
+ }
69
+ test('receipt refuses duplicate/retried Native results', t => {
70
+ const f = fixture(t); includeWindows(f);
71
+ fs.writeFileSync(f.junit, fs.readFileSync(f.junit, 'utf8').replace('</testsuite>', testcase(identity) + '</testsuite>'));
72
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /duplicate\/retried/);
73
+ });
74
+ test('receipt refuses compiled image mutated after its Native invocation', t => {
75
+ const f = fixture(t); includeWindows(f); fs.appendFileSync(f.host, 'changed');
76
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /qualified input changed/);
77
+ });
78
+ test('receipt refuses prior output directory and noncanonical filename', t => {
79
+ const f = fixture(t); includeWindows(f); fs.mkdirSync(f.output);
80
+ assert.notEqual(f.run().status, 0);
81
+ fs.rmSync(f.output, { recursive: true });
82
+ const other = path.join(f.root, 'host-imposter'); fs.renameSync(f.host, other);
83
+ f.args[f.args.indexOf('--compiled-image') + 1] = other;
84
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /canonical image basename/);
85
+ });
86
+ test('Windows unit extraction requires all actual LPAC/reparse consumers', t => {
87
+ const f = fixture(t);
88
+ const invoke = (report) => spawnSync('python3', ['-c',
89
+ 'import runpy,sys; m=runpy.run_path(sys.argv[1]); print(m["extract"](sys.stdin.buffer.read(),"win32","x64")[0])', script],
90
+ { input: report, encoding: 'utf8' });
91
+ const compiled = testcase(identity, '<system-err>compiled-native-containment=passed platform=windows arch=x86_64\n</system-err>');
92
+ const compiledMemory = testcase(memoryIdentity, '<system-err>compiled-native-memory=passed platform=windows arch=x86_64\n</system-err>');
93
+ const r = invoke(`<testsuites><testsuite>${compiled}${compiledMemory}${windowsCases()}</testsuite></testsuites>`);
94
+ assert.equal(r.status, 0, r.stderr); assert.equal(r.stdout.trim(), '9');
95
+ const missing = invoke(`<testsuites><testsuite>${compiled}${compiledMemory}${windowsCases('windows_native_lpac_node_owner_boundary')}</testsuite></testsuites>`);
96
+ assert.notEqual(missing.status, 0); assert.match(missing.stderr, /required Native testcase is missing/);
97
+ });
98
+ test('receipt refuses XML entities and another OS completion marker', t => {
99
+ const f = fixture(t); fs.writeFileSync(f.junit, '<!DOCTYPE x><testsuites/>');
100
+ assert.match(f.run().stderr, /invalid or oversized/);
101
+ const r = spawnSync('python3', ['-c', 'import runpy,sys; m=runpy.run_path(sys.argv[1]); m["extract"](sys.stdin.buffer.read(),"win32","x64")', script],
102
+ { input: `<testsuites><testsuite>${testcase(identity, '<system-err>compiled-native-containment=passed platform=macos arch=x86_64\n</system-err>')}${testcase(memoryIdentity, '<system-err>compiled-native-memory=passed platform=windows arch=x86_64\n</system-err>')}${windowsCases()}</testsuite></testsuites>`, encoding: 'utf8' });
103
+ assert.notEqual(r.status, 0); assert.match(r.stderr, /another native target/);
104
+ });
105
+
106
+ test('receipt refuses architecture/emulation label transfer even with matching image bytes', t => {
107
+ const f = fixture(t); includeWindows(f);
108
+ f.args[f.args.indexOf('--image-arch') + 1] = process.arch === 'arm64' ? 'x64' : 'arm64';
109
+ const r = f.run(); assert.notEqual(r.status, 0); assert.match(r.stderr, /identity differs from native runner/);
110
+ });
@@ -19,7 +19,11 @@ const {
19
19
  assertChecksumManifestIncludes,
20
20
  assertPackageVersionMatchesBinaryVersion,
21
21
  assertReleaseAssetsFresh,
22
+ downloadJson,
23
+ downloadText,
22
24
  findReleaseWorkflowRun,
25
+ limits,
26
+ requestStatus,
23
27
  parseChecksumManifest,
24
28
  } = require("../scripts/verify-release-assets");
25
29
 
@@ -308,3 +312,107 @@ test("full local release fixture satisfies the public asset inventory", () => {
308
312
  fs.rmSync(fixtureRoot, { recursive: true, force: true });
309
313
  }
310
314
  });
315
+
316
+ test("downloadJson confines credentials to the GitHub API origin across redirects", async (t) => {
317
+ const https = require("node:https");
318
+ const { PassThrough } = require("node:stream");
319
+ const { EventEmitter } = require("node:events");
320
+ const previous = process.env.GITHUB_TOKEN;
321
+ process.env.GITHUB_TOKEN = "test-release-token";
322
+ t.after(() => {
323
+ if (previous === undefined) delete process.env.GITHUB_TOKEN;
324
+ else process.env.GITHUB_TOKEN = previous;
325
+ });
326
+ const calls = [];
327
+ const replies = [
328
+ { status: 302, location: "/next" },
329
+ { status: 302, location: "https://downloads.example.test/metadata" },
330
+ { status: 200 },
331
+ { status: 302, location: "http://downloads.example.test/metadata" },
332
+ { status: 302, location: "ftp://downloads.example.test/metadata" },
333
+ { status: 200 },
334
+ ];
335
+ t.mock.method(https, "get", (url, options, callback) => {
336
+ calls.push({ url, headers: options.headers });
337
+ const reply = replies.shift();
338
+ assert.ok(reply, "unexpected metadata request");
339
+ const res = new PassThrough();
340
+ res.statusCode = reply.status;
341
+ res.headers = reply.location ? { location: reply.location } : {};
342
+ process.nextTick(() => { callback(res); res.end("{}"); });
343
+ return new EventEmitter();
344
+ });
345
+ t.mock.method(require("node:http"), "get", () => { throw new Error("unexpected HTTP request"); });
346
+ assert.deepEqual(await downloadJson("https://api.github.com/start"), {});
347
+ assert.equal(calls[0].headers.Authorization, "Bearer test-release-token");
348
+ assert.equal(calls[1].headers.Authorization, "Bearer test-release-token");
349
+ assert.equal(calls[2].headers.Authorization, undefined);
350
+ await assert.rejects(downloadJson("https://api.github.com/downgrade"), /requires HTTPS/);
351
+ await assert.rejects(downloadJson("https://api.github.com/other-scheme"), /requires HTTPS/);
352
+ assert.deepEqual(await downloadJson("https://api.github.com:444/metadata"), {});
353
+ assert.equal(calls.at(-1).headers.Authorization, undefined);
354
+ assert.equal(calls.length, 6);
355
+ });
356
+
357
+ function shrinkLimits(t, overrides) {
358
+ const previous = { ...limits };
359
+ Object.assign(limits, overrides);
360
+ t.after(() => Object.assign(limits, previous));
361
+ }
362
+
363
+ function listen(t, handler) {
364
+ const http = require("node:http");
365
+ const server = http.createServer(handler);
366
+ t.after(() => {
367
+ server.closeAllConnections();
368
+ server.close();
369
+ });
370
+ return new Promise((resolve) =>
371
+ server.listen(0, "127.0.0.1", () => resolve(`http://127.0.0.1:${server.address().port}/x`)),
372
+ );
373
+ }
374
+
375
+ test("verifier requests give up on a server that never answers", async (t) => {
376
+ shrinkLimits(t, { idleMs: 150, totalMs: 5_000 });
377
+ const url = await listen(t, () => {});
378
+ await assert.rejects(requestStatus(url, "HEAD"), /timed out/);
379
+ await assert.rejects(downloadText(url), /timed out/);
380
+ });
381
+
382
+ test("verifier requests have a total deadline even while a body keeps trickling", async (t) => {
383
+ shrinkLimits(t, { idleMs: 5_000, totalMs: 300 });
384
+ const url = await listen(t, (req, res) => {
385
+ res.writeHead(200);
386
+ const timer = setInterval(() => res.write("x"), 50);
387
+ res.on("close", () => clearInterval(timer));
388
+ });
389
+ const started = Date.now();
390
+ await assert.rejects(downloadText(url), /exceeded/);
391
+ assert.ok(Date.now() - started < 3_000, "the deadline, not the server, ended the request");
392
+ });
393
+
394
+ test("verifier downloads refuse a body past the size cap and keep one within it", async (t) => {
395
+ shrinkLimits(t, { maxBodyBytes: 1024 });
396
+ const url = await listen(t, (req, res) => {
397
+ res.writeHead(200);
398
+ res.end("a".repeat(req.url.endsWith("big") ? 4096 : 512));
399
+ });
400
+ await assert.rejects(downloadText(`${url}big`), /exceeds 1024 bytes/);
401
+ assert.equal((await downloadText(url)).length, 512);
402
+ });
403
+
404
+ test("downloadJson destroys a metadata request that stalls", async (t) => {
405
+ const https = require("node:https");
406
+ const { EventEmitter } = require("node:events");
407
+ shrinkLimits(t, { idleMs: 40, totalMs: 5_000 });
408
+ let seen;
409
+ t.mock.method(https, "get", (url, options) => {
410
+ seen = options;
411
+ const req = new EventEmitter();
412
+ req.destroy = () => process.nextTick(() => req.emit("close"));
413
+ setTimeout(() => req.emit("timeout"), options.timeout);
414
+ return req;
415
+ });
416
+ await assert.rejects(downloadJson("https://api.github.com/stalled"), /timed out/);
417
+ assert.equal(seen.timeout, 40);
418
+ });
package/test/run.test.js CHANGED
@@ -1,7 +1,20 @@
1
1
  const assert = require("node:assert/strict");
2
+ const { spawn } = require("node:child_process");
3
+ const { EventEmitter } = require("node:events");
4
+ const fs = require("node:fs");
5
+ const os = require("node:os");
6
+ const path = require("node:path");
2
7
  const test = require("node:test");
3
8
 
4
- const { run, _internal } = require("../scripts/run");
9
+ const { run, reportStartFailure, _internal } = require("../scripts/run");
10
+
11
+ // A stand-in for a spawned child that exits on the next tick.
12
+ function exitingChild(status, signal = null) {
13
+ const child = new EventEmitter();
14
+ child.kill = () => true;
15
+ process.nextTick(() => child.emit("exit", status, signal));
16
+ return child;
17
+ }
5
18
 
6
19
  test("version fallback handles only version flags", () => {
7
20
  assert.equal(_internal.isVersionFlag(["--version"]), true);
@@ -17,12 +30,12 @@ test("version flags prefer the installed binary over package metadata", async ()
17
30
  await run("codewhale", {
18
31
  args: ["--version"],
19
32
  getBinaryPath: async () => "/tmp/codewhale-test-binary",
20
- spawnSync: (binary, args, options) => {
33
+ spawn: (binary, args, options) => {
21
34
  spawned = true;
22
35
  assert.equal(binary, "/tmp/codewhale-test-binary");
23
36
  assert.deepEqual(args, ["--version"]);
24
37
  assert.deepEqual(options, { stdio: "inherit" });
25
- return { status: 0 };
38
+ return exitingChild(0);
26
39
  },
27
40
  exit: (status) => {
28
41
  exits.push(status);
@@ -43,9 +56,9 @@ test("codew wrapper dispatches the native shortcut binary", async () => {
43
56
  resolvedNames.push(name);
44
57
  return "/tmp/codew-test-binary";
45
58
  },
46
- spawnSync: (binary, args) => {
59
+ spawn: (binary, args) => {
47
60
  spawned.push({ binary, args });
48
- return { status: 0 };
61
+ return exitingChild(0);
49
62
  },
50
63
  exit: () => {},
51
64
  });
@@ -58,16 +71,21 @@ test("codew wrapper dispatches the native shortcut binary", async () => {
58
71
 
59
72
  test("version flags fall back to package metadata when the binary is unavailable", async () => {
60
73
  const originalLog = console.log;
74
+ const originalError = console.error;
61
75
  const lines = [];
76
+ const errors = [];
62
77
  const exits = [];
63
78
  console.log = (line) => lines.push(line);
79
+ console.error = (...parts) => errors.push(parts.join(" "));
64
80
  try {
65
81
  await run("codewhale", {
66
82
  args: ["--version"],
67
83
  getBinaryPath: async () => {
68
- throw new Error("download unavailable");
84
+ throw Object.assign(new Error("getaddrinfo ENOTFOUND github.com"), {
85
+ code: "ENOTFOUND",
86
+ });
69
87
  },
70
- spawnSync: () => {
88
+ spawn: () => {
71
89
  throw new Error("spawn should not run without a binary");
72
90
  },
73
91
  exit: (status) => {
@@ -76,9 +94,201 @@ test("version flags fall back to package metadata when the binary is unavailable
76
94
  });
77
95
  } finally {
78
96
  console.log = originalLog;
97
+ console.error = originalError;
79
98
  }
80
99
 
81
100
  assert.deepEqual(exits, [0]);
82
101
  assert.match(lines.join("\n"), /codewhale \(npm wrapper\) v/);
83
- assert.match(lines.join("\n"), /binary version: v/);
102
+ // The fallback must not claim a binary version that is not installed.
103
+ assert.doesNotMatch(lines.join("\n"), /binary version: v/);
104
+ assert.match(lines.join("\n"), /binary: not installed \(expected v[^)]+\)/);
105
+ const stderr = errors.join("\n");
106
+ assert.match(stderr, /ENOTFOUND github\.com/);
107
+ assert.match(stderr, /codewhale install hint:/);
108
+ });
109
+
110
+ test("start failures print the install hint for download errors", () => {
111
+ const logged = [];
112
+ const log = (...parts) => logged.push(parts.join(" "));
113
+
114
+ reportStartFailure(
115
+ "codew",
116
+ Object.assign(new Error("download stalled"), { code: "EDOWNLOADTIMEOUT" }),
117
+ log,
118
+ );
119
+ const output = logged.join("\n");
120
+ assert.match(output, /^Failed to start codew: download stalled/);
121
+ assert.match(output, /codewhale install hint:/);
122
+ assert.match(
123
+ output,
124
+ /https:\/\/github\.com\/codewhale-hq\/CodeWhale\/blob\/main\/docs\/INSTALL\.md#npm-binary-download-times-out/,
125
+ );
126
+
127
+ logged.length = 0;
128
+ reportStartFailure("codewhale", new Error("permission denied"), log);
129
+ assert.deepEqual(logged, ["Failed to start codewhale: permission denied"]);
84
130
  });
131
+
132
+ test("termination signals reach the native child and its status is kept", async () => {
133
+ const proc = new EventEmitter();
134
+ const child = new EventEmitter();
135
+ const killed = [];
136
+ child.kill = (signal) => {
137
+ killed.push(signal);
138
+ process.nextTick(() => child.emit("exit", 143, null));
139
+ return true;
140
+ };
141
+ const exits = [];
142
+ const running = run("codewhale", {
143
+ args: ["exec", "--auto", "task"],
144
+ getBinaryPath: async () => "/tmp/codewhale-test-binary",
145
+ spawn: () => child,
146
+ exit: (status) => exits.push(status),
147
+ process: proc,
148
+ });
149
+ await new Promise((resolve) => setImmediate(resolve));
150
+ // Terminal signals are outlived, never forwarded: the terminal already
151
+ // delivered them to the child through the process group.
152
+ for (const signal of ["SIGINT", "SIGHUP"]) {
153
+ assert.equal(proc.listenerCount(signal), 1, signal);
154
+ proc.emit(signal);
155
+ }
156
+ assert.deepEqual(killed, []);
157
+ const forwarded = _internal.FORWARDED_SIGNALS;
158
+ assert.deepEqual(forwarded, process.platform === "win32" ? [] : ["SIGTERM"]);
159
+ if (forwarded.length === 0) {
160
+ child.emit("exit", 0, null);
161
+ await running;
162
+ assert.deepEqual(exits, [0]);
163
+ return;
164
+ }
165
+ assert.equal(proc.listenerCount("SIGTERM"), 1);
166
+ proc.emit("SIGTERM");
167
+ await running;
168
+
169
+ assert.deepEqual(killed, ["SIGTERM"]);
170
+ assert.deepEqual(exits, [143]);
171
+ for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
172
+ assert.equal(proc.listenerCount(signal), 0, signal);
173
+ }
174
+ });
175
+
176
+ test("a child killed by a signal is reported as that signal", async () => {
177
+ const proc = new EventEmitter();
178
+ const raised = [];
179
+ proc.pid = 4242;
180
+ proc.kill = (pid, signal) => raised.push({ pid, signal });
181
+ const exits = [];
182
+ await run("codewhale", {
183
+ args: [],
184
+ getBinaryPath: async () => "/tmp/codewhale-test-binary",
185
+ spawn: () => exitingChild(null, "SIGTERM"),
186
+ exit: (status) => exits.push(status),
187
+ process: proc,
188
+ });
189
+ assert.deepEqual(raised, [{ pid: 4242, signal: "SIGTERM" }]);
190
+ assert.deepEqual(exits, [128 + os.constants.signals.SIGTERM]);
191
+ });
192
+
193
+ test(
194
+ "SIGTERM to the wrapper process does not leave the native child running",
195
+ { skip: process.platform === "win32" && "POSIX signals only", timeout: 20000 },
196
+ async () => {
197
+ const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "codewhale-run-signal-"));
198
+ const pidFile = path.join(dir, "child.pid");
199
+ const childScript =
200
+ `require("fs").writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));` +
201
+ "setInterval(() => {}, 1000);";
202
+ const wrapperScript =
203
+ `require(${JSON.stringify(path.join(__dirname, "..", "scripts", "run.js"))})` +
204
+ `.run("codewhale", { getBinaryPath: async () => process.execPath, ` +
205
+ `args: ["-e", ${JSON.stringify(childScript)}] });`;
206
+ const wrapper = spawn(process.execPath, ["-e", wrapperScript], { stdio: "ignore" });
207
+ let childPid = null;
208
+ const alive = (pid) => {
209
+ try {
210
+ process.kill(pid, 0);
211
+ return true;
212
+ } catch {
213
+ return false;
214
+ }
215
+ };
216
+ try {
217
+ const deadline = Date.now() + 10000;
218
+ while (childPid === null && Date.now() < deadline) {
219
+ try {
220
+ childPid = Number.parseInt(fs.readFileSync(pidFile, "utf8"), 10) || null;
221
+ } catch {
222
+ await new Promise((resolve) => setTimeout(resolve, 25));
223
+ }
224
+ }
225
+ assert.ok(childPid, "native child should have started");
226
+ const wrapperExit = new Promise((resolve) => wrapper.once("exit", resolve));
227
+ wrapper.kill("SIGTERM");
228
+ await wrapperExit;
229
+ const settle = Date.now() + 5000;
230
+ while (alive(childPid) && Date.now() < settle) {
231
+ await new Promise((resolve) => setTimeout(resolve, 25));
232
+ }
233
+ assert.equal(alive(childPid), false, "native child outlived the wrapper");
234
+ } finally {
235
+ if (childPid && alive(childPid)) process.kill(childPid, "SIGKILL");
236
+ if (wrapper.exitCode === null && wrapper.signalCode === null) wrapper.kill("SIGKILL");
237
+ await fs.promises.rm(dir, { recursive: true, force: true });
238
+ }
239
+ },
240
+ );
241
+
242
+ test(
243
+ "Ctrl-C to the terminal's process group reaches the native child once",
244
+ { skip: process.platform === "win32" && "POSIX process groups only", timeout: 20000 },
245
+ async () => {
246
+ const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "codewhale-run-sigint-"));
247
+ const pidFile = path.join(dir, "child.pid");
248
+ const logFile = path.join(dir, "signals.log");
249
+ // Count every SIGINT, then exit the way a Ctrl-C'd CLI does.
250
+ const childScript =
251
+ `const fs = require("fs");` +
252
+ `process.on("SIGINT", () => { fs.appendFileSync(${JSON.stringify(logFile)}, "INT\\n");` +
253
+ ` setTimeout(() => process.exit(130), 300); });` +
254
+ `fs.writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));` +
255
+ "setInterval(() => {}, 1000);";
256
+ const wrapperScript =
257
+ `require(${JSON.stringify(path.join(__dirname, "..", "scripts", "run.js"))})` +
258
+ `.run("codewhale", { getBinaryPath: async () => process.execPath, ` +
259
+ `args: ["-e", ${JSON.stringify(childScript)}] });`;
260
+ // `detached` makes the wrapper a process-group leader, standing in for the
261
+ // terminal's foreground job; the native child joins that group.
262
+ const wrapper = spawn(process.execPath, ["-e", wrapperScript], {
263
+ stdio: "ignore",
264
+ detached: true,
265
+ });
266
+ let childPid = null;
267
+ try {
268
+ const deadline = Date.now() + 10000;
269
+ while (childPid === null && Date.now() < deadline) {
270
+ try {
271
+ childPid = Number.parseInt(fs.readFileSync(pidFile, "utf8"), 10) || null;
272
+ } catch {
273
+ await new Promise((resolve) => setTimeout(resolve, 25));
274
+ }
275
+ }
276
+ assert.ok(childPid, "native child should have started");
277
+ const wrapperExit = new Promise((resolve) =>
278
+ wrapper.once("exit", (code, signal) => resolve({ code, signal })),
279
+ );
280
+ process.kill(-wrapper.pid, "SIGINT");
281
+ const ended = await wrapperExit;
282
+ const received = fs.readFileSync(logFile, "utf8").trim().split("\n");
283
+ assert.deepEqual(received, ["INT"], "the child must see one SIGINT, not a forwarded copy");
284
+ assert.deepEqual(ended, { code: 130, signal: null });
285
+ } finally {
286
+ try {
287
+ process.kill(-wrapper.pid, "SIGKILL");
288
+ } catch {
289
+ // already gone
290
+ }
291
+ await fs.promises.rm(dir, { recursive: true, force: true });
292
+ }
293
+ },
294
+ );
@@ -18,7 +18,7 @@ const {
18
18
  const { run, _internal } = require("../scripts/install");
19
19
 
20
20
  const VERSION = "0.9.10";
21
- const REPO = "Hmbown/CodeWhale";
21
+ const REPO = "codewhale-hq/CodeWhale";
22
22
  const CODEWHALE_ASSET = "codewhale-linux-x64";
23
23
  const CODEW_ASSET = "codew-linux-x64";
24
24
  const REQUIRED_ASSETS = [CODEWHALE_ASSET, CODEW_ASSET];