| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- import { expect, test, type Page } from "@playwright/test"
- declare global {
- interface Window {
- paasUX: {
- update(id: string, value: Record<string, unknown>): void
- add(value: Record<string, unknown>): void
- holdRun: string
- holdThread: string
- held: boolean
- connectError: string
- catalogError: string
- emptyCatalog: boolean
- configVersion: string
- configError: boolean
- permissions?: string[]
- verifyError: string
- checks: Record<string, unknown>[]
- external: string[]
- progress(event: import("../../src/paas").PaasProgress): void
- release(): void
- }
- paasTestCalls: Record<string, unknown>[]
- }
- }
- async function connect(page: Page) {
- await page.goto("/e2e/paas/index.html?ux")
- await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
- await page.getByRole("button", { name: "连接平台", exact: true }).click()
- await expect(page.getByRole("textbox", { name: "消息", exact: true })).toBeEnabled()
- }
- async function submit(page: Page, input: string) {
- await page.getByRole("textbox", { name: "消息", exact: true }).fill(input)
- await page.getByRole("button", { name: "发送消息", exact: true }).click()
- await expect(page.getByRole("button", { name: "暂停", exact: true })).toBeVisible()
- }
- test("cached conversation merges remote results without replacing draft or attachments", async ({ page }) => {
- await connect(page)
- await submit(page, "原始研究")
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("不要覆盖的草稿")
- await page.getByRole("button", { name: "添加文件", exact: true }).click()
- await expect(page.getByRole("button", { name: "移除成功.txt", exact: true })).toBeVisible()
- await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
- await page.evaluate(() => {
- window.paasUX.update("ux_1", { status: "completed", output: "远端修订后的结论" })
- window.paasUX.add({
- run_id: "external_2",
- agent_id: "research",
- thread_id: "thread_ux_1",
- input: "其他窗口的追加研究",
- output: "其他窗口的新增成果",
- status: "completed",
- })
- })
- await page.getByRole("button", { name: /^原始研究/ }).click()
- await expect(page.getByText("其他窗口的新增成果", { exact: true })).toBeVisible()
- await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("不要覆盖的草稿")
- await expect(page.getByRole("button", { name: "移除成功.txt", exact: true })).toBeVisible()
- await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
- await page.getByRole("button", { name: /^原始研究/ }).click()
- await expect(page.locator('.paas-chat-message[data-role="assistant"]')).toHaveCount(2)
- })
- test("late task open cannot replace the user's newer selected conversation", async ({ page }) => {
- await connect(page)
- await page.evaluate(() => {
- for (const id of ["slow", "fast"])
- window.paasUX.add({
- run_id: id,
- agent_id: "research",
- thread_id: `thread_${id}`,
- input: `${id}任务`,
- output: `${id}结果`,
- status: "completed",
- })
- window.paasUX.holdRun = "slow"
- })
- await page.getByRole("button", { name: "平台任务", exact: true }).click()
- await page.getByLabel("项目范围", { exact: true }).selectOption("all")
- await page.getByRole("button", { name: "刷新任务", exact: true }).click()
- await page.getByRole("row").filter({ hasText: "slow任务" }).getByRole("button", { name: "打开任务" }).click()
- await expect.poll(() => page.evaluate(() => window.paasUX.held)).toBe(true)
- await page.getByRole("button", { name: "平台任务", exact: true }).click()
- await page.getByLabel("项目范围", { exact: true }).selectOption("all")
- await page.getByRole("row").filter({ hasText: "fast任务" }).getByRole("button", { name: "打开任务" }).click()
- await expect(page.getByText("fast结果", { exact: true })).toBeVisible()
- await page.evaluate(() => window.paasUX.release())
- await expect(page.getByText("slow结果", { exact: true })).toHaveCount(0)
- await expect(page.getByText("fast结果", { exact: true })).toBeVisible()
- })
- test("late thread synchronization cannot switch away from a newly created chat", async ({ page }) => {
- await connect(page)
- await submit(page, "旧会话")
- await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
- await page.evaluate(() => {
- window.paasUX.holdThread = "thread_ux_1"
- })
- await page.getByRole("button", { name: /^旧会话/ }).click()
- await expect.poll(() => page.evaluate(() => window.paasUX.held)).toBe(true)
- await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("新的编辑内容")
- await page.evaluate(() => window.paasUX.release())
- await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("新的编辑内容")
- await expect(page.getByRole("log", { name: "会话消息" })).not.toContainText("旧会话")
- })
- test("lost submission receipt recovers the canonical run without issuing another execute", async ({ page }) => {
- await connect(page)
- await submit(page, "回执丢失但任务已创建")
- await expect(page.getByText("已自动找回原任务,未重新提交。", { exact: true })).toBeVisible()
- const calls = await page.evaluate(() => window.paasTestCalls)
- const execution = calls.find((call) => call.action === "execute")!
- expect(execution.requestID).toBeTruthy()
- expect(calls.filter((call) => call.action === "execute")).toHaveLength(1)
- expect(calls.find((call) => call.action === "lookup-request")?.requestID).toBe(execution.requestID)
- await expect(page.locator('.paas-chat-message[data-role="assistant"]')).toHaveCount(1)
- })
- test("conversation rename, search and local archive persist without cancelling a running task", async ({ page }) => {
- await connect(page)
- await submit(page, "需要整理的任务")
- await page.locator(".paas-more-actions > summary").click()
- await page.getByRole("button", { name: "重命名会话", exact: true }).click()
- await page.getByRole("textbox", { name: "会话名称", exact: true }).fill("光学研究记录")
- await page.getByRole("button", { name: "保存名称", exact: true }).click()
- await page.getByRole("textbox", { name: "搜索会话标题", exact: true }).fill("光学")
- await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toBeVisible()
- await page.locator(".paas-more-actions > summary").click()
- await page.getByRole("button", { name: "在本机归档", exact: true }).click()
- await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toHaveCount(0)
- await page.getByRole("button", { name: "查看本机归档", exact: true }).click()
- await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toBeVisible()
- expect(await page.evaluate(() => window.paasTestCalls.some((c) => c.action === "cancel"))).toBe(false)
- await page.reload()
- await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
- await page.getByRole("button", { name: "连接平台", exact: true }).click()
- await expect(page.locator(".paas-topbar-chat-title")).toHaveText("光学研究记录")
- await page.locator(".paas-more-actions > summary").click()
- await expect(page.getByRole("button", { name: "取消归档", exact: true })).toBeVisible()
- await page.getByRole("button", { name: "取消归档", exact: true }).click()
- await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toBeVisible()
- })
- test("report export and continuation preserve the report and draft without executing again", async ({ page }) => {
- await connect(page)
- await submit(page, "报告导出研究")
- await page.evaluate(() =>
- window.paasUX.update("ux_1", { status: "completed", output: "# 可追溯研究方案\n\n保留原始证据。" }),
- )
- await page.getByRole("button", { name: "刷新状态", exact: true }).click()
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("已有的补充要求")
- await page.getByRole("button", { name: "导出报告", exact: true }).click()
- await expect(page.getByText("已导出 研究报告.md", { exact: true })).toBeVisible()
- await page.getByRole("button", { name: "打开保存位置", exact: true }).click()
- await page.getByRole("button", { name: "基于此方案继续", exact: true }).click()
- await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("已有的补充要求")
- const calls = await page.evaluate(() => window.paasTestCalls)
- expect(calls.filter((c) => c.action === "execute")).toHaveLength(1)
- expect(calls.find((c) => c.action === "export-report")?.text).toBe("# 可追溯研究方案\n\n保留原始证据。")
- })
- test("reconnecting refreshes the selected conversation while keeping its draft", async ({ page }) => {
- await connect(page)
- await submit(page, "断线期间继续研究")
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("仍在编辑的要求")
- await page.getByRole("button", { name: "平台设置", exact: true }).click()
- await page.getByRole("button", { name: "断开并忘记连接", exact: true }).click()
- await page.evaluate(() =>
- window.paasUX.add({
- run_id: "external_reconnect",
- agent_id: "research",
- thread_id: "thread_ux_1",
- input: "另一端追加的研究",
- output: "重新连接时同步的新成果",
- status: "completed",
- }),
- )
- await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
- await page.getByRole("button", { name: "连接平台", exact: true }).click()
- await expect(page.getByText("重新连接时同步的新成果", { exact: true })).toBeVisible()
- await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("仍在编辑的要求")
- })
- test("late stream events cannot undo completion or block the next draft", async ({ page }) => {
- await connect(page)
- await submit(page, "检查迟到事件")
- await page.evaluate(() => window.paasUX.update("ux_1", { status: "completed", output: "已交付报告" }))
- await page.getByRole("button", { name: "刷新状态", exact: true }).click()
- await expect(page.getByText("已交付报告", { exact: true })).toBeVisible()
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("继续研究")
- await page.evaluate(() => {
- const execution = window.paasTestCalls.find((c) => c.action === "execute")!
- ;(window.paasUX as unknown as { progress(e: unknown): void }).progress({
- executionID: execution.executionID,
- type: "started",
- runID: "ux_1",
- status: "running",
- })
- })
- await expect(page.getByRole("button", { name: "发送消息", exact: true })).toBeEnabled()
- await expect(page.getByRole("button", { name: "暂停", exact: true })).toHaveCount(0)
- })
- test("old active tasks remain visible and earlier history can be loaded", async ({ page }) => {
- await connect(page)
- await page.evaluate(() => {
- for (let i = 0; i < 26; i++)
- window.paasUX.add({
- run_id: `page_${i}`,
- agent_id: "research",
- input: `分页研究${i}`,
- status: i === 0 ? "paused" : "completed",
- })
- })
- await page.getByRole("button", { name: "平台任务", exact: true }).click()
- await page.getByLabel("项目范围", { exact: true }).selectOption("all")
- await page.getByRole("button", { name: "刷新任务", exact: true }).click()
- await expect(page.getByRole("row").filter({ hasText: "分页研究0" })).toBeVisible()
- await expect(
- page
- .getByRole("row")
- .filter({ hasText: "分页研究1" })
- .filter({ hasNotText: /分页研究1\d/ }),
- ).toHaveCount(0)
- await page.getByRole("button", { name: "加载更早任务", exact: true }).click()
- await expect(page.getByRole("row")).toHaveCount(27)
- await expect(page.getByRole("button", { name: "加载更早任务", exact: true })).toHaveCount(0)
- await page.getByRole("button", { name: "刷新任务", exact: true }).click()
- await expect(page.getByRole("row")).toHaveCount(27)
- })
- test("research settings return to the same draft without executing or changing budgets automatically", async ({
- page,
- }, testInfo) => {
- await connect(page)
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("保留我的下一轮要求")
- await page.getByRole("button", { name: "研究设置", exact: true }).click()
- await page.getByLabel("累计 Token 硬上限", { exact: true }).fill("2000000")
- await page
- .getByRole("group", { name: "科研预算与迭代", exact: true })
- .screenshot({ path: testInfo.outputPath("research-settings.png") })
- await page.getByRole("button", { name: "保存修改", exact: true }).click()
- await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("保留我的下一轮要求")
- const calls = await page.evaluate(() => window.paasTestCalls)
- expect(calls.some((c) => c.action === "execute")).toBe(false)
- expect(calls.find((c) => c.action === "update-agent")).toMatchObject({
- update: { config: { scientific: { max_actual_tokens: 2000000, token_budget: 500000 } } },
- })
- })
- test("partial results explain the next step and continuation keeps the user's draft", async ({ page }, testInfo) => {
- await connect(page)
- await submit(page, "中断后的继续研究")
- await page.evaluate(() => window.paasUX.update("ux_1", { status: "partial", output: "已完成的研究依据与初步方案" }))
- await page.getByRole("button", { name: "刷新状态", exact: true }).click()
- await expect(page.getByText(/当前成果已保留,可先导出/)).toBeVisible()
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("补充新的实验条件")
- await page.getByRole("button", { name: "基于已有成果续研", exact: true }).click()
- const draft = await page.getByRole("textbox", { name: "消息", exact: true }).inputValue()
- expect(draft).toContain("补充新的实验条件")
- await expect(page.getByLabel("已引用研究报告")).toContainText("已完成的研究依据与初步方案")
- expect(await page.evaluate(() => window.paasTestCalls.filter((c) => c.action === "execute").length)).toBe(1)
- await page.screenshot({ path: testInfo.outputPath("continuation.png"), fullPage: true })
- })
- test("unopened cached conversations survive repeated reloads without being treated as empty", async ({ page }) => {
- await page.goto("/e2e/paas/index.html?ux")
- await page.evaluate(() => {
- const scope = "test-recovery-test"
- const root = `lambda-scientist.chats.v2:${scope}:main`
- localStorage.setItem(`lambda-scientist.chats.v2:${scope}:latest`, root)
- localStorage.setItem(`${root}:index`, JSON.stringify({ ids: ["old", "current"], active: "current" }))
- for (const id of ["old", "current"]) {
- localStorage.setItem(
- `${root}:${id}:meta`,
- JSON.stringify({
- id,
- agentID: "research",
- name: "科研助手",
- title: `本机记录${id}`,
- threadID: "",
- draft: "",
- files: [],
- source: root,
- messageIDs: [`message-${id}`],
- live: false,
- }),
- )
- localStorage.setItem(
- `${root}:${id}:message:message-${id}`,
- JSON.stringify({
- id: `message-${id}`,
- role: "assistant",
- text: `已保留的报告${id}`,
- status: "completed",
- }),
- )
- }
- })
- for (let i = 0; i < 2; i++) {
- await page.reload()
- await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
- await page.getByRole("button", { name: "连接平台", exact: true }).click()
- await expect(page.locator(".paas-sidebar").getByRole("button", { name: "本机记录old", exact: true })).toBeVisible()
- await page.locator(".paas-new-session").click()
- await page.getByRole("textbox", { name: "消息", exact: true }).fill("新的草稿")
- }
- await page.locator(".paas-sidebar").getByRole("button", { name: "本机记录old", exact: true }).click()
- await expect(page.getByText("已保留的报告old", { exact: true })).toBeVisible()
- })
|