|
| 1 | +import { test, expect } from "./fixtures" |
| 2 | +import { promptSelector } from "./utils" |
| 3 | + |
| 4 | +function sessionIDFromUrl(url: string) { |
| 5 | + const match = /\/session\/([^/?#]+)/.exec(url) |
| 6 | + return match?.[1] |
| 7 | +} |
| 8 | + |
| 9 | +test("can send a prompt and receive a reply", async ({ page, sdk, gotoSession }) => { |
| 10 | + test.setTimeout(120_000) |
| 11 | + |
| 12 | + const pageErrors: string[] = [] |
| 13 | + const onPageError = (err: Error) => { |
| 14 | + pageErrors.push(err.message) |
| 15 | + } |
| 16 | + page.on("pageerror", onPageError) |
| 17 | + |
| 18 | + await gotoSession() |
| 19 | + |
| 20 | + const token = `E2E_OK_${Date.now()}` |
| 21 | + |
| 22 | + const prompt = page.locator(promptSelector) |
| 23 | + await prompt.click() |
| 24 | + await page.keyboard.type(`Reply with exactly: ${token}`) |
| 25 | + await page.keyboard.press("Enter") |
| 26 | + |
| 27 | + await expect(page).toHaveURL(/\/session\/[^/?#]+/, { timeout: 30_000 }) |
| 28 | + |
| 29 | + const sessionID = (() => { |
| 30 | + const id = sessionIDFromUrl(page.url()) |
| 31 | + if (!id) throw new Error(`Failed to parse session id from url: ${page.url()}`) |
| 32 | + return id |
| 33 | + })() |
| 34 | + |
| 35 | + try { |
| 36 | + await expect |
| 37 | + .poll( |
| 38 | + async () => { |
| 39 | + const messages = await sdk.session.messages({ sessionID, limit: 50 }).then((r) => r.data ?? []) |
| 40 | + const assistant = messages |
| 41 | + .slice() |
| 42 | + .reverse() |
| 43 | + .find((m) => m.info.role === "assistant") |
| 44 | + |
| 45 | + return ( |
| 46 | + assistant?.parts |
| 47 | + .filter((p) => p.type === "text") |
| 48 | + .map((p) => p.text) |
| 49 | + .join("\n") ?? "" |
| 50 | + ) |
| 51 | + }, |
| 52 | + { timeout: 90_000 }, |
| 53 | + ) |
| 54 | + |
| 55 | + .toContain(token) |
| 56 | + |
| 57 | + const reply = page.locator('[data-component="text-part"]').filter({ hasText: token }).first() |
| 58 | + await expect(reply).toBeVisible({ timeout: 90_000 }) |
| 59 | + } finally { |
| 60 | + page.off("pageerror", onPageError) |
| 61 | + await sdk.session.delete({ sessionID }).catch(() => undefined) |
| 62 | + } |
| 63 | + |
| 64 | + if (pageErrors.length > 0) { |
| 65 | + throw new Error(`Page error(s):\n${pageErrors.join("\n")}`) |
| 66 | + } |
| 67 | +}) |
0 commit comments