Files
STCourses/packages/db/test/migrate.test.ts
T
Lucas WintherandClaude Opus 5.5 777ae2cc37 feat: resolve Phase 1 contract requests and compose every feature
Contracts and core:
- Feature gains auth and stop; FeatureFactory; core composeServer builds the
  context, runs the factories, then the job queue from the union of their jobs
  (R8/R14). startServer uses it; main.ts composes learner, import, studio, AI.
- AppConfig/loadConfig own the layout and settings: courseDirs include
  <dataDir>/courses and <dataDir>/worktrees, studioReposDir, cacheDir,
  importRoots, uploadMaxMb, aiAllowedBaseUrls, transcribeUrl, python.
  resolveConfig derives the same for tests; cachePaths shares the importer's
  cache layout.
- canView: the global author role views every course (R14); the learner,
  studio and AI packages all call it.
- migrate(db, packages): core first, then packages in name order.
- Media: nosniff on every response, a sandbox CSP on svg/html/js; a Range on
  an empty file is 416.
- courseUrl/lessonUrl and the lesson route patterns live in @stc/contracts.

Packages:
- import, studio and AI read AppConfig instead of process.env; the importer
  refuses ids used by studio/repos.
- learner migrations run through core migrate; library covers fall back to
  the importer's cached candidate behind a canView-checked route.
- studio preview renders through the learner LessonView (anchors honoured,
  draft URLs); AI drafts go to the studio draft branch for studio courses (as
  the requesting user) and to <dataDir>/drafts otherwise, and the generator
  page says which.

Web: tsconfig paths for @stc/<feature>/web browser entries; features.ts
imports them by package name; nav.roles hides links by role.

Tests: compose, config, canView, media headers, migrations order, covers,
preview, nav, SPA bundle without server code, and an end-to-end flow on
SQLite and Postgres.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 15:02:46 +01:00

52 lines
3.3 KiB
TypeScript

import { expect } from "bun:test";
import { forEachDb } from "./helpers";
import { migrate } from "../src/migrate";
forEachDb("applies 0001 once", async (db) => {
// helpers already migrated once; a second run applies nothing
expect(await migrate(db)).toEqual([]);
const rows = await db.sql`SELECT name FROM schema_migrations`;
expect(rows.map((r: { name: string }) => r.name)).toEqual(["0001_init.sql"]);
});
forEachDb("all tables exist", async (db) => {
const s = db.sql;
const t = "2026-01-01T00:00:00.000Z";
const one = async (table: string, rows: Promise<unknown>) => { await rows; expect((await s.unsafe(`SELECT COUNT(*) AS n FROM ${table}`))[0].n.toString()).toBe("1"); };
await one("user_roles", s`INSERT INTO user_roles VALUES ('u','owner')`);
await one("groups", s`INSERT INTO groups VALUES ('g','G')`);
await one("group_members", s`INSERT INTO group_members VALUES ('g','u')`);
await one("course_authors", s`INSERT INTO course_authors VALUES ('c','u')`);
await one("course_acl", s`INSERT INTO course_acl VALUES ('c','user','u')`);
await one("progress", s`INSERT INTO progress VALUES ('u','c','l','done',1.5,${t})`);
await one("notes", s`INSERT INTO notes VALUES ('n','u','c','l','none',NULL,NULL,'b',${t},${t})`);
await one("exercise_attempts", s`INSERT INTO exercise_attempts VALUES ('u','c','l','py','x',NULL,1,${t})`);
await one("user_api_keys", s`INSERT INTO user_api_keys VALUES ('u','p','ct',${t})`);
await one("ai_settings", s`INSERT INTO ai_settings VALUES ('server','',0,'{}',${t})`);
await one("ai_conversations", s`INSERT INTO ai_conversations VALUES ('a','u','c',NULL,${t})`);
await one("ai_messages", s`INSERT INTO ai_messages VALUES ('m','a','user','hi',${t})`);
await one("library_settings", s`INSERT INTO library_settings VALUES ('k','v')`);
await one("collections", s`INSERT INTO collections VALUES ('co','T',0)`);
await one("collection_courses", s`INSERT INTO collection_courses VALUES ('co','c',0)`);
await one("jobs", s`INSERT INTO jobs VALUES ('j','k','queued',0,NULL,'{}',NULL,NULL,${t},${t})`);
});
forEachDb("package migrations run after core, in package-name order, once", async (db) => {
const { mkdtempSync, writeFileSync, mkdirSync } = await import("node:fs");
const { join } = await import("node:path");
const { tmpdir } = await import("node:os");
const root = mkdtempSync(join(tmpdir(), "stc-mig-"));
const a = join(root, "a"), z = join(root, "z");
mkdirSync(a); mkdirSync(z);
// z's table is referenced by a's second file only through ordering by package name: "alpha" runs before "zeta".
writeFileSync(join(z, "0001_zeta.sql"), "CREATE TABLE zeta_t (id TEXT PRIMARY KEY);");
writeFileSync(join(a, "0001_alpha.sql"), "CREATE TABLE alpha_t (id TEXT PRIMARY KEY);");
writeFileSync(join(a, "0002_alpha.sql"), "INSERT INTO alpha_t VALUES ('x');");
expect(await migrate(db, { zeta: [z], alpha: [a] })).toEqual(["0001_alpha.sql", "0002_alpha.sql", "0001_zeta.sql"]);
expect(await migrate(db, { zeta: [z], alpha: [a] })).toEqual([]);
expect((await db.sql`SELECT id FROM alpha_t`).length).toBe(1);
// the same file name in two packages is refused rather than silently skipped
writeFileSync(join(z, "0001_alpha.sql"), "SELECT 1;");
await expect(migrate(db, { zeta: [z], alpha: [a] })).rejects.toThrow(/exists in both/);
});