Files
stack/packages/project/tests/rename-v1-revisions.test.mjs

42 lines
2.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { execFileSync } from 'node:child_process';
import { initProject, readSpec } from '../src/index.mjs';
const git = (repo, ...args) => execFileSync('git', ['-c', 'core.hooksPath=/dev/null', '-c', 'commit.gpgsign=false', '-c', 'user.name=fixture', '-c', '[email protected]', '-C', repo, ...args], { encoding: 'utf8' });
const bytes = (repo, rev, path) => git(repo, 'show', `${rev}:${path}`);
test('v1 metadata at each reachable committed revision is checked before reading', async t => {
const root = mkdtempSync(join(tmpdir(), 'revision-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const business = { id: 'acme', human: 'jason', projects: { stack: { root: join(root, 'code'), repo: join(root, 'records') } } };
const ctx = { business, project: 'stack', dataRoot: root };
const { repo, commit: initial } = await initProject(ctx);
const original = bytes(repo, initial, 'project.json');
const meta = JSON.parse(original);
assert.deepEqual({ ...meta, createdAt: '<time>' }, { layoutVersion: 1, business: 'acme', project: 'stack', name: 'stack', createdAt: '<time>' });
assert.match(meta.createdAt, /^\d{4}-\d\d-\d\dT/);
assert.equal(bytes(repo, initial, 'templates/project.json'), readFileSync(new URL('../templates/project.json', import.meta.url), 'utf8'));
const save = (metadata, content) => {
writeFileSync(join(repo, 'project.json'), JSON.stringify(metadata, null, 2) + '\n');
writeFileSync(join(repo, 'spec/north-star.md'), content);
git(repo, 'add', 'project.json', 'spec/north-star.md');
git(repo, 'commit', '-m', 'fixture revision');
return git(repo, 'rev-parse', 'HEAD').trim();
};
const valid = save({ ...meta, name: 'Second' }, '# Second\n');
const badOrg = save({ ...meta, business: 'other' }, '# Wrong org\n');
const badProject = save({ ...meta, project: 'other' }, '# Wrong project\n');
const badMix = save({ ...meta, organization: 'acme' }, '# Mixed\n');
const head = save(meta, '# Head\n');
for (const rev of [initial, valid, head]) assert.equal((await readSpec({ ...ctx, commit: rev })).revision, rev);
assert.equal((await readSpec({ ...ctx, commit: valid })).content, '# Second\n');
for (const rev of [badOrg, badProject, badMix]) await assert.rejects(readSpec({ ...ctx, commit: rev }), { code: 'project-invalid' });
writeFileSync(join(repo, 'project.json'), '{"business":"dirty"}');
assert.equal((await readSpec({ ...ctx, commit: initial })).revision, initial);
assert.equal(bytes(repo, initial, 'project.json'), original);
});