feat(studio): published-worktree watcher and DraftWriter API for the AI package

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5.5 committed 2026-10-30 03:16:13 +01:00
1 parent c2385b472c
commit 06b6ea6168
5 files changed
+173

No files matched your search

@@ -0,0 +1,44 @@
import { expect, test } from "bun:test";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { createCourseRepository } from "@stc/course-format";
import { createPublishedWatcher } from "../src/server/watch";
import { studioCourseDirs } from "../src/server/paths";
import { ada, extGit, newStore } from "./helpers";
test("an external commit in the published worktree reloads the CourseRepository", async () => {
const { dataDir, store } = await newStore();
const courses = await createCourseRepository(studioCourseDirs({ dataDir, courseDirs: [] }));
let reloads = 0;
const realReload = courses.reload.bind(courses);
courses.reload = async (id?: string) => { reloads++; await realReload(id); };
const w = createPublishedWatcher(store, courses, { intervalMs: 50 });
await store.createCourse({ id: "c1", title: "C" }, ada);
expect(await w.check()).toEqual(["c1"]); // new course, not yet loaded
expect(courses.get("c1")?.status).toBe("ok");
expect(await w.check()).toEqual([]);
const pub = store.paths("c1").published;
writeFileSync(join(pub, "lessons/welcome.md"), "# External\n");
await extGit(pub, "commit", "-qam", "external");
expect(await w.check()).toEqual(["c1"]);
expect(await courses.readLessonMarkdown("c1", "welcome")).toBe("# External\n");
expect(reloads).toBe(2);
// the timer path
writeFileSync(join(pub, "lessons/welcome.md"), "# External 2\n");
await extGit(pub, "commit", "-qam", "external 2");
w.start();
for (let i = 0; i < 40 && (await courses.readLessonMarkdown("c1", "welcome")) !== "# External 2\n"; i++) await Bun.sleep(25);
w.stop();
expect(await courses.readLessonMarkdown("c1", "welcome")).toBe("# External 2\n");
});
test("drafts are not discovered as courses", async () => {
const { dataDir, store } = await newStore();
await store.createCourse({ id: "c1", title: "C" }, ada);
await store.ensureDraft("c1", ada.id);
const courses = await createCourseRepository(studioCourseDirs({ dataDir, courseDirs: [] }));
expect(courses.list().map((c) => [c.id, c.root])).toEqual([["c1", store.paths("c1").published]]);
});