Files
STCourses/packages/feature-studio/test/watch.test.ts
T

45 lines
2.1 KiB
TypeScript

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]]);
});