session-timeline-projection.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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(
  38. "prt_question",
  39. "question",
  40. "completed",
  41. { questions: [{ question: "Keep stable?", header: "Stability", options: [] }] },
  42. { metadata: { answers: [["Yes"]] } },
  43. ),
  44. toolPart("prt_skill", "skill", "completed", { name: "stability" }),
  45. toolPart("prt_custom", "custom_mcp_tool", "completed", { target: "timeline", count: 2 }),
  46. ]
  47. await setupTimeline(page, { messages: [userMessage(), assistantMessage(parts)] })
  48. await expect(
  49. page.locator('[data-timeline-part-ids="prt_01_read,prt_02_glob,prt_03_grep,prt_04_list"]'),
  50. ).toBeVisible()
  51. for (const id of [
  52. "prt_webfetch",
  53. "prt_websearch",
  54. "prt_task",
  55. "prt_bash",
  56. "prt_edit",
  57. "prt_write",
  58. "prt_patch",
  59. "prt_question",
  60. "prt_skill",
  61. "prt_custom",
  62. ]) {
  63. await expect(page.locator(`[data-timeline-part-id="${id}"]`).first(), id).toBeVisible()
  64. }
  65. })
  66. test("projects gaps, dividers, assistant parts, and errors together", async ({ page }) => {
  67. const firstUser = userMessage(
  68. [
  69. userText("The user made the following comment regarding lines 4 through 8 of src/a.ts: Keep this stable", {
  70. id: "prt_comment",
  71. synthetic: true,
  72. metadata: {
  73. opencodeComment: {
  74. path: "src/a.ts",
  75. selection: { startLine: 4, startChar: 0, endLine: 8, endChar: 0 },
  76. comment: "Keep this stable",
  77. },
  78. },
  79. }),
  80. userText("Continue after the comment", { id: "prt_visible_user" }),
  81. ],
  82. { summary: { diffs: Array.from({ length: 11 }, (_, index) => summaryDiff(index)) } },
  83. )
  84. const aborted = assistantMessage(
  85. [
  86. { id: "prt_before_abort", type: "text", text: "Before interruption" },
  87. { id: "prt_compaction", type: "compaction", auto: true },
  88. ],
  89. {
  90. id: "msg_1001_assistant_aborted",
  91. error: { name: "MessageAbortedError", data: { message: "Stopped" } },
  92. },
  93. )
  94. const failed = assistantMessage([{ id: "prt_after_abort", type: "text", text: "After interruption" }], {
  95. id: "msg_1002_assistant_failed",
  96. error: {
  97. name: "APIError",
  98. data: {
  99. message: JSON.stringify({ error: { type: "provider_error", message: "Visible provider failure" } }),
  100. isRetryable: false,
  101. },
  102. },
  103. created: 1700000003000,
  104. })
  105. const nextUser = userMessage([userText("Second turn", { id: "prt_second_user" })], {
  106. id: "msg_2000_second_user",
  107. created: 1700000005000,
  108. })
  109. const nextAssistant = assistantMessage([{ id: "prt_second_text", type: "text", text: "Second response" }], {
  110. id: "msg_2001_second_assistant",
  111. parentID: "msg_2000_second_user",
  112. created: 1700000006000,
  113. })
  114. const timeline = await setupTimeline(page, { messages: [firstUser, aborted, failed, nextUser, nextAssistant] })
  115. await timeline.send(status("idle"), 100)
  116. const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
  117. await scroller.evaluate((element) => (element.scrollTop = 0))
  118. await expect(page.locator('[data-timeline-row="TurnDivider"]')).toHaveCount(1)
  119. await expect(page.getByText("Session compacted", { exact: true })).toBeVisible()
  120. await expect(page.getByText("Visible provider failure")).toBeVisible()
  121. await scroller.evaluate((element) => (element.scrollTop = element.scrollHeight))
  122. await expect(page.locator('[data-timeline-row="TurnGap"]')).toBeVisible()
  123. })
  124. test("renders comment strips and historical diff summary overflow", async ({ page }) => {
  125. const user = userMessage(
  126. [
  127. userText("The user made the following comment regarding lines 4 through 8 of src/a.ts: Keep this stable", {
  128. id: "prt_comment_only",
  129. synthetic: true,
  130. metadata: {
  131. opencodeComment: {
  132. path: "src/a.ts",
  133. selection: { startLine: 4, startChar: 0, endLine: 8, endChar: 0 },
  134. comment: "Keep this stable",
  135. },
  136. },
  137. }),
  138. userText("Continue after the comment", { id: "prt_comment_visible" }),
  139. ],
  140. { summary: { diffs: Array.from({ length: 11 }, (_, index) => summaryDiff(index)) } },
  141. )
  142. const nextUser = userMessage(undefined, { id: "msg_2000_diff_next_user", created: 1700000010000 })
  143. const nextAssistant = assistantMessage([], {
  144. id: "msg_2001_diff_next_assistant",
  145. parentID: "msg_2000_diff_next_user",
  146. created: 1700000011000,
  147. })
  148. await setupTimeline(page, {
  149. messages: [user, assistantMessage(), nextUser, nextAssistant],
  150. settings: { newLayoutDesigns: false },
  151. })
  152. const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
  153. await scroller.evaluate((element) => (element.scrollTop = 0))
  154. await expect(page.locator('[data-timeline-row="CommentStrip"]')).toBeVisible()
  155. await expect(page.getByText("Keep this stable", { exact: true })).toBeVisible()
  156. await expect(page.locator('[data-timeline-row="DiffSummary"]')).toBeVisible()
  157. await expect(page.getByText(/show all/i)).toBeVisible()
  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. "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. patch: `@@ -1 +1 @@\n-export const value = ${index}\n+export const value = ${index + 1}`,
  270. }
  271. }