session-model-persistence.spec.ts 12 KB

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