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

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