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); }); });