import { describe, expect, it, vi } from "vitest"; import type { WebContents } from "electron"; import { createPagedDocumentPayload, defaultExportConfig, type DocxMediaCapturePlan } from "@md-to-pdf/core"; import { captureDocxMediaWithElectronWebContents } from "../src/electron-docx-media-capture.js"; const plan: DocxMediaCapturePlan = { targets: [ { id: "docx-media-1", kind: "image", ordinal: 1, kindOrdinal: 1, altText: "图片", displayWidthPx: 200, displayHeightPx: 100, captureX: 12, captureY: 24, captureWidthPx: 200, captureHeightPx: 100, rasterScale: 3.125 } ], echartsErrors: [], mermaidErrors: [] }; const request = { payload: createPagedDocumentPayload({ document: { rendererVersion: 1, articleHtml: '
', bodyHtml: "", metadata: { title: "", author: "", subject: "", keywords: [], language: "zh-CN" }, features: [], warnings: [] }, fileName: "test.md", themeCss: "", exportConfig: defaultExportConfig }), dimensions: { contentWidthPx: 600, contentHeightPx: 900 } }; describe("Electron DOCX 媒体捕获", () => { it("复用隐藏 Chromium 并在捕获后释放调试会话", async () => { let attached = false; const attach = vi.fn(() => { attached = true; }); const detach = vi.fn(() => { attached = false; }); const sendCommand = vi.fn(async () => ({ data: Buffer.from("png").toString("base64") })); const webContents = { executeJavaScript: vi.fn(async () => plan), debugger: { isAttached: vi.fn(() => attached), attach, detach, sendCommand } } as unknown as WebContents; const result = await captureDocxMediaWithElectronWebContents( webContents, request ); expect(result.captures[0]).toEqual({ id: "docx-media-1", png: Buffer.from("png") }); expect(attach).toHaveBeenCalledWith("1.3"); expect(sendCommand).toHaveBeenCalledWith( "Page.captureScreenshot", expect.objectContaining({ format: "png", clip: expect.objectContaining({ scale: 3.125 }) }) ); expect(detach).toHaveBeenCalledOnce(); }); });