native-smoke.cjs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Packaged-app smoke test: synthetic HTTP API; native file dialog selections are stubbed.
  2. const { createRequire } = require("node:module")
  3. const req = createRequire(require("node:path").resolve(__dirname, "../../app/package.json"))
  4. const { _electron: electron, expect } = req("@playwright/test")
  5. const http = require("node:http")
  6. const fs = require("node:fs/promises")
  7. const path = require("node:path")
  8. ;(async () => {
  9. if (!process.argv[2]) throw new Error("Usage: node scripts/native-smoke.cjs <packaged executable>")
  10. const root = await fs.mkdtemp(path.join(require("node:os").tmpdir(), "lambda-native-"))
  11. const material = path.join(root, "material.txt"),
  12. saved = path.join(root, "research-plan.txt"),
  13. report = path.join(root, "research-report.md")
  14. await fs.writeFile(material, "Native desktop research material")
  15. const calls = []
  16. const run = {
  17. run_id: "run_native",
  18. agent_id: "research",
  19. thread_id: "thread_native",
  20. status: "completed",
  21. output: "Native research plan completed",
  22. }
  23. const server = http.createServer(async (request, response) => {
  24. const url = new URL(request.url, "http://localhost")
  25. const chunks = []
  26. for await (const chunk of request) chunks.push(chunk)
  27. const body = Buffer.concat(chunks)
  28. calls.push({ path: url.pathname, method: request.method, body: body.toString() })
  29. response.setHeader("Content-Type", "application/json")
  30. const send = (value) => response.end(JSON.stringify(value))
  31. if (url.pathname === "/api/v1/agents")
  32. return send({ agents: [{ id: "research", name: "科研助手", current_version: 1 }] })
  33. if (url.pathname.endsWith("/run-options")) return send({ efforts: [] })
  34. if (url.pathname.endsWith("/threads")) return send({ threads: [] })
  35. if (url.pathname.endsWith("/files/capabilities")) return send({ direct_upload_max_bytes: 268435456 })
  36. if (url.pathname === "/api/v1/files") return send({ file_id: "native_file", name: "material.txt" })
  37. if (url.pathname.endsWith("/run/stream")) {
  38. response.setHeader("Content-Type", "text/event-stream")
  39. return response.end(
  40. "event: started\ndata: " +
  41. JSON.stringify({ run_id: run.run_id, thread_id: run.thread_id }) +
  42. "\n\nevent: done\ndata: " +
  43. JSON.stringify(run) +
  44. "\n\n",
  45. )
  46. }
  47. if (url.pathname.endsWith("/workspace/plan.txt")) {
  48. response.setHeader("Content-Type", "text/plain")
  49. return response.end("Saved from native IPC")
  50. }
  51. if (url.pathname.endsWith("/workspace")) return send({ files: [{ name: "plan.txt", path: "plan.txt" }] })
  52. if (url.pathname.endsWith("/runs/run_native")) return send(run)
  53. if (url.pathname.endsWith("/runs")) return send({ runs: [run] })
  54. return send({ threads: [], runs: [] })
  55. })
  56. await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
  57. const url = "http://127.0.0.1:" + server.address().port
  58. let app
  59. try {
  60. app = await electron.launch({
  61. executablePath: path.resolve(process.argv[2]),
  62. env: { ...process.env, OPENCODE_TEST_ONBOARDING: "1", AGENTPAAS_URL: url, AGENTPAAS_API_KEY: "native-test-only" },
  63. timeout: 60000,
  64. })
  65. const window = await app.firstWindow()
  66. const errors = []
  67. window.on("pageerror", (e) => {
  68. errors.push(e.message)
  69. console.log("PAGEERROR", e.message)
  70. })
  71. await window.waitForLoadState("domcontentloaded")
  72. console.log("window", await window.title())
  73. await expect(window.getByRole("button", { name: "智能体", exact: true })).toBeVisible({ timeout: 30000 })
  74. await window.getByRole("button", { name: "智能体", exact: true }).click()
  75. await window.getByRole("button", { name: "与 科研助手 对话", exact: true }).click()
  76. await app.evaluate(
  77. ({ dialog }, { material, saved, report }) => {
  78. dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [material] })
  79. dialog.showSaveDialog = async (options) => ({
  80. canceled: false,
  81. filePath: options.title === "导出研究报告" ? report : saved,
  82. })
  83. },
  84. { material, saved, report },
  85. )
  86. await window.getByRole("button", { name: "添加研究材料", exact: true }).click()
  87. await expect(window.getByRole("button", { name: "移除material.txt", exact: true })).toBeVisible()
  88. await window.getByRole("textbox", { name: "消息", exact: true }).fill("Create a research plan")
  89. await window.reload()
  90. await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("Create a research plan", {
  91. timeout: 30000,
  92. })
  93. await expect(window.getByRole("button", { name: "移除material.txt", exact: true })).toBeVisible()
  94. await window.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  95. await expect(window.getByText("Native research plan completed", { exact: true }).first()).toBeVisible()
  96. await window.getByRole("button", { name: "查看工作文件", exact: true }).click()
  97. await window.getByRole("button", { name: "保存文件", exact: true }).click()
  98. await expect(window.getByText("文件已保存。", { exact: true })).toBeVisible()
  99. if ((await fs.readFile(saved, "utf8")) !== "Saved from native IPC") throw new Error("Downloaded bytes mismatch")
  100. await window.getByRole("button", { name: "导出报告", exact: true }).click()
  101. await expect(window.getByText("已导出 research-report.md", { exact: true })).toBeVisible()
  102. if ((await fs.readFile(report, "utf8")) !== run.output) throw new Error("Exported report mismatch")
  103. await expect(window.getByRole("button", { name: "打开保存位置", exact: true }).first()).toBeVisible()
  104. await window.getByRole("button", { name: "基于此方案继续", exact: true }).click()
  105. await expect(window.getByRole("textbox", { name: "消息", exact: true })).toHaveValue(new RegExp(run.output))
  106. const execute = calls.filter((c) => c.path.endsWith("/run/stream"))
  107. if (execute.length !== 1 || JSON.parse(execute[0].body).input_files[0].file_id !== "native_file")
  108. throw new Error("Task/file binding mismatch")
  109. if (!calls.some((c) => c.path === "/api/v1/files" && c.body === "Native desktop research material"))
  110. throw new Error("Upload bytes mismatch")
  111. if (errors.length) throw new Error(errors.join("\n"))
  112. await window.screenshot({ path: path.join(root, "native-success.png"), fullPage: true })
  113. console.log(
  114. JSON.stringify({
  115. status: "passed",
  116. packaged: await app.evaluate(({ app }) => app.isPackaged),
  117. root,
  118. requests: calls.length,
  119. modelCalls: 0,
  120. notes: "Actual packaged Electron/preload/IPC and file IO; synthetic API; native dialog selection stubbed",
  121. }),
  122. )
  123. } catch (error) {
  124. if (app) {
  125. const win = await app.firstWindow()
  126. console.log("PAGE", await win.locator("body").innerText())
  127. await win.screenshot({ path: path.join(root, "native-failure.png"), fullPage: true })
  128. }
  129. console.log("REQUESTS", JSON.stringify(calls))
  130. throw error
  131. } finally {
  132. if (app) await app.close()
  133. await new Promise((resolve) => server.close(resolve))
  134. }
  135. })().catch((e) => {
  136. console.error(e)
  137. process.exitCode = 1
  138. })