session-todo-dock-navigation.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { base64Encode } from "@opencode-ai/core/util/encode"
  2. import { expect, test, type Page } from "@playwright/test"
  3. import { mockOpenCodeServer } from "../utils/mock-server"
  4. import { expectSessionTitle } from "../utils/waits"
  5. const directory = "C:/OpenCode/TodoDockNavigation"
  6. const projectID = "proj_todo_dock_navigation"
  7. const sourceID = "ses_todo_dock_source"
  8. const otherID = "ses_todo_dock_other"
  9. const sourceTitle = "Todo dock animation"
  10. const otherTitle = "Separate session"
  11. const activeTodos = [
  12. { id: "todo-1", content: "Receive todos in the active session", status: "completed", priority: "high" },
  13. { id: "todo-2", content: "Keep the dock visible across tabs", status: "completed", priority: "high" },
  14. { id: "todo-3", content: "Close after the final todo", status: "in_progress", priority: "high" },
  15. ]
  16. type EventPayload = {
  17. directory: string
  18. payload: Record<string, unknown>
  19. }
  20. test.use({ viewport: { width: 1440, height: 900 }, reducedMotion: "no-preference" })
  21. test("animates todo lifecycle without replaying it across session tabs", async ({ page }) => {
  22. test.setTimeout(90_000)
  23. const events: EventPayload[] = []
  24. const todos: Record<string, typeof activeTodos> = { [sourceID]: [], [otherID]: [] }
  25. const sessionStatus: Record<string, { type: "busy" | "idle" }> = {}
  26. await mockOpenCodeServer(page, {
  27. directory,
  28. project: {
  29. id: projectID,
  30. worktree: directory,
  31. vcs: "git",
  32. name: "todo-dock-navigation",
  33. time: { created: 1700000000000, updated: 1700000000000 },
  34. sandboxes: [],
  35. },
  36. provider: {
  37. all: [
  38. {
  39. id: "opencode",
  40. name: "OpenCode",
  41. models: {
  42. "claude-opus-4-6": {
  43. id: "claude-opus-4-6",
  44. name: "Claude Opus 4.6",
  45. limit: { context: 200_000 },
  46. },
  47. },
  48. },
  49. ],
  50. connected: ["opencode"],
  51. default: { providerID: "opencode", modelID: "claude-opus-4-6" },
  52. },
  53. sessions: [session(sourceID, sourceTitle, 1700000000000), session(otherID, otherTitle, 1700000001000)],
  54. sessionStatus: { [sourceID]: { type: "busy" } },
  55. pageMessages: () => ({ items: [] }),
  56. events: () => events.splice(0, 1),
  57. eventRetry: 16,
  58. sessionStatus: () => sessionStatus,
  59. todos: (sessionID) => todos[sessionID] ?? [],
  60. })
  61. await configurePage(page)
  62. await page.goto(sessionHref(sourceID))
  63. await expectSessionTitle(page, sourceTitle)
  64. const dock = page.locator('[data-component="session-todo-dock"]')
  65. await expect(dock).toHaveCount(0)
  66. sessionStatus[sourceID] = { type: "busy" }
  67. events.push(statusEvent(sourceID, "busy"))
  68. await expect(page.getByRole("button", { name: "Stop" })).toBeVisible()
  69. await page.waitForTimeout(700)
  70. const opening = sampleDock(page, 1_000)
  71. todos[sourceID] = activeTodos
  72. events.push(todoEvent(sourceID, activeTodos))
  73. await expect(dock).toBeVisible()
  74. await expect(dock.locator('[data-state="in_progress"]')).toHaveCount(1)
  75. expect((await opening).some((sample) => sample.opacity > 0.05 && sample.opacity < 0.95)).toBe(true)
  76. await switchSession(page, otherID, otherTitle)
  77. await expect(dock).toHaveCount(0)
  78. const returningOpen = sampleDock(page, 700)
  79. await switchSession(page, sourceID, sourceTitle)
  80. const openSamples = (await returningOpen).filter((sample) => sample.present)
  81. expect(openSamples.length).toBeGreaterThan(0)
  82. expect(openSamples[0]!.opacity).toBeGreaterThan(0.98)
  83. expect(openSamples[0]!.height).toBeGreaterThan(70)
  84. await expect(dock.locator('[data-state="in_progress"]')).toHaveCount(1)
  85. const completedTodos = activeTodos.map((todo) => ({ ...todo, status: "completed" }))
  86. const closing = sampleDock(page, 1_000)
  87. todos[sourceID] = completedTodos
  88. events.push(todoEvent(sourceID, completedTodos))
  89. await expect(dock).toHaveCount(0)
  90. expect((await closing).some((sample) => sample.opacity > 0.05 && sample.opacity < 0.95)).toBe(true)
  91. todos[sourceID] = []
  92. events.push(todoEvent(sourceID, []))
  93. await switchSession(page, otherID, otherTitle)
  94. const returningEmpty = sampleDock(page, 700)
  95. await switchSession(page, sourceID, sourceTitle)
  96. await expect(dock).toHaveCount(0)
  97. expect((await returningEmpty).every((sample) => !sample.present)).toBe(true)
  98. })
  99. function session(id: string, title: string, created: number) {
  100. return {
  101. id,
  102. slug: id,
  103. projectID,
  104. directory,
  105. title,
  106. version: "dev",
  107. time: { created, updated: created },
  108. }
  109. }
  110. function statusEvent(sessionID: string, type: "busy" | "idle"): EventPayload {
  111. return {
  112. directory,
  113. payload: { type: "session.status", properties: { sessionID, status: { type } } },
  114. }
  115. }
  116. function todoEvent(sessionID: string, next: typeof activeTodos): EventPayload {
  117. return {
  118. directory,
  119. payload: { type: "todo.updated", properties: { sessionID, todos: next } },
  120. }
  121. }
  122. async function configurePage(page: Page) {
  123. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  124. await page.addInitScript(
  125. ({ directory, dirBase64, server, sessionIDs }) => {
  126. localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } }))
  127. localStorage.setItem(
  128. "opencode.global.dat:server",
  129. JSON.stringify({
  130. projects: { local: [{ worktree: directory, expanded: true }] },
  131. lastProject: { local: directory },
  132. }),
  133. )
  134. localStorage.setItem(
  135. "opencode.window.browser.dat:tabs",
  136. JSON.stringify(sessionIDs.map((sessionId) => ({ type: "session", server, dirBase64, sessionId }))),
  137. )
  138. },
  139. { directory, dirBase64: base64Encode(directory), server, sessionIDs: [sourceID, otherID] },
  140. )
  141. }
  142. function sessionHref(sessionID: string) {
  143. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  144. return `/server/${base64Encode(server)}/session/${sessionID}`
  145. }
  146. async function switchSession(page: Page, sessionID: string, title: string) {
  147. const href = sessionHref(sessionID)
  148. const tab = page.locator(`[data-slot="titlebar-tabs"] a[href="${href}"]`).first()
  149. await expect(tab).toBeVisible()
  150. await tab.click()
  151. await expectSessionTitle(page, title)
  152. }
  153. function sampleDock(page: Page, duration: number) {
  154. return page.evaluate(async (duration) => {
  155. const samples: { present: boolean; height: number; opacity: number }[] = []
  156. const start = performance.now()
  157. while (performance.now() - start < duration) {
  158. const dock = document.querySelector<HTMLElement>('[data-component="session-todo-dock"]')
  159. const clip = dock?.parentElement?.parentElement
  160. const label = dock?.querySelector<HTMLElement>('[data-action="session-todo-toggle"] span[aria-label]')
  161. samples.push({
  162. present: !!dock,
  163. height: clip?.getBoundingClientRect().height ?? 0,
  164. opacity: label ? Number.parseFloat(getComputedStyle(label).opacity) : 0,
  165. })
  166. await new Promise(requestAnimationFrame)
  167. }
  168. return samples
  169. }, duration)
  170. }