Files
curriculum-project-hub/hub/test/unit/file-delivery.test.ts
T

74 lines
2.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { mkdir, realpath, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { resolveDeliverableFile } from "../../src/feishu/fileDelivery.js";
const itOnLinux = process.platform === "linux" ? it : it.skip;
describe("file delivery path resolution", () => {
itOnLinux("never resolves a Git-root file outside the current project workspace", async () => {
const root = await makeRepo();
try {
const workspace = join(root, "examples", "TH-141");
await writeFile(join(root, "README.md"), "# root readme\n");
await expect(resolveDeliverableFile("README.md", join(root, "examples"), workspace)).resolves.toBeNull();
} finally {
await rm(root, { recursive: true, force: true });
}
});
itOnLinux("resolves a bare build filename against workspace/build", async () => {
const root = await makeRepo();
try {
const workspace = join(root, "examples", "TH-141");
const pdf = join(workspace, "build", "student.pdf");
await writeFile(pdf, "pdf bytes");
const resolved = await resolveDeliverableFile("student.pdf", join(root, "examples"), workspace);
expect(resolved).toMatchObject({ path: await realpath(pdf), name: "student.pdf" });
expect(resolved?.data.toString("utf8")).toBe("pdf bytes");
} finally {
await rm(root, { recursive: true, force: true });
}
});
itOnLinux("rejects a deliverable symlink even when its target exists", async () => {
const root = await makeRepo();
try {
const workspace = join(root, "examples", "TH-141");
const outside = join(root, "outside.pdf");
await writeFile(outside, "outside secret");
await symlink(outside, join(workspace, "build", "student.pdf"));
await expect(
resolveDeliverableFile("student.pdf", join(root, "examples"), workspace),
).rejects.toThrow(/symlink|no-follow|directory/i);
} finally {
await rm(root, { recursive: true, force: true });
}
});
it("does not infer files from natural-language prompts", async () => {
const root = await makeRepo();
try {
const workspace = join(root, "examples", "TH-141");
await writeFile(join(workspace, "build", "student.pdf"), "pdf bytes");
await expect(
resolveDeliverableFile("cph build 生成 PDF 给我", join(root, "examples"), workspace),
).resolves.toBeNull();
} finally {
await rm(root, { recursive: true, force: true });
}
});
});
async function makeRepo(): Promise<string> {
const root = await mkdir(join(tmpdir(), `hub-file-delivery-${Date.now()}-${Math.random().toString(36).slice(2)}`), { recursive: true });
await mkdir(join(root, ".git"));
await mkdir(join(root, "examples", "TH-141", "build"), { recursive: true });
return root;
}