|
| 1 | +import type { Locator, Page } from "@playwright/test" |
| 2 | +import { test, expect } from "../fixtures" |
| 3 | +import { promptAgentSelector, promptModelSelector, promptSelector } from "../selectors" |
| 4 | + |
| 5 | +type Probe = { |
| 6 | + agent?: string |
| 7 | + model?: { providerID: string; modelID: string; name?: string } |
| 8 | + models?: Array<{ providerID: string; modelID: string; name: string }> |
| 9 | + agents?: Array<{ name: string }> |
| 10 | +} |
| 11 | + |
| 12 | +async function probe(page: Page): Promise<Probe | null> { |
| 13 | + return page.evaluate(() => { |
| 14 | + const win = window as Window & { |
| 15 | + __opencode_e2e?: { |
| 16 | + model?: { |
| 17 | + current?: Probe |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + return win.__opencode_e2e?.model?.current ?? null |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +async function state(page: Page) { |
| 26 | + const value = await probe(page) |
| 27 | + if (!value) throw new Error("Failed to resolve model selection probe") |
| 28 | + return value |
| 29 | +} |
| 30 | + |
| 31 | +async function ready(page: Page) { |
| 32 | + const prompt = page.locator(promptSelector) |
| 33 | + await prompt.click() |
| 34 | + await expect(prompt).toBeFocused() |
| 35 | + await prompt.pressSequentially("focus") |
| 36 | + return prompt |
| 37 | +} |
| 38 | + |
| 39 | +async function body(prompt: Locator) { |
| 40 | + return prompt.evaluate((el) => (el as HTMLElement).innerText) |
| 41 | +} |
| 42 | + |
| 43 | +test("agent select returns focus to the prompt", async ({ page, gotoSession }) => { |
| 44 | + await gotoSession() |
| 45 | + |
| 46 | + const prompt = await ready(page) |
| 47 | + |
| 48 | + const info = await state(page) |
| 49 | + const next = info.agents?.map((item) => item.name).find((name) => name !== info.agent) |
| 50 | + test.skip(!next, "only one agent available") |
| 51 | + if (!next) return |
| 52 | + |
| 53 | + await page.locator(`${promptAgentSelector} [data-slot="select-select-trigger"]`).first().click() |
| 54 | + |
| 55 | + const item = page.locator('[data-slot="select-select-item"]').filter({ hasText: next }).first() |
| 56 | + await expect(item).toBeVisible() |
| 57 | + await item.click({ force: true }) |
| 58 | + |
| 59 | + await expect(page.locator(`${promptAgentSelector} [data-slot="select-select-trigger-value"]`).first()).toHaveText( |
| 60 | + next, |
| 61 | + ) |
| 62 | + await expect(prompt).toBeFocused() |
| 63 | + await prompt.pressSequentially(" agent") |
| 64 | + await expect.poll(() => body(prompt)).toContain("focus agent") |
| 65 | +}) |
| 66 | + |
| 67 | +test("model select returns focus to the prompt", async ({ page, gotoSession }) => { |
| 68 | + await gotoSession() |
| 69 | + |
| 70 | + const prompt = await ready(page) |
| 71 | + |
| 72 | + const info = await state(page) |
| 73 | + const key = info.model ? `${info.model.providerID}:${info.model.modelID}` : null |
| 74 | + const next = info.models?.find((item) => `${item.providerID}:${item.modelID}` !== key) |
| 75 | + test.skip(!next, "only one model available") |
| 76 | + if (!next) return |
| 77 | + |
| 78 | + await page.locator(`${promptModelSelector} [data-action="prompt-model"]`).first().click() |
| 79 | + |
| 80 | + const item = page.locator(`[data-slot="list-item"][data-key="${next.providerID}:${next.modelID}"]`).first() |
| 81 | + await expect(item).toBeVisible() |
| 82 | + await item.click({ force: true }) |
| 83 | + |
| 84 | + await expect(page.locator(`${promptModelSelector} [data-action="prompt-model"] span`).first()).toHaveText(next.name) |
| 85 | + await expect(prompt).toBeFocused() |
| 86 | + await prompt.pressSequentially(" model") |
| 87 | + await expect.poll(() => body(prompt)).toContain("focus model") |
| 88 | +}) |
0 commit comments