recovery.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { expect, test, type Page } from "@playwright/test"
  2. declare global {
  3. interface Window {
  4. paasUX: {
  5. update(id: string, value: Record<string, unknown>): void
  6. add(value: Record<string, unknown>): void
  7. holdRun: string
  8. holdThread: string
  9. held: boolean
  10. connectError: string
  11. catalogError: string
  12. emptyCatalog: boolean
  13. configVersion: string
  14. configError: boolean
  15. permissions?: string[]
  16. verifyError: string
  17. checks: Record<string, unknown>[]
  18. external: string[]
  19. progress(event: import("../../src/paas").PaasProgress): void
  20. release(): void
  21. }
  22. paasTestCalls: Record<string, unknown>[]
  23. }
  24. }
  25. async function connect(page: Page) {
  26. await page.goto("/e2e/paas/index.html?ux")
  27. await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
  28. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  29. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toBeEnabled()
  30. }
  31. async function submit(page: Page, input: string) {
  32. await page.getByRole("textbox", { name: "消息", exact: true }).fill(input)
  33. await page.getByRole("button", { name: "发送消息", exact: true }).click()
  34. await expect(page.getByRole("button", { name: "暂停", exact: true })).toBeVisible()
  35. }
  36. test("cached conversation merges remote results without replacing draft or attachments", async ({ page }) => {
  37. await connect(page)
  38. await submit(page, "原始研究")
  39. await page.getByRole("textbox", { name: "消息", exact: true }).fill("不要覆盖的草稿")
  40. await page.getByRole("button", { name: "添加文件", exact: true }).click()
  41. await expect(page.getByRole("button", { name: "移除成功.txt", exact: true })).toBeVisible()
  42. await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
  43. await page.evaluate(() => {
  44. window.paasUX.update("ux_1", { status: "completed", output: "远端修订后的结论" })
  45. window.paasUX.add({
  46. run_id: "external_2",
  47. agent_id: "research",
  48. thread_id: "thread_ux_1",
  49. input: "其他窗口的追加研究",
  50. output: "其他窗口的新增成果",
  51. status: "completed",
  52. })
  53. })
  54. await page.getByRole("button", { name: /^原始研究/ }).click()
  55. await expect(page.getByText("其他窗口的新增成果", { exact: true })).toBeVisible()
  56. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("不要覆盖的草稿")
  57. await expect(page.getByRole("button", { name: "移除成功.txt", exact: true })).toBeVisible()
  58. await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
  59. await page.getByRole("button", { name: /^原始研究/ }).click()
  60. await expect(page.locator('.paas-chat-message[data-role="assistant"]')).toHaveCount(2)
  61. })
  62. test("late task open cannot replace the user's newer selected conversation", async ({ page }) => {
  63. await connect(page)
  64. await page.evaluate(() => {
  65. for (const id of ["slow", "fast"])
  66. window.paasUX.add({
  67. run_id: id,
  68. agent_id: "research",
  69. thread_id: `thread_${id}`,
  70. input: `${id}任务`,
  71. output: `${id}结果`,
  72. status: "completed",
  73. })
  74. window.paasUX.holdRun = "slow"
  75. })
  76. await page.getByRole("button", { name: "平台任务", exact: true }).click()
  77. await page.getByLabel("项目范围", { exact: true }).selectOption("all")
  78. await page.getByRole("button", { name: "刷新任务", exact: true }).click()
  79. await page.getByRole("row").filter({ hasText: "slow任务" }).getByRole("button", { name: "打开任务" }).click()
  80. await expect.poll(() => page.evaluate(() => window.paasUX.held)).toBe(true)
  81. await page.getByRole("button", { name: "平台任务", exact: true }).click()
  82. await page.getByLabel("项目范围", { exact: true }).selectOption("all")
  83. await page.getByRole("row").filter({ hasText: "fast任务" }).getByRole("button", { name: "打开任务" }).click()
  84. await expect(page.getByText("fast结果", { exact: true })).toBeVisible()
  85. await page.evaluate(() => window.paasUX.release())
  86. await expect(page.getByText("slow结果", { exact: true })).toHaveCount(0)
  87. await expect(page.getByText("fast结果", { exact: true })).toBeVisible()
  88. })
  89. test("late thread synchronization cannot switch away from a newly created chat", async ({ page }) => {
  90. await connect(page)
  91. await submit(page, "旧会话")
  92. await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
  93. await page.evaluate(() => {
  94. window.paasUX.holdThread = "thread_ux_1"
  95. })
  96. await page.getByRole("button", { name: /^旧会话/ }).click()
  97. await expect.poll(() => page.evaluate(() => window.paasUX.held)).toBe(true)
  98. await page.locator(".paas-topbar-chat-actions").getByRole("button", { name: "新会话", exact: true }).click()
  99. await page.getByRole("textbox", { name: "消息", exact: true }).fill("新的编辑内容")
  100. await page.evaluate(() => window.paasUX.release())
  101. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("新的编辑内容")
  102. await expect(page.getByRole("log", { name: "会话消息" })).not.toContainText("旧会话")
  103. })
  104. test("lost submission receipt recovers the canonical run without issuing another execute", async ({ page }) => {
  105. await connect(page)
  106. await submit(page, "回执丢失但任务已创建")
  107. await expect(page.getByText("已自动找回原任务,未重新提交。", { exact: true })).toBeVisible()
  108. const calls = await page.evaluate(() => window.paasTestCalls)
  109. const execution = calls.find((call) => call.action === "execute")!
  110. expect(execution.requestID).toBeTruthy()
  111. expect(calls.filter((call) => call.action === "execute")).toHaveLength(1)
  112. expect(calls.find((call) => call.action === "lookup-request")?.requestID).toBe(execution.requestID)
  113. await expect(page.locator('.paas-chat-message[data-role="assistant"]')).toHaveCount(1)
  114. })
  115. test("conversation rename, search and local archive persist without cancelling a running task", async ({ page }) => {
  116. await connect(page)
  117. await submit(page, "需要整理的任务")
  118. await page.locator(".paas-more-actions > summary").click()
  119. await page.getByRole("button", { name: "重命名会话", exact: true }).click()
  120. await page.getByRole("textbox", { name: "会话名称", exact: true }).fill("光学研究记录")
  121. await page.getByRole("button", { name: "保存名称", exact: true }).click()
  122. await page.getByRole("textbox", { name: "搜索会话标题", exact: true }).fill("光学")
  123. await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toBeVisible()
  124. await page.locator(".paas-more-actions > summary").click()
  125. await page.getByRole("button", { name: "在本机归档", exact: true }).click()
  126. await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toHaveCount(0)
  127. await page.getByRole("button", { name: "查看本机归档", exact: true }).click()
  128. await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toBeVisible()
  129. expect(await page.evaluate(() => window.paasTestCalls.some((c) => c.action === "cancel"))).toBe(false)
  130. await page.reload()
  131. await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
  132. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  133. await expect(page.locator(".paas-topbar-chat-title")).toHaveText("光学研究记录")
  134. await page.locator(".paas-more-actions > summary").click()
  135. await expect(page.getByRole("button", { name: "取消归档", exact: true })).toBeVisible()
  136. await page.getByRole("button", { name: "取消归档", exact: true }).click()
  137. await expect(page.locator(".paas-session-items").getByRole("button", { name: /光学研究记录/ })).toBeVisible()
  138. })
  139. test("report export and continuation preserve the report and draft without executing again", async ({ page }) => {
  140. await connect(page)
  141. await submit(page, "报告导出研究")
  142. await page.evaluate(() =>
  143. window.paasUX.update("ux_1", { status: "completed", output: "# 可追溯研究方案\n\n保留原始证据。" }),
  144. )
  145. await page.getByRole("button", { name: "刷新状态", exact: true }).click()
  146. await page.getByRole("textbox", { name: "消息", exact: true }).fill("已有的补充要求")
  147. await page.getByRole("button", { name: "导出报告", exact: true }).click()
  148. await expect(page.getByText("已导出 研究报告.md", { exact: true })).toBeVisible()
  149. await page.getByRole("button", { name: "打开保存位置", exact: true }).click()
  150. await page.getByRole("button", { name: "基于此方案继续", exact: true }).click()
  151. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("已有的补充要求")
  152. const calls = await page.evaluate(() => window.paasTestCalls)
  153. expect(calls.filter((c) => c.action === "execute")).toHaveLength(1)
  154. expect(calls.find((c) => c.action === "export-report")?.text).toBe("# 可追溯研究方案\n\n保留原始证据。")
  155. })
  156. test("reconnecting refreshes the selected conversation while keeping its draft", async ({ page }) => {
  157. await connect(page)
  158. await submit(page, "断线期间继续研究")
  159. await page.getByRole("textbox", { name: "消息", exact: true }).fill("仍在编辑的要求")
  160. await page.getByRole("button", { name: "平台设置", exact: true }).click()
  161. await page.getByRole("button", { name: "断开并忘记连接", exact: true }).click()
  162. await page.evaluate(() =>
  163. window.paasUX.add({
  164. run_id: "external_reconnect",
  165. agent_id: "research",
  166. thread_id: "thread_ux_1",
  167. input: "另一端追加的研究",
  168. output: "重新连接时同步的新成果",
  169. status: "completed",
  170. }),
  171. )
  172. await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
  173. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  174. await expect(page.getByText("重新连接时同步的新成果", { exact: true })).toBeVisible()
  175. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("仍在编辑的要求")
  176. })
  177. test("late stream events cannot undo completion or block the next draft", async ({ page }) => {
  178. await connect(page)
  179. await submit(page, "检查迟到事件")
  180. await page.evaluate(() => window.paasUX.update("ux_1", { status: "completed", output: "已交付报告" }))
  181. await page.getByRole("button", { name: "刷新状态", exact: true }).click()
  182. await expect(page.getByText("已交付报告", { exact: true })).toBeVisible()
  183. await page.getByRole("textbox", { name: "消息", exact: true }).fill("继续研究")
  184. await page.evaluate(() => {
  185. const execution = window.paasTestCalls.find((c) => c.action === "execute")!
  186. ;(window.paasUX as unknown as { progress(e: unknown): void }).progress({
  187. executionID: execution.executionID,
  188. type: "started",
  189. runID: "ux_1",
  190. status: "running",
  191. })
  192. })
  193. await expect(page.getByRole("button", { name: "发送消息", exact: true })).toBeEnabled()
  194. await expect(page.getByRole("button", { name: "暂停", exact: true })).toHaveCount(0)
  195. })
  196. test("old active tasks remain visible and earlier history can be loaded", async ({ page }) => {
  197. await connect(page)
  198. await page.evaluate(() => {
  199. for (let i = 0; i < 26; i++)
  200. window.paasUX.add({
  201. run_id: `page_${i}`,
  202. agent_id: "research",
  203. input: `分页研究${i}`,
  204. status: i === 0 ? "paused" : "completed",
  205. })
  206. })
  207. await page.getByRole("button", { name: "平台任务", exact: true }).click()
  208. await page.getByLabel("项目范围", { exact: true }).selectOption("all")
  209. await page.getByRole("button", { name: "刷新任务", exact: true }).click()
  210. await expect(page.getByRole("row").filter({ hasText: "分页研究0" })).toBeVisible()
  211. await expect(
  212. page
  213. .getByRole("row")
  214. .filter({ hasText: "分页研究1" })
  215. .filter({ hasNotText: /分页研究1\d/ }),
  216. ).toHaveCount(0)
  217. await page.getByRole("button", { name: "加载更早任务", exact: true }).click()
  218. await expect(page.getByRole("row")).toHaveCount(27)
  219. await expect(page.getByRole("button", { name: "加载更早任务", exact: true })).toHaveCount(0)
  220. await page.getByRole("button", { name: "刷新任务", exact: true }).click()
  221. await expect(page.getByRole("row")).toHaveCount(27)
  222. })
  223. test("research settings return to the same draft without executing or changing budgets automatically", async ({
  224. page,
  225. }, testInfo) => {
  226. await connect(page)
  227. await page.getByRole("textbox", { name: "消息", exact: true }).fill("保留我的下一轮要求")
  228. await page.getByRole("button", { name: "研究设置", exact: true }).click()
  229. await page.getByLabel("累计 Token 硬上限", { exact: true }).fill("2000000")
  230. await page
  231. .getByRole("group", { name: "科研预算与迭代", exact: true })
  232. .screenshot({ path: testInfo.outputPath("research-settings.png") })
  233. await page.getByRole("button", { name: "保存修改", exact: true }).click()
  234. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue("保留我的下一轮要求")
  235. const calls = await page.evaluate(() => window.paasTestCalls)
  236. expect(calls.some((c) => c.action === "execute")).toBe(false)
  237. expect(calls.find((c) => c.action === "update-agent")).toMatchObject({
  238. update: { config: { scientific: { max_actual_tokens: 2000000, token_budget: 500000 } } },
  239. })
  240. })
  241. test("partial results explain the next step and continuation keeps the user's draft", async ({ page }, testInfo) => {
  242. await connect(page)
  243. await submit(page, "中断后的继续研究")
  244. await page.evaluate(() => window.paasUX.update("ux_1", { status: "partial", output: "已完成的研究依据与初步方案" }))
  245. await page.getByRole("button", { name: "刷新状态", exact: true }).click()
  246. await expect(page.getByText(/当前成果已保留,可先导出/)).toBeVisible()
  247. await page.getByRole("textbox", { name: "消息", exact: true }).fill("补充新的实验条件")
  248. await page.getByRole("button", { name: "基于已有成果续研", exact: true }).click()
  249. const draft = await page.getByRole("textbox", { name: "消息", exact: true }).inputValue()
  250. expect(draft).toContain("补充新的实验条件")
  251. await expect(page.getByLabel("已引用研究报告")).toContainText("已完成的研究依据与初步方案")
  252. expect(await page.evaluate(() => window.paasTestCalls.filter((c) => c.action === "execute").length)).toBe(1)
  253. await page.screenshot({ path: testInfo.outputPath("continuation.png"), fullPage: true })
  254. })
  255. test("unopened cached conversations survive repeated reloads without being treated as empty", async ({ page }) => {
  256. await page.goto("/e2e/paas/index.html?ux")
  257. await page.evaluate(() => {
  258. const scope = "test-recovery-test"
  259. const root = `lambda-scientist.chats.v2:${scope}:main`
  260. localStorage.setItem(`lambda-scientist.chats.v2:${scope}:latest`, root)
  261. localStorage.setItem(`${root}:index`, JSON.stringify({ ids: ["old", "current"], active: "current" }))
  262. for (const id of ["old", "current"]) {
  263. localStorage.setItem(
  264. `${root}:${id}:meta`,
  265. JSON.stringify({
  266. id,
  267. agentID: "research",
  268. name: "科研助手",
  269. title: `本机记录${id}`,
  270. threadID: "",
  271. draft: "",
  272. files: [],
  273. source: root,
  274. messageIDs: [`message-${id}`],
  275. live: false,
  276. }),
  277. )
  278. localStorage.setItem(
  279. `${root}:${id}:message:message-${id}`,
  280. JSON.stringify({
  281. id: `message-${id}`,
  282. role: "assistant",
  283. text: `已保留的报告${id}`,
  284. status: "completed",
  285. }),
  286. )
  287. }
  288. })
  289. for (let i = 0; i < 2; i++) {
  290. await page.reload()
  291. await page.getByLabel("API Key", { exact: true }).fill("recovery-test")
  292. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  293. await expect(page.locator(".paas-sidebar").getByRole("button", { name: "本机记录old", exact: true })).toBeVisible()
  294. await page.locator(".paas-new-session").click()
  295. await page.getByRole("textbox", { name: "消息", exact: true }).fill("新的草稿")
  296. }
  297. await page.locator(".paas-sidebar").getByRole("button", { name: "本机记录old", exact: true }).click()
  298. await expect(page.getByText("已保留的报告old", { exact: true })).toBeVisible()
  299. })