@@ -48,7 +48,11 @@ const FAKE_PDF_MEDIA = {
4848 fileName : "doc.pdf" ,
4949} as const ;
5050
51- function requirePdfTool ( tool : Awaited < ReturnType < typeof loadCreatePdfTool > > extends ( ...args : any [ ] ) => infer R ? R : never ) {
51+ function requirePdfTool (
52+ tool : Awaited < ReturnType < typeof loadCreatePdfTool > > extends ( ...args : any [ ] ) => infer R
53+ ? R
54+ : never ,
55+ ) {
5256 expect ( tool ) . not . toBeNull ( ) ;
5357 if ( ! tool ) {
5458 throw new Error ( "expected pdf tool" ) ;
@@ -69,6 +73,16 @@ async function withAnthropicPdfTool(
6973 } ) ;
7074}
7175
76+ async function withConfiguredPdfTool (
77+ run : ( tool : PdfToolInstance , agentDir : string ) => Promise < void > ,
78+ ) {
79+ await withTempAgentDir ( async ( agentDir ) => {
80+ const cfg = withPdfModel ( ANTHROPIC_PDF_MODEL ) ;
81+ const tool = requirePdfTool ( ( await loadCreatePdfTool ( ) ) ( { config : cfg , agentDir } ) ) ;
82+ await run ( tool , agentDir ) ;
83+ } ) ;
84+ }
85+
7286function resetAuthEnv ( ) {
7387 vi . stubEnv ( "OPENAI_API_KEY" , "" ) ;
7488 vi . stubEnv ( "ANTHROPIC_API_KEY" , "" ) ;
@@ -154,22 +168,22 @@ describe("createPdfTool", () => {
154168 expect ( ( ) => createTool ( { config : cfg } ) ) . toThrow ( "requires agentDir" ) ;
155169 } ) ;
156170
157- it ( "creates tool when auth is available " , async ( ) => {
158- await withAnthropicPdfTool ( async ( tool ) => {
171+ it ( "creates tool when a PDF model is configured " , async ( ) => {
172+ await withConfiguredPdfTool ( async ( tool ) => {
159173 expect ( tool . name ) . toBe ( "pdf" ) ;
160174 expect ( tool . label ) . toBe ( "PDF" ) ;
161175 expect ( tool . description ) . toContain ( "PDF documents" ) ;
162176 } ) ;
163177 } ) ;
164178
165179 it ( "rejects when no pdf input provided" , async ( ) => {
166- await withAnthropicPdfTool ( async ( tool ) => {
180+ await withConfiguredPdfTool ( async ( tool ) => {
167181 await expect ( tool . execute ( "t1" , { prompt : "test" } ) ) . rejects . toThrow ( "pdf required" ) ;
168182 } ) ;
169183 } ) ;
170184
171185 it ( "rejects too many PDFs" , async ( ) => {
172- await withAnthropicPdfTool ( async ( tool ) => {
186+ await withConfiguredPdfTool ( async ( tool ) => {
173187 const manyPdfs = Array . from ( { length : 15 } , ( _ , i ) => `/tmp/doc${ i } .pdf` ) ;
174188 const result = await tool . execute ( "t1" , { prompt : "test" , pdfs : manyPdfs } ) ;
175189 expect ( result ) . toMatchObject ( {
@@ -180,11 +194,10 @@ describe("createPdfTool", () => {
180194
181195 it ( "respects fsPolicy.workspaceOnly for non-sandbox pdf paths" , async ( ) => {
182196 await withTempAgentDir ( async ( agentDir ) => {
183- vi . stubEnv ( "ANTHROPIC_API_KEY" , "anthropic-test" ) ;
184197 const workspaceDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-pdf-ws-" ) ) ;
185198 const outsideDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-pdf-out-" ) ) ;
186199 try {
187- const cfg = withDefaultModel ( ANTHROPIC_PDF_MODEL ) ;
200+ const cfg = withPdfModel ( ANTHROPIC_PDF_MODEL ) ;
188201 const tool = requirePdfTool (
189202 ( await loadCreatePdfTool ( ) ) ( {
190203 config : cfg ,
@@ -208,7 +221,7 @@ describe("createPdfTool", () => {
208221 } ) ;
209222
210223 it ( "rejects unsupported scheme references" , async ( ) => {
211- await withAnthropicPdfTool ( async ( tool ) => {
224+ await withConfiguredPdfTool ( async ( tool ) => {
212225 const result = await tool . execute ( "t1" , {
213226 prompt : "test" ,
214227 pdf : "ftp://example.com/doc.pdf" ,
@@ -219,24 +232,6 @@ describe("createPdfTool", () => {
219232 } ) ;
220233 } ) ;
221234
222- it ( "deduplicates pdf inputs before loading" , async ( ) => {
223- await withTempAgentDir ( async ( agentDir ) => {
224- const { loadSpy } = await stubPdfToolInfra ( agentDir , { modelFound : false } ) ;
225- const cfg = withPdfModel ( ANTHROPIC_PDF_MODEL ) ;
226- const tool = requirePdfTool ( ( await loadCreatePdfTool ( ) ) ( { config : cfg , agentDir } ) ) ;
227-
228- await expect (
229- tool . execute ( "t1" , {
230- prompt : "test" ,
231- pdf : "/tmp/nonexistent.pdf" ,
232- pdfs : [ "/tmp/nonexistent.pdf" ] ,
233- } ) ,
234- ) . rejects . toThrow ( "Unknown model" ) ;
235-
236- expect ( loadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
237- } ) ;
238- } ) ;
239-
240235 it ( "uses native PDF path without eager extraction" , async ( ) => {
241236 await withTempAgentDir ( async ( agentDir ) => {
242237 await stubPdfToolInfra ( agentDir , { provider : "anthropic" , input : [ "text" , "document" ] } ) ;
0 commit comments