Skip to content

Commit 05b8e4c

Browse files
committed
Updated tests to support new template parameter
1 parent 8da1e84 commit 05b8e4c

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

index.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ describe("schema", () => {
6262
const data = [{ userId: "u1", fileName: "doc", typeOfFile: "PDF", markdown: "text", variables: { lang: 42 } }];
6363
expect(schema.safeParse(data).success).toBe(false);
6464
});
65+
66+
it("validates a request with template \"default\"", () => {
67+
const data = [{ userId: "u1", fileName: "doc", typeOfFile: "PDF", markdown: "text", template: "default" }];
68+
expect(schema.safeParse(data).success).toBe(true);
69+
});
70+
71+
it("validates a request with template \"cjk\"", () => {
72+
const data = [{ userId: "u1", fileName: "doc", typeOfFile: "PDF", markdown: "text", template: "cjk" }];
73+
expect(schema.safeParse(data).success).toBe(true);
74+
});
75+
76+
it("rejects an unknown template value", () => {
77+
const data = [{ userId: "u1", fileName: "doc", typeOfFile: "PDF", markdown: "text", template: "korean" }];
78+
expect(schema.safeParse(data).success).toBe(false);
79+
});
6580
});
6681

6782
describe("handler", () => {
@@ -117,6 +132,28 @@ describe("handler", () => {
117132
expect(calledArgs).toContain("--variable=CJKmainfont:Noto Sans CJK KR");
118133
});
119134

135+
it("uses the default template when template is omitted", async () => {
136+
const executeMock = vi.fn().mockResolvedValue("");
137+
vi.mocked(PdcTs).mockImplementationOnce(() => ({ Execute: executeMock }) as any);
138+
139+
const event = [{ userId: "user1", fileName: "test-doc", typeOfFile: "TEX", markdown: "# Hello" }];
140+
await handler(event as any);
141+
142+
const calledArgs: string[] = executeMock.mock.calls[0]?.[0]?.pandocArgs ?? [];
143+
expect(calledArgs).toContain("--template=./templates/default.latex");
144+
});
145+
146+
it("uses the cjk template when template is \"cjk\"", async () => {
147+
const executeMock = vi.fn().mockResolvedValue("");
148+
vi.mocked(PdcTs).mockImplementationOnce(() => ({ Execute: executeMock }) as any);
149+
150+
const event = [{ userId: "user1", fileName: "test-doc", typeOfFile: "TEX", markdown: "# Hello", template: "cjk" }];
151+
await handler(event as any);
152+
153+
const calledArgs: string[] = executeMock.mock.calls[0]?.[0]?.pandocArgs ?? [];
154+
expect(calledArgs).toContain("--template=./templates/cjk.latex");
155+
});
156+
120157
it("returns 500 when Pandoc execution fails", async () => {
121158
vi.mocked(PdcTs).mockImplementationOnce(() => ({
122159
Execute: vi.fn()

src/compile.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import { PdcTs } from "pdc-ts";
88
// These run the full production pipeline: markdown → Pandoc + template.latex → xelatex → PDF.
99
// They are intentionally slow (~5-15s per compile).
1010

11-
// Absolute path so Pandoc can locate the template regardless of its working directory
12-
const TEMPLATE = path.resolve(__dirname, "template.latex");
11+
// Absolute paths so Pandoc can locate the templates regardless of its working directory
12+
const DEFAULT_TEMPLATE = path.resolve(__dirname, "templates/default.latex");
13+
const CJK_TEMPLATE = path.resolve(__dirname, "templates/cjk.latex");
1314

1415
const pendingPdfs: string[] = [];
1516

16-
const compileToPdf = async (markdown: string, id: string) => {
17+
const compileToPdf = async (markdown: string, id: string, template: string = DEFAULT_TEMPLATE) => {
1718
const tmpPath = `/tmp/compile-test-${id}.pdf`;
1819
pendingPdfs.push(tmpPath);
1920
await new PdcTs().Execute({
2021
from: "markdown",
2122
to: "latex",
22-
pandocArgs: ["--pdf-engine=xelatex", `--template=${TEMPLATE}`],
23+
pandocArgs: ["--pdf-engine=xelatex", `--template=${template}`],
2324
spawnOpts: { argv0: "+RTS -M512M -RTS" },
2425
outputToFile: true,
2526
sourceText: markdown,
@@ -84,4 +85,22 @@ describe("PDF compile (end-to-end pipeline)", () => {
8485
},
8586
{ timeout: 60_000 }
8687
);
88+
89+
it(
90+
"renders Korean text via the cjk template",
91+
async () => {
92+
const pdf = await compileToPdf(
93+
"# 수학 문서\n\n이차 방정식의 판별식은 $\\Delta = b^2 - 4ac$ 입니다.",
94+
"korean",
95+
CJK_TEMPLATE
96+
);
97+
const text = extractText(pdf);
98+
// pdftotext collapses inter-word spacing between Hangul syllable blocks,
99+
// so match the individual words rather than the spaced phrase
100+
expect(text).toContain("수학");
101+
expect(text).toContain("문서");
102+
expect(text).toContain("판별식");
103+
},
104+
{ timeout: 60_000 }
105+
);
87106
});

0 commit comments

Comments
 (0)