Files
portal/tools/enforce-snapshot.test.mjs
T
Дмитрий d221ba499d fix(m6): аудит-правки — G-5 egress токен, единый findOpenGrant, escape-журнал, уникальный id снимка
Аудит М6 (audit-context-building + sharp-edges + корректность; комплекс М1–М6), 4 практичных фикса (TDD):
- FIX-1: enforce-mcp-classification печатает точный FLOOR-ESCAPE токен в egress/verdict-блоке (G-5 для egress).
- FIX-2: escape-grant.findOpenGrant — единый предикат свежести для open и consume (гасит ИМЕННО открывший грант; чинит утечку one-shot при дублях/future-ts).
- FIX-3: enforce-supreme-gate.runGate — best-effort журнал escape (escape:true), указатель не двигается, сбой журнала не блокирует.
- FIX-4: enforce-snapshot — уникальный дефолтный id снимка (ts-pid-счётчик) против ms-коллизии refs/floor-snapshots.

Регрессия tools-only 2843 passed + 2 skip (+9, 0 регрессий). FIX-5 (подпись гранта) сознательно не делали (нулевая защита без ключа; protected-path уже гарантирует).
2026-06-07 19:43:05 +03:00

37 lines
2.1 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import { snapshotDecision } from './enforce-snapshot.mjs';
describe('enforce-snapshot snapshotDecision', () => {
const ev = { tool_name: 'Bash', tool_input: { command: 'git reset --hard' }, session_id: 's' };
it('успешный снимок → block:false + записан restore-point', () => {
const writes = [];
const r = snapshotDecision(ev, {
gitImpl: () => ({ stashOut: 'sha9', headOut: 'h', error: false }),
writeImpl: (rec) => writes.push(rec), idImpl: () => 'id1', now: 5,
});
expect(r.block).toBe(false);
expect(writes[0]).toMatchObject({ snapshot_ref: 'refs/floor-snapshots/id1', action: 'bash:git reset --hard' });
});
it('ошибка git на разрушительном → block:true (fail-close), без записи', () => {
const writes = [];
const r = snapshotDecision(ev, { gitImpl: () => ({ error: true }), writeImpl: (rec) => writes.push(rec), idImpl: () => 'id1', now: 5 });
expect(r.block).toBe(true);
expect(writes.length).toBe(0);
});
it('неразрушительное → block:false, снимка нет', () => {
const writes = [];
const r = snapshotDecision({ tool_name: 'Bash', tool_input: { command: 'git status' }, session_id: 's' },
{ gitImpl: () => { throw new Error('git не должен зваться'); }, writeImpl: (rec) => writes.push(rec), idImpl: () => 'x', now: 5 });
expect(r.block).toBe(false);
expect(writes.length).toBe(0);
});
// enforce-snapshot.mjs FIX-4: уникальный id снимка по умолчанию (без idImpl) — два снимка в одну ms не клобберят ref
it('без idImpl два снимка в одну ms → разные snapshot_ref', () => {
const writes = [];
const opts = { gitImpl: () => ({ stashOut: 'sha', headOut: 'h', error: false }), writeImpl: (rec) => writes.push(rec), now: 5 };
snapshotDecision(ev, opts);
snapshotDecision(ev, opts);
expect(writes.length).toBe(2);
expect(writes[0].snapshot_ref).not.toBe(writes[1].snapshot_ref);
});
});