mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
fix(pdf): reject empty parsed page ranges (#97698)
This commit is contained in:
@@ -62,6 +62,10 @@ describe("parsePageRange", () => {
|
||||
expect(parsePageRange("1-100", 5)).toEqual([1, 2, 3, 4, 5]);
|
||||
});
|
||||
|
||||
it("throws when no requested pages are within maxPages", () => {
|
||||
expect(() => parsePageRange("999", 20)).toThrow('No PDF pages matched requested range "999"');
|
||||
});
|
||||
|
||||
it("deduplicates and sorts", () => {
|
||||
expect(parsePageRange("5,3,1,3,5", 20)).toEqual([1, 3, 5]);
|
||||
});
|
||||
|
||||
@@ -74,7 +74,11 @@ export function parsePageRange(range: string, maxPages: number): number[] {
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(pages).toSorted((a, b) => a - b);
|
||||
const parsedPages = Array.from(pages).toSorted((a, b) => a - b);
|
||||
if (parsedPages.length === 0) {
|
||||
throw new Error(`No PDF pages matched requested range "${range}"`);
|
||||
}
|
||||
return parsedPages;
|
||||
}
|
||||
|
||||
/** Converts a provider assistant message into PDF text or throws a model-labelled failure. */
|
||||
|
||||
@@ -548,6 +548,26 @@ describe("createPdfTool", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects explicit page ranges that resolve to no pages before native PDF analysis", async () => {
|
||||
await withTempPdfAgentDir(async (agentDir) => {
|
||||
await stubPdfToolInfra(agentDir, { provider: "anthropic", input: ["text", "document"] });
|
||||
const nativeSpy = vi
|
||||
.spyOn(pdfNativeProviders, "anthropicAnalyzePdf")
|
||||
.mockResolvedValue("native summary");
|
||||
const cfg = withPdfModel(ANTHROPIC_PDF_MODEL);
|
||||
const tool = requirePdfTool((await loadCreatePdfTool())({ config: cfg, agentDir }));
|
||||
|
||||
await expect(
|
||||
tool.execute("t1", {
|
||||
prompt: "summarize",
|
||||
pdf: "/tmp/doc.pdf",
|
||||
pages: "999",
|
||||
}),
|
||||
).rejects.toThrow('No PDF pages matched requested range "999"');
|
||||
expect(nativeSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects password parameter for native PDF providers", async () => {
|
||||
await withTempPdfAgentDir(async (agentDir) => {
|
||||
await stubPdfToolInfra(agentDir, { provider: "anthropic", input: ["text", "document"] });
|
||||
|
||||
Reference in New Issue
Block a user