projects-switch.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { base64Decode } from "@opencode-ai/util/encode"
  2. import type { Page } from "@playwright/test"
  3. import { test, expect } from "../fixtures"
  4. import { defocus, createTestProject, cleanupTestProject, openSidebar, sessionIDFromUrl, waitSlug } from "../actions"
  5. import { projectSwitchSelector, promptSelector, workspaceItemSelector, workspaceNewSessionSelector } from "../selectors"
  6. import { dirSlug, resolveDirectory } from "../utils"
  7. async function workspaces(page: Page, directory: string, enabled: boolean) {
  8. await page.evaluate(
  9. ({ directory, enabled }: { directory: string; enabled: boolean }) => {
  10. const key = "opencode.global.dat:layout"
  11. const raw = localStorage.getItem(key)
  12. const data = raw ? JSON.parse(raw) : {}
  13. const sidebar = data.sidebar && typeof data.sidebar === "object" ? data.sidebar : {}
  14. const current =
  15. sidebar.workspaces && typeof sidebar.workspaces === "object" && !Array.isArray(sidebar.workspaces)
  16. ? sidebar.workspaces
  17. : {}
  18. const next = { ...current }
  19. if (enabled) next[directory] = true
  20. if (!enabled) delete next[directory]
  21. localStorage.setItem(
  22. key,
  23. JSON.stringify({
  24. ...data,
  25. sidebar: {
  26. ...sidebar,
  27. workspaces: next,
  28. },
  29. }),
  30. )
  31. },
  32. { directory, enabled },
  33. )
  34. }
  35. test("can switch between projects from sidebar", async ({ page, withProject }) => {
  36. await page.setViewportSize({ width: 1400, height: 800 })
  37. const other = await createTestProject()
  38. const otherSlug = dirSlug(other)
  39. try {
  40. await withProject(
  41. async ({ directory }) => {
  42. await defocus(page)
  43. const currentSlug = dirSlug(directory)
  44. const otherButton = page.locator(projectSwitchSelector(otherSlug)).first()
  45. await expect(otherButton).toBeVisible()
  46. await otherButton.click()
  47. await expect(page).toHaveURL(new RegExp(`/${otherSlug}/session`))
  48. const currentButton = page.locator(projectSwitchSelector(currentSlug)).first()
  49. await expect(currentButton).toBeVisible()
  50. await currentButton.click()
  51. await expect(page).toHaveURL(new RegExp(`/${currentSlug}/session`))
  52. },
  53. { extra: [other] },
  54. )
  55. } finally {
  56. await cleanupTestProject(other)
  57. }
  58. })
  59. test("switching back to a project opens the latest workspace session", async ({ page, withProject }) => {
  60. await page.setViewportSize({ width: 1400, height: 800 })
  61. const other = await createTestProject()
  62. const otherSlug = dirSlug(other)
  63. try {
  64. await withProject(
  65. async ({ directory, slug, trackSession, trackDirectory }) => {
  66. await defocus(page)
  67. await workspaces(page, directory, true)
  68. await page.reload()
  69. await expect(page.locator(promptSelector)).toBeVisible()
  70. await openSidebar(page)
  71. await expect(page.getByRole("button", { name: "New workspace" }).first()).toBeVisible()
  72. await page.getByRole("button", { name: "New workspace" }).first().click()
  73. const raw = await waitSlug(page, [slug])
  74. const dir = base64Decode(raw)
  75. if (!dir) throw new Error(`Failed to decode workspace slug: ${raw}`)
  76. const space = await resolveDirectory(dir)
  77. const next = dirSlug(space)
  78. trackDirectory(space)
  79. await openSidebar(page)
  80. const item = page.locator(`${workspaceItemSelector(next)}, ${workspaceItemSelector(raw)}`).first()
  81. await expect(item).toBeVisible()
  82. await item.hover()
  83. const btn = page.locator(`${workspaceNewSessionSelector(next)}, ${workspaceNewSessionSelector(raw)}`).first()
  84. await expect(btn).toBeVisible()
  85. await btn.click({ force: true })
  86. // A new workspace can be discovered via a transient slug before the route and sidebar
  87. // settle to the canonical workspace path on Windows, so interact with either and assert
  88. // against the resolved workspace slug.
  89. await waitSlug(page)
  90. await expect(page).toHaveURL(new RegExp(`/${next}/session(?:[/?#]|$)`))
  91. // Create a session by sending a prompt
  92. const prompt = page.locator(promptSelector)
  93. await expect(prompt).toBeVisible()
  94. await prompt.fill("test")
  95. await page.keyboard.press("Enter")
  96. // Wait for the URL to update with the new session ID
  97. await expect.poll(() => sessionIDFromUrl(page.url()) ?? "", { timeout: 15_000 }).not.toBe("")
  98. const created = sessionIDFromUrl(page.url())
  99. if (!created) throw new Error(`Failed to get session ID from url: ${page.url()}`)
  100. trackSession(created, space)
  101. await expect(page).toHaveURL(new RegExp(`/${next}/session/${created}(?:[/?#]|$)`))
  102. await openSidebar(page)
  103. const otherButton = page.locator(projectSwitchSelector(otherSlug)).first()
  104. await expect(otherButton).toBeVisible()
  105. await otherButton.click()
  106. await expect(page).toHaveURL(new RegExp(`/${otherSlug}/session`))
  107. const rootButton = page.locator(projectSwitchSelector(slug)).first()
  108. await expect(rootButton).toBeVisible()
  109. await rootButton.click()
  110. await expect.poll(() => sessionIDFromUrl(page.url()) ?? "").toBe(created)
  111. await expect(page).toHaveURL(new RegExp(`/session/${created}(?:[/?#]|$)`))
  112. },
  113. { extra: [other] },
  114. )
  115. } finally {
  116. await cleanupTestProject(other)
  117. }
  118. })