wishforge-ide/tests/migrations.test.ts
eust-w 0e4ef42771 ♻️ Unify project lessons into self-evolving project skills
Lessons become skills with a category (workflow / pitfall / convention):
one model, one prompt section, one permission rule. A repaired failure
records or updates a pitfall skill deduplicated by failure signature;
harvest sees every skill and may merge into an existing one; corroborated
skills auto-activate per project (skillAutoActivation, default on).
Runs given a skill are counted (skill_usage); platform-maintained skills
retire after 3 failing runs in a row or 90 days unused. Skills can be
imported from SKILL.md / zip, copied between projects and searched across
projects. Migration 004 converts existing lesson rows to skills.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-04 12:37:36 +08:00

59 lines
3.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { applyMigrations, loadMigrations, type Migration } from '../apps/api/src/db/migrate.js';
/** Records statements and keeps schema_migrations in memory, like a database would. */
function database() {
const versions = new Map<string, string>();
const executed: string[] = [];
return {
executed,
versions,
async query(sql: string, values: unknown[] = []) {
if (sql.startsWith('SELECT version, checksum FROM schema_migrations'))
return { rows: [...versions].map(([version, checksum]) => ({ version, checksum })) };
if (sql.startsWith('INSERT INTO schema_migrations')) {
versions.set(String(values[0]), String(values[1]));
return { rows: [] };
}
executed.push(sql);
return { rows: [] };
},
};
}
const migration = (version: string, sql = `-- ${version}`): Migration => ({ version, sql });
describe('versioned database migrations (requirements FR-K2)', () => {
it('ships ordered, uniquely numbered migrations', async () => {
const migrations = await loadMigrations();
const versions = migrations.map(m => m.version);
expect(versions.slice(0, 2)).toEqual(['001_initial', '002_run_contracts']);
expect([...versions].sort()).toEqual(versions);
expect(new Set(versions.map(v => v.slice(0, 3))).size).toBe(versions.length);
// Data migrations must not run silently restricted by row-level security.
expect(migrations[1]!.sql).toContain('SET LOCAL row_security = off');
// Lessons became skills: every lesson row is converted, keeping its id, then removed.
const lessons = migrations.find(m => m.version === '004_lessons_to_skills')!;
expect(lessons.sql).toContain('SET LOCAL row_security = off');
expect(lessons.sql).toContain("WHERE l.kind = 'lesson'");
expect(lessons.sql).toContain('ON CONFLICT (tenant_id, kind, id) DO NOTHING');
expect(lessons.sql).toContain("DELETE FROM entities WHERE kind = 'lesson'");
});
it('applies only what has not run, in order, and never repeats a migration', async () => {
const db = database();
expect(await applyMigrations(db, [migration('001_a'), migration('002_b')])).toEqual(['001_a', '002_b']);
expect(db.executed.filter(sql => sql.startsWith('--'))).toEqual(['-- 001_a', '-- 002_b']);
expect(await applyMigrations(db, [migration('001_a'), migration('002_b')])).toEqual([]);
expect(await applyMigrations(db, [migration('001_a'), migration('002_b'), migration('003_c')])).toEqual(['003_c']);
expect(db.executed.filter(sql => sql.startsWith('--'))).toEqual(['-- 001_a', '-- 002_b', '-- 003_c']);
expect([...db.versions.keys()]).toEqual(['001_a', '002_b', '003_c']);
});
it('refuses to continue when an applied migration was edited', async () => {
const db = database();
await applyMigrations(db, [migration('001_a')]);
await expect(applyMigrations(db, [migration('001_a', '-- edited'), migration('002_b')])).rejects.toThrow(
/001_a changed after it was applied/,
);
expect(db.versions.has('002_b')).toBe(false);
});
});