import { afterEach, beforeEach, describe, expect, test } from 'bun:test' import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { join } from 'node:path' import { tmpdir } from 'node:os' // --------------------------------------------------------------------------- // Import the real module directly — no mocks. // We test what's testable: the exported functions exist, result shapes, and // SQLite preprocessing which is pure filesystem logic. // --------------------------------------------------------------------------- const { runDatabaseMigration, resetDatabase: resetDatabaseFn, generateMigrations, generateMigrations2, } = await import('../src/migrations') // --------------------------------------------------------------------------- // Tests — function exports and result shapes // --------------------------------------------------------------------------- describe('Database Migrations', () => { test('fresh resets preserve the committed migration corpus and model snapshot', () => { const source = readFileSync(join(import.meta.dir, '..', 'src', 'migrations.ts'), 'utf8') const calls = source.match(/qbResetDatabase\([^\n]+preserveMigrationState: true/g) ?? [] expect(calls).toHaveLength(2) }) test('corpus regeneration requests a full model emit', () => { const source = readFileSync(join(import.meta.dir, '..', 'src', 'migrations.ts'), 'utf8') // The emit lives in `generateFullModelMigration`, which regeneration and // `migrate:align` share. const generation = source.slice(source.indexOf('export async function generateFullModelMigration'), source.indexOf('export interface RegeneratedCorpus')) const regeneration = source.slice(source.indexOf('export async function regenerateMigrationCorpus')) expect(generation).toContain('full: true') expect(regeneration).toContain('generateFullModelMigration(') }) test('corpus regeneration persists its dialect snapshot after SQL files', () => { const source = readFileSync(join(import.meta.dir, '..', 'src', 'migrations.ts'), 'utf8') const regeneration = source.slice(source.indexOf('export async function regenerateMigrationCorpus')) const writes = regeneration.indexOf('writableGroups.forEach') const snapshot = regeneration.indexOf('saveMigrationSnapshot(plan') expect(writes).toBeGreaterThan(-1) expect(snapshot).toBeGreaterThan(writes) }) test('runDatabaseMigration is a function', () => { expect(typeof runDatabaseMigration).toBe('function') }) test('resetDatabase is a function', () => { expect(typeof resetDatabaseFn).toBe('function') }) test('generateMigrations is a function', () => { expect(typeof generateMigrations).toBe('function') }) test('generateMigrations2 is a function', () => { expect(typeof generateMigrations2).toBe('function') }) test('runDatabaseMigration is a callable function', () => { // NOTE: We do NOT call migration functions in tests — they modify the DB schema. expect(typeof runDatabaseMigration).toBe('function') }) test('generateMigrations is a callable function', () => { expect(typeof generateMigrations).toBe('function') }) test('resetDatabase is a callable function', () => { // NOTE: We do NOT call resetDatabase() in tests — it drops all tables. expect(typeof resetDatabaseFn).toBe('function') }) test('MigrationResult type has correct shape', () => { // Verify the exported interface shape at runtime const result = { migrationName: '001_create_users', direction: 'Up' as const, status: 'Success' as const, } expect(result.migrationName).toBe('001_create_users') expect(result.direction).toBe('Up') expect(result.status).toBe('Success') }) }) // --------------------------------------------------------------------------- // SQLite preprocessing tests (using real filesystem in temp dir) // --------------------------------------------------------------------------- describe('SQLite migration preprocessing', () => { const tempDir = join(tmpdir(), `stacks-migration-test-${Date.now()}`) const migrationsDir = join(tempDir, 'database', 'migrations') const originalCwd = process.cwd() beforeEach(() => { mkdirSync(migrationsDir, { recursive: true }) process.chdir(tempDir) }) afterEach(() => { process.chdir(originalCwd) try { rmSync(tempDir, { recursive: true, force: true }) } catch { // ignore cleanup errors } }) test('migration SQL files can be written to temp dir', () => { const migrationFile = join(migrationsDir, '001_test.sql') writeFileSync(migrationFile, 'SELECT 1') const content = readFileSync(migrationFile, 'utf-8') expect(content).toBe('SELECT 1') }) test('migration files with ALTER TABLE pattern are detectable', () => { const sql = 'ALTER TABLE posts ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id)' expect(sql).toContain('ALTER TABLE') expect(sql).toContain('ADD CONSTRAINT') }) })