import { afterEach, describe, expect, test } from 'bun:test' import { Database } from 'bun:sqlite' import { mkdtempSync, rmSync } from 'fs' import { tmpdir } from 'os' import { join } from 'path' import { createEvidenceStore } from '../../src/artifacts/EvidenceStore.js' import { createNullEventIngestor } from '../../src/events/EventIngestor.js' describe('EvidenceStore SQLite persistence', () => { const created: string[] = [] afterEach(() => { for (const dir of created.splice(0)) rmSync(dir, { recursive: true, force: true }) }) test('persists evidence refs in SQLite and can list them by entity', async () => { const dir = mkdtempSync(join(tmpdir(), 'air-evidence-store-')) created.push(dir) const dbPath = join(dir, 'evidence.db') const db1 = new Database(dbPath) const store1 = createEvidenceStore('session_evidence' as any, db1, createNullEventIngestor() as any) const createdRef = await store1.create({ kind: 'command_output', ref: 'artifact://stdout.txt', claim: 'command produced expected output', task_id: 'task_1' as any, location_json: { line: 1 }, }) expect(createdRef.evidence_ref_id).toStartWith('evi_') expect((await store1.list_for_entity('task', 'task_1'))[0]).toMatchObject({ evidence_ref_id: createdRef.evidence_ref_id, kind: 'command_output', ref: 'artifact://stdout.txt', claim: 'command produced expected output', location_json: { line: 1 }, }) db1.close() const db2 = new Database(dbPath) const rows = db2.query('SELECT evidence_ref_id, session_id, kind, ref, claim, location_json, task_id FROM evidence_refs').all() as any[] expect(rows).toHaveLength(1) expect(rows[0]).toMatchObject({ evidence_ref_id: createdRef.evidence_ref_id, session_id: 'session_evidence', kind: 'command_output', ref: 'artifact://stdout.txt', claim: 'command produced expected output', task_id: 'task_1', }) expect(JSON.parse(rows[0].location_json)).toEqual({ line: 1 }) expect(db2.query("PRAGMA journal_mode").get()).toEqual({ journal_mode: 'wal' }) db2.close() }) })