Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244import { mkdtemp, rm, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, } from 'node:path';import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import nanoSpawn from 'nano-spawn';import { resolveRealGit as resolveGit, } from '@monochromatic-dev/git-executable/ts';import { POST_COMMIT_LIFECYCLE_DEPENDENCIES, runPostCommitLifecycle,} from './post-commit-lifecycle.ts';import type { RuntimePolicyDefinition, } from './types.ts';
/** Absolute real Git executable. */const realGitPath = await resolveGit();/** Expected landed file contents. */const LANDED_CONTENT = 'landed content\n';
/** Disposable repository fixture. */type RepositoryFixture = Readonly<{ /** Repository root. */ path: string; /** Removes repository after test. */ [Symbol.asyncDispose]: () => Promise<void>;}>;
/** Policy validating exact landed OID and committed bytes. */const GROUND_TRUTH_POLICY: RuntimePolicyDefinition = { name: 'fixture/ground-truth', defaultSeverity: 'error', warnSafe: false, triggers: ['post-commit',], check: async function checkGroundTruth({ context, }) { /** Exact landed object ID. */ const landedOid = await context.git.landedCommitOid(); if ((typeof landedOid) === 'symbol') return [{ code: 'oid-absent', message: 'Landed OID was absent.', },]; /** Landed-delta candidates without unchanged parent-tree entries. */ const candidates = await context.git.candidates(); const landedFile = candidates.find(function isLandedFile(candidate,) { return candidate.path === 'landed.txt'; },); if (landedFile === undefined) return [{ code: 'file-absent', message: 'Landed file was absent.', },]; /** Stable parent-commit file that must stay outside the landed delta. */ const stableFile = candidates.find(function isStableFile(candidate,) { return candidate.path === 'stable.txt'; },); if ((landedFile.change !== 'added') || (stableFile !== undefined)) return [{ code: 'change-wrong', message: 'Landed change classification differs.', },]; /** Exact committed file bytes. */ const contents = new TextDecoder().decode(await landedFile.bytes(),); return contents === LANDED_CONTENT ? [] : [{ code: 'bytes-differ', message: 'Committed bytes differ.', },]; },};/** Policy blocking automatic backup after commit. */const BLOCK_POLICY: RuntimePolicyDefinition = { name: 'fixture/block', defaultSeverity: 'error', warnSafe: false, triggers: ['post-commit',], check: function blockBackup() { return Promise.resolve([{ code: 'backup-blocked', message: 'Fixture blocked backup.', },],); },};/** Policy failing post-commit engine completion. */const THROW_POLICY: RuntimePolicyDefinition = { name: 'fixture/throw', defaultSeverity: 'error', warnSafe: false, triggers: ['post-commit',], check: function throwDuringGate() { return Promise.reject(new Error('fixture post-commit failure',),); },};
/** Creates repository with one real landed commit. @returns disposable committed repository */async function createRepository(): Promise<RepositoryFixture> { /** Disposable repository root. */ const path = await mkdtemp(join(tmpdir(), 'cli-git-post-commit-',),); await nanoSpawn(realGitPath, ['init', '--quiet',], { cwd: path, },); await nanoSpawn(realGitPath, ['config', 'user.email', 'cli-git@example.invalid',], { cwd: path, },); await nanoSpawn(realGitPath, ['config', 'user.name', 'cli-git fixture',], { cwd: path, },); await writeFile(join(path, 'stable.txt',), 'stable content\n',); await nanoSpawn(realGitPath, ['add', 'stable.txt',], { cwd: path, },); await nanoSpawn(realGitPath, ['commit', '--quiet', '-m', 'baseline',], { cwd: path, },); await writeFile(join(path, 'landed.txt',), LANDED_CONTENT,); await nanoSpawn(realGitPath, ['add', 'landed.txt',], { cwd: path, },); await nanoSpawn(realGitPath, ['commit', '--quiet', '-m', 'landed',], { cwd: path, },); return { path, async [Symbol.asyncDispose](): Promise<void> { await rm(path, { recursive: true, force: true, },); }, };}
await describe({ name: 'post-commit lifecycle', children: [ it({ name: 'checks exact landed tree before allowing backup', fn: async function testLandedGroundTruth() { await using repository = await createRepository(); /** Settled clean post-commit gate. */ const result = await runPostCommitLifecycle({ rawArgs: ['commit', '-m', 'landed', 'landed.txt',], transformedArgs: ['commit', '-o', '-m', 'landed', 'landed.txt',], gitPath: realGitPath, cwd: repository.path, registeredPolicies: [GROUND_TRUTH_POLICY,], },); expect(result.blocked,).toBe(false,); expect(result.events,).toEqual([],); }, },), it({ name: 'blocks backup with explicit landed commit event', fn: async function testBlockedBackup() { await using repository = await createRepository(); /** Settled blocked post-commit gate. */ const result = await runPostCommitLifecycle({ rawArgs: ['commit', 'landed.txt',], transformedArgs: ['commit', '-o', 'landed.txt',], gitPath: realGitPath, cwd: repository.path, registeredPolicies: [BLOCK_POLICY,], },); expect(result.blocked,).toBe(true,); expect(result.events[0]?.type,).toBe('finding',); expect(result.events[1],).toEqual({ schemaVersion: 1, sequence: 1, type: 'commit-landed', oid: result.oid, outcome: 'post-commit-blocked', message: `Commit ${result.oid} remains local; post-commit gate blocked automatic backup.`, },); }, },), it({ name: 'allows warning-only post-commit result', fn: async function testWarningBackup() { await using repository = await createRepository(); /** Warning-only post-commit gate. */ const result = await runPostCommitLifecycle({ rawArgs: ['commit', 'landed.txt',], transformedArgs: ['commit', '-o', 'landed.txt',], gitPath: realGitPath, cwd: repository.path, policySeverities: { 'fixture/block': 'warn', }, registeredPolicies: [BLOCK_POLICY,], },); expect(result.blocked,).toBe(false,); expect(result.events.map(function eventType(event,) { return event.type; },),).toEqual(['finding', 'configuration-warning',],); }, },), it({ name: 'retains commit after post-commit engine failure', fn: async function testEngineFailure() { await using repository = await createRepository(); /** Failed post-commit gate. */ const result = await runPostCommitLifecycle({ rawArgs: ['commit', 'landed.txt',], transformedArgs: ['commit', '-o', 'landed.txt',], gitPath: realGitPath, cwd: repository.path, registeredPolicies: [THROW_POLICY,], },); expect(result.blocked,).toBe(true,); expect(result.events.map(function eventType(event,) { return event.type; },),).toEqual(['engine-failure', 'commit-landed',],); }, },), it({ name: 'renders setup failure with known landed commit', fn: async function testSetupFailure() { await using repository = await createRepository(); /** Setup failure after landed OID resolution. */ const result = await runPostCommitLifecycle({ rawArgs: ['commit', 'landed.txt',], transformedArgs: ['commit', '-o', 'landed.txt',], gitPath: realGitPath, cwd: repository.path, dependencies: { ...POST_COMMIT_LIFECYCLE_DEPENDENCIES, resolvePostCommitRepositoryRoot: function failRootResolution() { return Promise.reject(new Error('injected root failure',),); }, }, },); expect(result.blocked,).toBe(true,); expect(result.events,).toEqual([{ schemaVersion: 1, sequence: 0, type: 'engine-failure', code: 'content-unavailable', message: 'injected root failure', trigger: 'post-commit', }, { schemaVersion: 1, sequence: 1, type: 'commit-landed', oid: result.oid, outcome: 'post-commit-blocked', message: `Commit ${result.oid} remains local; post-commit gate blocked automatic backup.`, },],); }, },), it({ name: 'honors full-lifecycle policy escape', fn: async function testPostCommitEscape() { await using repository = await createRepository(); /** Escaped post-commit gate. */ const result = await runPostCommitLifecycle({ rawArgs: ['commit', '--no-enforce-fixture/block', 'landed.txt',], transformedArgs: ['commit', '-o', 'landed.txt',], gitPath: realGitPath, cwd: repository.path, registeredPolicies: [BLOCK_POLICY,], },); expect(result.blocked,).toBe(false,); expect(result.events,).toEqual([],); }, },), ],},);