import { mkdtemp, readFile, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import type { CatalogComponent, FlowNode } from "../../shared/types"; import type { RunContext } from "../engine/types"; import { getHandler } from "../engine/registry"; import "./word"; import "./word-extended"; const directories: string[] = []; const context = {} as RunContext; const component = {} as CatalogComponent; const node = (id: string): FlowNode => ({ id, componentId: id, position: { x: 0, y: 0 }, values: {}, options: {} }); async function run(id: string, args: Record = {}) { const handler = getHandler(id); expect(handler, id).toBeTruthy(); return handler!(context, node(id), args, component); } afterEach(async () => Promise.all(directories.splice(0).map((directory) => rm(directory, { recursive: true, force: true })))); const ids = ["Word_New_V1", "Word_GetPath", "Word_ReadAll", "Word_Replace", "Word_Enter", "Word_InsertPage", "Word_SetFontName", "Word_SetFontSize", "Word_SetFontStyle", "Word_SetFontColor", "Word_SetAlign", "Word_InsertPicture", "Word_WriteTable", "Word_Close"]; describe("Word extended capabilities", () => { it("registers automated Word operations", () => { for (const id of ids) expect(getHandler(id), id).toBeTruthy(); }); it("creates, writes, reads and replaces Word content", async () => { if (process.platform !== "win32") return; const directory = await mkdtemp(path.join(os.tmpdir(), "rpa-word-extended-")); directories.push(directory); const handle = await run("Word_New_V1", { filePath: directory, name: "extended.docx" }); await run("Word_Write", { objWord: handle, text: "原始内容" }); await expect(run("Word_ReadAll", { objWord: handle })).resolves.toContain("原始内容"); await run("Word_Replace", { objWord: handle, oldText: "原始", newText: "替换后" }); await expect(run("Word_ReadAll", { objWord: handle })).resolves.toContain("替换后内容"); await run("Word_SetFontSize", { objWord: handle, fontSize: 12 }); await run("Word_SetFontStyle", { objWord: handle, isBold: "True", isItalic: "False", isUnderline: "False" }); await run("Word_Enter", { objWord: handle }); expect((await readFile(path.join(directory, "extended.docx"))).byteLength).toBeGreaterThan(1000); }, 60000); });