Files
gstack/test/state-root-resolver-failure.test.ts
garrytan d807ef24a3 W1b: template and resolver prose resolve state through guarded gstack-paths; ratchet (a)
Every gstack-paths eval in templates and resolvers carries the fail-stop
guard; executable ~/.gstack paths in bash blocks (context recovery preamble,
eureka log, analytics, project artifacts, upgrade snooze, setup-gbrain lock,
retro snapshots, ship consent marker) use $GSTACK_STATE_ROOT, and the writer
prose that pairs with them points at the printed PROJECT_DIR / RETRO_FILE.
ship drops export GSTACK_STATE_ROOT. SKILL.md regenerated (claude + codex),
ship goldens re-pinned, parity and context-budget caps raised to the measured
sizes with notes. test/state-root-ratchet.test.ts enforces the rule with a
reasoned allowlist; W1 touchfile entries plus a superset golden.
2026-09-30 06:38:50 +00:00

74 lines
3.3 KiB
TypeScript

/**
* Resolver failure is fail-stop (W1). With bin/gstack-state-root.sh removed
* from an install, a real bash block copied from a generated SKILL.md and a
* migrated executable both stop with the reinstall message, and nothing is
* written outside the test's temp root.
*/
import { describe, test, expect } from 'bun:test';
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { resolveStateRoot } from '../lib/state-root';
const ROOT = path.resolve(import.meta.dir, '..');
/** The first ```bash block of a generated SKILL.md that evals gstack-paths. */
function contextRecoveryBlock(): string {
const md = fs.readFileSync(path.join(ROOT, 'qa', 'SKILL.md'), 'utf-8');
const start = md.indexOf('## Context Recovery');
const open = md.indexOf('```bash\n', start) + '```bash\n'.length;
return md.slice(open, md.indexOf('\n```', open));
}
/** Every path under dir, except a host git wrapper's own log dir (not gstack's). */
function listTree(dir: string): string[] {
const out: string[] = [];
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
if (e.name === '.capy') continue;
const p = path.join(dir, e.name);
out.push(p);
if (e.isDirectory() && !e.isSymbolicLink()) out.push(...listTree(p));
}
return out;
}
describe('state-root resolver failure', () => {
test('a generated SKILL.md block and a migrated bin stop with the reinstall message when the twin is missing', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-resolver-fail-'));
try {
const home = path.join(tmp, 'home');
const bin = path.join(home, '.claude', 'skills', 'gstack', 'bin');
const cwd = path.join(tmp, 'cwd');
fs.mkdirSync(bin, { recursive: true });
fs.mkdirSync(cwd);
for (const b of ['gstack-paths', 'gstack-slug', 'gstack-config']) {
fs.copyFileSync(path.join(ROOT, 'bin', b), path.join(bin, b));
fs.chmodSync(path.join(bin, b), 0o755);
}
const env = {
PATH: process.env.PATH ?? '', HOME: home, TMPDIR: path.join(tmp, 'tmp'),
GSTACK_HOME: '', GSTACK_STATE_ROOT: '', GSTACK_STATE_DIR: '', CLAUDE_PLUGIN_DATA: '',
};
expect(resolveStateRoot(env)).toBe(`${home}/.gstack`);
const before = listTree(tmp).sort();
const block = contextRecoveryBlock();
expect(block).toContain('gstack-paths)"; : "${GSTACK_STATE_ROOT:?gstack-paths failed; reinstall with ./setup or /gstack-upgrade}"');
const skill = spawnSync('bash', ['-c', block + '\necho REACHED_END'], { cwd, env, encoding: 'utf-8', timeout: 30_000 });
expect(skill.status).not.toBe(0);
expect(skill.stderr).toContain('gstack-paths failed; reinstall with ./setup or /gstack-upgrade');
expect(skill.stdout).not.toContain('REACHED_END');
const config = spawnSync('bash', [path.join(bin, 'gstack-config'), 'set', 'telemetry', 'off'], { cwd, env, encoding: 'utf-8', timeout: 30_000 });
expect(config.status).toBe(1);
expect(config.stderr).toContain('cannot resolve the gstack state root');
expect(config.stderr).toContain('reinstall with ./setup or /gstack-upgrade');
expect(listTree(tmp).sort()).toEqual(before);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
});
});