"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const memory_archive_1 = require("../walls/06-memory/archive/memory-archive");
function makeTmpDir() {
    const dir = path.join(os.tmpdir(), `cell-archive-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
    fs.mkdirSync(dir, { recursive: true });
    return dir;
}
function cleanTmpDir(dir) {
    try {
        fs.rmSync(dir, { recursive: true, force: true });
    }
    catch { }
}
describe('Memory Archive — encrypted + compressed', () => {
    let tmpDir;
    beforeEach(() => {
        tmpDir = makeTmpDir();
    });
    afterEach(() => {
        cleanTmpDir(tmpDir);
    });
    it('appendToArchive creates encrypted file', () => {
        const result = (0, memory_archive_1.appendToArchive)(tmpDir, 'test', [
            { type: 'edit', summary: 'Test record', importance: 5, created_at: new Date().toISOString() },
        ]);
        expect(result).not.toBeNull();
        expect(result.records).toBe(1);
        expect(result.compression).toBe('gzip');
        expect(result.algorithm).toBe('aes-256-gcm');
        expect(result.filePath).toContain('.jsonl.gz.enc');
    });
    it('readArchive returns decrypted records', () => {
        const now = new Date();
        (0, memory_archive_1.appendToArchive)(tmpDir, 'test', [
            { type: 'decision', summary: 'Archived decision', importance: 8, created_at: now.toISOString() },
        ]);
        const records = (0, memory_archive_1.readArchive)(tmpDir, 'test', now);
        expect(records.length).toBeGreaterThanOrEqual(1);
        expect(records[0].summary).toBe('Archived decision');
    });
    it('searchArchive finds matching records', () => {
        const now = new Date();
        (0, memory_archive_1.appendToArchive)(tmpDir, 'test', [
            { type: 'edit', summary: 'Fixed autostart bug', importance: 5, created_at: now.toISOString() },
            { type: 'error', summary: 'Database timeout', importance: 7, created_at: now.toISOString() },
        ]);
        const results = (0, memory_archive_1.searchArchive)(tmpDir, 'test', 'autostart');
        expect(results.length).toBeGreaterThanOrEqual(1);
        expect(results[0].summary).toContain('autostart');
    });
    it('getArchiveStats returns stats', () => {
        (0, memory_archive_1.appendToArchive)(tmpDir, 'test', [
            { type: 'edit', summary: 'Stats test', importance: 3, created_at: new Date().toISOString() },
        ]);
        const stats = (0, memory_archive_1.getArchiveStats)(tmpDir);
        expect(stats.totalChunks).toBeGreaterThanOrEqual(1);
        expect(stats.totalRecords).toBeGreaterThanOrEqual(1);
        expect(stats.totalSizeBytes).toBeGreaterThan(0);
        expect(stats.categories).toHaveProperty('test');
    });
    it('handles multiple categories', () => {
        (0, memory_archive_1.appendToArchive)(tmpDir, 'logs', [
            { type: 'edit', summary: 'A record', importance: 3, created_at: new Date().toISOString() },
        ]);
        (0, memory_archive_1.appendToArchive)(tmpDir, 'events', [
            { type: 'error', summary: 'B record', importance: 7, created_at: new Date().toISOString() },
        ]);
        const stats = (0, memory_archive_1.getArchiveStats)(tmpDir);
        expect(stats.categories).toHaveProperty('logs');
        expect(stats.categories).toHaveProperty('events');
    });
    it('readArchive returns empty for nonexistent category', () => {
        const records = (0, memory_archive_1.readArchive)(tmpDir, 'nonexistent', new Date());
        expect(records).toEqual([]);
    });
});
//# sourceMappingURL=memory-archive.test.js.map