wishforge-ide/tests/migrations.test.ts
eust-w 5aea9b73a9 fix: harden the control plane's storage, identities and recovery paths
- Storage: read-only queries use a read-only transaction without the tenant
  lock, so event streams and dashboards no longer queue behind writers; lists
  filter on indexed project, run and status fields and the run list pages with
  limit/before. Dispatchers claim outbox items with SKIP LOCKED and a claim
  deadline; delivered items and old idempotency records are pruned hourly.
- Migrations are versioned files recorded with checksums in schema_migrations;
  002 adds the claim column and run index and migrates preview-stage runs and
  free-text waiting reasons. Run events and usage are append-only for the app.
- Identities: static service tokens reach only their routes (coordinator: state,
  wait, repair; deployer: lease, transition, releases); the static verifier
  identity is gone; Git credentials go only to the lease holder; internal bodies
  are validated; framework and body-parser errors use the API error envelope.
- Quotas: only queued or running runs (or ones with an unsettled model request)
  hold token reservations; continuing a run reserves its remaining budget again.
- Recovery: owners can list and reconcile model requests with an unknown outcome,
  and requests whose proxy died are marked unknown; definite deployment failures
  free the environment, admins can unlock an environment after checking the
  target, approvals expire with their release, and production approvals need a
  second person. The builder's Docker context is operator configuration.
- MCP: list_projects, absolute console links, request_release resolves the run's
  evidence and default environment, and tool changes are audited as the user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 16:16:47 +08:00

53 lines
2.6 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');
});
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);
});
});