"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 blob_vault_1 = require("../walls/06-memory/archive/blob-vault");
const TEST_DIR = path.join(__dirname, '__test_blob_vault__');
function cleanup() {
    try {
        fs.rmSync(TEST_DIR, { recursive: true, force: true });
    }
    catch { /* ignore */ }
}
describe('Blob Vault', () => {
    beforeAll(() => {
        cleanup();
        fs.mkdirSync(TEST_DIR, { recursive: true });
    });
    afterEach(cleanup);
    afterAll(cleanup);
    it('writeBlob returns hash', () => {
        const content = Buffer.from('Hello, blob vault!');
        const hash = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        expect(hash).not.toBeNull();
        expect(hash.length).toBe(64); // SHA-256 hex
    });
    it('writeBlob deduplicates same content', () => {
        const content = Buffer.from('Same content for dedup test');
        const hash1 = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        const hash2 = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        expect(hash1).toBe(hash2);
        const meta = (0, blob_vault_1.getBlobMeta)(TEST_DIR, hash1);
        expect(meta).not.toBeNull();
        expect(meta.ref_count).toBe(2);
    });
    it('readBlob returns original content', () => {
        const content = Buffer.from('Round-trip test content');
        const hash = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        expect(hash).not.toBeNull();
        const retrieved = (0, blob_vault_1.readBlob)(TEST_DIR, hash);
        expect(retrieved).not.toBeNull();
        expect(retrieved.equals(content)).toBe(true);
    });
    it('blobExists works', () => {
        const content = Buffer.from('Existence check');
        const hash = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        expect((0, blob_vault_1.blobExists)(TEST_DIR, hash)).toBe(true);
        expect((0, blob_vault_1.blobExists)(TEST_DIR, 'nonexistent')).toBe(false);
    });
    it('getBlobMeta returns correct metadata', () => {
        const content = Buffer.from('Metadata test');
        const hash = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'snapshot');
        const meta = (0, blob_vault_1.getBlobMeta)(TEST_DIR, hash);
        expect(meta).not.toBeNull();
        expect(meta.hash).toBe(hash);
        expect(meta.original_size).toBe(content.length);
        expect(meta.content_type).toBe('snapshot');
        expect(meta.ref_count).toBe(1);
    });
    it('deleteBlob decrements ref_count', () => {
        const content = Buffer.from('Delete test');
        const hash = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event'); // ref_count = 2
        const deleted = (0, blob_vault_1.deleteBlob)(TEST_DIR, hash);
        expect(deleted).toBe(true);
        const meta = (0, blob_vault_1.getBlobMeta)(TEST_DIR, hash);
        expect(meta.ref_count).toBe(1);
    });
    it('deleteBlob removes blob when ref_count=1', () => {
        const content = Buffer.from('Final delete test');
        const hash = (0, blob_vault_1.writeBlob)(TEST_DIR, content, 'event');
        const deleted = (0, blob_vault_1.deleteBlob)(TEST_DIR, hash);
        expect(deleted).toBe(true);
        expect((0, blob_vault_1.blobExists)(TEST_DIR, hash)).toBe(false);
    });
    it('getBlobStats returns stats', () => {
        const stats = (0, blob_vault_1.getBlobStats)(TEST_DIR);
        expect(typeof stats.totalBlobs).toBe('number');
        expect(typeof stats.totalOriginalBytes).toBe('number');
        expect(typeof stats.dedupSavingsPercent).toBe('number');
    });
    it('listBlobs returns array', () => {
        (0, blob_vault_1.writeBlob)(TEST_DIR, Buffer.from('blob1'), 'event');
        (0, blob_vault_1.writeBlob)(TEST_DIR, Buffer.from('blob2'), 'snapshot');
        const blobs = (0, blob_vault_1.listBlobs)(TEST_DIR);
        expect(blobs.length).toBeGreaterThanOrEqual(2);
    });
    it('readBlob returns null for missing hash', () => {
        const result = (0, blob_vault_1.readBlob)(TEST_DIR, 'nonexistent');
        expect(result).toBeNull();
    });
});
//# sourceMappingURL=blob-vault.test.js.map