30 lines
1.9 KiB
TypeScript
30 lines
1.9 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { createDraftWriter } from "../src/server/draft-writer";
|
|
import { ada, extGit, newStore } from "./helpers";
|
|
|
|
test("writeDraftFiles commits on draft/<author>, never on the published branch", async () => {
|
|
const { dataDir, store } = await newStore();
|
|
await store.createCourse({ id: "c1", title: "C" }, ada);
|
|
const writer = createDraftWriter({ config: { dataDir } }, { identity: async (id) => (id === ada.id ? ada : null) });
|
|
const r = await writer.writeDraftFiles("c1", ada.id, [{ path: "lessons/generated.md", content: "# Generated\n" }], "AI draft: module 2");
|
|
expect(r.branch).toBe("draft/u-ada");
|
|
const draft = store.paths("c1").draft(ada.id);
|
|
expect(await extGit(draft, "rev-parse", "HEAD")).toBe(r.commit);
|
|
expect(await extGit(draft, "log", "-1", "--format=%an|%s")).toBe("Ada Lovelace|AI draft: module 2");
|
|
expect(readFileSync(join(draft, "lessons/generated.md"), "utf8")).toBe("# Generated\n");
|
|
expect(existsSync(join(store.paths("c1").published, "lessons/generated.md"))).toBe(false);
|
|
});
|
|
|
|
test("unknown identity falls back to the author id; unmanaged course and bad paths throw", async () => {
|
|
const { dataDir, store } = await newStore();
|
|
await store.createCourse({ id: "c1", title: "C" }, ada);
|
|
const writer = createDraftWriter({ config: { dataDir } });
|
|
const r = await writer.writeDraftFiles("c1", "u-x", [{ path: "a.md", content: "a" }], "m");
|
|
expect(await extGit(store.paths("c1").draft("u-x"), "log", "-1", "--format=%an <%ae>")).toBe("u-x <[email protected]>");
|
|
expect(r.branch).toBe("draft/u-x");
|
|
await expect(writer.writeDraftFiles("pure-snake", "u-x", [{ path: "a.md", content: "a" }], "m")).rejects.toThrow(/not managed/);
|
|
await expect(writer.writeDraftFiles("c1", "u-x", [{ path: "../a.md", content: "a" }], "m")).rejects.toThrow(/path/);
|
|
});
|