import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import Database from 'better-sqlite3';
import { runMigrations } from '../migrations.js';
describe('scheduler schema', () => {
    let db;
    beforeEach(() => {
        db = new Database(':memory:');
        runMigrations(db);
    });
    afterEach(() => {
        db.close();
    });
    it('should create scheduled_jobs table', () => {
        const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='scheduled_jobs'").all();
        expect(tables).toHaveLength(1);
    });
    it('should insert and query scheduled_jobs', () => {
        db.prepare(`
      INSERT INTO scheduled_jobs (squad_name, cron_expression, prompt)
      VALUES (?, ?, ?)
    `).run('test-squad', '0 9 * * 1', 'Run test');
        const row = db.prepare('SELECT * FROM scheduled_jobs WHERE squad_name = ?').get('test-squad');
        expect(row.squad_name).toBe('test-squad');
        expect(row.cron_expression).toBe('0 9 * * 1');
        expect(row.status).toBe('active');
        expect(row.mode).toBe('autonomous');
        expect(row.timeout_minutes).toBe(30);
    });
    it('should enforce unique squad_name in scheduled_jobs', () => {
        db.prepare(`
      INSERT INTO scheduled_jobs (squad_name, cron_expression, prompt)
      VALUES (?, ?, ?)
    `).run('test-squad', '0 9 * * 1', 'Run test');
        expect(() => {
            db.prepare(`
        INSERT INTO scheduled_jobs (squad_name, cron_expression, prompt)
        VALUES (?, ?, ?)
      `).run('test-squad', '0 10 * * 1', 'Run again');
        }).toThrow();
    });
    it('should have attempt column in squad_activities', () => {
        const columns = db.prepare('PRAGMA table_info(squad_activities)').all();
        const attemptCol = columns.find((c) => c.name === 'attempt');
        expect(attemptCol).toBeDefined();
        expect(attemptCol.dflt_value).toBe('1');
    });
    it('should have index on squad_activities triggered_by', () => {
        const indexes = db.prepare("SELECT name FROM sqlite_master WHERE type='index' AND name='idx_squad_activities_triggered_by'").all();
        expect(indexes).toHaveLength(1);
    });
});
//# sourceMappingURL=scheduler-schema.test.js.map