native-smoke.cjs 6.0 KB

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