a8489a22c7
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
125 lines
6.0 KiB
JavaScript
125 lines
6.0 KiB
JavaScript
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
signLoopMarker, verifyLoopMarker, computeFingerprint,
|
|
decideStopTeeth, resolveOwnerArbitration, buildGate3ProductFromMarker,
|
|
} from './enforce-gate3-loop.mjs';
|
|
|
|
const KEY = 'test-key-123';
|
|
|
|
describe('signLoopMarker/verifyLoopMarker', () => {
|
|
it('roundtrip', () => {
|
|
const m = signLoopMarker({ taskId: 't1', planId: 'p1', artifactId: 'a1', steps: [], at: 1 }, KEY);
|
|
expect(typeof m.sig).toBe('string');
|
|
expect(verifyLoopMarker(m, KEY)).toBe(true);
|
|
});
|
|
it('подмена поля ломает подпись', () => {
|
|
const m = signLoopMarker({ taskId: 't1', planId: 'p1', artifactId: 'a1', steps: [], at: 1 }, KEY);
|
|
expect(verifyLoopMarker({ ...m, planId: 'p2' }, KEY)).toBe(false);
|
|
});
|
|
it('нет sig → false', () => { expect(verifyLoopMarker({ taskId: 't1' }, KEY)).toBe(false); });
|
|
});
|
|
|
|
describe('computeFingerprint', () => {
|
|
it('детерминирован, не зависит от порядка greens', () => {
|
|
expect(computeFingerprint({ planId: 'p', greenIds: ['c2', 'c1'], negotiationText: 'x' }))
|
|
.toBe(computeFingerprint({ planId: 'p', greenIds: ['c1', 'c2'], negotiationText: 'x' }));
|
|
});
|
|
it('меняется на новый green', () => {
|
|
expect(computeFingerprint({ planId: 'p', greenIds: ['c1'], negotiationText: 'x' }))
|
|
.not.toBe(computeFingerprint({ planId: 'p', greenIds: ['c1', 'c2'], negotiationText: 'x' }));
|
|
});
|
|
it('меняется на новый довод', () => {
|
|
expect(computeFingerprint({ planId: 'p', greenIds: ['c1'], negotiationText: 'x' }))
|
|
.not.toBe(computeFingerprint({ planId: 'p', greenIds: ['c1'], negotiationText: 'y' }));
|
|
});
|
|
});
|
|
|
|
describe('decideStopTeeth', () => {
|
|
const go = { wired: true, decision: 'GO' };
|
|
const nogo = { wired: true, decision: 'NO-GO' };
|
|
const degraded = { wired: false, decision: 'GO' };
|
|
it('GO → allow + clear', () => {
|
|
const r = decideStopTeeth({ verdict: go, noGoCount: 0, ownerArbitration: null });
|
|
expect(r.block).toBe(false); expect(r.clear).toBe(true);
|
|
});
|
|
it('accept → allow + clear', () => {
|
|
const r = decideStopTeeth({ verdict: nogo, noGoCount: 0, ownerArbitration: 'accept' });
|
|
expect(r.block).toBe(false); expect(r.clear).toBe(true);
|
|
});
|
|
it('continue → allow, держим', () => {
|
|
const r = decideStopTeeth({ verdict: nogo, noGoCount: 0, ownerArbitration: 'continue' });
|
|
expect(r.block).toBe(false); expect(r.clear).toBe(false);
|
|
});
|
|
it('NO-GO круг<3 → block negotiate', () => {
|
|
const r = decideStopTeeth({ verdict: nogo, noGoCount: 1, ownerArbitration: null });
|
|
expect(r.block).toBe(true); expect(r.state).toBe('negotiate');
|
|
});
|
|
it('NO-GO круг>=3 → block arbitrate+card', () => {
|
|
const r = decideStopTeeth({ verdict: nogo, noGoCount: 3, ownerArbitration: null });
|
|
expect(r.block).toBe(true); expect(r.state).toBe('arbitrate'); expect(r.card).toBe(true);
|
|
});
|
|
it('degraded → block, НЕ closed/arbitrate', () => {
|
|
const r = decideStopTeeth({ verdict: degraded, noGoCount: 5, ownerArbitration: null });
|
|
expect(r.block).toBe(true); expect(r.clear).toBe(false); expect(r.state).toBe('negotiate');
|
|
});
|
|
});
|
|
|
|
describe('resolveOwnerArbitration', () => {
|
|
const fp = 'abc';
|
|
const g = (action, ts = 1000) => ({ action, ts });
|
|
it('accept грант → accept', () => {
|
|
expect(resolveOwnerArbitration({ fingerprint: fp, grants: [g(`gate3-arb:accept:${fp}`)], consumed: [], now: 1000 })).toBe('accept');
|
|
});
|
|
it('continue грант → continue', () => {
|
|
expect(resolveOwnerArbitration({ fingerprint: fp, grants: [g(`gate3-arb:continue:${fp}`)], consumed: [], now: 1000 })).toBe('continue');
|
|
});
|
|
it('нет гранта → null', () => {
|
|
expect(resolveOwnerArbitration({ fingerprint: fp, grants: [], consumed: [], now: 1000 })).toBe(null);
|
|
});
|
|
it('грант на другой отпечаток → null (анти-реплей)', () => {
|
|
expect(resolveOwnerArbitration({ fingerprint: fp, grants: [g('gate3-arb:accept:OTHER')], consumed: [], now: 1000 })).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe('buildGate3ProductFromMarker', () => {
|
|
it('цель из секций + шаги + greens', () => {
|
|
const marker = { steps: [{ op: 'Write', object: 'a.mjs', criterion_id: 'c1' }], planId: 'p' };
|
|
const frozenArtifact = { sections: { s1: 'построить X', s2: 'критерий Y' } };
|
|
const out = buildGate3ProductFromMarker({ marker, frozenArtifact, greens: [{ criterion_id: 'c1', green: true }] });
|
|
expect(out.goal).toContain('построить X');
|
|
expect(out.product).toContain('a.mjs');
|
|
expect(out.product).toContain('green');
|
|
});
|
|
});
|
|
|
|
import { gate3SurfaceRecord } from './enforce-gate3-loop.mjs';
|
|
|
|
describe('gate3SurfaceRecord (видимость gate3)', () => {
|
|
it('GO verdict → stage judge:gate3, status GO', () => {
|
|
expect(gate3SurfaceRecord({ verdict: { wired: true, decision: 'GO' }, hash: 'h' }))
|
|
.toEqual({ stage: 'judge:gate3', hash: 'h', status: 'GO', reason: '' });
|
|
});
|
|
it('NO-GO → status NO-GO, reason дословно', () => {
|
|
const r = gate3SurfaceRecord({ verdict: { wired: true, decision: 'NO-GO', reason: 'не достигнуто' }, hash: 'h' });
|
|
expect(r.status).toBe('NO-GO');
|
|
expect(r.reason).toBe('не достигнуто');
|
|
});
|
|
it('degraded (wired:false) → degraded', () => {
|
|
expect(gate3SurfaceRecord({ verdict: { wired: false }, hash: 'h' }).status).toBe('degraded');
|
|
});
|
|
it('нет verdict → skip, hash null', () => {
|
|
expect(gate3SurfaceRecord({}).status).toBe('skip');
|
|
expect(gate3SurfaceRecord({}).hash).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe('loop marker delivery', () => {
|
|
const KEY = 'k-loop-deliv';
|
|
it('delivery в подписанной метке верифицируется и ломается при подмене', () => {
|
|
const m = signLoopMarker({ taskId: 't', planId: 'p', artifactId: 'a', steps: [], delivery: 'user-result', at: 1 }, KEY);
|
|
expect(m.delivery).toBe('user-result');
|
|
expect(verifyLoopMarker(m, KEY)).toBe(true);
|
|
expect(verifyLoopMarker({ ...m, delivery: 'internal' }, KEY)).toBe(false);
|
|
});
|
|
});
|