/**
 * Single-source rule for drizzle migration state:
 *   - Reads ONLY from `<project>/quickback/drizzle/...`.
 *   - Anything under project-root `drizzle/...` is ignored.
 *
 * This is what prevents the "duplicate migrations every compile" bug:
 * if the CLI ever falls back to a stale project-root `drizzle/` while
 * the canonical state lives under `quickback/drizzle/`, drizzle-kit
 * sees an older journal than what's on disk and regenerates a fresh
 * migration to fill the perceived gap, every time.
 */
import { describe, expect, test } from 'bun:test';
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { loadDrizzleMeta } from './file-loader.js';
async function makeFixture(shape) {
    const root = await mkdtemp(join(tmpdir(), 'qb-meta-'));
    for (const [path, content] of Object.entries(shape)) {
        const full = join(root, path);
        await mkdir(join(full, '..'), { recursive: true });
        await writeFile(full, content);
    }
    return root;
}
describe('loadDrizzleMeta — single source of truth', () => {
    test('reads from quickback/drizzle and ignores stray project-root drizzle/', async () => {
        const root = await makeFixture({
            // Stray project-root copy must not be read at all.
            'drizzle/features/meta/_journal.json': '{"tag":"STRAY"}',
            'drizzle/features/meta/0000_snap.json': '{"stray":true}',
            'drizzle/features/0000_initial.sql': '-- stray sql',
            // Canonical location.
            'quickback/drizzle/features/0000_initial.sql': '-- canonical sql',
            'quickback/drizzle/features/meta/_journal.json': '{"tag":"CANONICAL","version":"7"}',
            'quickback/drizzle/features/meta/0000_snap.json': '{"canonical":true}',
            'quickback/drizzle/features/meta/0001_snap.json': '{"canonical":true}',
        });
        try {
            const meta = await loadDrizzleMeta(root);
            expect(meta).toBeDefined();
            // Wire keys include the `quickback/` prefix so the server stages
            // them where drizzle.config.ts's `out:` looks on the temp dir.
            expect(meta['quickback/drizzle/features/meta/_journal.json']).toContain('CANONICAL');
            expect(meta['quickback/drizzle/features/meta/_journal.json']).not.toContain('STRAY');
            expect(meta['quickback/drizzle/features/0000_initial.sql']).toBe('-- canonical sql');
            expect(meta['quickback/drizzle/features/meta/0000_snap.json']).toContain('"canonical":true');
            expect(meta['quickback/drizzle/features/meta/0000_snap.json']).not.toContain('"stray":true');
            // 0001 only exists in quickback — confirms quickback is the source.
            expect(meta['quickback/drizzle/features/meta/0001_snap.json']).toBeDefined();
        }
        finally {
            await rm(root, { recursive: true, force: true });
        }
    });
    test('returns undefined when only stray project-root drizzle/ exists', async () => {
        const root = await makeFixture({
            'drizzle/features/meta/_journal.json': '{"tag":"STRAY"}',
        });
        try {
            // No fallback — Quickback owns migration state inside quickback/.
            expect(await loadDrizzleMeta(root)).toBeUndefined();
        }
        finally {
            await rm(root, { recursive: true, force: true });
        }
    });
    test('returns undefined when quickback/ exists but has no drizzle subdir', async () => {
        // A quickback/ dir can exist just to hold definitions, with no state yet.
        const root = await makeFixture({
            'quickback/features/placeholder.ts': '// definitions only',
        });
        try {
            expect(await loadDrizzleMeta(root)).toBeUndefined();
        }
        finally {
            await rm(root, { recursive: true, force: true });
        }
    });
    test('returns undefined when neither location has meta', async () => {
        const root = await makeFixture({ 'quickback.config.ts': '// config only' });
        try {
            expect(await loadDrizzleMeta(root)).toBeUndefined();
        }
        finally {
            await rm(root, { recursive: true, force: true });
        }
    });
    test('loads all scoped folders (auth, features, files, webhooks, audit)', async () => {
        const root = await makeFixture({
            'quickback/drizzle/auth/0000_initial.sql': '-- auth',
            'quickback/drizzle/auth/meta/_journal.json': '{"scope":"auth"}',
            'quickback/drizzle/features/0000_initial.sql': '-- features',
            'quickback/drizzle/features/meta/_journal.json': '{"scope":"features"}',
            'quickback/drizzle/files/0000_initial.sql': '-- files',
            'quickback/drizzle/files/meta/_journal.json': '{"scope":"files"}',
            'quickback/drizzle/webhooks/0000_initial.sql': '-- webhooks',
            'quickback/drizzle/webhooks/meta/_journal.json': '{"scope":"webhooks"}',
            'quickback/drizzle/audit/0000_initial.sql': '-- audit',
            'quickback/drizzle/audit/meta/_journal.json': '{"scope":"audit"}',
        });
        try {
            const meta = await loadDrizzleMeta(root);
            expect(meta).toBeDefined();
            expect(Object.keys(meta).sort()).toEqual([
                'quickback/drizzle/audit/0000_initial.sql',
                'quickback/drizzle/audit/meta/_journal.json',
                'quickback/drizzle/auth/0000_initial.sql',
                'quickback/drizzle/auth/meta/_journal.json',
                'quickback/drizzle/features/0000_initial.sql',
                'quickback/drizzle/features/meta/_journal.json',
                'quickback/drizzle/files/0000_initial.sql',
                'quickback/drizzle/files/meta/_journal.json',
                'quickback/drizzle/webhooks/0000_initial.sql',
                'quickback/drizzle/webhooks/meta/_journal.json',
            ]);
        }
        finally {
            await rm(root, { recursive: true, force: true });
        }
    });
    test('does not fall back to legacy single-db `quickback/drizzle/meta` layout', async () => {
        const root = await makeFixture({
            'quickback/drizzle/0000_initial.sql': '-- single',
            'quickback/drizzle/meta/_journal.json': '{"mode":"single-db"}',
        });
        try {
            const meta = await loadDrizzleMeta(root);
            expect(meta).toBeUndefined();
        }
        finally {
            await rm(root, { recursive: true, force: true });
        }
    });
});
//# sourceMappingURL=drizzle-meta-routing.test.js.map