"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const path_1 = require("path");
const promises_1 = require("fs/promises");
const os_1 = require("os");
const persistence_js_1 = require("./persistence.js");
const file_ops_js_1 = require("../data-store/file-ops.js");
function makeItem(overrides = {}) {
    return {
        id: overrides.id ?? 'item-1',
        type: overrides.type ?? 'chat_response',
        priority: overrides.priority ?? 2,
        status: overrides.status ?? 'pending',
        characterId: overrides.characterId ?? 'char-1',
        payload: overrides.payload ?? {},
        retryCount: overrides.retryCount ?? 0,
        maxRetries: overrides.maxRetries ?? 3,
        createdAt: overrides.createdAt ?? '2024-01-01T00:00:00Z',
        updatedAt: overrides.updatedAt ?? '2024-01-01T00:00:00Z',
        error: overrides.error,
    };
}
(0, vitest_1.describe)('QueuePersistence', () => {
    let tempDir;
    let persistence;
    (0, vitest_1.beforeEach)(async () => {
        tempDir = await (0, promises_1.mkdtemp)((0, path_1.resolve)((0, os_1.tmpdir)(), 'queue-persist-'));
        persistence = new persistence_js_1.QueuePersistence(tempDir);
        await persistence.init();
    });
    (0, vitest_1.afterEach)(async () => {
        await (0, promises_1.rm)(tempDir, { recursive: true, force: true });
    });
    (0, vitest_1.describe)('init', () => {
        (0, vitest_1.it)('should create the queue directory', async () => {
            const fresh = new persistence_js_1.QueuePersistence(tempDir);
            await fresh.init();
            // No error means directory was created successfully
        });
    });
    (0, vitest_1.describe)('savePending', () => {
        (0, vitest_1.it)('should separate pending and failed items into different files', async () => {
            const items = [
                makeItem({ id: 'p1', status: 'pending' }),
                makeItem({ id: 'p2', status: 'processing' }),
                makeItem({ id: 'f1', status: 'failed', error: 'timeout' }),
                makeItem({ id: 'c1', status: 'completed' }),
            ];
            await persistence.savePending(items);
            const pendingFile = await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(tempDir, 'queue', 'pending.json'));
            const failedFile = await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(tempDir, 'queue', 'failed.json'));
            (0, vitest_1.expect)(pendingFile).toHaveLength(2);
            (0, vitest_1.expect)(pendingFile.map(i => i.id).sort()).toEqual(['p1', 'p2']);
            (0, vitest_1.expect)(failedFile).toHaveLength(1);
            (0, vitest_1.expect)(failedFile[0].id).toBe('f1');
        });
        (0, vitest_1.it)('should write empty arrays when no matching items', async () => {
            await persistence.savePending([]);
            const pendingFile = await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(tempDir, 'queue', 'pending.json'));
            const failedFile = await (0, file_ops_js_1.readJSON)((0, path_1.resolve)(tempDir, 'queue', 'failed.json'));
            (0, vitest_1.expect)(pendingFile).toEqual([]);
            (0, vitest_1.expect)(failedFile).toEqual([]);
        });
    });
    (0, vitest_1.describe)('loadPending', () => {
        (0, vitest_1.it)('should return empty array when no file exists', async () => {
            const items = await persistence.loadPending();
            (0, vitest_1.expect)(items).toEqual([]);
        });
        (0, vitest_1.it)('should load pending items from file', async () => {
            await persistence.savePending([
                makeItem({ id: 'a', status: 'pending' }),
                makeItem({ id: 'b', status: 'pending' }),
            ]);
            const loaded = await persistence.loadPending();
            (0, vitest_1.expect)(loaded).toHaveLength(2);
            (0, vitest_1.expect)(loaded[0].id).toBe('a');
            (0, vitest_1.expect)(loaded[1].id).toBe('b');
        });
        (0, vitest_1.it)('should reset processing items back to pending on load', async () => {
            await persistence.savePending([
                makeItem({ id: 'x', status: 'processing' }),
            ]);
            const loaded = await persistence.loadPending();
            (0, vitest_1.expect)(loaded).toHaveLength(1);
            (0, vitest_1.expect)(loaded[0].status).toBe('pending');
        });
    });
    (0, vitest_1.describe)('loadFailed', () => {
        (0, vitest_1.it)('should return empty array when no file exists', async () => {
            const items = await persistence.loadFailed();
            (0, vitest_1.expect)(items).toEqual([]);
        });
        (0, vitest_1.it)('should load failed items from file', async () => {
            await persistence.savePending([
                makeItem({ id: 'f1', status: 'failed', error: 'err1' }),
                makeItem({ id: 'f2', status: 'failed', error: 'err2' }),
            ]);
            const loaded = await persistence.loadFailed();
            (0, vitest_1.expect)(loaded).toHaveLength(2);
            (0, vitest_1.expect)(loaded[0].error).toBe('err1');
        });
    });
    (0, vitest_1.describe)('loadAll', () => {
        (0, vitest_1.it)('should combine pending and failed items', async () => {
            await persistence.savePending([
                makeItem({ id: 'p1', status: 'pending' }),
                makeItem({ id: 'f1', status: 'failed', error: 'err' }),
            ]);
            const all = await persistence.loadAll();
            (0, vitest_1.expect)(all).toHaveLength(2);
            const ids = all.map(i => i.id).sort();
            (0, vitest_1.expect)(ids).toEqual(['f1', 'p1']);
        });
        (0, vitest_1.it)('should return empty array when no files exist', async () => {
            const all = await persistence.loadAll();
            (0, vitest_1.expect)(all).toEqual([]);
        });
    });
    (0, vitest_1.describe)('round-trip persistence', () => {
        (0, vitest_1.it)('should preserve all item fields through save and load', async () => {
            const original = makeItem({
                id: 'rt-1',
                type: 'image_generation',
                priority: 3,
                status: 'pending',
                characterId: 'char-42',
                payload: { prompt: 'test prompt', model: 'flux-2' },
                retryCount: 1,
                maxRetries: 5,
                createdAt: '2024-06-15T10:30:00Z',
                updatedAt: '2024-06-15T10:31:00Z',
            });
            await persistence.savePending([original]);
            const loaded = await persistence.loadPending();
            (0, vitest_1.expect)(loaded).toHaveLength(1);
            (0, vitest_1.expect)(loaded[0]).toEqual(original);
        });
    });
});
//# sourceMappingURL=persistence.test.js.map