tools.spec.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { expect, test } from "@playwright/test"
  2. import {
  3. defineVisualRegions,
  4. reportVisualStability,
  5. startVisualProbe,
  6. stopVisualProbe,
  7. visualPlan,
  8. } from "../../utils/visual-stability"
  9. import {
  10. assistantMessage,
  11. directory,
  12. partUpdated,
  13. session,
  14. sessionID,
  15. setupTimeline,
  16. status,
  17. textPart,
  18. toolPart,
  19. userMessage,
  20. } from "./fixture"
  21. test.describe("timeline tool state stability", () => {
  22. test("moves lightweight tools through pending, running, and completed without replacing rows", async ({
  23. page,
  24. }, testInfo) => {
  25. const ids = ["webfetch", "websearch", "task", "skill", "custom"] as const
  26. const inputs = {
  27. webfetch: { url: "https://example.com/docs" },
  28. websearch: { query: "timeline stability" },
  29. task: { description: "Inspect timeline", subagent_type: "explore" },
  30. skill: { name: "stability" },
  31. custom: { target: "timeline", depth: 2 },
  32. }
  33. const names = { webfetch: "webfetch", websearch: "websearch", task: "task", skill: "skill", custom: "mcp_probe" }
  34. const questionID = "prt_state_question"
  35. const initial = [
  36. ...ids.map((id) => toolPart(`prt_state_${id}`, names[id], "pending", inputs[id])),
  37. toolPart(questionID, "question", "pending", questionInput()),
  38. textPart("prt_state_following", "Following lightweight tools"),
  39. ]
  40. const childID = "ses_timeline_child"
  41. const timeline = await setupTimeline(page, {
  42. messages: [userMessage(), assistantMessage(initial, { completed: false })],
  43. sessions: [session(), session({ id: childID, parentID: sessionID, title: "Inspect timeline" })],
  44. cpuRate: 4,
  45. })
  46. await timeline.send(status("busy"), 120)
  47. for (const id of ids) await timeline.waitForPart(`prt_state_${id}`)
  48. await expect(page.locator(`[data-timeline-part-id="${questionID}"]`)).toHaveCount(0)
  49. const regionIDs = [
  50. "prt_state_webfetch",
  51. "prt_state_websearch",
  52. "prt_state_task",
  53. "prt_state_skill",
  54. "prt_state_custom",
  55. ] as const
  56. const regions = defineVisualRegions({
  57. prt_state_webfetch: toolRegion(regionIDs[0]),
  58. prt_state_websearch: toolRegion(regionIDs[1]),
  59. prt_state_task: toolRegion(regionIDs[2]),
  60. prt_state_skill: toolRegion(regionIDs[3]),
  61. prt_state_custom: toolRegion(regionIDs[4]),
  62. })
  63. await startVisualProbe(page, regions)
  64. for (const [index, id] of ids.entries()) {
  65. await timeline.send(
  66. partUpdated(toolPart(`prt_state_${id}`, names[id], "running", inputs[id])),
  67. [80, 240, 100, 360, 140][index],
  68. )
  69. }
  70. for (const [index, id] of ["skill", "webfetch", "custom", "task", "websearch"].entries()) {
  71. const key = id as (typeof ids)[number]
  72. const metadata = key === "task" ? { sessionId: childID } : key === "websearch" ? { provider: "exa" } : {}
  73. const output = key === "websearch" ? "Result https://example.com/result" : "Completed"
  74. await timeline.send(
  75. partUpdated(toolPart(`prt_state_${key}`, names[key], "completed", inputs[key], { metadata, output })),
  76. [110, 70, 280, 130, 420][index],
  77. )
  78. }
  79. await timeline.send(
  80. partUpdated(
  81. toolPart(questionID, "question", "completed", questionInput(), { metadata: { answers: [["Keep it stable"]] } }),
  82. ),
  83. 350,
  84. )
  85. await timeline.waitForPart(questionID)
  86. await timeline.send(status("idle"), 500)
  87. const trace = await stopVisualProbe<keyof typeof regions>(page)
  88. await reportVisualStability(
  89. testInfo,
  90. "lightweight-tools",
  91. trace,
  92. visualPlan(regions, [
  93. { type: "required", regions: regionIDs },
  94. { type: "unique", regions: regionIDs },
  95. { type: "stable", regions: regionIDs },
  96. { type: "opacity", regions: "all" },
  97. { type: "continuity", regions: "all" },
  98. { type: "motion", regions: "all", maxReversals: 4 },
  99. { type: "label-stability", regions: "all" },
  100. ]),
  101. )
  102. await expect(page.locator(`[data-timeline-part-id="${questionID}"]`)).toContainText("Keep it stable")
  103. await expect(
  104. page.locator(`a[href$="/session/${childID}"]`, { has: page.locator('[data-component="task-tool-card"]') }),
  105. ).toBeVisible()
  106. await expect(page.getByRole("button", { name: /Exa Web Search/ })).toBeVisible()
  107. })
  108. test("keeps an expanded mixed context group stable through staggered completion and error", async ({
  109. page,
  110. }, testInfo) => {
  111. const ids = ["prt_ctx_01_read", "prt_ctx_02_glob", "prt_ctx_03_grep", "prt_ctx_04_list"]
  112. const tools = ["read", "glob", "grep", "list"]
  113. const inputs = [
  114. { filePath: "src/a.ts", offset: 0, limit: 120 },
  115. { path: directory, pattern: "**/*.ts" },
  116. { path: directory, pattern: "stability", include: "*.ts" },
  117. { path: "src" },
  118. ]
  119. const context = ids.map((id, index) => toolPart(id, tools[index]!, "pending", inputs[index]!))
  120. const timeline = await setupTimeline(page, {
  121. messages: [
  122. userMessage(),
  123. assistantMessage([...context, textPart("prt_ctx_following", "Following context")], { completed: false }),
  124. ],
  125. cpuRate: 4,
  126. })
  127. await timeline.send(status("busy"), 100)
  128. const groupSelector = `[data-timeline-part-ids="${ids.join(",")}"]`
  129. const group = page.locator(groupSelector)
  130. await expect(group).toBeVisible()
  131. await group.locator('[data-slot="collapsible-trigger"]').click()
  132. await expect(group.locator('[data-slot="collapsible-trigger"]')).toHaveAttribute("aria-expanded", "true")
  133. const regions = defineVisualRegions({
  134. status: {
  135. selector: `${groupSelector} [data-component="tool-status-title"]`,
  136. opacitySelectors: ['[data-slot="tool-status-active"]', '[data-slot="tool-status-done"]'],
  137. },
  138. context: { selector: groupSelector, closest: '[data-timeline-row="AssistantPart"]' },
  139. following: {
  140. selector: '[data-timeline-part-id="prt_ctx_following"]',
  141. closest: '[data-timeline-row="AssistantPart"]',
  142. },
  143. })
  144. await startVisualProbe(page, regions)
  145. for (const [index, delay] of [90, 260, 70, 380].entries()) {
  146. await timeline.send(partUpdated(toolPart(ids[index]!, tools[index]!, "running", inputs[index]!)), delay)
  147. }
  148. await timeline.send(partUpdated(toolPart(ids[1]!, tools[1]!, "completed", inputs[1]!)), 130)
  149. await timeline.send(partUpdated(toolPart(ids[3]!, tools[3]!, "completed", inputs[3]!)), 210)
  150. await timeline.send(
  151. partUpdated(toolPart(ids[0]!, tools[0]!, "error", inputs[0]!, { error: "Read interrupted" })),
  152. 110,
  153. )
  154. await timeline.send(partUpdated(toolPart(ids[2]!, tools[2]!, "completed", inputs[2]!)), 250)
  155. await expect(group.locator('[data-component="tool-status-title"]')).toHaveAttribute("aria-label", "Explored")
  156. await timeline.send(status("idle"), 700)
  157. const trace = await stopVisualProbe<keyof typeof regions>(page)
  158. await reportVisualStability(
  159. testInfo,
  160. "mixed-context",
  161. trace,
  162. visualPlan(regions, [
  163. { type: "required", regions: ["context", "following"] },
  164. { type: "unique", regions: ["context"] },
  165. { type: "stable", regions: ["context"] },
  166. { type: "opacity", regions: "all" },
  167. { type: "continuity", regions: "all" },
  168. { type: "motion", regions: "all", maxReversals: 4 },
  169. { type: "label-stability", regions: "all" },
  170. { type: "flow", regions: ["context", "following"] },
  171. ]),
  172. )
  173. await expect(group.locator('[data-component="tool-status-title"]')).toHaveAttribute("aria-label", "Explored")
  174. await expect(group.locator('[data-slot="collapsible-trigger"]')).toHaveAttribute("aria-expanded", "true")
  175. await group.locator('[data-slot="collapsible-trigger"]').click()
  176. await expect(group.locator('[data-slot="collapsible-trigger"]')).toHaveAttribute("aria-expanded", "false")
  177. await timeline.send(partUpdated(textPart("prt_ctx_late_sibling", "Later sibling content")), 200)
  178. await expect(group.locator('[data-slot="collapsible-trigger"]')).toHaveAttribute("aria-expanded", "false")
  179. await group.locator('[data-slot="collapsible-trigger"]').click()
  180. await expect(group.locator('[data-slot="collapsible-trigger"]')).toHaveAttribute("aria-expanded", "true")
  181. })
  182. })
  183. function questionInput() {
  184. return { questions: [{ header: "Stability", question: "Keep it stable?", options: [] }] }
  185. }
  186. function toolRegion(id: string) {
  187. return { selector: `[data-timeline-part-id="${id}"]`, closest: '[data-timeline-row="AssistantPart"]' }
  188. }