session-timeline-projection.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { expect, test } from "@playwright/test"
  2. import {
  3. assistantMessage,
  4. setupTimeline,
  5. status,
  6. toolPart,
  7. userMessage,
  8. userText,
  9. type PartSeed,
  10. } from "../performance/timeline-stability/fixture"
  11. test.describe("session timeline projection", () => {
  12. test("renders every admitted tool family and hides timeline-only exclusions", async ({ page }) => {
  13. const parts = [
  14. toolPart("prt_01_read", "read", "completed", { filePath: "src/a.ts" }),
  15. toolPart("prt_02_glob", "glob", "completed", { path: ".", pattern: "**/*.ts" }),
  16. toolPart("prt_03_grep", "grep", "completed", { path: ".", pattern: "value" }),
  17. toolPart("prt_04_list", "list", "completed", { path: "src" }),
  18. toolPart("prt_webfetch", "webfetch", "completed", { url: "https://example.com" }),
  19. toolPart(
  20. "prt_websearch",
  21. "websearch",
  22. "completed",
  23. { query: "timeline stability" },
  24. { output: "https://example.com/result" },
  25. ),
  26. toolPart("prt_task", "task", "completed", { description: "Inspect timeline", subagent_type: "explore" }),
  27. toolPart(
  28. "prt_bash",
  29. "bash",
  30. "completed",
  31. { command: "printf stable" },
  32. { output: "stable", title: "printf stable" },
  33. ),
  34. editPart("prt_edit"),
  35. toolPart("prt_write", "write", "completed", { filePath: "src/new.ts", content: "export const stable = true\n" }),
  36. patchPart("prt_patch"),
  37. toolPart("prt_todo", "todowrite", "completed", { todos: [{ content: "Hidden", status: "pending" }] }),
  38. toolPart(
  39. "prt_question",
  40. "question",
  41. "completed",
  42. { questions: [{ question: "Keep stable?", header: "Stability", options: [] }] },
  43. { metadata: { answers: [["Yes"]] } },
  44. ),
  45. toolPart("prt_skill", "skill", "completed", { name: "stability" }),
  46. toolPart("prt_custom", "custom_mcp_tool", "completed", { target: "timeline", count: 2 }),
  47. ]
  48. await setupTimeline(page, { messages: [userMessage(), assistantMessage(parts)] })
  49. await expect(
  50. page.locator('[data-timeline-part-ids="prt_01_read,prt_02_glob,prt_03_grep,prt_04_list"]'),
  51. ).toBeVisible()
  52. for (const id of [
  53. "prt_webfetch",
  54. "prt_websearch",
  55. "prt_task",
  56. "prt_bash",
  57. "prt_edit",
  58. "prt_write",
  59. "prt_patch",
  60. "prt_question",
  61. "prt_skill",
  62. "prt_custom",
  63. ]) {
  64. await expect(page.locator(`[data-timeline-part-id="${id}"]`).first(), id).toBeVisible()
  65. }
  66. await expect(page.locator('[data-timeline-part-id="prt_todo"]')).toHaveCount(0)
  67. })
  68. test("projects gaps, dividers, assistant parts, and errors together", async ({ page }) => {
  69. const firstUser = userMessage(
  70. [
  71. userText("The user made the following comment regarding lines 4 through 8 of src/a.ts: Keep this stable", {
  72. id: "prt_comment",
  73. synthetic: true,
  74. metadata: {
  75. opencodeComment: {
  76. path: "src/a.ts",
  77. selection: { startLine: 4, startChar: 0, endLine: 8, endChar: 0 },
  78. comment: "Keep this stable",
  79. },
  80. },
  81. }),
  82. userText("Continue after the comment", { id: "prt_visible_user" }),
  83. ],
  84. { summary: { diffs: Array.from({ length: 11 }, (_, index) => summaryDiff(index)) } },
  85. )
  86. const aborted = assistantMessage([{ id: "prt_before_abort", type: "text", text: "Before interruption" }], {
  87. id: "msg_1001_assistant_aborted",
  88. error: { name: "MessageAbortedError", data: { message: "Stopped" } },
  89. })
  90. const failed = assistantMessage([{ id: "prt_after_abort", type: "text", text: "After interruption" }], {
  91. id: "msg_1002_assistant_failed",
  92. error: {
  93. name: "APIError",
  94. data: {
  95. message: JSON.stringify({ error: { type: "provider_error", message: "Visible provider failure" } }),
  96. isRetryable: false,
  97. },
  98. },
  99. created: 1700000003000,
  100. })
  101. const nextUser = userMessage([userText("Second turn", { id: "prt_second_user" })], {
  102. id: "msg_2000_second_user",
  103. created: 1700000005000,
  104. })
  105. const nextAssistant = assistantMessage([{ id: "prt_second_text", type: "text", text: "Second response" }], {
  106. id: "msg_2001_second_assistant",
  107. parentID: "msg_2000_second_user",
  108. created: 1700000006000,
  109. })
  110. const timeline = await setupTimeline(page, { messages: [firstUser, aborted, failed, nextUser, nextAssistant] })
  111. await timeline.send(status("idle"), 100)
  112. const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
  113. await scroller.evaluate((element) => (element.scrollTop = 0))
  114. await expect(page.locator('[data-timeline-row="TurnDivider"]')).toHaveCount(1)
  115. await expect(page.getByText("Before interruption", { exact: true })).toBeVisible()
  116. await expect(page.getByText("Visible provider failure")).toBeVisible()
  117. await scroller.evaluate((element) => (element.scrollTop = element.scrollHeight))
  118. await expect(page.locator('[data-timeline-row="TurnGap"]')).toBeVisible()
  119. })
  120. test("renders legacy synthetic comments as ordinary V2 user text", async ({ page }) => {
  121. const user = userMessage(
  122. [
  123. userText("The user made the following comment regarding lines 4 through 8 of src/a.ts: Keep this stable", {
  124. id: "prt_comment_only",
  125. synthetic: true,
  126. metadata: {
  127. opencodeComment: {
  128. path: "src/a.ts",
  129. selection: { startLine: 4, startChar: 0, endLine: 8, endChar: 0 },
  130. comment: "Keep this stable",
  131. },
  132. },
  133. }),
  134. userText("Continue after the comment", { id: "prt_comment_visible" }),
  135. ],
  136. { summary: { diffs: Array.from({ length: 11 }, (_, index) => summaryDiff(index)) } },
  137. )
  138. const nextUser = userMessage(undefined, { id: "msg_2000_diff_next_user", created: 1700000010000 })
  139. const nextAssistant = assistantMessage([], {
  140. id: "msg_2001_diff_next_assistant",
  141. parentID: "msg_2000_diff_next_user",
  142. created: 1700000011000,
  143. })
  144. await setupTimeline(page, {
  145. messages: [user, assistantMessage(), nextUser, nextAssistant],
  146. settings: { newLayoutDesigns: false },
  147. })
  148. const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
  149. await scroller.evaluate((element) => (element.scrollTop = 0))
  150. await expect(
  151. page.getByText(
  152. "The user made the following comment regarding lines 4 through 8 of src/a.ts: Keep this stable Continue after the comment",
  153. { exact: true },
  154. ),
  155. ).toBeVisible()
  156. await expect(page.locator('[data-timeline-row="CommentStrip"]')).toHaveCount(0)
  157. await expect(page.locator('[data-timeline-row="DiffSummary"]')).toHaveCount(0)
  158. })
  159. test("renders interruption independently when the turn is not compacted", async ({ page }) => {
  160. const user = userMessage()
  161. const before = assistantMessage([{ id: "prt_before", type: "text", text: "Before" }], {
  162. id: "msg_1001_before",
  163. error: { name: "MessageAbortedError", data: { message: "Stopped" } },
  164. })
  165. const after = assistantMessage([{ id: "prt_after", type: "text", text: "After" }], {
  166. id: "msg_1002_after",
  167. created: 1700000003000,
  168. })
  169. await setupTimeline(page, { messages: [user, before, after] })
  170. await expect(page.getByText("Interrupted", { exact: true })).toBeVisible()
  171. const rows = await page
  172. .locator('[data-timeline-row="AssistantPart"], [data-timeline-row="TurnDivider"]')
  173. .evaluateAll((elements) => elements.map((element) => element.getAttribute("data-timeline-row")))
  174. expect(rows).toEqual(["AssistantPart", "TurnDivider", "AssistantPart"])
  175. })
  176. test("renders user image, file attachment, file reference, and agent reference", async ({ page }) => {
  177. const text = "Use @explore with @src/a.ts and inspect the attachments"
  178. const parts: PartSeed<"user">[] = [
  179. userText(text, { id: "prt_user_rich" }),
  180. {
  181. id: "prt_user_image",
  182. type: "file",
  183. mime: "image/png",
  184. filename: "pixel.png",
  185. url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
  186. },
  187. {
  188. id: "prt_user_attachment",
  189. type: "file",
  190. mime: "application/json",
  191. filename: "tsconfig.json",
  192. url: "data:application/json;base64,e30=",
  193. },
  194. {
  195. id: "prt_user_reference",
  196. type: "file",
  197. mime: "text/plain",
  198. filename: "a.ts",
  199. url: "src/a.ts",
  200. source: { type: "file", path: "src/a.ts", text: { value: "@src/a.ts", start: 18, end: 27 } },
  201. },
  202. {
  203. id: "prt_user_agent",
  204. type: "agent",
  205. name: "explore",
  206. source: { value: "@explore", start: 4, end: 12 },
  207. },
  208. ]
  209. await setupTimeline(page, { messages: [userMessage(parts), assistantMessage()] })
  210. await expect(page.getByAltText("pixel.png")).toBeVisible()
  211. await expect(page.getByText("tsconfig.json")).toBeVisible()
  212. await expect(page.getByText("@src/a.ts", { exact: true })).toBeVisible()
  213. await expect(page.getByText("@explore", { exact: true })).toBeVisible()
  214. })
  215. })
  216. function editPart(id: string) {
  217. return toolPart(
  218. id,
  219. "edit",
  220. "completed",
  221. { filePath: "src/a.ts" },
  222. {
  223. metadata: {
  224. filediff: {
  225. file: "src/a.ts",
  226. additions: 1,
  227. deletions: 1,
  228. before: "export const value = 1\n",
  229. after: "export const value = 2\n",
  230. },
  231. },
  232. },
  233. )
  234. }
  235. function patchPart(id: string) {
  236. return toolPart(
  237. id,
  238. "apply_patch",
  239. "completed",
  240. { files: ["src/a.ts", "src/b.ts"] },
  241. {
  242. metadata: {
  243. files: [
  244. patchFile("src/a.ts", "update"),
  245. patchFile("src/b.ts", "add"),
  246. patchFile("src/old.ts", "delete"),
  247. { ...patchFile("src/moved.ts", "move"), move: "src/new-place.ts" },
  248. ],
  249. },
  250. },
  251. )
  252. }
  253. function patchFile(filePath: string, type: "add" | "update" | "delete" | "move") {
  254. return {
  255. filePath,
  256. relativePath: filePath,
  257. type,
  258. additions: type === "delete" ? 0 : 1,
  259. deletions: type === "add" ? 0 : 1,
  260. before: type === "add" ? undefined : "export const before = true\n",
  261. after: type === "delete" ? undefined : "export const after = true\n",
  262. }
  263. }
  264. function summaryDiff(index: number) {
  265. return {
  266. file: `src/diff-${index}.ts`,
  267. additions: 1,
  268. deletions: 1,
  269. status: "modified" as const,
  270. patch: `@@ -1 +1 @@\n-export const value = ${index}\n+export const value = ${index + 1}`,
  271. }
  272. }