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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 opening 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. await switchSession(page, sourceID, sourceTitle)
  79. await expect(dock).toHaveCount(0)
  80. })
  81. function session(id: string, title: string, created: number) {
  82. return {
  83. id,
  84. slug: id,
  85. projectID,
  86. directory,
  87. title,
  88. version: "dev",
  89. time: { created, updated: created },
  90. }
  91. }
  92. function statusEvent(sessionID: string, type: "busy" | "idle"): EventPayload {
  93. return {
  94. directory,
  95. payload: { type: "session.status", properties: { sessionID, status: { type } } },
  96. }
  97. }
  98. function todoEvent(sessionID: string, next: typeof activeTodos): EventPayload {
  99. return {
  100. directory,
  101. payload: { type: "todo.updated", properties: { sessionID, todos: next } },
  102. }
  103. }
  104. async function configurePage(page: Page) {
  105. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  106. await page.addInitScript(
  107. ({ directory, dirBase64, server, sessionIDs }) => {
  108. localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } }))
  109. localStorage.setItem(
  110. "opencode.global.dat:server",
  111. JSON.stringify({
  112. projects: { local: [{ worktree: directory, expanded: true }] },
  113. lastProject: { local: directory },
  114. }),
  115. )
  116. localStorage.setItem(
  117. "opencode.window.browser.dat:tabs",
  118. JSON.stringify(sessionIDs.map((sessionId) => ({ type: "session", server, dirBase64, sessionId }))),
  119. )
  120. },
  121. { directory, dirBase64: base64Encode(directory), server, sessionIDs: [sourceID, otherID] },
  122. )
  123. }
  124. function sessionHref(sessionID: string) {
  125. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  126. return `/server/${base64Encode(server)}/session/${sessionID}`
  127. }
  128. async function switchSession(page: Page, sessionID: string, title: string) {
  129. const href = sessionHref(sessionID)
  130. const tab = page.locator(`[data-slot="titlebar-tabs"] a[href="${href}"]`).first()
  131. await expect(tab).toBeVisible()
  132. await tab.click()
  133. await expectSessionTitle(page, title)
  134. }
  135. function sampleDock(page: Page, duration: number) {
  136. return page.evaluate(async (duration) => {
  137. const samples: { present: boolean; height: number; opacity: number }[] = []
  138. const start = performance.now()
  139. while (performance.now() - start < duration) {
  140. const dock = document.querySelector<HTMLElement>('[data-component="session-todo-dock"]')
  141. const clip = dock?.parentElement?.parentElement
  142. const label = dock?.querySelector<HTMLElement>('[data-action="session-todo-toggle"] span[aria-label]')
  143. samples.push({
  144. present: !!dock,
  145. height: clip?.getBoundingClientRect().height ?? 0,
  146. opacity: label ? Number.parseFloat(getComputedStyle(label).opacity) : 0,
  147. })
  148. await new Promise(requestAnimationFrame)
  149. }
  150. return samples
  151. }, duration)
  152. }