import assert from 'node:assert/strict';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { closeConnection } from '../../../modules/database/connection.js';
import { initializeDatabase } from '../../../modules/database/init-db.js';
import { sessionsDb } from '../../../modules/database/repositories/sessions.db.js';
import { userDb } from '../../../modules/database/repositories/users.js';
async function withIsolatedDatabase(runTest) {
    const previousDatabasePath = process.env.DATABASE_PATH;
    const tempDirectory = await mkdtemp(path.join(tmpdir(), 'sessions-db-'));
    const databasePath = path.join(tempDirectory, 'auth.db');
    closeConnection();
    process.env.DATABASE_PATH = databasePath;
    await initializeDatabase();
    const created = userDb.createUser('test-user', 'hash');
    const userId = Number(created.id);
    try {
        await runTest(userId);
    }
    finally {
        closeConnection();
        if (previousDatabasePath === undefined) {
            delete process.env.DATABASE_PATH;
        }
        else {
            process.env.DATABASE_PATH = previousDatabasePath;
        }
        await rm(tempDirectory, { recursive: true, force: true });
    }
}
test('session archive queries hide archived rows from active project views', async () => {
    await withIsolatedDatabase((userId) => {
        sessionsDb.createSession(userId, 'session-active', 'claude', '/workspace/demo-project', 'Active Session');
        sessionsDb.createSession(userId, 'session-archived', 'claude', '/workspace/demo-project', 'Archived Session');
        sessionsDb.updateSessionIsArchived(userId, 'session-archived', true);
        const activeSessions = sessionsDb.getAllSessions(userId);
        const archivedSessions = sessionsDb.getArchivedSessions(userId);
        const activeProjectSessions = sessionsDb.getSessionsByProjectPath(userId, '/workspace/demo-project');
        const allProjectSessions = sessionsDb.getSessionsByProjectPathIncludingArchived(userId, '/workspace/demo-project');
        assert.deepEqual(activeSessions.map((session) => session.session_id), ['session-active']);
        assert.deepEqual(archivedSessions.map((session) => session.session_id), ['session-archived']);
        assert.deepEqual(activeProjectSessions.map((session) => session.session_id), ['session-active']);
        assert.deepEqual(allProjectSessions.map((session) => session.session_id).sort(), ['session-active', 'session-archived']);
        assert.equal(sessionsDb.countSessionsByProjectPath(userId, '/workspace/demo-project'), 1);
    });
});
test('createSession reactivates archived rows when the session becomes active again', async () => {
    await withIsolatedDatabase((userId) => {
        sessionsDb.createSession(userId, 'session-reused', 'claude', '/workspace/demo-project', 'First Name');
        sessionsDb.updateSessionIsArchived(userId, 'session-reused', true);
        sessionsDb.createSession(userId, 'session-reused', 'claude', '/workspace/demo-project', 'Updated Name');
        const activeSessions = sessionsDb.getAllSessions(userId);
        const archivedSessions = sessionsDb.getArchivedSessions(userId);
        const restoredSession = sessionsDb.getSessionById(userId, 'session-reused');
        assert.equal(activeSessions.length, 1);
        assert.equal(activeSessions[0]?.session_id, 'session-reused');
        assert.equal(activeSessions[0]?.custom_name, 'Updated Name');
        assert.equal(archivedSessions.length, 0);
        assert.equal(restoredSession?.isArchived, 0);
    });
});
//# sourceMappingURL=sessions.db.integration.test.js.map