"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 archive_versioning_1 = require("../walls/06-memory/archive/archive-versioning");
const TEST_DIR = path.join(__dirname, '__test_archive_versioning__');
function cleanup() {
    try {
        fs.rmSync(TEST_DIR, { recursive: true, force: true });
    }
    catch { /* ignore */ }
}
describe('Archive Versioning', () => {
    beforeAll(cleanup);
    afterEach(cleanup);
    afterAll(cleanup);
    it('readArchiveChunkMeta returns null for missing chunk', () => {
        const result = (0, archive_versioning_1.readArchiveChunkMeta)(TEST_DIR, 'events', new Date());
        expect(result).toBeNull();
    });
    it('readArchiveChunkMeta reads v1 metadata (backward compat)', () => {
        const date = new Date();
        const year = date.getFullYear().toString();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const dir = path.join(TEST_DIR, '.cell', 'memory', 'archive', year, month);
        fs.mkdirSync(dir, { recursive: true });
        // Write v1-style metadata (no format_version)
        const dateStr = date.toISOString().slice(0, 10);
        const metaPath = path.join(dir, `events-${dateStr}.meta.json`);
        fs.writeFileSync(metaPath, JSON.stringify({
            chunk_type: 'events',
            project: 'test',
            date: dateStr,
            event_count: 5,
            created_at: new Date().toISOString(),
        }));
        const result = (0, archive_versioning_1.readArchiveChunkMeta)(TEST_DIR, 'events', date);
        expect(result).not.toBeNull();
        expect(result.format_version).toBe(1); // defaults to 1
        expect(result.event_count).toBe(5);
        expect(result.compression).toBe('gzip');
    });
    it('readArchiveChunkMeta reads v2 metadata', () => {
        const date = new Date();
        const year = date.getFullYear().toString();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const dir = path.join(TEST_DIR, '.cell', 'memory', 'archive', year, month);
        fs.mkdirSync(dir, { recursive: true });
        const dateStr = date.toISOString().slice(0, 10);
        const metaPath = path.join(dir, `events-${dateStr}.meta.json`);
        fs.writeFileSync(metaPath, JSON.stringify({
            format_version: archive_versioning_1.ARCHIVE_FORMAT_VERSION,
            compression: 'gzip',
            encryption: 'aes-256-gcm',
            chunk_type: 'events',
            project: 'test',
            date: dateStr,
            event_count: 10,
            created_at: new Date().toISOString(),
        }));
        const result = (0, archive_versioning_1.readArchiveChunkMeta)(TEST_DIR, 'events', date);
        expect(result).not.toBeNull();
        expect(result.format_version).toBe(archive_versioning_1.ARCHIVE_FORMAT_VERSION);
        expect(result.event_count).toBe(10);
    });
    it('listArchiveChunks returns empty for no archive', () => {
        const result = (0, archive_versioning_1.listArchiveChunks)(TEST_DIR);
        expect(result).toEqual([]);
    });
    it('verifyArchiveIntegrity reports issues for old chunks', () => {
        const date = new Date();
        const year = date.getFullYear().toString();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const dir = path.join(TEST_DIR, '.cell', 'memory', 'archive', year, month);
        fs.mkdirSync(dir, { recursive: true });
        // Create an .enc file and a v1 meta
        const dateStr = date.toISOString().slice(0, 10);
        fs.writeFileSync(path.join(dir, `events-${dateStr}.enc`), 'fake-data');
        fs.writeFileSync(path.join(dir, `events-${dateStr}.meta.json`), JSON.stringify({
            chunk_type: 'events',
            project: 'test',
            date: dateStr,
            event_count: 3,
            created_at: new Date().toISOString(),
        }));
        // Create an unversioned chunk (no meta)
        fs.writeFileSync(path.join(dir, `sessions-${dateStr}.enc`), 'fake-data');
        const result = (0, archive_versioning_1.verifyArchiveIntegrity)(TEST_DIR);
        expect(result.totalChunks).toBe(2);
        expect(result.versionedChunks).toBe(1);
        expect(result.unversionedChunks).toBe(1);
        expect(result.issues.length).toBeGreaterThan(0);
    });
    it('ARCHIVE_FORMAT_VERSION is 2', () => {
        expect(archive_versioning_1.ARCHIVE_FORMAT_VERSION).toBe(2);
    });
});
//# sourceMappingURL=archive-versioning.test.js.map