sidenote.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { expect, test, type Page } from "@playwright/test"
  2. import type { PaasRequest } from "../../src/paas"
  3. async function open(page: Page, extra = "") {
  4. await page.goto(`/e2e/paas/index.html?completed&unified&sidenotes${extra}`)
  5. await page.getByLabel("API Key", { exact: true }).fill("sidenote-test")
  6. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  7. }
  8. async function send(page: Page) {
  9. await page.getByRole("textbox", { name: "消息", exact: true }).fill("更新搜索")
  10. await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  11. await expect(page.locator(".lambda-sidenote-toggle")).toHaveAttribute("aria-expanded", "false")
  12. await page.locator(".lambda-sidenote-toggle").click()
  13. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(5)
  14. }
  15. async function calls(page: Page) {
  16. return page.evaluate(() => (window as Window & { paasTestCalls: PaasRequest[] }).paasTestCalls)
  17. }
  18. for (const theme of ["light", "dark"] as const) {
  19. test(`SideNote readable layout, navigation, follow-up and export in ${theme}`, async ({ page }) => {
  20. await page.emulateMedia({ colorScheme: theme })
  21. await page.setViewportSize({ width: 1600, height: 1000 })
  22. await open(page)
  23. await send(page)
  24. await expect(page.getByRole("region", { name: "回答正文" })).toBeVisible()
  25. await expect(page.locator(".lambda-sidenote-answer")).toHaveCSS("border-top-style", "solid")
  26. await expect(page.locator(".lambda-sidenote-panel")).toHaveCSS("border-top-style", "solid")
  27. const body = page.locator(".lambda-sidenote-body")
  28. const panel = page.getByRole("dialog", { name: "回答侧注", exact: true })
  29. await expect(body).not.toContainText("sidenotes")
  30. await expect(body).not.toContainText("默认使用系统")
  31. await expect(panel).toContainText("1 项存疑")
  32. await expect(panel.getByRole("button", { name: "定位原文" })).toHaveCount(5)
  33. await expect.poll(() => page.evaluate(() => CSS.highlights.size)).toBe(5)
  34. const box = await body.boundingBox(),
  35. notes = await panel.boundingBox()
  36. expect(notes!.x).toBeGreaterThan(box!.x + box!.width)
  37. await page.getByLabel("筛选侧注类型").selectOption("risk")
  38. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(1)
  39. await expect(panel).toContainText("尚未运行浏览器端到端测试")
  40. await page.getByRole("textbox", { name: "消息", exact: true }).fill("保留的草稿")
  41. await panel.getByRole("button", { name: "追问", exact: true }).click()
  42. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toHaveValue(
  43. /保留的草稿[\s\S]*尚未运行浏览器/,
  44. )
  45. expect((await calls(page)).filter((c) => c.action === "execute")).toHaveLength(1)
  46. await expect(panel).toHaveCount(0)
  47. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(0)
  48. await expect(page.locator(".lambda-sidenote-toggle")).toContainText("1 项存疑")
  49. await page.getByRole("button", { name: "导出报告", exact: true }).click()
  50. const exported = (await calls(page)).find((c) => c.action === "export-report")
  51. expect(exported && "text" in exported && exported.text).toContain("**存疑**")
  52. const receipt = await page.locator(".paas-export-receipt").boundingBox()
  53. const composer = await page.locator(".paas-composer").boundingBox()
  54. expect(Math.abs(receipt!.x - composer!.x)).toBeLessThan(2)
  55. expect(Math.abs(receipt!.width - composer!.width)).toBeLessThan(2)
  56. await page.getByRole("button", { name: "关闭导出提示" }).click()
  57. await expect(page.locator(".paas-export-receipt")).toHaveCount(0)
  58. await page.locator(".lambda-sidenote-toggle").click()
  59. await page.getByLabel("筛选侧注类型").selectOption("all")
  60. await page.locator(".lambda-sidenote-answer").scrollIntoViewIfNeeded()
  61. await page.screenshot({ path: `/private/tmp/lambda-sidenotes-${theme}.png`, fullPage: true })
  62. await page.setViewportSize({ width: 620, height: 900 })
  63. await expect(panel).toHaveAttribute("data-narrow", "true")
  64. expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true)
  65. await page.screenshot({ path: `/private/tmp/lambda-sidenotes-${theme}-narrow.png`, fullPage: true })
  66. await page.keyboard.press("Escape")
  67. await expect(panel).toHaveCount(0)
  68. await expect(page.locator(".lambda-sidenote-toggle")).toBeFocused()
  69. })
  70. }
  71. test("extension preference persists and is sent with each new task; existing notes remain accessible", async ({
  72. page,
  73. }) => {
  74. await open(page)
  75. await page.getByRole("button", { name: "应用与插件", exact: true }).click()
  76. await page.getByRole("tab", { name: "界面扩展" }).click()
  77. await page.getByRole("button", { name: "停用插件", exact: true }).click()
  78. await page.reload()
  79. if (await page.getByLabel("API Key", { exact: true }).isVisible()) {
  80. await page.getByLabel("API Key", { exact: true }).fill("sidenote-test")
  81. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  82. }
  83. await page.getByRole("button", { name: "会话", exact: true }).click()
  84. await page.getByRole("textbox", { name: "消息", exact: true }).fill("下一项任务")
  85. await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  86. await expect(page.locator(".lambda-sidenote-toggle")).toBeVisible()
  87. await expect(page.locator(".lambda-sidenote-toggle")).toHaveAttribute("aria-expanded", "false")
  88. expect((await calls(page)).find((c) => c.action === "execute")).toMatchObject({ sideNotes: false })
  89. await page.locator(".lambda-sidenote-toggle").click()
  90. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(5)
  91. })
  92. test("malformed annotations remain available without contaminating the main answer", async ({ page }) => {
  93. await open(page, "&malformed-notes")
  94. await send(page)
  95. await page.getByText("1 行侧注格式未识别,查看原文").click()
  96. await expect(page.locator(".lambda-sidenote-unparsed pre")).toContainText("保留这段未知格式")
  97. await expect(page.locator(".lambda-sidenote-body")).not.toContainText("unknown")
  98. })
  99. test("partial run snapshots buffer incomplete notes and completed snapshots become navigable", async ({ page }) => {
  100. await open(page, "&ux")
  101. await page.getByRole("textbox", { name: "消息", exact: true }).fill("逐步处理")
  102. await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  103. await expect.poll(async () => (await calls(page)).filter((c) => c.action === "execute").length).toBe(1)
  104. await page.evaluate(() => {
  105. const ux = (window as Window & { paasUX: { update: (id: string, value: object) => void } }).paasUX
  106. ux.update("ux_1", { output: "已经处理正文\n```sidenotes\n- 做法 | 正文 | 已完成扫描\n- 存疑 | 正文 | 尚未" })
  107. })
  108. await page.locator(".lambda-sidenote-toggle").click()
  109. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(1, { timeout: 10000 })
  110. await expect(page.locator(".lambda-sidenote-body")).toHaveText("已经处理正文")
  111. await expect(page.getByText("侧注生成中…", { exact: true })).toBeVisible()
  112. await expect(page.getByRole("button", { name: "定位原文", exact: true })).toHaveCount(0)
  113. await page.evaluate(() => {
  114. const ux = (window as Window & { paasUX: { update: (id: string, value: object) => void } }).paasUX
  115. ux.update("ux_1", {
  116. status: "completed",
  117. output: "已经处理正文\n```sidenotes\n- 做法 | 正文 | 已完成扫描\n- 存疑 | 正文 | 尚未验证结果\n```",
  118. })
  119. })
  120. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(2, { timeout: 10000 })
  121. await expect(page.getByRole("button", { name: "定位原文", exact: true })).toHaveCount(2)
  122. await expect(page.getByText("侧注生成中…", { exact: true })).toHaveCount(0)
  123. await page.reload()
  124. await page.getByLabel("API Key", { exact: true }).fill("sidenote-test")
  125. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  126. await page.locator(".lambda-sidenote-toggle").click()
  127. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(2)
  128. })
  129. test("source click selects the linked note and copy includes hidden limitations", async ({ page, context }) => {
  130. await context.grantPermissions(["clipboard-read", "clipboard-write"])
  131. await open(page)
  132. await send(page)
  133. await page.getByRole("button", { name: "关闭侧注" }).click()
  134. await page.locator(".lambda-sidenote-body code").click()
  135. await expect(page.locator('.lambda-sidenote-card[data-note-index="0"]')).toHaveAttribute("data-selected", "true")
  136. await page.getByRole("button", { name: "关闭侧注" }).click()
  137. await page.getByRole("button", { name: "复制报告", exact: true }).click()
  138. expect(await page.evaluate(() => navigator.clipboard.readText())).toContain("**存疑**(界面交互尚未验证)")
  139. })
  140. test("native code-workspace text parts use the same adapter during streaming and copy", async ({ page, context }) => {
  141. await context.grantPermissions(["clipboard-read", "clipboard-write"])
  142. await page.goto("/e2e/paas/index.html?native-sidenote")
  143. await expect(page.locator('[data-slot="text-part-body"]')).toContainText("已更新搜索")
  144. await page.evaluate(() => {
  145. const control = (window as Window & { nativeSideNotes: { update: (value: object) => void } }).nativeSideNotes
  146. control.update({ text: "已更新搜索\n```sidenotes\n- 存疑 | 已更新搜索 | 尚未实测\n" })
  147. })
  148. await page.locator(".lambda-sidenote-toggle").click()
  149. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(1)
  150. await expect(page.locator(".lambda-sidenote-body")).toHaveText("已更新搜索")
  151. await page.evaluate(() => {
  152. const control = (window as Window & { nativeSideNotes: { update: (value: object) => void } }).nativeSideNotes
  153. control.update({ text: "已更新搜索\n```sidenotes\n- 存疑 | 已更新搜索 | 尚未实测\n```", completed: true })
  154. })
  155. await expect(page.getByRole("button", { name: "定位原文" })).toBeVisible()
  156. await page.getByRole("button", { name: "关闭侧注" }).click()
  157. await page.locator('[data-component="text-part"]').hover()
  158. await page.getByRole("button", { name: /复制回复|Copy response/ }).click()
  159. expect(await page.evaluate(() => navigator.clipboard.readText())).toContain("**存疑**")
  160. })
  161. test("leaving a conversation closes its inspector and restores the full conversation width", async ({ page }) => {
  162. await page.setViewportSize({ width: 1600, height: 1000 })
  163. await open(page)
  164. await send(page)
  165. await expect(page.locator(".paas-conversation[data-ui-plugin-panel-open]")).toHaveCount(1)
  166. await page.getByRole("button", { name: "应用与插件", exact: true }).click()
  167. await expect(page.locator(".lambda-sidenote-panel")).toHaveCount(0)
  168. await expect(page.locator(".paas-conversation[data-ui-plugin-panel-open]")).toHaveCount(0)
  169. await page.getByRole("button", { name: "会话", exact: true }).click()
  170. await page.locator(".lambda-sidenote-toggle").click()
  171. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(5)
  172. })
  173. test("opening another answer replaces the inspector rather than stacking panels", async ({ page }) => {
  174. await page.setViewportSize({ width: 1600, height: 1000 })
  175. await open(page)
  176. await send(page)
  177. await page.getByRole("button", { name: "关闭侧注" }).click()
  178. await page.getByRole("textbox", { name: "消息", exact: true }).fill("继续更新搜索")
  179. await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  180. const toggles = page.locator(".lambda-sidenote-toggle")
  181. await expect(toggles).toHaveCount(2)
  182. await toggles.first().click()
  183. await expect(toggles.first()).toHaveAttribute("aria-expanded", "true")
  184. await toggles.last().click()
  185. await expect(toggles.first()).toHaveAttribute("aria-expanded", "false")
  186. await expect(toggles.last()).toHaveAttribute("aria-expanded", "true")
  187. await expect(page.locator(".lambda-sidenote-panel")).toHaveCount(1)
  188. })