import { describe, expect, it } from 'vitest';
import { MIGRATIONS } from './migrations.js';

describe('track release-date migration', () => {
  const migration = MIGRATIONS.find(
    ({ id }) => id === '20260801_track_release_dates_from_albums',
  );

  it('backfills from albums with a null-safe, deterministic conflict rule', () => {
    expect(migration).toBeDefined();
    expect(migration!.sql).toContain('ALTER TABLE albums ADD COLUMN IF NOT EXISTS release_date DATE');
    expect(migration!.sql).toContain('UPDATE tracks AS t');
    expect(migration!.sql).toContain('SET release_date = al.release_date');
    expect(migration!.sql).toContain('t.release_date IS DISTINCT FROM al.release_date');
  });

  it('keeps new tracks and album metadata updates synchronized', () => {
    expect(migration!.sql).toContain('BEFORE INSERT OR UPDATE OF album_id, release_date ON tracks');
    expect(migration!.sql).toContain('AFTER UPDATE OF release_date ON albums');
    expect(migration!.sql).toContain('WHEN (OLD.release_date IS DISTINCT FROM NEW.release_date)');
  });
});

describe('Vibe durable session migration', () => {
  const migration = MIGRATIONS.find(
    ({ id }) => id === '20260801_vibe_session_persistence',
  );

  it('creates the event ledger, retry key, and revisioned plan tables', () => {
    expect(migration).toBeDefined();
    expect(migration!.sql).toContain('CREATE TABLE IF NOT EXISTS vibe_sessions');
    expect(migration!.sql).toContain('CREATE TABLE IF NOT EXISTS vibe_events');
    expect(migration!.sql).toContain('UNIQUE (session_id, client_event_id)');
    expect(migration!.sql).toContain('CREATE TABLE IF NOT EXISTS vibe_plan_versions');
    expect(migration!.sql).toContain('UNIQUE (session_id, version)');
    expect(migration!.sql).toContain('CREATE TABLE IF NOT EXISTS vibe_plan_items');
    expect(migration!.sql).toContain('UNIQUE (plan_version_id, track_id)');
  });

  it('indexes session and event reads along their required time axes', () => {
    expect(migration!.sql).toContain('idx_vibe_sessions_user_last_event');
    expect(migration!.sql).toContain('idx_vibe_events_session_occurred');
    expect(migration!.sql).toContain('idx_vibe_events_user_occurred');
  });
});

describe('Vibe context memory migration', () => {
  it('adds bounded session exploration state and exactly-once projection storage', () => {
    const migration = MIGRATIONS.find(({ id }) => id === '20260802_vibe_context_memory_exploration');
    expect(migration?.sql).toContain('CREATE TABLE IF NOT EXISTS vibe_session_profiles');
    expect(migration?.sql).toContain('exploration_coefficient');
    expect(migration?.sql).toContain('vibe_session_feedback_projections');
  });

  it('backfills profiles for durable sessions created before context memory', () => {
    const migration = MIGRATIONS.find(({ id }) => id === '20260802_vibe_session_profile_backfill');
    expect(migration?.sql).toContain('INSERT INTO vibe_session_profiles');
    expect(migration?.sql).toContain('SELECT id, user_id FROM vibe_sessions');
    expect(migration?.sql).toContain('ON CONFLICT (session_id) DO NOTHING');
  });
});
