// Packaged-app smoke test: synthetic HTTP API; native file dialog selections are stubbed. const { createRequire } = require("node:module") const req = createRequire(require("node:path").resolve(__dirname, "../../app/package.json")) const { _electron: electron, expect } = req("@playwright/test") const http = require("node:http") const fs = require("node:fs/promises") const path = require("node:path") ;(async () => { if (!process.argv[2]) throw new Error("Usage: node scripts/native-smoke.cjs ") const root = await fs.mkdtemp(path.join(require("node:os").tmpdir(), "lambda-native-")) const material = path.join(root, "material.txt"), saved = path.join(root, "research-plan.txt"), report = path.join(root, "research-report.md") await fs.writeFile(material, "Native desktop research material") const calls = [] const run = { run_id: "run_native", agent_id: "research", thread_id: "thread_native", status: "completed", output: "# 科研方案验收\n\nNative research plan completed\n\n## 对照与判断\n\n1. 明确适用条件。\n2. 保留引用与验证步骤。\n\n| 方案 | 样本量 |\n|---|---|\n| 对照组 | 20 |\n| 实验组 | 20 |\n\n待验证公式:$\\delta > 0$。\n\n## 后续研究\n\n本文件为桌面导出测试材料,不代表真实实验结果。", } const server = http.createServer(async (request, response) => { const url = new URL(request.url, "http://localhost") const chunks = [] for await (const chunk of request) chunks.push(chunk) const body = Buffer.concat(chunks) calls.push({ path: url.pathname, method: request.method, body: body.toString() }) response.setHeader("Content-Type", "application/json") const send = (value) => response.end(JSON.stringify(value)) if (url.pathname === "/api/v1/agents") return send({ agents: [{ id: "research", name: "科研助手", current_version: 1 }] }) if (url.pathname.endsWith("/run-options")) return send({ efforts: [] }) if (url.pathname.endsWith("/threads")) return send({ threads: [] }) if (url.pathname.endsWith("/files/capabilities")) return send({ direct_upload_max_bytes: 268435456 }) if (url.pathname === "/api/v1/files") return send({ file_id: "native_file", name: "material.txt" }) if (url.pathname.endsWith("/run/stream")) { response.setHeader("Content-Type", "text/event-stream") return response.end( "event: started\ndata: " + JSON.stringify({ run_id: run.run_id, thread_id: run.thread_id }) + "\n\nevent: done\ndata: " + JSON.stringify(run) + "\n\n", ) } if (url.pathname.endsWith("/workspace/plan.txt")) { response.setHeader("Content-Type", "text/plain") return response.end("Saved from native IPC") } if (url.pathname.endsWith("/workspace")) return send({ files: [{ name: "plan.txt", path: "plan.txt" }] }) if (url.pathname.endsWith("/runs/run_native")) return send(run) if (url.pathname.endsWith("/runs")) return send({ runs: [run] }) return send({ threads: [], runs: [] }) }) await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)) const url = "http://127.0.0.1:" + server.address().port let app try { app = await electron.launch({ executablePath: path.resolve(process.argv[2]), env: { ...process.env, OPENCODE_TEST_ONBOARDING: "1", AGENTPAAS_URL: url, AGENTPAAS_API_KEY: "native-test-only" }, timeout: 60000, }) const window = await app.firstWindow() const errors = [] window.on("pageerror", (e) => { errors.push(e.message) console.log("PAGEERROR", e.message) }) await window.waitForLoadState("domcontentloaded") console.log("window", await window.title()) await expect(window.getByRole("button", { name: "智能体", exact: true })).toBeVisible({ timeout: 30000 }) await window.getByRole("button", { name: "智能体", exact: true }).click() await window.getByRole("button", { name: "与 科研助手 对话", exact: true }).click() await app.evaluate( ({ dialog }, { material, saved, report }) => { dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [material] }) dialog.showSaveDialog = async (options) => ({ canceled: false, filePath: options.title === "导出研究报告" ? report.replace(/\.md$/, "." + options.filters[0].extensions[0]) : saved, }) }, { material, saved, report }, ) await window.getByRole("button", { name: "添加研究材料", exact: true }).click() await expect(window.getByRole("button", { name: "移除material.txt", exact: true })).toBeVisible() await window.getByRole("textbox", { name: "消息", exact: true }).fill("Create a research plan") await window.reload() await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("Create a research plan", { timeout: 30000, }) await expect(window.getByRole("button", { name: "移除material.txt", exact: true })).toBeVisible() await window.getByRole("textbox", { name: "消息", exact: true }).press("Enter") await expect(window.getByText("Native research plan completed", { exact: true }).first()).toBeVisible() await window.getByRole("button", { name: "查看工作文件", exact: true }).click() await window.getByRole("button", { name: "保存文件", exact: true }).click() await expect(window.getByText("文件已保存。", { exact: true })).toBeVisible() if ((await fs.readFile(saved, "utf8")) !== "Saved from native IPC") throw new Error("Downloaded bytes mismatch") await window.getByRole("button", { name: "导出报告", exact: true }).click() await expect(window.getByText("已导出 research-report.md", { exact: true })).toBeVisible() if ((await fs.readFile(report, "utf8")) !== run.output) throw new Error("Exported report mismatch") await expect(window.getByRole("button", { name: "打开保存位置", exact: true }).first()).toBeVisible() await window.getByRole("button", { name: "基于此方案继续", exact: true }).click() await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("") await expect(window.getByLabel("已引用研究报告")).toContainText(run.output) await window.getByRole("button", { name: "阅读与对比", exact: true }).click() await window.getByRole("button", { name: "导出 Word", exact: true }).click() await expect(window.getByRole("dialog").getByText("已导出 research-report.docx", { exact: true })).toBeVisible() const word = await fs.readFile(report.replace(/\.md$/, ".docx")) if (word.subarray(0, 2).toString() !== "PK") throw new Error("Invalid DOCX container") await window.getByRole("button", { name: "导出 PDF", exact: true }).click() await expect(window.getByRole("dialog").getByText("已导出 research-report.pdf", { exact: true })).toBeVisible() const pdf = await fs.readFile(report.replace(/\.md$/, ".pdf")) if (pdf.subarray(0, 5).toString() !== "%PDF-") throw new Error("Invalid PDF") await app.evaluate(({ dialog }) => { dialog.showSaveDialog = async () => ({ canceled: true }) }) await window.getByRole("button", { name: "导出 Word", exact: true }).click() await expect(window.getByRole("dialog").getByText("已取消导出,报告仍保留。", { exact: true })).toBeVisible() if (!(await fs.readFile(report.replace(/\.md$/, ".docx"))).equals(word)) throw new Error("Cancelled export changed original file") await window.keyboard.press("Escape") const execute = calls.filter((c) => c.path.endsWith("/run/stream")) if (execute.length !== 1 || JSON.parse(execute[0].body).input_files[0].file_id !== "native_file") throw new Error("Task/file binding mismatch") if (!calls.some((c) => c.path === "/api/v1/files" && c.body === "Native desktop research material")) throw new Error("Upload bytes mismatch") if (errors.length) throw new Error(errors.join("\n")) await window.screenshot({ path: path.join(root, "native-success.png"), fullPage: true }) console.log( JSON.stringify({ status: "passed", packaged: await app.evaluate(({ app }) => app.isPackaged), root, requests: calls.length, modelCalls: 0, notes: "Actual packaged Electron/preload/IPC and file IO; synthetic API; native dialog selection stubbed", }), ) } catch (error) { if (app) { const win = await app.firstWindow() console.log("PAGE", await win.locator("body").innerText()) await win.screenshot({ path: path.join(root, "native-failure.png"), fullPage: true }) } console.log("REQUESTS", JSON.stringify(calls)) throw error } finally { if (app) await app.close() await new Promise((resolve) => server.close(resolve)) } })().catch((e) => { console.error(e) process.exitCode = 1 })