session-model-persistence.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { base64Decode } from "@opencode-ai/util/encode"
  2. import type { Locator, Page } from "@playwright/test"
  3. import { test, expect } from "../fixtures"
  4. import { openSidebar, sessionIDFromUrl, setWorkspacesEnabled, waitSessionIdle, waitSlug } from "../actions"
  5. import {
  6. promptAgentSelector,
  7. promptModelSelector,
  8. promptSelector,
  9. promptVariantSelector,
  10. workspaceItemSelector,
  11. workspaceNewSessionSelector,
  12. } from "../selectors"
  13. import { createSdk, sessionPath } from "../utils"
  14. type Footer = {
  15. agent: string
  16. model: string
  17. variant: string
  18. }
  19. type Probe = {
  20. dir?: string
  21. sessionID?: string
  22. model?: { providerID: string; modelID: string }
  23. }
  24. const escape = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
  25. const text = async (locator: Locator) => ((await locator.textContent()) ?? "").trim()
  26. const modelKey = (state: Probe | null) => (state?.model ? `${state.model.providerID}:${state.model.modelID}` : null)
  27. const dirKey = (state: Probe | null) => state?.dir ?? ""
  28. async function probe(page: Page): Promise<Probe | null> {
  29. return page.evaluate(() => {
  30. const win = window as Window & {
  31. __opencode_e2e?: {
  32. model?: {
  33. current?: Probe
  34. }
  35. }
  36. }
  37. return win.__opencode_e2e?.model?.current ?? null
  38. })
  39. }
  40. async function currentDir(page: Page) {
  41. let hit = ""
  42. await expect
  43. .poll(
  44. async () => {
  45. const next = dirKey(await probe(page))
  46. if (next) hit = next
  47. return next
  48. },
  49. { timeout: 30_000 },
  50. )
  51. .not.toBe("")
  52. return hit
  53. }
  54. async function read(page: Page): Promise<Footer> {
  55. return {
  56. agent: await text(page.locator(`${promptAgentSelector} [data-slot="select-select-trigger-value"]`).first()),
  57. model: await text(page.locator(`${promptModelSelector} [data-action="prompt-model"] span`).first()),
  58. variant: await text(page.locator(`${promptVariantSelector} [data-slot="select-select-trigger-value"]`).first()),
  59. }
  60. }
  61. async function waitFooter(page: Page, expected: Partial<Footer>) {
  62. let hit: Footer | null = null
  63. await expect
  64. .poll(
  65. async () => {
  66. const state = await read(page)
  67. const ok = Object.entries(expected).every(([key, value]) => state[key as keyof Footer] === value)
  68. if (ok) hit = state
  69. return ok
  70. },
  71. { timeout: 30_000 },
  72. )
  73. .toBe(true)
  74. if (!hit) throw new Error("Failed to resolve prompt footer state")
  75. return hit
  76. }
  77. async function waitModel(page: Page, value: string) {
  78. await expect.poll(() => probe(page).then(modelKey), { timeout: 30_000 }).toBe(value)
  79. }
  80. async function choose(page: Page, root: string, value: string) {
  81. const select = page.locator(root)
  82. await expect(select).toBeVisible()
  83. await select.locator('[data-action], [data-slot="select-select-trigger"]').first().click()
  84. const item = page
  85. .locator('[data-slot="select-select-item"]')
  86. .filter({ hasText: new RegExp(`^\\s*${escape(value)}\\s*$`) })
  87. .first()
  88. await expect(item).toBeVisible()
  89. await item.click()
  90. }
  91. async function variantCount(page: Page) {
  92. const select = page.locator(promptVariantSelector)
  93. await expect(select).toBeVisible()
  94. await select.locator('[data-slot="select-select-trigger"]').click()
  95. const count = await page.locator('[data-slot="select-select-item"]').count()
  96. await page.keyboard.press("Escape")
  97. return count
  98. }
  99. async function agents(page: Page) {
  100. const select = page.locator(promptAgentSelector)
  101. await expect(select).toBeVisible()
  102. await select.locator('[data-action], [data-slot="select-select-trigger"]').first().click()
  103. const labels = await page.locator('[data-slot="select-select-item-label"]').allTextContents()
  104. await page.keyboard.press("Escape")
  105. return labels.map((item) => item.trim()).filter(Boolean)
  106. }
  107. async function ensureVariant(page: Page, directory: string): Promise<Footer> {
  108. const current = await read(page)
  109. if ((await variantCount(page)) >= 2) return current
  110. const cfg = await createSdk(directory)
  111. .config.get()
  112. .then((x) => x.data)
  113. const visible = new Set(await agents(page))
  114. const entry = Object.entries(cfg?.agent ?? {}).find((item) => {
  115. const value = item[1]
  116. return !!value && typeof value === "object" && "variant" in value && "model" in value && visible.has(item[0])
  117. })
  118. const name = entry?.[0]
  119. test.skip(!name, "no agent with alternate variants available")
  120. if (!name) return current
  121. await choose(page, promptAgentSelector, name)
  122. await expect.poll(() => variantCount(page), { timeout: 30_000 }).toBeGreaterThanOrEqual(2)
  123. return waitFooter(page, { agent: name })
  124. }
  125. async function chooseDifferentVariant(page: Page): Promise<Footer> {
  126. const current = await read(page)
  127. const select = page.locator(promptVariantSelector)
  128. await expect(select).toBeVisible()
  129. await select.locator('[data-slot="select-select-trigger"]').click()
  130. const items = page.locator('[data-slot="select-select-item"]')
  131. const count = await items.count()
  132. if (count < 2) throw new Error("Current model has no alternate variant to select")
  133. for (let i = 0; i < count; i++) {
  134. const item = items.nth(i)
  135. const next = await text(item.locator('[data-slot="select-select-item-label"]').first())
  136. if (!next || next === current.variant) continue
  137. await item.click()
  138. return waitFooter(page, { agent: current.agent, model: current.model, variant: next })
  139. }
  140. throw new Error("Failed to choose a different variant")
  141. }
  142. async function chooseOtherModel(page: Page): Promise<Footer> {
  143. const current = await read(page)
  144. const button = page.locator(`${promptModelSelector} [data-action="prompt-model"]`)
  145. await expect(button).toBeVisible()
  146. await button.click()
  147. const dialog = page.getByRole("dialog")
  148. await expect(dialog).toBeVisible()
  149. const items = dialog.locator('[data-slot="list-item"]')
  150. const count = await items.count()
  151. expect(count).toBeGreaterThan(1)
  152. for (let i = 0; i < count; i++) {
  153. const item = items.nth(i)
  154. const selected = (await item.getAttribute("data-selected")) === "true"
  155. if (selected) continue
  156. await item.click()
  157. await expect(dialog).toHaveCount(0)
  158. await expect.poll(async () => (await read(page)).model !== current.model, { timeout: 30_000 }).toBe(true)
  159. return read(page)
  160. }
  161. throw new Error("Failed to choose a different model")
  162. }
  163. async function goto(page: Page, directory: string, sessionID?: string) {
  164. await page.goto(sessionPath(directory, sessionID))
  165. await expect(page.locator(promptSelector)).toBeVisible()
  166. await expect.poll(async () => dirKey(await probe(page)), { timeout: 30_000 }).toBe(directory)
  167. }
  168. async function submit(page: Page, value: string) {
  169. const prompt = page.locator(promptSelector)
  170. await expect(prompt).toBeVisible()
  171. await prompt.click()
  172. await prompt.fill(value)
  173. await prompt.press("Enter")
  174. await expect.poll(() => sessionIDFromUrl(page.url()) ?? "", { timeout: 30_000 }).not.toBe("")
  175. const id = sessionIDFromUrl(page.url())
  176. if (!id) throw new Error(`Failed to resolve session id from ${page.url()}`)
  177. return id
  178. }
  179. async function waitUser(directory: string, sessionID: string) {
  180. const sdk = createSdk(directory)
  181. await expect
  182. .poll(
  183. async () => {
  184. const items = await sdk.session.messages({ sessionID, limit: 20 }).then((x) => x.data ?? [])
  185. return items.some((item) => item.info.role === "user")
  186. },
  187. { timeout: 30_000 },
  188. )
  189. .toBe(true)
  190. await sdk.session.abort({ sessionID }).catch(() => undefined)
  191. await waitSessionIdle(sdk, sessionID, 30_000).catch(() => undefined)
  192. }
  193. async function createWorkspace(page: Page, root: string, seen: string[]) {
  194. await openSidebar(page)
  195. await page.getByRole("button", { name: "New workspace" }).first().click()
  196. const slug = await waitSlug(page, [root, ...seen])
  197. const directory = base64Decode(slug)
  198. if (!directory) throw new Error(`Failed to decode workspace slug: ${slug}`)
  199. return { slug, directory }
  200. }
  201. async function waitWorkspace(page: Page, slug: string) {
  202. await openSidebar(page)
  203. await expect
  204. .poll(
  205. async () => {
  206. const item = page.locator(workspaceItemSelector(slug)).first()
  207. try {
  208. await item.hover({ timeout: 500 })
  209. return true
  210. } catch {
  211. return false
  212. }
  213. },
  214. { timeout: 60_000 },
  215. )
  216. .toBe(true)
  217. }
  218. async function newWorkspaceSession(page: Page, slug: string) {
  219. await waitWorkspace(page, slug)
  220. const item = page.locator(workspaceItemSelector(slug)).first()
  221. await item.hover()
  222. const button = page.locator(workspaceNewSessionSelector(slug)).first()
  223. await expect(button).toBeVisible()
  224. await button.click({ force: true })
  225. const next = await waitSlug(page)
  226. await expect(page).toHaveURL(new RegExp(`/${next}/session(?:[/?#]|$)`))
  227. await expect(page.locator(promptSelector)).toBeVisible()
  228. return currentDir(page)
  229. }
  230. test("session model and variant restore per session without leaking into new sessions", async ({
  231. page,
  232. withProject,
  233. }) => {
  234. await page.setViewportSize({ width: 1440, height: 900 })
  235. await withProject(async ({ directory, gotoSession, trackSession }) => {
  236. await gotoSession()
  237. await ensureVariant(page, directory)
  238. const firstState = await chooseDifferentVariant(page)
  239. const first = await submit(page, `session variant ${Date.now()}`)
  240. trackSession(first)
  241. await waitUser(directory, first)
  242. await page.reload()
  243. await expect(page.locator(promptSelector)).toBeVisible()
  244. await waitFooter(page, firstState)
  245. await gotoSession()
  246. const fresh = await ensureVariant(page, directory)
  247. expect(fresh.variant).not.toBe(firstState.variant)
  248. const secondState = await chooseOtherModel(page)
  249. const second = await submit(page, `session model ${Date.now()}`)
  250. trackSession(second)
  251. await waitUser(directory, second)
  252. await goto(page, directory, first)
  253. await waitFooter(page, firstState)
  254. await goto(page, directory, second)
  255. await waitFooter(page, secondState)
  256. await gotoSession()
  257. await waitFooter(page, fresh)
  258. })
  259. })
  260. test("session model restore across workspaces", async ({ page, withProject }) => {
  261. await page.setViewportSize({ width: 1440, height: 900 })
  262. await withProject(async ({ directory: root, slug, gotoSession, trackDirectory, trackSession }) => {
  263. await gotoSession()
  264. await ensureVariant(page, root)
  265. const firstState = await chooseDifferentVariant(page)
  266. const first = await submit(page, `root session ${Date.now()}`)
  267. trackSession(first, root)
  268. await waitUser(root, first)
  269. await openSidebar(page)
  270. await setWorkspacesEnabled(page, slug, true)
  271. const one = await createWorkspace(page, slug, [])
  272. const oneDir = await newWorkspaceSession(page, one.slug)
  273. trackDirectory(oneDir)
  274. const secondState = await chooseOtherModel(page)
  275. const second = await submit(page, `workspace one ${Date.now()}`)
  276. trackSession(second, oneDir)
  277. await waitUser(oneDir, second)
  278. const two = await createWorkspace(page, slug, [one.slug])
  279. const twoDir = await newWorkspaceSession(page, two.slug)
  280. trackDirectory(twoDir)
  281. await ensureVariant(page, twoDir)
  282. const thirdState = await chooseDifferentVariant(page)
  283. const third = await submit(page, `workspace two ${Date.now()}`)
  284. trackSession(third, twoDir)
  285. await waitUser(twoDir, third)
  286. await goto(page, root, first)
  287. await waitFooter(page, firstState)
  288. await goto(page, oneDir, second)
  289. await waitFooter(page, secondState)
  290. await goto(page, twoDir, third)
  291. await waitFooter(page, thirdState)
  292. await goto(page, root, first)
  293. await waitFooter(page, firstState)
  294. })
  295. })
  296. test("variant preserved when switching agent modes", async ({ page, withProject }) => {
  297. await page.setViewportSize({ width: 1440, height: 900 })
  298. await withProject(async ({ directory, gotoSession }) => {
  299. await gotoSession()
  300. await ensureVariant(page, directory)
  301. const updated = await chooseDifferentVariant(page)
  302. const available = await agents(page)
  303. const other = available.find((name) => name !== updated.agent)
  304. test.skip(!other, "only one agent available")
  305. if (!other) return
  306. await choose(page, promptAgentSelector, other)
  307. await waitFooter(page, { agent: other, variant: updated.variant })
  308. await choose(page, promptAgentSelector, updated.agent)
  309. await waitFooter(page, { agent: updated.agent, variant: updated.variant })
  310. })
  311. })