| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- // 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 <packaged executable>")
- 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 === "/api/v1/agents/research") return send({ id: "research", name: "科研助手", work_dir: root })
- 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/research-report.pdf")) {
- response.setHeader("Content-Type", "application/pdf")
- return response.end(await fs.readFile(report.replace(/\.md$/, ".pdf")))
- }
- if (url.pathname.endsWith("/workspace")) {
- const files = [{ name: "plan.txt", path: "plan.txt" }]
- if (
- await fs.stat(report.replace(/\.md$/, ".pdf")).then(
- () => true,
- () => false,
- )
- )
- files.push({ name: "research-report.pdf", path: "research-report.pdf" })
- return send({ files })
- }
- 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 window.locator(".paas-suggestions button").first().click()
- await expect(window.getByRole("textbox", { name: "消息", exact: true })).toBeFocused()
- await window.getByRole("textbox", { name: "消息", exact: true }).fill("中文确认不会自动发送")
- await window.getByRole("textbox", { name: "消息", exact: true }).dispatchEvent("keydown", {
- key: "Enter",
- code: "Enter",
- keyCode: 229,
- bubbles: true,
- })
- await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("中文确认不会自动发送")
- if (calls.some((call) => call.path.endsWith("/run/stream"))) throw new Error("IME confirmation submitted research")
- await app.evaluate(({ Menu }) => {
- const entry = Menu.getApplicationMenu()
- .items.flatMap((item) => item.submenu?.items || [])
- .find((item) => item.label === "Settings")
- entry.click()
- })
- await expect(window.getByRole("heading", { name: "平台连接", exact: true })).toBeVisible()
- await app.evaluate(({ Menu }) => {
- const entry = Menu.getApplicationMenu()
- .items.flatMap((item) => item.submenu?.items || [])
- .find((item) => item.label === "New Session")
- entry.click()
- })
- await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("")
- await app.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows()[0].setSize(587, 800))
- await window.getByRole("button", { name: "打开导航", exact: true }).click()
- await app.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows()[0].setSize(1280, 800))
- await expect(window.locator(".paas-nav-open")).toHaveCount(0)
- 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("button", { name: "代码工作区", exact: true }).click()
- await expect(window.locator(".paas-code-host")).toBeVisible()
- await window.getByRole("button", { name: "会话", exact: true }).click()
- await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("Create a research plan")
- await expect(window.getByRole("button", { name: "移除material.txt", exact: true })).toBeVisible()
- await window.locator(".paas-more-actions > summary").click()
- await window.getByRole("button", { name: "在开发区打开工作区", exact: true }).click()
- await expect(window.locator(".paas-code-host")).toBeVisible()
- await expect(window.locator('[data-component="prompt-input"][contenteditable="true"]')).toBeVisible({
- timeout: 30000,
- })
- await expect(window.getByText(path.basename(root), { exact: true }).first()).toBeVisible({ timeout: 10000 })
- await window
- .locator('[data-component="prompt-input"][contenteditable="true"]')
- .fill("检查工作目录中的成果,不自动执行")
- await expect(window.locator('[data-component="prompt-input"][contenteditable="true"]')).toContainText(
- "检查工作目录中的成果,不自动执行",
- )
- await expect(window.locator('[data-component="prompt-input"][contenteditable="true"]')).toBeInViewport()
- await expect(window.getByText(path.basename(root), { exact: true }).first()).toBeInViewport()
- await expect(window.getByLabel("λ-Agent 工作台导航")).toBeVisible()
- await window.getByRole("button", { name: "会话", exact: true }).click()
- await window.getByRole("button", { name: "代码工作区", exact: true }).click()
- await expect(window.locator('[data-component="prompt-input"][contenteditable="true"]')).toContainText("检查工作目录中的成果,不自动执行")
- await window.screenshot({ path: path.join(root, "developer-handoff.png") })
- await app.evaluate(({ Menu }) => {
- Menu.getApplicationMenu()
- .items.flatMap((item) => item.submenu?.items || [])
- .find((item) => item.label === "Settings")
- .click()
- })
- await expect(window.getByRole("dialog")).toBeVisible()
- await window.keyboard.press("Escape")
- await expect(window.getByRole("dialog")).toBeHidden()
- await window.getByRole("button", { name: "会话", exact: true }).click()
- await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("Create a research plan")
- 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: /^(查看|刷新)工作文件$/ }).click()
- await app.evaluate(({ dialog }) => {
- const save = dialog.showSaveDialog
- dialog.showSaveDialog = async (...args) => {
- dialog.showSaveDialog = save
- return { canceled: true }
- }
- })
- await window.getByRole("button", { name: "保存文件", exact: true }).click()
- await expect(window.getByText("已取消保存,文件仍保留在平台。", { exact: true })).toBeVisible()
- if (
- await fs.stat(saved).then(
- () => true,
- () => false,
- )
- )
- throw new Error("Cancelled download created a file")
- 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: "预览 plan.txt", exact: true }).first().click()
- const preview = window.locator(".paas-preview")
- await expect(preview.locator("pre")).toHaveText("Saved from native IPC")
- await preview.getByRole("button", { name: "保存文件", exact: true }).click()
- await expect(preview.getByText(/已保存/)).toBeVisible()
- await window.screenshot({ path: path.join(root, "native-preview.png") })
- await window.keyboard.press("Escape")
- await expect(preview).toHaveCount(0)
- 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.getByRole("button", { name: /^(查看|刷新)工作文件$/ }).click()
- await window.getByRole("button", { name: "预览 research-report.pdf", exact: true }).click()
- await expect(window.locator(".paas-preview iframe")).toBeVisible()
- await expect(window.locator(".paas-preview iframe")).toHaveAttribute("src", /^blob:/)
- // Chromium's PDF viewer paints after the iframe is attached.
- await window.waitForTimeout(1000)
- await window.screenshot({ path: path.join(root, "native-pdf-preview.png") })
- await window.keyboard.press("Escape")
- await window.screenshot({ path: path.join(root, "native-success.png"), fullPage: true })
- await app.evaluate(({ Menu }) => {
- Menu.getApplicationMenu()
- .items.flatMap((item) => item.submenu?.items || [])
- .find((item) => item.label === "Settings")
- .click()
- })
- await expect(window.getByRole("heading", { name: "平台连接", exact: true })).toBeVisible()
- await expect(window.locator(".paas-code-host")).toBeHidden()
- await app.evaluate(({ Menu }) => {
- Menu.getApplicationMenu()
- .items.flatMap((item) => item.submenu?.items || [])
- .find((item) => item.label === "New Session")
- .click()
- })
- await expect(window.getByRole("textbox", { name: "消息", exact: true })).toBeVisible()
- 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
- })
|