Files
gstack/test/upgrade-migration-v1.test.ts
T
Garry Tan 2a55953387 fix(tests): repair 7 pre-existing failures (env pollution + stale markers)
All 7 failures existed on main before this branch — verified via `git stash`
round-trip. Bundling them into the long-lived-sidebar PR because we kept
tripping over them while running `bun test` to verify Commit 0.

  * Global afterEach restores `process.env.PATH` (new bunfig.toml +
    test-setup.ts). browser-skill-commands.test.ts sets
    `PATH = '/test/bin:/usr/bin'` to exercise a scrubbed-env fixture and
    used the broken `process.env = origEnv` reassignment pattern that
    swaps the proxy reference; the underlying env stayed mutated and
    leaked downstream. Fixed three call sites in that file and added a
    narrow PATH-only global guardrail so a future polluter can't bring
    the bug back. Killed: pair-agent-tunnel-eval (bun ENOENT),
    security.test.ts > resolveBashBinary (Bun.which('bash') null),
    server-no-import-side-effects (bun ENOENT).
  * server-auth.test.ts: two `sliceBetween` markers referenced strings
    deleted when sidebar-agent.ts was ripped — `'Sidebar agent started'`
    → `'Terminal agent started'`, `'Sidebar endpoints'` → `'Batch endpoint'`.
    Also fixed the pair-agent BROWSE_PARENT_PID assertion (the literal
    `serverEnv.BROWSE_PARENT_PID` never existed in source; the actual
    contract is the object-literal `BROWSE_PARENT_PID: '0'` inside the
    `const serverEnv` declaration).
  * test/upgrade-migration-v1.test.ts: also overrides HOME in the spawn
    env. The migration shells out to `${HOME}/.claude/skills/gstack/bin/gstack-config`
    and a developer's real config with `explain_level` set causes the
    script to take the "user already decided" branch and skip writing
    the pending-prompt flag the test asserts on.
  * test/setup-codesign.test.ts: replaced fragile `bun run build`
    string-match (which hit a comment 700 lines later) with the actual
    invocation `bun_cmd run build` used in the setup script.

Net: full suite is now green; CI no longer trips on bash/bun-ENOENT
from PATH pollution or on test markers that drifted with the codebase.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 19:25:06 -07:00

81 lines
2.8 KiB
TypeScript

/**
* gstack-upgrade/migrations/v1.0.0.0.sh — writing style migration.
*
* Coverage:
* - Fresh state: writes the pending-prompt flag
* - Idempotent: second run does nothing if .writing-style-prompted exists
* - Pre-set explain_level: counts as answered (user already decided)
*/
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { spawnSync } from 'child_process';
const ROOT = path.resolve(import.meta.dir, '..');
const MIGRATION = path.join(ROOT, 'gstack-upgrade', 'migrations', 'v1.0.0.0.sh');
let tmpHome: string;
beforeEach(() => {
tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-mig-test-'));
});
afterEach(() => {
fs.rmSync(tmpHome, { recursive: true, force: true });
});
function run(): { stdout: string; stderr: string; status: number } {
// Override HOME too — the migration reads `${HOME}/.claude/skills/gstack/bin/gstack-config`,
// and the developer's real config may have `explain_level` set, which the
// migration interprets as "user already decided" and short-circuits without
// writing the pending-prompt flag (breaking these tests).
const res = spawnSync('bash', [MIGRATION], {
encoding: 'utf-8',
env: { ...process.env, GSTACK_HOME: tmpHome, HOME: tmpHome },
});
return {
stdout: (res.stdout ?? '').trim(),
stderr: (res.stderr ?? '').trim(),
status: res.status ?? -1,
};
}
describe('v1.0.0.0 upgrade migration', () => {
test('migration file exists and is executable', () => {
expect(fs.existsSync(MIGRATION)).toBe(true);
const stat = fs.statSync(MIGRATION);
// Owner execute bit should be set
expect(stat.mode & 0o100).toBeGreaterThan(0);
});
test('fresh state: writes pending-prompt flag', () => {
const result = run();
expect(result.status).toBe(0);
expect(fs.existsSync(path.join(tmpHome, '.writing-style-prompt-pending'))).toBe(true);
});
test('idempotent: second run after user answered is a no-op', () => {
// Simulate user answered: flag exists
fs.writeFileSync(path.join(tmpHome, '.writing-style-prompted'), '');
const result = run();
expect(result.status).toBe(0);
// No pending flag created
expect(fs.existsSync(path.join(tmpHome, '.writing-style-prompt-pending'))).toBe(false);
});
test('idempotent: pre-existing pending flag is not duplicated', () => {
// First run
run();
const firstStat = fs.statSync(path.join(tmpHome, '.writing-style-prompt-pending'));
// Second run — flag stays, no error
const result = run();
expect(result.status).toBe(0);
// Flag still exists; mtime may update but existence is stable
expect(fs.existsSync(path.join(tmpHome, '.writing-style-prompt-pending'))).toBe(true);
void firstStat;
});
});