"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const database_1 = require("../walls/06-memory/database/database");
describe('Query Cache', () => {
    beforeEach(() => {
        const { cacheClear } = require('../walls/06-memory/database/query-cache');
        cacheClear();
    });
    test('cacheSet and cacheGet work', () => {
        const { cacheSet, cacheGet } = require('../walls/06-memory/database/query-cache');
        cacheSet('test-key', { hello: 'world' });
        const result = cacheGet('test-key');
        expect(result).toEqual({ hello: 'world' });
    });
    test('cacheGet returns undefined for missing keys', () => {
        const { cacheGet } = require('../walls/06-memory/database/query-cache');
        expect(cacheGet('nonexistent')).toBeUndefined();
    });
    test('cacheClear removes all entries', () => {
        const { cacheSet, cacheGet, cacheClear } = require('../walls/06-memory/database/query-cache');
        cacheSet('a', 1);
        cacheSet('b', 2);
        cacheClear();
        expect(cacheGet('a')).toBeUndefined();
        expect(cacheGet('b')).toBeUndefined();
    });
    test('cacheClear with pattern removes matching entries', () => {
        const { cacheSet, cacheGet, cacheClear } = require('../walls/06-memory/database/query-cache');
        cacheSet('events:list', [1, 2]);
        cacheSet('events:count', 42);
        cacheSet('other', 'x');
        cacheClear('events');
        expect(cacheGet('events:list')).toBeUndefined();
        expect(cacheGet('events:count')).toBeUndefined();
        expect(cacheGet('other')).toBe('x');
    });
    test('cachedQuery caches results', () => {
        const { cachedQuery, getCacheStats } = require('../walls/06-memory/database/query-cache');
        const db = (0, database_1.getDb)();
        db.prepare('DELETE FROM memory_events WHERE project = ?').run('test-cache');
        db.prepare("INSERT INTO memory_events (project, type, summary) VALUES ('test-cache', 'test', 'cache test')").run();
        const result1 = cachedQuery('test-query', 'SELECT project FROM memory_events WHERE project = ?', ['test-cache'], 10000);
        expect(result1.length).toBeGreaterThanOrEqual(1);
        const result2 = cachedQuery('test-query', 'SELECT project FROM memory_events WHERE project = ?', ['test-cache'], 10000);
        expect(result2).toEqual(result1);
    });
    test('invalidateOnWrite clears matching cache patterns', () => {
        const { cacheSet, cacheGet, invalidateOnWrite } = require('../walls/06-memory/database/query-cache');
        cacheSet('project:foo:events', 'data1');
        cacheSet('project:bar:events', 'data2');
        invalidateOnWrite(['foo']);
        expect(cacheGet('project:foo:events')).toBeUndefined();
        expect(cacheGet('project:bar:events')).toBe('data2');
    });
});
describe('Batch Write', () => {
    test('batchWrite executes operations', () => {
        const { batchWrite } = require('../walls/06-memory/database/batch-write');
        const db = (0, database_1.getDb)();
        db.prepare("INSERT INTO memory_events (project, type, summary) VALUES ('test-batch', 'test', 'batch pre')").run();
        const result = batchWrite([
            { sql: "INSERT INTO memory_events (project, type, summary) VALUES ('test-batch', 'test', 'batch a')", params: [] },
            { sql: "INSERT INTO memory_events (project, type, summary) VALUES ('test-batch', 'test', 'batch b')", params: [] },
        ]);
        expect(result.total).toBe(2);
        expect(result.errors).toBe(0);
    });
    test('batchInsert inserts rows', () => {
        const { batchInsert } = require('../walls/06-memory/database/batch-write');
        const result = batchInsert('memory_events', [
            { project: 'test-insert', type: 'test', summary: 'batch insert a' },
            { project: 'test-insert', type: 'test', summary: 'batch insert b' },
            { project: 'test-insert', type: 'test', summary: 'batch insert c' },
        ]);
        expect(result.inserted).toBe(3);
    });
});
//# sourceMappingURL=performance.test.js.map