Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
23.2 KB
Raw Blame
/**
 * scripts/pregate-remote-gate.sh — gbrain's gstack-pregate remote gate.
 *
 * Protects: the receipt gstack-pregate --remote reads to clear the
 * requires-remote obligation on a small machine (PREGATE_REMOTE: tree=…
 * tests=… failed=…).
 * Fails when: the tree reported is not the working tree's content fingerprint
 * (untracked files included, ignored files excluded), the totals stop summing
 * every summary lane, a gate with no summary or a moved tree still prints a
 * receipt, the gate's exit code is lost, or a missing UBI_OWNER or a
 * PREGATE_TREE mismatch reaches the gate, or the default gate is anything
 * but the development form `bun run ci:ubicloud:diff` (the full gate runs
 * once per PR, at handoff).
 * Remote-gate reuse: a clean tree whose Test and E2E full runs passed on GitHub
 * takes its receipt from them (same line, byte for byte) and starts no gate; a
 * failed full run refuses; a dirty tree, no repository, GBRAIN_PREGATE_REUSE=0
 * or no reuse runs the gate.
 * Seams: GBRAIN_PREGATE_GATE_CMD replaces `bun run ci:ubicloud:diff`,
 * GBRAIN_PREGATE_REUSE_CMD replaces `bun scripts/ci-reuse.ts --gate`,
 * GBRAIN_PREGATE_REPO names the repository.
 * --list: prints the gate command's --list output (the inventory gstack's
 * clear --run checks runs against) without UBI_OWNER or a gate; a failing or
 * empty inventory exits non-zero.
 * VM cleanup: the gate's UBICLOUD: left= line (UBI_LEFT_FILE) must say 0, or
 * the run is PREGATE_REMOTE_INFRA (exit 4, no receipt), never a test result;
 * a missing line counts as not confirmed.
 * Policy floor: .gstack/pregate.json's run_profiles and remote_gate_runs are read
 * only by gstack 1.91.377.0 and newer, so .gstack/ship-policy.json must declare
 * at least that min_gstack_version while they exist; an older gstack would
 * otherwise refuse the policy as invalid instead of naming the upgrade.
 */
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
import { spawnSync } from 'node:child_process';
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';

const SCRIPT = resolve(import.meta.dir, '..', '..', 'scripts/pregate-remote-gate.sh');
const LEFT0 = `echo 'UBICLOUD: left=0 owner=gbra0test vms=1 others=0' >> "$UBI_LEFT_FILE"`;
const SUMMARY = [
  '=== ci-ubicloud summary ===',
  '  gitleaks 1 passed, 0 failed, 8s compute',
  '  unit     30 passed, 2 failed, 80s compute',
  '  e2e      5 passed, 0 failed, 60s compute',
  '  VMs: 8/8 ready, setup 62-72s',
].join('\n');

let repo: string;
const git = (...args: string[]) => spawnSync('git', args, { cwd: repo, encoding: 'utf8', env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1' } }).stdout.trim();

beforeEach(() => {
  repo = mkdtempSync(join(tmpdir(), 'pregate-remote-gate-'));
  git('init', '-q', '-b', 'main');
  git('config', 'user.email', '[email protected]');
  git('config', 'user.name', 'Test');
  writeFileSync(join(repo, '.gitignore'), 'ignored.txt\n');
  writeFileSync(join(repo, 'a.txt'), 'a\n');
  git('add', '.');
  git('commit', '-q', '-m', 'init');
});
afterEach(() => rmSync(repo, { recursive: true, force: true }));

/** Runs the script with `gate` as the gate command; unless `raw`, the gate first reports a clean VM teardown (left=0). */
function run(gate: string, env: Record<string, string | undefined> = {}, raw = false) {
  const r = spawnSync('bash', [SCRIPT], {
    cwd: repo, encoding: 'utf8',
    env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: 'gbra0test', UBI_LEFT_FILE: undefined, PREGATE_TREE: undefined, GBRAIN_PREGATE_REPO: undefined, GBRAIN_PREGATE_REUSE: undefined, GBRAIN_PREGATE_REUSE_CMD: undefined, GBRAIN_PREGATE_GATE_CMD: raw ? gate : `${LEFT0}; ${gate}`, ...env },
  });
  return { code: r.status, out: r.stdout, err: r.stderr };
}

/** The content tree of tracked plus untracked-unignored files, as gstack-wtree computes it. */
function contentTree(): string {
  const idx = join(repo, '.git', 'test-index');
  const env = { ...process.env, GIT_INDEX_FILE: idx, GIT_CONFIG_NOSYSTEM: '1' };
  spawnSync('git', ['read-tree', 'HEAD'], { cwd: repo, env });
  spawnSync('git', ['add', '-A', '.'], { cwd: repo, env });
  const tree = spawnSync('git', ['write-tree'], { cwd: repo, env, encoding: 'utf8' }).stdout.trim();
  rmSync(idx, { force: true });
  return tree;
}

describe('pregate-remote-gate.sh', () => {
  it('reports the working tree with untracked files and sums every summary lane', () => {
    writeFileSync(join(repo, 'new.txt'), 'untracked\n');
    writeFileSync(join(repo, 'ignored.txt'), 'scratch\n');
    const expected = contentTree();
    expect(expected).not.toBe(git('rev-parse', 'HEAD^{tree}'));
    const gate = join(repo, '.git', 'gate.sh');
    writeFileSync(gate, 'printf \'%s\\n\' "$SUMMARY_TEXT"; exit 1\n');
    const r = run(`bash ${gate}`, { SUMMARY_TEXT: SUMMARY });
    expect(r.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${expected} tests=38 failed=2`);
    expect(r.code).toBe(1);
  });

  it('a green gate exits 0 with failed=0 and receives the extra arguments', () => {
    const gate = join(repo, '.git', 'gate.sh');
    writeFileSync(gate, `echo "args: $*"; ${LEFT0}; printf '%s\\n' '=== ci-ubicloud summary ===' '  unit     4 passed, 0 failed, 1s compute'\n`);
    const r = spawnSync('bash', [SCRIPT, '--vms', '8'], {
      cwd: repo, encoding: 'utf8',
      env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: 'gbra0test', GBRAIN_PREGATE_GATE_CMD: `bash ${gate}` } as Record<string, string>,
    });
    expect(r.stdout).toContain('args: --vms 8');
    expect(r.stdout.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=4 failed=0`);
    expect(r.status).toBe(0);
  });

  it('runs the development gate, bun run ci:ubicloud:diff, by default and passes the extra arguments on', () => {
    const bin = join(repo, '.git', 'fake-bin');
    mkdirSync(bin);
    writeFileSync(join(bin, 'bun'), `#!/usr/bin/env bash\necho "fake-bun: $*"\n${LEFT0}\nprintf '%s\\n' '=== ci-ubicloud summary ===' '  gitleaks 1 passed, 0 failed, 2s compute'\n`);
    chmodSync(join(bin, 'bun'), 0o755);
    const r = spawnSync('bash', [SCRIPT, '--lanes', 'unit'], {
      cwd: repo, encoding: 'utf8',
      env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: 'gbra0test', PATH: `${bin}:${process.env.PATH}`, GBRAIN_PREGATE_GATE_CMD: undefined, PREGATE_TREE: undefined },
    });
    expect(r.stdout).toContain('fake-bun: run ci:ubicloud:diff --lanes unit');
    expect(r.stdout.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=1 failed=0`);
    expect(r.status).toBe(0);
  });

  it('prints no receipt when the gate printed no summary', () => {
    const r = run('echo provisioning failed; exit 3');
    expect(r.out).not.toContain('PREGATE_REMOTE:');
    expect(r.err).toContain('no ci-ubicloud summary');
    expect(r.code).toBe(3);
  });

  it('prints no receipt when the working tree changes during the gate', () => {
    const r = run(`echo changed > moved.txt; printf '%s\\n' '=== ci-ubicloud summary ===' '  unit     4 passed, 0 failed, 1s compute'`);
    expect(r.out).not.toContain('PREGATE_REMOTE:');
    expect(r.err).toContain('changed during the gate');
    expect(r.code).toBe(1);
  });

  it('a gate whose VM cleanup is not confirmed (left=1, left=unknown or no left line) is PREGATE_REMOTE_INFRA, exit 4, never a test result', () => {
    const green = `printf '%s\\n' '=== ci-ubicloud summary ===' '  unit     4 passed, 0 failed, 1s compute'`;
    for (const [report, shown] of [[`echo 'UBICLOUD: left=1 owner=gbra0test vms=2 others=0' >> "$UBI_LEFT_FILE"; `, 'left=1'], [`echo 'UBICLOUD: left=unknown owner=gbra0test vms=2 others=unknown' >> "$UBI_LEFT_FILE"; `, 'left=unknown'], ['', 'left=missing']] as const) {
      for (const gate of [`${report}${green}`, `${report}${green}; exit 1`]) {
        const r = run(gate, {}, true);
        expect(r.code).toBe(4);
        expect(r.err).toContain(`pregate-remote-gate: PREGATE_REMOTE_INFRA: VM cleanup not confirmed (UBICLOUD: ${shown}, gate exit `);
        expect(r.err).toContain('an infrastructure failure, not a test result, so no receipt. Check and destroy leftovers: UBI_OWNER=gbra0test scripts/ubicloud/ubi-runner.sh list --mine');
        expect(r.out).not.toContain('PREGATE_REMOTE:');
      }
    }
    // A clean teardown keeps the gate's own result: a test failure exits 1 with its receipt.
    const failing = run(`printf '%s\\n' '=== ci-ubicloud summary ===' '  unit     4 passed, 1 failed, 1s compute'; exit 1`);
    expect(failing.code).toBe(1);
    expect(failing.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=5 failed=1`);
  });

  it('a ci-ubicloud run that exits before it requests any VM reports left=0 vms=0, so its failure stays a gate result, not PREGATE_REMOTE_INFRA', () => {
    const ci = resolve(import.meta.dir, '..', '..', 'scripts/ci-ubicloud.ts');
    const r = run(`bun ${ci}`, { UBICLOUD_API_KEY: undefined, UBICLOUD_API_TOKEN: undefined }, true);
    expect(r.out).toContain('UBICLOUD_API_KEY (or UBICLOUD_API_TOKEN) is not set');
    expect(r.out).toContain('UBICLOUD: left=0 owner=gbra0test vms=0 others=unknown');
    expect(r.err).not.toContain('PREGATE_REMOTE_INFRA');
    expect(r.err).toContain('no ci-ubicloud summary');
    expect(r.code).toBe(2);
  });

  it('refuses before the gate without UBI_OWNER or when PREGATE_TREE is another tree', () => {
    const marker = join(repo, '.git', 'ran');
    const noOwner = run(`touch ${marker}`, { UBI_OWNER: '' });
    expect(noOwner.code).toBe(2);
    expect(noOwner.err).toContain('UBI_OWNER is not set');
    const stale = run(`touch ${marker}`, { PREGATE_TREE: '0'.repeat(40) });
    expect(stale.code).toBe(1);
    expect(stale.err).toContain('not the tree pregate selected');
    expect(spawnSync('test', ['-e', marker]).status).not.toBe(0);
  });

  describe('reuse of GitHub full runs on the identical tree', () => {
    const GREEN_GATE = `printf '%s\\n' '=== ci-ubicloud summary ===' '  unit     4 passed, 0 failed, 1s compute'`;
    const reuser = (body: string) => {
      const file = join(repo, '.git', 'reuse.sh');
      writeFileSync(file, `echo "reuse-args: $*" > .git/reuse-args\n${body}\n`);
      return `bash ${file}`;
    };
    const ran = (name: string) => spawnSync('test', ['-e', join(repo, '.git', name)]).status === 0;
    const gateMarking = `touch .git/gate-ran; ${GREEN_GATE}`;
    beforeEach(() => git('update-ref', 'refs/remotes/origin/master', 'HEAD'));

    it('runs the reuse check without the model and database keys the gate strips', () => {
      const keys = ['OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'VOYAGE_API_KEY', 'GEMINI_API_KEY', 'JEV_TYPESAFE_API_KEY', 'DATABASE_URL'];
      const seen = keys.map(k => `echo "${k}=\${${k}:-unset}" >> .git/reuse-env`).join('; ');
      run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser(`${seen}; exit 4`), ...Object.fromEntries(keys.map(k => [k, 'secret-example'])) });
      expect(spawnSync('cat', [join(repo, '.git', 'reuse-env')], { encoding: 'utf8' }).stdout.trim().split('\n')).toEqual(keys.map(k => `${k}=unset`));
    });

    it('a tree that changes the full-ci workflows or the scripts that attest its runs relative to origin/master never reuses its own runs', () => {
      for (const path of ['.github/workflows/test.yml', '.github/workflows/e2e.yml', 'scripts/ci-reuse.ts', 'scripts/ci-full-suite-tree.sh', 'scripts/ci-executed-counts.ts', 'scripts/lib/gh-issue.ts', 'scripts/ci-manifest.ts']) {
        rmSync(join(repo, '.git', 'reuse-args'), { force: true });
        rmSync(join(repo, '.git', 'gate-ran'), { force: true });
        mkdirSync(join(repo, path, '..'), { recursive: true });
        writeFileSync(join(repo, path), `changed ${path}\n`);
        git('add', '.');
        git('commit', '-q', '-m', `change ${path}`);
        const r = run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser("echo 'GBRAIN_REUSE_RESULT tests=9 failed=0'") });
        expect(ran('reuse-args')).toBe(false);
        expect(ran('gate-ran')).toBe(true);
        expect(r.err).toContain(`pregate-remote-gate: the tree changes ${path} relative to origin/master`);
        expect(r.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=4 failed=0`);
        git('update-ref', 'refs/remotes/origin/master', 'HEAD');
      }
      // Without origin/master to compare against, reuse is not trusted either.
      git('update-ref', '-d', 'refs/remotes/origin/master');
      rmSync(join(repo, '.git', 'reuse-args'), { force: true });
      run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser("echo 'GBRAIN_REUSE_RESULT tests=9 failed=0'") });
      expect(ran('reuse-args')).toBe(false);
    });

    it('takes the receipt from the full runs, with the gate\'s exact line, and starts no gate', () => {
      const r = run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser("echo '[ci-reuse] gate: Test run passed'; echo 'GBRAIN_REUSE_RESULT tests=58853 failed=0'") });
      const tree = git('rev-parse', 'HEAD^{tree}');
      expect(r.out.endsWith(`PREGATE_REMOTE: tree=${tree} tests=58853 failed=0\n`)).toBe(true);
      expect(r.code).toBe(0);
      expect(ran('gate-ran')).toBe(false);
      expect(spawnSync('cat', [join(repo, '.git', 'reuse-args')], { encoding: 'utf8' }).stdout.trim())
        .toBe(`reuse-args: --sha ${git('rev-parse', 'HEAD')} --tree ${tree} --repo owner-example/repo-example`);
    });

    it('a full run on the tree that failed refuses: exit 1, no receipt, no gate', () => {
      const r = run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser('exit 3') });
      expect(r.out).not.toContain('PREGATE_REMOTE:');
      expect(r.err).toContain(`pregate-remote-gate: a full run on tree ${git('rev-parse', 'HEAD^{tree}')} failed`);
      expect(r.code).toBe(1);
      expect(ran('gate-ran')).toBe(false);
    });

    it('a reused run that recorded failed tests prints its receipt with exit 1, and starts no gate', () => {
      const r = run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser("echo 'GBRAIN_REUSE_RESULT tests=10 failed=2'") });
      expect(r.out.endsWith(`PREGATE_REMOTE: tree=${git('rev-parse', 'HEAD^{tree}')} tests=10 failed=2\n`)).toBe(true);
      expect(r.code).toBe(1);
      expect(ran('gate-ran')).toBe(false);
    });

    it('a reuse step that exits 0 without a result line, or exits with any other code, runs the gate', () => {
      for (const body of ["echo '[ci-reuse] gate: something'", 'exit 2']) {
        rmSync(join(repo, '.git', 'gate-ran'), { force: true });
        const r = run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser(body) });
        expect(ran('gate-ran')).toBe(true);
        expect(r.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=4 failed=0`);
        expect(r.code).toBe(0);
      }
    });

    it('no reuse runs the gate, whose summary is the receipt', () => {
      const r = run(gateMarking, { GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE_CMD: reuser('exit 4') });
      expect(ran('gate-ran')).toBe(true);
      expect(r.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=4 failed=0`);
      expect(r.code).toBe(0);
    });

    it('a dirty tree, GBRAIN_PREGATE_REUSE=0 or no repository never asks for reuse', () => {
      const env = { GBRAIN_PREGATE_REUSE_CMD: reuser("echo 'GBRAIN_REUSE_RESULT tests=9 failed=0'") };
      run(gateMarking, { ...env, GBRAIN_PREGATE_REPO: 'owner-example/repo-example', GBRAIN_PREGATE_REUSE: '0' });
      run(gateMarking, env);
      writeFileSync(join(repo, 'edited.txt'), 'not committed\n');
      const dirty = run(gateMarking, { ...env, GBRAIN_PREGATE_REPO: 'owner-example/repo-example' });
      expect(ran('reuse-args')).toBe(false);
      expect(dirty.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=4 failed=0`);
    });

    it('reads the repository from the origin remote', () => {
      git('remote', 'add', 'origin', 'https://git.example.invalid/owner-example/repo-example.git');
      run(gateMarking, { GBRAIN_PREGATE_REUSE_CMD: reuser('exit 4') });
      expect(spawnSync('cat', [join(repo, '.git', 'reuse-args')], { encoding: 'utf8' }).stdout).toContain('--repo owner-example/repo-example');
    });
  });
});

describe('pregate-remote-gate.sh --list', () => {
  beforeEach(() => git('update-ref', 'refs/remotes/origin/master', 'HEAD'));
  /** Runs --list with `gate` as the gate command and no UBI_OWNER. */
  const list = (gate: string | undefined, args: string[] = [], env: Record<string, string> = {}) => {
    const r = spawnSync('bash', [SCRIPT, '--list', ...args], {
      cwd: repo, encoding: 'utf8',
      env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: undefined, UBI_LEFT_FILE: undefined, GBRAIN_PREGATE_GATE_CMD: gate, ...env },
    });
    return { code: r.status, out: r.stdout, err: r.stderr };
  };

  it("prints the gate command's --list output at the checkout top, with the extra arguments, needing no UBI_OWNER", () => {
    mkdirSync(join(repo, 'sub'));
    const gate = join(repo, '.git', 'gate.sh');
    writeFileSync(gate, 'echo "args: $*" >&2; echo "cwd: $PWD" >&2; printf "%s\\n" test/a.test.ts test/e2e/b.test.ts\n');
    const r = spawnSync('bash', [SCRIPT, '--list', '--lanes', 'unit'], {
      cwd: join(repo, 'sub'), encoding: 'utf8',
      env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: undefined, GBRAIN_PREGATE_GATE_CMD: `bash ${gate}` },
    });
    expect(r.status, r.stderr).toBe(0);
    expect(r.stdout).toBe('test/a.test.ts\ntest/e2e/b.test.ts\n');
    expect(r.stderr).toContain('args: --list --lanes unit');
    expect(r.stderr).toContain(`cwd: ${repo}`);
    expect(r.stderr).not.toContain('UBI_OWNER');
  });

  it('lists through the development gate, bun run ci:ubicloud:diff --list, by default', () => {
    const bin = join(repo, '.git', 'fake-bin');
    mkdirSync(bin);
    writeFileSync(join(bin, 'bun'), '#!/usr/bin/env bash\necho "fake-bun: $*" >&2\necho test/a.test.ts\n');
    chmodSync(join(bin, 'bun'), 0o755);
    const r = list(undefined, [], { PATH: `${bin}:${process.env.PATH}` });
    expect(r.code, r.err).toBe(0);
    expect(r.err).toContain('fake-bun: run ci:ubicloud:diff --list');
    expect(r.out).toBe('test/a.test.ts\n');
  });

  it('an empty or failing inventory exits non-zero with no list, and never starts the gate', () => {
    const empty = list('true');
    expect(empty.code).toBe(1);
    expect(empty.out).toBe('');
    expect(empty.err).toContain("the gate's inventory is empty");
    const failing = list('echo test/a.test.ts; exit 3; echo');
    expect(failing.code).toBe(1);
    expect(failing.out).toBe('');
    expect(failing.err).toContain("the gate's inventory failed (exit 3)");
    for (const r of [empty, failing]) expect(r.out + r.err).not.toContain('PREGATE_REMOTE');
  });

  it('a tree whose full-ci runs would attest to themselves gets no inventory, so gstack clear --run refuses it and the gate runs', () => {
    const gate = 'touch .git/list-ran; echo test/a.test.ts';
    const refused = (r: { code: number | null; out: string }) => { expect(r.code).toBe(1); expect(r.out).toBe(''); };
    // pregate-remote-gate.sh and ci-ubicloud.ts build the inventory itself, and gstack runs the branch's own copy.
    for (const path of ['.github/workflows/test.yml', '.github/workflows/e2e.yml', 'scripts/ci-reuse.ts', 'scripts/ci-full-suite-tree.sh', 'scripts/ci-executed-counts.ts', 'scripts/lib/gh-issue.ts', 'scripts/ci-manifest.ts', 'scripts/pregate-remote-gate.sh', 'scripts/ci-ubicloud.ts']) {
      mkdirSync(join(repo, path, '..'), { recursive: true });
      writeFileSync(join(repo, path), `changed ${path}\n`);
      git('add', '.');
      git('commit', '-q', '-m', `change ${path}`);
      const r = list(gate);
      refused(r);
      expect(r.err).toContain(`pregate-remote-gate: the tree changes ${path} relative to origin/master`);
      expect(r.err).toContain('no inventory');
      git('update-ref', 'refs/remotes/origin/master', 'HEAD');
    }
    refused(list(gate, [], { GBRAIN_PREGATE_REUSE: '0' }));
    git('update-ref', '-d', 'refs/remotes/origin/master');
    refused(list(gate));
    expect(spawnSync('test', ['-e', join(repo, '.git', 'list-ran')]).status).not.toBe(0);
  });
});

describe('.gstack policy floor', () => {
  it('ship-policy.json declares min_gstack_version 1.91.377.0 or newer while pregate.json declares run_profiles or remote_gate_runs', () => {
    const root = join(import.meta.dir, '..', '..');
    const pregate = JSON.parse(readFileSync(join(root, '.gstack/pregate.json'), 'utf8'));
    const ship = JSON.parse(readFileSync(join(root, '.gstack/ship-policy.json'), 'utf8'));
    if (!('run_profiles' in pregate) && !('remote_gate_runs' in pregate)) return;
    const floor = String(ship.min_gstack_version ?? '');
    expect(floor).toMatch(/^\d+\.\d+\.\d+(\.\d+)?$/);
    const [a, b, c] = floor.split('.').map(Number);
    expect(a * 1e8 + b * 1e4 + c).toBeGreaterThanOrEqual(1 * 1e8 + 91 * 1e4 + 377);
  });
});

describe('ship_lanes_from (/ship lanes carried from CI, docs/ci-event-parity.md#ship-lanes)', () => {
  const root = join(import.meta.dir, '..', '..');
  const pregate = JSON.parse(readFileSync(join(root, '.gstack/pregate.json'), 'utf8'));
  const ship = JSON.parse(readFileSync(join(root, '.gstack/ship-policy.json'), 'utf8'));
  const lanes: Record<string, { profiles: string[]; inventory: string }> = pregate.ship_lanes_from ?? {};
  const doc = readFileSync(join(root, 'docs/ci-event-parity.md'), 'utf8');
  const start = doc.indexOf('<a id="ship-lanes"></a>');
  const body = start < 0 ? '' : doc.slice(doc.indexOf('\n', doc.indexOf('### ', start)));
  const section = body.slice(0, body.search(/\n#{1,6} /) >>> 0);
  const rows = [...section.matchAll(/^\| `([a-z0-9-]+)` \| `([^`]+)` \| ([^|]+) \| `([a-z0-9,-]+)` \|$/gm)]
    .map(([, label, command, profiles, gateLanes]) => ({ label, command, profiles: [...profiles.matchAll(/`([a-z0-9-]+)`/g)].map(m => m[1]), gateLanes }));

  it('maps exactly the documented lane commands, byte for byte, to the documented run profiles', () => {
    expect(rows.length).toBeGreaterThan(0);
    expect(Object.keys(lanes).sort()).toEqual(rows.map(r => r.command).sort());
    for (const r of rows) expect(lanes[r.command]!.profiles).toEqual(r.profiles);
    for (const m of Object.values(lanes)) for (const name of m.profiles) expect(Object.keys(pregate.run_profiles)).toContain(name);
  });

  it("lists each lane's files through the full gate's --list, so a tree that changes the CI reuse files never carries its own runs", () => {
    expect(rows.length).toBeGreaterThan(0);
    for (const r of rows) {
      expect(lanes[r.command]!.inventory).toBe(`GBRAIN_PREGATE_GATE_CMD='bun run ci:ubicloud' bash scripts/pregate-remote-gate.sh --list --lanes ${r.gateLanes}`);
    }
  });

  it('ship-policy.json declares min_gstack_version 1.94.0.0 or newer, the first gstack that reads ship_lanes_from', () => {
    expect(Object.keys(lanes).length).toBeGreaterThan(0);
    const [a, b, c] = String(ship.min_gstack_version ?? '').split('.').map(Number);
    expect(a * 1e8 + b * 1e4 + c).toBeGreaterThanOrEqual(1 * 1e8 + 94 * 1e4 + 0);
  });
});