Files
heidi/lib/db/course-migration.test.ts
CatoandClaude 083dd52f0d feat(engine): courses in data, pack-declared forms, level sources, and meanings in the learner's language (#355)
* feat(course): every stored record names its course, and today's progress is the Zurich course's

A course registry (lib/course/registry.ts) lists the one course there is;
lib/variety/active.ts resolves its pack. Browser stores that hold course
progress move to an envelope keyed by course under .v2 keys, and a value
under the old .v1 key is read as the default course without being rewritten.
progress_devices, certificates and entitlements gain a course column that
defaults to the Zurich course. Item ids are unique across courses, and the
default course's ids are unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* refactor(variety): the pack declares its articles, persons, respellings, voices and prompt lines

Article, FormLabel and VocabularyGroup were closed unions of the Zurich
values, the answer comparison folded Zurich respellings for every language,
word distance lower-cased as German, the voice ranking named two German tags
and the prompt builder carried Swiss spelling advice. Each is now a field of
the pack, read by the engine; a contract test over every pack refuses a value
the pack did not declare, and the leak test also looks for the taught
language's own words in engine code. The Ukrainian pack gains one verb with
its own pronouns and no articles. The 2,956 published items and the system
prompt are byte-identical.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* refactor(practice): an item's level comes from a source the course chooses

A course entered from a sibling language measures each item's distance from
that language, exactly as before; a course entered from zero reads a declared
CEFR band on the word, the line or the topic. levels.ts exposes both as
LevelSource and picks by Course.entry. Every published item keeps its level.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* feat(meaning): a meaning can be shown in the learner's language

A meaning is the bridge form plus an optional gloss in the interface
language. One pure rule (lib/domain/meaning/meaning.ts) decides what is
printed from the learner's answer to «Wie gut ist dein Deutsch?»: good shows
German only, as before and by default; some shows German and the gloss; none
shows the gloss; without a gloss German is always shown. Every exercise, the
word list and the scene pages print meanings through one component. The
answer is a course store, set in the settings and asked once after the start
card, only in an interface language that has glosses. Gloss sets are one
file per course and language, loaded on demand.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* content(glosses): English and Russian meanings for every word and scene line; French and Italian for every word

Machine drafts from the Zurich form and its German meaning together, marked
unreviewed in the data. One file per language beside the pack, keyed by the
German the course gives. Tests refuse a key no entry carries, an empty gloss
and two glosses for one German form; pnpm run glosses:coverage prints what
is covered.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* content(glosses): French and Italian meanings for every scene line

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* docs: courses, pack-declared forms, level sources and glosses, as they now are

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

* refactor(variety): the pack says which grammatical person each of its labels is

The gloss check and the prompt's auxiliary table, both new on main, matched
the Zurich labels «ich» and «er» by name. The forms pass of the leak test
caught it on rebase. The pack now declares personOf, and both read it; the
system prompt and the published items are unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-25 14:17:35 +01:00

66 lines
3.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { PGlite } from "@electric-sql/pglite";
import { DEFAULT_COURSE } from "../course/registry.ts";
/**
* The course migration, run over rows as they are on the live database.
*
* The deploy applies new migration files to a database that already holds
* learners' synced progress, their certificates and their passes. So: build
* the schema as it was the day before, write rows the way the code of that
* day wrote them, apply everything since, and check that nothing was lost,
* every old row belongs to the default course, and an insert that does not
* name a course (a server still running the old code during the deploy)
* still succeeds.
*/
const DIR = new URL("../../drizzle/", import.meta.url);
const FILES = readdirSync(DIR).filter((f) => /^\d{4}_.*\.sql$/.test(f)).sort();
const COURSE = "0015_course.sql";
async function apply(db: PGlite, file: string): Promise<void> {
for (const statement of readFileSync(new URL(file, DIR), "utf8").split("--> statement-breakpoint")) {
if (statement.trim()) await db.exec(statement);
}
}
test("rows written before courses existed survive the migration as the default course", async () => {
const at = FILES.indexOf(COURSE);
assert.ok(at > 0, "the course migration is missing");
const db = new PGlite();
for (const file of FILES.slice(0, at)) await apply(db, file);
const model = { topics: {}, scenes: { restaurant: { asked: 4, missed: 1 } }, groups: {}, words: {}, skills: {}, levels: {}, lines: {} };
await db.query(`insert into progress_devices (actor_id, device_id, key, value) values ('alice', 'phone-aaaaaaaaaaaaaaaa', 'model', $1)`, [
JSON.stringify(model),
]);
await db.exec(`insert into certificates (actor_id, scene_id, askable, held, stuck) values ('alice', 'restaurant', 10, 9, 0)`);
await db.exec(`insert into entitlements (actor_id, plan_id, expires_at) values ('alice', 'month', now() + interval '30 days')`);
for (const file of FILES.slice(at)) await apply(db, file);
for (const table of ["progress_devices", "certificates", "entitlements"]) {
const { rows } = await db.query<{ course: string }>(`select course from ${table}`);
assert.deepEqual(rows, [{ course: DEFAULT_COURSE.id }], `${table}: the old row is not the default course's`);
}
const kept = await db.query<{ value: unknown }>(`select value from progress_devices`);
assert.deepEqual(kept.rows[0]!.value, model, "the synced record changed");
// The statement the previous release runs, unchanged, against the new schema.
await db.exec(
`insert into progress_devices (actor_id, device_id, key, value) values ('alice', 'phone-aaaaaaaaaaaaaaaa', 'model', '{}')
on conflict (actor_id, device_id, key) do update set value = excluded.value`,
);
await db.exec(`insert into certificates (actor_id, scene_id, askable, held, stuck) values ('bob', 'doctor', 8, 8, 0)`);
const after = await db.query<{ n: number }>(`select count(*)::int as n from certificates where course = $1`, [DEFAULT_COURSE.id]);
assert.equal(after.rows[0]!.n, 2);
await db.close();
});
test("the course migration only adds", () => {
const sql = readFileSync(new URL(COURSE, DIR), "utf8");
assert.doesNotMatch(sql, /DROP|TRUNCATE|DELETE\s+FROM|ALTER COLUMN|RENAME/i);
assert.equal((sql.match(/ADD COLUMN "course" text DEFAULT 'gsw-zh' NOT NULL/g) ?? []).length, 3);
});