|
| 1 | +// PDF model resolution must consume configured facts from static prepared runtimes. |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { loadConfig } from "../../config/config.js"; |
| 5 | +import * as pdfExtractModule from "../../media/pdf-extract.js"; |
| 6 | +import * as webMedia from "../../media/web-media.js"; |
| 7 | +import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js"; |
| 8 | +import { acquireAgentRunPreparedModelRuntime } from "../prepared-model-runtime.js"; |
| 9 | + |
| 10 | +const completeMock = vi.hoisted(() => vi.fn()); |
| 11 | + |
| 12 | +vi.mock("../../llm/stream.js", async () => { |
| 13 | + const actual = await vi.importActual<typeof import("../../llm/stream.js")>("../../llm/stream.js"); |
| 14 | + return { ...actual, complete: completeMock }; |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock("../provider-stream.js", () => ({ |
| 18 | + registerProviderStreamForModel: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +describe("PDF tool static prepared runtime", () => { |
| 22 | + afterEach(() => { |
| 23 | + completeMock.mockReset(); |
| 24 | + vi.restoreAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("resolves a config-inline model when the static registry is empty", async () => { |
| 28 | + await withOpenClawTestState( |
| 29 | + { |
| 30 | + label: "pdf-static-inline-model", |
| 31 | + env: { OPENAI_API_KEY: "test-key" }, |
| 32 | + }, |
| 33 | + async (state) => { |
| 34 | + const modelId = "gpt-5.6-luna"; |
| 35 | + const modelRef = `openai/${modelId}`; |
| 36 | + await state.writeConfig({ |
| 37 | + models: { |
| 38 | + mode: "replace", |
| 39 | + providers: { |
| 40 | + openai: { |
| 41 | + api: "openai-responses", |
| 42 | + baseUrl: "http://127.0.0.1:9/v1", |
| 43 | + models: [ |
| 44 | + { |
| 45 | + id: modelId, |
| 46 | + name: "GPT-5.6 Luna inline fixture", |
| 47 | + reasoning: true, |
| 48 | + input: ["text"], |
| 49 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
| 50 | + contextWindow: 128_000, |
| 51 | + maxTokens: 8_192, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + agents: { |
| 58 | + defaults: { |
| 59 | + model: { primary: modelRef }, |
| 60 | + pdfModel: { primary: modelRef }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }); |
| 64 | + const config = loadConfig({ |
| 65 | + pin: false, |
| 66 | + skipPluginValidation: true, |
| 67 | + skipShellEnvFallback: true, |
| 68 | + }); |
| 69 | + const agentDir = state.agentDir(); |
| 70 | + await fs.mkdir(agentDir, { recursive: true }); |
| 71 | + const lease = await acquireAgentRunPreparedModelRuntime( |
| 72 | + { agentDir, config }, |
| 73 | + { catalogMode: "static" }, |
| 74 | + ); |
| 75 | + |
| 76 | + try { |
| 77 | + const stores = lease.snapshot.createStores(); |
| 78 | + expect(stores.modelRegistry.find("openai", modelId)).toBeUndefined(); |
| 79 | + expect( |
| 80 | + lease.snapshot.configuredRuntimeModels.map((entry) => ({ |
| 81 | + provider: entry.provider, |
| 82 | + modelId: entry.modelId, |
| 83 | + })), |
| 84 | + ).toContainEqual({ provider: "openai", modelId }); |
| 85 | + |
| 86 | + vi.spyOn(webMedia, "loadWebMediaRaw").mockResolvedValue({ |
| 87 | + kind: "document", |
| 88 | + buffer: Buffer.from("%PDF-1.4 inline model fixture"), |
| 89 | + contentType: "application/pdf", |
| 90 | + fileName: "inline-model.pdf", |
| 91 | + }); |
| 92 | + vi.spyOn(pdfExtractModule, "extractPdfContent").mockResolvedValue({ |
| 93 | + text: "Inline model regression fixture", |
| 94 | + images: [], |
| 95 | + }); |
| 96 | + completeMock.mockResolvedValue({ |
| 97 | + role: "assistant", |
| 98 | + stopReason: "stop", |
| 99 | + content: [{ type: "text", text: "resolved inline model" }], |
| 100 | + }); |
| 101 | + |
| 102 | + const { createPdfTool } = await import("./pdf-tool.js"); |
| 103 | + const tool = createPdfTool({ |
| 104 | + config, |
| 105 | + agentDir, |
| 106 | + preparedModelRuntime: lease.snapshot, |
| 107 | + }); |
| 108 | + if (!tool) { |
| 109 | + throw new Error("expected PDF tool"); |
| 110 | + } |
| 111 | + |
| 112 | + const result = await tool.execute("pdf-static-inline", { |
| 113 | + prompt: "Summarize", |
| 114 | + pdf: "/tmp/inline-model.pdf", |
| 115 | + }); |
| 116 | + |
| 117 | + expect(completeMock).toHaveBeenCalledWith( |
| 118 | + expect.objectContaining({ provider: "openai", id: modelId }), |
| 119 | + expect.any(Object), |
| 120 | + expect.any(Object), |
| 121 | + ); |
| 122 | + expect(result.details).toMatchObject({ model: modelRef, native: false }); |
| 123 | + } finally { |
| 124 | + lease.release(); |
| 125 | + } |
| 126 | + }, |
| 127 | + ); |
| 128 | + }, 180_000); |
| 129 | +}); |
0 commit comments