| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // 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")
- 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: "Native research plan completed",
- }
- 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 }) => {
- dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [material] })
- dialog.showSaveDialog = async () => ({ canceled: false, filePath: saved })
- },
- { material, saved },
- )
- 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.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")
- 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
- })
|