ui-plugins.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { expect, test, type Page } from "@playwright/test"
  2. async function open(page: Page) {
  3. await page.goto("/e2e/paas/index.html?completed&unified&sidenotes")
  4. await page.getByLabel("API Key", { exact: true }).fill("plugin-test")
  5. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  6. }
  7. async function manager(page: Page) {
  8. await page.getByRole("button", { name: "应用与插件", exact: true }).click()
  9. await page.getByRole("tab", { name: "界面扩展", exact: true }).click()
  10. }
  11. test("uninstall and reinstall retain history and register the answer contribution immediately", async ({ page }) => {
  12. await open(page)
  13. await page.getByRole("textbox", { name: "消息", exact: true }).fill("插件生命周期验收")
  14. await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  15. await expect(page.locator(".lambda-sidenote-toggle")).toBeVisible()
  16. await manager(page)
  17. await expect(page.getByRole("article", { name: "侧注 SideNote" })).toContainText("v0.1.0")
  18. await page.getByRole("button", { name: "卸载插件", exact: true }).click()
  19. await expect(page.getByRole("button", { name: "安装插件", exact: true })).toBeVisible()
  20. await page.getByRole("button", { name: "会话", exact: true }).click()
  21. await expect(page.locator(".lambda-sidenote-toggle")).toHaveCount(0)
  22. await expect(page.locator(".paas-report").last()).toContainText("尚未运行浏览器端到端测试")
  23. await page.reload()
  24. await page.getByLabel("API Key", { exact: true }).fill("plugin-test")
  25. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  26. await expect(page.locator(".lambda-sidenote-toggle")).toHaveCount(0)
  27. await manager(page)
  28. await page.getByRole("button", { name: "安装插件", exact: true }).click()
  29. await page.getByRole("button", { name: "会话", exact: true }).click()
  30. await page.locator(".lambda-sidenote-toggle").click()
  31. await expect(page.locator(".lambda-sidenote-card")).toHaveCount(5)
  32. })
  33. test("a rendering failure falls back to original text without losing notes", async ({ page }) => {
  34. await page.goto("/e2e/paas/index.html?native-sidenote")
  35. await page.evaluate(async () => {
  36. const control = (
  37. window as Window & { nativeSideNotes: { fail: () => Promise<void>; update: (value: object) => void } }
  38. ).nativeSideNotes
  39. await control.fail()
  40. control.update({ text: "保留我的答案\n```sidenotes\n- 存疑 | 我的答案 | 不能丢失的限制\n```", completed: true })
  41. })
  42. await expect(page.locator('[data-slot="text-part-body"]')).toContainText("保留我的答案")
  43. await expect(page.locator('[data-slot="text-part-body"]')).toContainText("不能丢失的限制")
  44. await expect(page.locator(".lambda-sidenote-panel")).toHaveCount(0)
  45. })
  46. test("unavailable preference storage does not falsely report uninstall success", async ({ page }) => {
  47. await open(page)
  48. await manager(page)
  49. await page.evaluate(() => {
  50. const original = Storage.prototype.setItem
  51. Storage.prototype.setItem = function (key, value) {
  52. if (key === "lambda.desktop.ui-plugins.v1") throw new Error("Synthetic storage failure")
  53. return original.call(this, key, value)
  54. }
  55. })
  56. await page.getByRole("button", { name: "卸载插件", exact: true }).click()
  57. await expect(page.getByRole("alert")).toContainText("当前状态未更改")
  58. await expect(page.getByRole("button", { name: "停用插件", exact: true })).toBeVisible()
  59. })
  60. for (const theme of ["light", "dark"] as const) {
  61. test(`plugin controls are readable in ${theme} and in narrow windows`, async ({ page }) => {
  62. await page.emulateMedia({ colorScheme: theme })
  63. await open(page)
  64. await manager(page)
  65. await page.screenshot({ path: `/private/tmp/ui-plugins-${theme}.png` })
  66. await page.setViewportSize({ width: 390, height: 720 })
  67. expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true)
  68. await expect(page.getByRole("button", { name: "停用插件", exact: true })).toBeVisible()
  69. await page.screenshot({ path: `/private/tmp/ui-plugins-${theme}-narrow.png` })
  70. })
  71. }
  72. test("plugin installation synchronizes across open windows without reloading their conversation", async ({
  73. page,
  74. context,
  75. }) => {
  76. await open(page)
  77. await manager(page)
  78. await page.getByRole("button", { name: "卸载插件", exact: true }).click()
  79. const other = await context.newPage()
  80. await open(other)
  81. await other.getByRole("textbox", { name: "消息", exact: true }).fill("跨窗口插件验收")
  82. await other.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  83. await expect(other.locator(".paas-report").last()).toContainText("尚未运行浏览器端到端测试")
  84. await expect(other.locator(".lambda-sidenote-toggle")).toHaveCount(0)
  85. await page.getByRole("button", { name: "安装插件", exact: true }).click()
  86. await expect(other.locator(".lambda-sidenote-toggle")).toBeVisible()
  87. await other.locator(".lambda-sidenote-toggle").click()
  88. await expect(other.locator(".lambda-sidenote-panel")).toBeVisible()
  89. await page.getByRole("button", { name: "卸载插件", exact: true }).click()
  90. await expect(other.locator(".lambda-sidenote-panel")).toHaveCount(0)
  91. await expect(other.locator(".paas-report").last()).toContainText("尚未运行浏览器端到端测试")
  92. await other.close()
  93. })
  94. test("the host selects drawer mode from available reading space, not window width", async ({ page }) => {
  95. await page.setViewportSize({ width: 1600, height: 1000 })
  96. await open(page)
  97. await page.getByRole("textbox", { name: "消息", exact: true }).fill("容器尺寸验收")
  98. await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
  99. await page.locator(".lambda-sidenote-toggle").click()
  100. const panel = page.locator(".lambda-sidenote-panel")
  101. await expect(panel).toHaveAttribute("data-narrow", "false")
  102. await page.locator("[data-ui-plugin-dock-host]").evaluate((element) => {
  103. ;(element as HTMLElement).style.maxWidth = "800px"
  104. })
  105. await expect(panel).toHaveAttribute("data-narrow", "true")
  106. await expect(page.locator("[data-ui-plugin-panel-open]")).toHaveCount(0)
  107. await page.locator("[data-ui-plugin-dock-host]").evaluate((element) => {
  108. ;(element as HTMLElement).style.removeProperty("max-width")
  109. })
  110. await expect(panel).toHaveAttribute("data-narrow", "false")
  111. await expect(page.locator("[data-ui-plugin-panel-open]")).toHaveCount(1)
  112. await page.getByRole("button", { name: "关闭侧注", exact: true }).click()
  113. await expect(page.locator("[data-ui-plugin-panel-open]")).toHaveCount(0)
  114. })
  115. test("hiding a mounted native workspace dismisses its portaled plugin panel", async ({ page }) => {
  116. await page.goto("/e2e/paas/index.html?native-sidenote")
  117. await page.evaluate(() => {
  118. ;(window as Window & { nativeSideNotes: { update: (value: object) => void } }).nativeSideNotes.update({
  119. text: "保留我的答案\n```sidenotes\n- 存疑 | 我的答案 | 尚未核验\n```",
  120. completed: true,
  121. })
  122. })
  123. await page.locator(".lambda-sidenote-toggle").click()
  124. await expect(page.locator(".lambda-sidenote-panel")).toBeVisible()
  125. await page.locator("main").evaluate((element) => {
  126. ;(element as HTMLElement).style.display = "none"
  127. })
  128. await expect(page.locator(".lambda-sidenote-panel")).toHaveCount(0)
  129. await page.locator("main").evaluate((element) => {
  130. ;(element as HTMLElement).style.removeProperty("display")
  131. })
  132. await expect(page.locator(".lambda-sidenote-toggle")).toHaveAttribute("aria-expanded", "false")
  133. })