setup.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { expect, test } from "@playwright/test"
  2. test("opening model setup starts at its heading instead of inheriting the previous page scroll", async ({ page }) => {
  3. await page.setViewportSize({ width: 1100, height: 700 })
  4. await page.goto("/e2e/paas/index.html?setup&ux&unified")
  5. await page.getByRole("button", { name: "准备本机服务并连接", exact: true }).click()
  6. await page.getByRole("button", { name: "打开导航", exact: true }).click()
  7. await page.getByRole("button", { name: "平台设置", exact: true }).click()
  8. await page.locator(".paas-content").evaluate((element) => {
  9. element.scrollTop = 400
  10. })
  11. await expect.poll(() => page.locator(".paas-content").evaluate((element) => element.scrollTop)).toBeGreaterThan(0)
  12. await page.getByRole("button", { name: "检查与配置资源", exact: true }).click()
  13. await expect(page.getByRole("heading", { name: "模型与资源", exact: true })).toBeInViewport()
  14. await expect.poll(() => page.locator(".paas-content").evaluate((element) => element.scrollTop)).toBe(0)
  15. })
  16. test("model controls stay readable throughout theme changes and while disabled", async ({ page }) => {
  17. await page.goto("/e2e/paas/index.html?setup&ux&unified")
  18. await page.getByRole("button", { name: "准备本机服务并连接", exact: true }).click()
  19. await page.getByRole("button", { name: "平台设置", exact: true }).click()
  20. await page.getByRole("button", { name: "检查与配置资源", exact: true }).click()
  21. const samples = await page.evaluate(async () => {
  22. const input = document.querySelector<HTMLInputElement>('.paas-model-setup input[aria-label="模型名称"]')!
  23. const select = document.querySelector<HTMLSelectElement>(".paas-model-setup select")!
  24. const button = document.querySelector<HTMLButtonElement>(".paas-setup-submit button")!
  25. const luminance = (color: string) => {
  26. const components = color
  27. .match(/[\d.]+/g)!
  28. .slice(0, 3)
  29. .map(Number)
  30. .map((value) => {
  31. const channel = value / 255
  32. return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4
  33. })
  34. return components[0] * 0.2126 + components[1] * 0.7152 + components[2] * 0.0722
  35. }
  36. const readings: { mode: string; control: string; contrast: number; background: string }[] = []
  37. for (const mode of ["dark", "light", "dark"]) {
  38. document.documentElement.style.colorScheme = mode
  39. for (let frame = 0; frame < 12; frame++) {
  40. await new Promise(requestAnimationFrame)
  41. for (const element of [input, select, button]) {
  42. const style = getComputedStyle(element)
  43. const fore = luminance(style.color)
  44. const back = luminance(style.backgroundColor)
  45. readings.push({
  46. mode,
  47. control: element.tagName,
  48. contrast: (Math.max(fore, back) + 0.05) / (Math.min(fore, back) + 0.05),
  49. background: style.backgroundColor,
  50. })
  51. }
  52. }
  53. }
  54. input.disabled = true
  55. select.disabled = true
  56. for (const element of [input, select]) {
  57. const style = getComputedStyle(element)
  58. const fore = luminance(style.color)
  59. const back = luminance(style.backgroundColor)
  60. readings.push({
  61. mode: "disabled",
  62. control: element.tagName,
  63. contrast: (Math.max(fore, back) + 0.05) / (Math.min(fore, back) + 0.05),
  64. background: style.backgroundColor,
  65. })
  66. }
  67. return readings
  68. })
  69. expect(samples.filter((sample) => sample.contrast < 4.5)).toEqual([])
  70. })
  71. test("first-use prepares local service, configures model and starts no task without sending", async ({ page }) => {
  72. await page.goto("/e2e/paas/index.html?setup&ux&unified")
  73. await page.getByRole("button", { name: "准备本机服务并连接", exact: true }).click()
  74. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toBeVisible()
  75. await page.getByRole("button", { name: "平台设置", exact: true }).click()
  76. await page.getByRole("button", { name: "检查与配置资源", exact: true }).click()
  77. const region = page.getByRole("region", { name: "模型与应用配置" })
  78. await region.getByLabel("模型服务", { exact: true }).selectOption("deepseek")
  79. await region.getByLabel("模型名称", { exact: true }).fill("deepseek-chat")
  80. await region.getByLabel("模型 API Key", { exact: true }).fill("synthetic-ui-key")
  81. await region.getByRole("checkbox", { name: "科研助手" }).check()
  82. await region.getByRole("button", { name: "保存模型配置", exact: true }).click()
  83. await expect(region.getByText(/模型已配置,后续新任务生效/)).toBeVisible()
  84. await expect(region.getByLabel("模型 API Key", { exact: true })).toHaveValue("")
  85. expect(
  86. await page.evaluate(() =>
  87. window.paasTestCalls.some((call) => call.action === "execute" || call.action === "verify"),
  88. ),
  89. ).toBe(false)
  90. await expect(region.getByRole("button", { name: "保存模型配置", exact: true })).toBeEnabled()
  91. await region.locator(".paas-setup-submit").scrollIntoViewIfNeeded()
  92. const checkbox = await region.getByRole("checkbox", { name: /科研助手/ }).boundingBox()
  93. const label = await region.locator(".paas-setup-target-name").first().boundingBox()
  94. expect(Math.abs(checkbox!.y + checkbox!.height / 2 - label!.y - label!.height / 2)).toBeLessThan(2)
  95. expect(label!.x - checkbox!.x - checkbox!.width).toBeGreaterThanOrEqual(8)
  96. await page.screenshot({ path: "/private/tmp/lambda-model-alignment-light.png", fullPage: true })
  97. await page.emulateMedia({ colorScheme: "dark" })
  98. await page.setViewportSize({ width: 700, height: 900 })
  99. await expect(region.getByLabel("模型名称", { exact: true })).toHaveCSS("background-color", "rgb(28, 30, 33)")
  100. expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)).toBe(true)
  101. await region.locator(".paas-setup-submit").scrollIntoViewIfNeeded()
  102. const narrowCheckbox = await region.getByRole("checkbox", { name: /科研助手/ }).boundingBox()
  103. const narrowLabel = await region.locator(".paas-setup-target-name").first().boundingBox()
  104. expect(
  105. Math.abs(narrowCheckbox!.y + narrowCheckbox!.height / 2 - narrowLabel!.y - narrowLabel!.height / 2),
  106. ).toBeLessThan(2)
  107. await page.screenshot({ path: "/private/tmp/lambda-model-alignment-dark.png", fullPage: true })
  108. await region.getByRole("button", { name: "开始任务", exact: true }).click()
  109. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toBeVisible()
  110. })
  111. test("model batch shows partial success and retains only failed applications for retry", async ({ page }) => {
  112. await page.goto("/e2e/paas/index.html?setup&ux&groups")
  113. await page.getByRole("button", { name: "准备本机服务并连接", exact: true }).click()
  114. await page.getByRole("button", { name: "平台设置", exact: true }).click()
  115. await page.getByRole("button", { name: "检查与配置资源", exact: true }).click()
  116. const region = page.getByRole("region", { name: "模型与应用配置" })
  117. await region.getByLabel("模型服务", { exact: true }).selectOption("deepseek")
  118. await region.getByLabel("模型名称", { exact: true }).fill("deepseek-chat")
  119. await region.getByLabel("模型 API Key", { exact: true }).fill("synthetic-ui-key")
  120. await region.getByRole("checkbox", { name: "科研助手", exact: true }).check()
  121. await region.getByRole("checkbox", { name: "未使用智能体", exact: true }).check()
  122. await region.getByRole("button", { name: "保存模型配置", exact: true }).click()
  123. await expect(region.getByText(/配置版本已变更/)).toBeVisible()
  124. await expect(region.getByRole("checkbox", { name: /科研助手/ })).not.toBeChecked()
  125. await expect(region.getByRole("checkbox", { name: "未使用智能体", exact: true })).toBeChecked()
  126. await region.getByRole("button", { name: "保存模型配置", exact: true }).click()
  127. await expect(region.getByText(/配置版本已变更/)).toBeVisible()
  128. expect(await page.evaluate(() => window.paasTestCalls.filter((call) => call.action === "save-provider").length)).toBe(
  129. 1,
  130. )
  131. })
  132. test("tool token is vaulted before inspection, never saved in the draft or agent config", async ({ page }) => {
  133. await page.goto("/e2e/paas/index.html?plugins&setup&unified&agent-groups")
  134. await page.getByRole("button", { name: "准备本机服务并连接", exact: true }).click()
  135. await page.getByRole("button", { name: "应用与插件", exact: true }).click()
  136. await page.getByRole("tab", { name: "工具扩展" }).click()
  137. await page.getByLabel("工具配置对象").selectOption("workspace")
  138. await page.getByRole("button", { name: "添加工具连接" }).click()
  139. await page.getByLabel("连接名称", { exact: true }).fill("文献检索")
  140. await page.getByLabel("服务地址", { exact: true }).fill("https://papers.example/mcp")
  141. await page.getByLabel("工具访问凭证(可选)", { exact: true }).fill("synthetic-plugin-key")
  142. await page.getByRole("button", { name: "读取工具", exact: true }).click()
  143. await expect(page.getByLabel("工具访问凭证(可选)", { exact: true })).toHaveValue("")
  144. expect(await page.evaluate(() => JSON.stringify(localStorage).includes("synthetic-plugin-key"))).toBe(false)
  145. await page.getByRole("button", { name: "保存连接", exact: true }).click()
  146. expect(
  147. await page.evaluate(() =>
  148. JSON.stringify(window.paasTestCalls.filter((call) => call.action === "update-agent")).includes(
  149. "synthetic-plugin-key",
  150. ),
  151. ),
  152. ).toBe(false)
  153. await page.getByRole("button", { name: "编辑连接", exact: true }).click()
  154. await page.getByLabel("服务地址", { exact: true }).fill("https://another.example/mcp")
  155. await expect(page.getByRole("button", { name: "保存连接", exact: true })).toBeDisabled()
  156. await page.getByRole("button", { name: "读取工具", exact: true }).click()
  157. const inspection = await page.evaluate(() =>
  158. window.paasTestCalls.filter((call) => call.action === "inspect-tool").at(-1),
  159. )
  160. expect(inspection).toMatchObject({ bearerEnv: "" })
  161. })