checkMigrationReadiness.test.mjs 4.6 KB
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
const script = path.join(projectRoot, 'scripts/check-migration-readiness.mjs');

function createFixture(options = {}) {
  const fixtureRoot = mkdtempSync(
    path.join(projectRoot, 'tests', '.migration-readiness-'),
  );
  mkdirSync(path.join(fixtureRoot, 'docs'), { recursive: true });
  mkdirSync(path.join(fixtureRoot, 'tests'), { recursive: true });
  writeFileSync(path.join(fixtureRoot, 'docs/api-inventory.md'), 'API inventory');
  writeFileSync(path.join(fixtureRoot, 'docs/acceptance.md'), 'Acceptance criteria');
  writeFileSync(path.join(fixtureRoot, 'tests/migration.test.mjs'), 'export {};');
  const fixturePath = path.relative(projectRoot, fixtureRoot).replaceAll('\\', '/');
  const manifest = {
    id: 'order-migration',
    title: 'Order migration',
    sourceProject: 'source-mini-program',
    affectedPaths: ['app.js'],
    interfaces: ['Order.create'],
    tests: [{ id: 'migration-contract', file: `${fixturePath}/tests/migration.test.mjs`, level: 'contract', expectedBefore: 'pass' }],
    baseline: { command: 'git status --short', result: 'clean' },
    apiInventory: `${fixturePath}/docs/api-inventory.md`,
    acceptance: `${fixturePath}/docs/acceptance.md`,
    verification: { commands: ['node --version'] },
    ...options,
  };
  const manifestPath = path.join(fixtureRoot, 'manifest.json');
  writeFileSync(manifestPath, JSON.stringify(manifest));
  return { fixtureRoot, manifestPath };
}

function run(manifestPath, phase) {
  try {
    return { status: 0, output: execFileSync(process.execPath, [script, '--manifest', manifestPath, '--phase', phase], { encoding: 'utf8' }) };
  } catch (error) {
    return { status: error.status, output: `${error.stdout || ''}${error.stderr || ''}` };
  }
}

function withFixture(options, assertion) {
  const fixture = createFixture(options);
  try {
    assertion(fixture);
  } finally {
    rmSync(fixture.fixtureRoot, { recursive: true, force: true });
  }
}

test('pre phase accepts a complete manifest with repository-local evidence', () => {
  withFixture({}, ({ manifestPath }) => {
    assert.equal(run(manifestPath, 'pre').status, 0);
  });
});

test('pre phase rejects missing migration material and placeholder content', () => {
  withFixture({ acceptance: 'docs/missing.md' }, ({ manifestPath }) => {
    assert.equal(run(manifestPath, 'pre').status, 1);
  });
  withFixture({}, ({ fixtureRoot, manifestPath }) => {
    writeFileSync(path.join(fixtureRoot, 'docs/api-inventory.md'), 'TODO: inventory');
    assert.equal(run(manifestPath, 'pre').status, 1);
  });
});

test('pre phase rejects missing test evidence and paths outside the repository', () => {
  withFixture({ tests: [{ id: 'missing-test', file: '.migration-readiness-missing/tests/missing.test.mjs', level: 'unit', expectedBefore: 'pass' }] }, ({ manifestPath }) => {
    assert.equal(run(manifestPath, 'pre').status, 1);
  });
  withFixture({ apiInventory: '../outside.md' }, ({ manifestPath }) => {
    assert.equal(run(manifestPath, 'pre').status, 1);
  });
});

test('post phase requires successful or explicitly blocked tests', () => {
  withFixture({}, ({ manifestPath }) => {
    const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
    manifest.tests[0].expectedBefore = 'fail';
    writeFileSync(manifestPath, JSON.stringify(manifest));
    assert.equal(run(manifestPath, 'post').status, 1);
  });
  withFixture({}, ({ fixtureRoot, manifestPath }) => {
    const fixturePath = path.relative(projectRoot, fixtureRoot).replaceAll('\\', '/');
    const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
    manifest.tests = [{ id: 'blocked-test', file: `${fixturePath}/tests/migration.test.mjs`, level: 'unit', expectedBefore: 'pass', status: 'blocked', blocker: 'requires staging credential' }];
    writeFileSync(manifestPath, JSON.stringify(manifest));
    assert.equal(run(manifestPath, 'post').status, 0);
  });
});

test('post phase runs unblocked verification commands and rejects nonzero results', () => {
  withFixture({ verification: { commands: ['node -e "process.exit(7)"'] } }, ({ manifestPath }) => {
    assert.equal(run(manifestPath, 'post').status, 1);
  });
});

test('invalid command-line arguments exit with usage status', () => {
  const result = run(path.join(projectRoot, 'missing.json'), 'invalid');
  assert.equal(result.status, 2);
});