checkMigrationReadiness.test.mjs
4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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({}, ({ fixtureRoot, manifestPath }) => {
const [fixtureTopLevelDirectory] = path.relative(projectRoot, fixtureRoot).split(path.sep);
assert.equal(fixtureTopLevelDirectory, 'tests');
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);
});