session-timeline-projection.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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(
  87. [
  88. { id: "prt_before_abort", type: "text", text: "Before interruption" },
  89. { id: "prt_compaction", type: "compaction", auto: true },
  90. ],
  91. {
  92. id: "msg_1001_assistant_aborted",
  93. error: { name: "MessageAbortedError", data: { message: "Stopped" } },
  94. },
  95. )
  96. const failed = assistantMessage([{ id: "prt_after_abort", type: "text", text: "After interruption" }], {
  97. id: "msg_1002_assistant_failed",
  98. error: {
  99. name: "APIError",
  100. data: {
  101. message: JSON.stringify({ error: { type: "provider_error", message: "Visible provider failure" } }),
  102. isRetryable: false,
  103. },
  104. },
  105. created: 1700000003000,
  106. })
  107. const nextUser = userMessage([userText("Second turn", { id: "prt_second_user" })], {
  108. id: "msg_2000_second_user",
  109. created: 1700000005000,
  110. })
  111. const nextAssistant = assistantMessage([{ id: "prt_second_text", type: "text", text: "Second response" }], {
  112. id: "msg_2001_second_assistant",
  113. parentID: "msg_2000_second_user",
  114. created: 1700000006000,
  115. })
  116. const timeline = await setupTimeline(page, { messages: [firstUser, aborted, failed, nextUser, nextAssistant] })
  117. await timeline.send(status("idle"), 100)
  118. const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
  119. await scroller.evaluate((element) => (element.scrollTop = 0))
  120. await expect(page.locator('[data-timeline-row="TurnDivider"]')).toHaveCount(1)
  121. await expect(page.getByText("Session compacted", { exact: true })).toBeVisible()
  122. await expect(page.getByText("Visible provider failure")).toBeVisible()
  123. await scroller.evaluate((element) => (element.scrollTop = element.scrollHeight))
  124. await expect(page.locator('[data-timeline-row="TurnGap"]')).toBeVisible()
  125. })
  126. test("renders comment strips and historical diff summary overflow", async ({ page }) => {
  127. const user = userMessage(
  128. [
  129. userText("The user made the following comment regarding lines 4 through 8 of src/a.ts: Keep this stable", {
  130. id: "prt_comment_only",
  131. synthetic: true,
  132. metadata: {
  133. opencodeComment: {
  134. path: "src/a.ts",
  135. selection: { startLine: 4, startChar: 0, endLine: 8, endChar: 0 },
  136. comment: "Keep this stable",
  137. },
  138. },
  139. }),
  140. userText("Continue after the comment", { id: "prt_comment_visible" }),
  141. ],
  142. { summary: { diffs: Array.from({ length: 11 }, (_, index) => summaryDiff(index)) } },
  143. )
  144. const nextUser = userMessage(undefined, { id: "msg_2000_diff_next_user", created: 1700000010000 })
  145. const nextAssistant = assistantMessage([], {
  146. id: "msg_2001_diff_next_assistant",
  147. parentID: "msg_2000_diff_next_user",
  148. created: 1700000011000,
  149. })
  150. await setupTimeline(page, {
  151. messages: [user, assistantMessage(), nextUser, nextAssistant],
  152. settings: { newLayoutDesigns: false },
  153. })
  154. const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
  155. await scroller.evaluate((element) => (element.scrollTop = 0))
  156. await expect(page.locator('[data-timeline-row="CommentStrip"]')).toBeVisible()
  157. await expect(page.getByText("Keep this stable", { exact: true })).toBeVisible()
  158. await expect(page.locator('[data-timeline-row="DiffSummary"]')).toBeVisible()
  159. await expect(page.getByText(/show all/i)).toBeVisible()
  160. })
  161. test("renders interruption independently when the turn is not compacted", async ({ page }) => {
  162. const user = userMessage()
  163. const before = assistantMessage([{ id: "prt_before", type: "text", text: "Before" }], {
  164. id: "msg_1001_before",
  165. error: { name: "MessageAbortedError", data: { message: "Stopped" } },
  166. })
  167. const after = assistantMessage([{ id: "prt_after", type: "text", text: "After" }], {
  168. id: "msg_1002_after",
  169. created: 1700000003000,
  170. })
  171. await setupTimeline(page, { messages: [user, before, after] })
  172. await expect(page.getByText("Interrupted", { exact: true })).toBeVisible()
  173. const rows = await page
  174. .locator('[data-timeline-row="AssistantPart"], [data-timeline-row="TurnDivider"]')
  175. .evaluateAll((elements) => elements.map((element) => element.getAttribute("data-timeline-row")))
  176. expect(rows).toEqual(["AssistantPart", "TurnDivider", "AssistantPart"])
  177. })
  178. test("renders user image, file attachment, file reference, and agent reference", async ({ page }) => {
  179. const text = "Use @explore with @src/a.ts and inspect the attachments"
  180. const parts: PartSeed<"user">[] = [
  181. userText(text, { id: "prt_user_rich" }),
  182. {
  183. id: "prt_user_image",
  184. type: "file",
  185. mime: "image/png",
  186. filename: "pixel.png",
  187. url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
  188. },
  189. {
  190. id: "prt_user_attachment",
  191. type: "file",
  192. mime: "application/json",
  193. filename: "tsconfig.json",
  194. url: "data:application/json;base64,e30=",
  195. },
  196. {
  197. id: "prt_user_reference",
  198. type: "file",
  199. mime: "text/plain",
  200. filename: "a.ts",
  201. url: "src/a.ts",
  202. source: { type: "file", path: "src/a.ts", text: { value: "@src/a.ts", start: 18, end: 27 } },
  203. },
  204. {
  205. id: "prt_user_agent",
  206. type: "agent",
  207. name: "explore",
  208. source: { value: "@explore", start: 4, end: 12 },
  209. },
  210. ]
  211. await setupTimeline(page, { messages: [userMessage(parts), assistantMessage()] })
  212. await expect(page.getByAltText("pixel.png")).toBeVisible()
  213. await expect(page.getByText("tsconfig.json")).toBeVisible()
  214. await expect(page.getByText("@src/a.ts", { exact: true })).toBeVisible()
  215. await expect(page.getByText("@explore", { exact: true })).toBeVisible()
  216. })
  217. })
  218. function editPart(id: string) {
  219. return toolPart(
  220. id,
  221. "edit",
  222. "completed",
  223. { filePath: "src/a.ts" },
  224. {
  225. metadata: {
  226. filediff: {
  227. file: "src/a.ts",
  228. additions: 1,
  229. deletions: 1,
  230. before: "export const value = 1\n",
  231. after: "export const value = 2\n",
  232. },
  233. },
  234. },
  235. )
  236. }
  237. function patchPart(id: string) {
  238. return toolPart(
  239. id,
  240. "apply_patch",
  241. "completed",
  242. { files: ["src/a.ts", "src/b.ts"] },
  243. {
  244. metadata: {
  245. files: [
  246. patchFile("src/a.ts", "update"),
  247. patchFile("src/b.ts", "add"),
  248. patchFile("src/old.ts", "delete"),
  249. { ...patchFile("src/moved.ts", "move"), move: "src/new-place.ts" },
  250. ],
  251. },
  252. },
  253. )
  254. }
  255. function patchFile(filePath: string, type: "add" | "update" | "delete" | "move") {
  256. return {
  257. filePath,
  258. relativePath: filePath,
  259. type,
  260. additions: type === "delete" ? 0 : 1,
  261. deletions: type === "add" ? 0 : 1,
  262. before: type === "add" ? undefined : "export const before = true\n",
  263. after: type === "delete" ? undefined : "export const after = true\n",
  264. }
  265. }
  266. function summaryDiff(index: number) {
  267. return {
  268. file: `src/diff-${index}.ts`,
  269. additions: 1,
  270. deletions: 1,
  271. patch: `@@ -1 +1 @@\n-export const value = ${index}\n+export const value = ${index + 1}`,
  272. }
  273. }