Files
msd-core/tests/bug-2649-sdk-fail-fast.test.cjs
Tom Boucher 334a64168e chore(npm): rebrand packages to @opengsd scope (#127)
* chore(npm): rebrand packages to @opengsd scope

Rename:
- get-shit-done-redux → @opengsd/get-shit-done-redux
- @gsd-redux/sdk → @opengsd/gsd-sdk

Add publishConfig.access=public for first-time scoped publish.
CLI binary names (get-shit-done-redux, gsd-sdk, gsd-tools) unchanged.

Sweeps install commands, npx invocations, CI publish/version-check
workflows, tests, docs, READMEs (all translations), and the
PACKAGE_NAME constant in check-latest-version.

Bumps qs 6.15.1 → 6.15.2 to clear a moderate advisory surfaced by
the audit-clean test (GHSA-q8mj-m7cp-5q26).

Closes #126

* chore: pin 2.0.0 release + remove canary workflow

- Bump both packages 1.50.0-canary.0 → 2.0.0 for first @opengsd publish
- Remove .github/workflows/canary.yml and canary dist-tag handling in
  release.yml / release-sdk.yml
- Drop canary section from VERSIONING.md

Refs #126

* chore: address review findings + harden tarball-smoke timeout

- .changeset/opengsd-org-rename.md: match project's custom
  parse.cjs frontmatter (type: Changed / pr: 127); the scoped
  @changesets/cli keys were silently rejected.
- CONTEXT.md: drop two canary-stream policy lines and a dangling
  DEFECT.CANARY-VERSION-LEAK.cross-ref now that canary.yml is gone.
- tests/release-tarball-smoke.install.test.cjs: pass
  timeout: 600_000 for npm pack + global install; the 3-minute
  runNpm default was timing out on slower Docker hosts (cartographer).

Refs #126

* fix(sdk): add missing type/runtime devDependencies for build

prepublishOnly invokes tsc which couldn't resolve @types/node,
@types/ws, or synckit. They had been hoisted from root but were
not declared in sdk/'s own package.json — first publish from a
clean SDK tree failed.

Refs #126

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(ci): use npm pack stdout instead of glob to find tarball

`npm pack --silent` for a scoped package (@opengsd/get-shit-done-redux)
produces `opengsd-get-shit-done-redux-*.tgz`, not `get-shit-done-redux-*.tgz`.
Capture the filename from stdout instead of a hardcoded glob so the step
works regardless of package name format.

Fixes smoke (ubuntu-latest, 22, false) CI failure.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* ci: treat workflow-file changes as test-skip eligible

`.github/workflows/install-smoke.yml` (and other workflow files)
were in neither `test.yml` paths nor `test-skip.yml` paths-ignore,
so neither workflow ran on a workflow-only commit — leaving the
required test-skip check perpetually missing.

Refs #126

* chore: reset version to 1.0.0 for first @opengsd publish

Nothing has been published yet under the @opengsd scope, so the
inaugural release uses 1.0.0 rather than 2.0.0. The "major bump"
in the changeset reflects the breaking install-command change for
users migrating from the prior unscoped `get-shit-done-redux`, not
a numeric continuation from a 1.x line under the new identity.

Refs #126

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 16:22:41 -04:00

195 lines
7.3 KiB
JavaScript

/**
* Regression test for #2649 — installer must fail fast with a clear,
* actionable error when `sdk/dist/cli.js` is missing, and must NOT attempt
* a nested `npm install` inside the sdk directory (which, on Windows, lives
* in the read-only npx cache `%LOCALAPPDATA%\\npm-cache\\_npx\\<hash>\\...`).
*
* Shares a root cause with #2647 (packaging drops sdk/dist/). This test
* covers the installer's defensive behavior when that packaging bug — or
* any future regression that loses the prebuilt dist — reaches users.
*/
'use strict';
// Migrated to typed-IR (#2974): the previous shape used regex assertions
// against stderr to verify fix-command and missing-artifact phrases. Now
// the test calls buildSdkFailFastReport(sdkDir) directly and asserts on
// the structured IR fields (reason, context, fix_command, missing_artifact,
// attempted_nested_install). The renderer's text shape is now an
// implementation detail.
const { test, describe, before } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const INSTALL_PATH = path.join(__dirname, '..', 'bin', 'install.js');
function loadInstaller() {
process.env.GSD_TEST_MODE = '1';
delete require.cache[require.resolve(INSTALL_PATH)];
return require(INSTALL_PATH);
}
function makeTempSdk({ npxCache = false } = {}) {
let root;
if (npxCache) {
root = path.join(os.tmpdir(), `gsd-npx-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, 'npm-cache', '_npx', 'deadbeefcafe0001', 'node_modules', 'get-shit-done-redux');
fs.mkdirSync(root, { recursive: true });
} else {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-clone-'));
}
const sdkDir = path.join(root, 'sdk');
fs.mkdirSync(sdkDir, { recursive: true });
// Note: intentionally no sdk/dist/ directory.
return { root, sdkDir };
}
function cleanup(dir) {
try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
}
function runWithIntercepts(fn) {
const stderr = [];
const stdout = [];
const origErr = console.error;
const origLog = console.log;
console.error = (...a) => stderr.push(a.join(' '));
console.log = (...a) => stdout.push(a.join(' '));
const origExit = process.exit;
let exitCode = null;
process.exit = (code) => { exitCode = code; throw new Error('__EXIT__'); };
const cp = require('child_process');
const origSpawnSync = cp.spawnSync;
const origExecSync = cp.execSync;
const spawnCalls = [];
cp.spawnSync = (cmd, argv, opts) => {
spawnCalls.push({ cmd, argv, opts });
return { status: 0, stdout: Buffer.from(''), stderr: Buffer.from('') };
};
cp.execSync = (cmd, opts) => {
spawnCalls.push({ cmd, opts, via: 'execSync' });
return Buffer.from('');
};
try {
try { fn(); } catch (e) { if (e.message !== '__EXIT__') throw e; }
} finally {
console.error = origErr;
console.log = origLog;
process.exit = origExit;
cp.spawnSync = origSpawnSync;
cp.execSync = origExecSync;
}
return {
stderr: stderr.join('\n'),
stdout: stdout.join('\n'),
exitCode,
spawnCalls,
};
}
describe('installer SDK dist-missing fail-fast (#2649)', () => {
let installer;
before(() => { installer = loadInstaller(); });
test('exposes test hooks for SDK check', () => {
assert.ok(typeof installer.installSdkIfNeeded === 'function',
'installSdkIfNeeded must be exported in test mode');
assert.ok(typeof installer.classifySdkInstall === 'function',
'classifySdkInstall must be exported in test mode');
});
test('classifySdkInstall tags npx cache paths as tarball + npxCache', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: true });
try {
const c = installer.classifySdkInstall(sdkDir);
assert.strictEqual(c.mode, 'tarball');
assert.strictEqual(c.npxCache, true);
assert.ok('readOnly' in c);
} finally {
cleanup(root);
}
});
test('classifySdkInstall tags plain git-clone dirs as dev-clone', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: false });
try {
fs.mkdirSync(path.join(root, '.git'), { recursive: true });
const c = installer.classifySdkInstall(sdkDir);
assert.strictEqual(c.mode, 'dev-clone');
assert.strictEqual(c.npxCache, false);
} finally {
cleanup(root);
}
});
test('missing dist in npx cache: fail fast, no nested npm install', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: true });
try {
// Behavioral check 1: installSdkIfNeeded exits 1 and never spawns nested npm.
const result = runWithIntercepts(() => {
installer.installSdkIfNeeded({ sdkDir });
});
assert.strictEqual(result.exitCode, 1, 'must exit non-zero');
const nestedInstall = result.spawnCalls.find((c) => {
const argv = Array.isArray(c.argv) ? c.argv : [];
const cwd = c.opts && c.opts.cwd;
const isNpm = /\bnpm(\.cmd)?$/i.test(String(c.cmd || ''));
const isInstall = argv.includes('install') || argv.includes('i');
const isInSdk = typeof cwd === 'string' && cwd.includes(sdkDir);
return isNpm && isInstall && isInSdk;
});
assert.strictEqual(nestedInstall, undefined,
'must NOT spawn `npm install` inside the npx-cache sdk dir');
// Behavioral check 2: the structured fail-fast IR identifies the npx-cache
// context, points at the right fix command, and asserts the no-nested-install
// contract via a typed boolean (no stderr grepping).
const ir = installer.buildSdkFailFastReport(sdkDir, path.join(sdkDir, 'dist', 'cli.js'));
assert.strictEqual(ir.ok, false);
assert.strictEqual(ir.reason, 'sdk_fail_fast');
assert.strictEqual(ir.context, 'npx-cache');
assert.strictEqual(ir.missing_artifact, 'sdk/dist');
assert.strictEqual(ir.fix_command, 'npm install -g @opengsd/get-shit-done-redux@latest');
assert.strictEqual(ir.attempted_nested_install, false,
'IR contract: nested-install must always be false (this is a hard invariant)');
} finally {
cleanup(root);
}
});
test('missing dist in a dev clone: fail fast with clone build hint', () => {
const { root, sdkDir } = makeTempSdk({ npxCache: false });
try {
fs.mkdirSync(path.join(root, '.git'), { recursive: true });
const result = runWithIntercepts(() => {
installer.installSdkIfNeeded({ sdkDir });
});
assert.strictEqual(result.exitCode, 1);
// Typed-IR migration #2974: dev-clone context surfaces the local-build
// fix command (not the global-install one).
const ir = installer.buildSdkFailFastReport(sdkDir, path.join(sdkDir, 'dist', 'cli.js'));
assert.strictEqual(ir.context, 'dev-clone');
assert.strictEqual(ir.fix_command, 'cd sdk && npm install && npm run build');
assert.strictEqual(ir.attempted_nested_install, false);
const nestedInstall = result.spawnCalls.find((c) => {
const argv = Array.isArray(c.argv) ? c.argv : [];
const isNpm = /\bnpm(\.cmd)?$/i.test(String(c.cmd || ''));
const isInstall = argv.includes('install') || argv.includes('i');
return isNpm && isInstall;
});
assert.strictEqual(nestedInstall, undefined,
'installer itself must never shell out to `npm install`; the user does that');
} finally {
cleanup(root);
}
});
});