import assert from "node:assert/strict"; import { mkdirSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; import { translateFor } from "@openbot/i18n"; import { chromium, type Page } from "playwright-core"; const OUT = resolve(import.meta.dirname, "../.openbot-build/task-plan-e2e"); const t = translateFor("en"); const storybook = process.argv.find((arg) => arg.startsWith("--storybook="))?.slice("--storybook=".length); assert.ok(storybook, "Pass --storybook= with the URL that `bun run storybook` reports."); interface Report { waitingForReplies?: { planSteps: number; waitingRows: number; agentReplyInQueue: boolean; waitingAboveQueue: boolean; }; planInProgress?: { expanded: boolean; done: number; total: number }; awaitingStates?: string[]; stacks?: Record; passed: boolean; } const report: Report = { passed: false }; async function openStory(page: Page, id: string) { await page.goto(`${storybook}/iframe.html?id=${id}&viewMode=story`); await page.locator("#storybook-root").waitFor(); } interface StackSeam { pair: string; overlap: number; lowerPaintsOver: boolean; upperTopVisible: boolean; } interface CardSeam { pair: string; overlap: number; } interface ComposerStack { seams: StackSeam[]; cards: CardSeam[]; cardsFillStack: boolean; } async function composerStack(page: Page): Promise { await page.waitForFunction(() => document .getAnimations() .every((animation) => animation.playState !== "running" || animation.effect?.getTiming().iterations === Infinity), ); return page.evaluate(() => { const wrap = document.querySelector(".composer-wrap"); if (!wrap) return { seams: [], cards: [], cardsFillStack: false }; const visible = (node: Element) => node.getBoundingClientRect().height > 0; const label = (node: Element) => [...node.classList].find((name) => !name.startsWith("task-list")) ?? node.tagName; const panels = [...wrap.children].filter(visible); const stack = wrap.querySelector(":scope > .composer-stack"); const cards = stack ? [...stack.querySelectorAll(":scope > *, :scope > .agent-queue-slot > .agent-queue-slot-inner > *")].filter( (node) => !node.classList.contains("agent-queue-slot") && visible(node), ) : []; const frame = stack?.getBoundingClientRect(); const border = stack ? Number.parseFloat(getComputedStyle(stack).borderLeftWidth) : 0; const cardsFillStack = frame !== undefined && cards.every((card) => { const box = card.getBoundingClientRect(); return ( Math.round(box.left) === Math.round(frame.left + border) && Math.round(box.right) === Math.round(frame.right - border) ); }); const cardSeams = cards.slice(1).flatMap((lower, index) => { const upper = cards[index]; if (!upper) return []; return [ { pair: `${label(upper)} > ${label(lower)}`, overlap: Math.round(upper.getBoundingClientRect().bottom - lower.getBoundingClientRect().top), }, ]; }); const seams = panels.slice(1).flatMap((lower, index) => { const upper = panels[index]; if (!upper) return []; const top = upper.getBoundingClientRect(); const bottom = lower.getBoundingClientRect(); const overlap = Math.round(top.bottom - bottom.top); const seam = document.elementFromPoint(bottom.left + 60, (top.bottom + bottom.top) / 2); const head = document.elementFromPoint(top.left + 40, top.top + 8); return [ { pair: `${label(upper)} > ${label(lower)}`, overlap, lowerPaintsOver: overlap <= 0 || Boolean(seam && lower.contains(seam)), upperTopVisible: Boolean(head && upper.contains(head)), }, ]; }); return { seams, cards: cardSeams, cardsFillStack }; }); } function header(page: Page, title: string) { return page.getByRole("button", { name: new RegExp(`^${title}`) }); } mkdirSync(OUT, { recursive: true }); const browser = await chromium.launch({ channel: "chrome" }); try { const page = await browser.newPage({ viewport: { width: 1200, height: 860 } }); await openStory(page, "conversation-conversation--waiting-for-replies"); const plan = header(page, t("chat.taskList.title")); await plan.waitFor(); assert.equal(await plan.count(), 1, "One plan turn gives one task list."); assert.match((await plan.textContent()) ?? "", /4 of 4 tasks done/u); assert.equal(await plan.getAttribute("aria-expanded"), "false", "A plan from an earlier turn starts closed."); await plan.click(); assert.equal(await plan.getAttribute("aria-expanded"), "true"); const planPanel = page.locator(`[id="${await plan.getAttribute("aria-controls")}"]`); assert.equal(await planPanel.getByRole("img", { name: t("chat.taskList.state.done") }).count(), 4); const waiting = header(page, t("chat.awaiting.title")); await waiting.waitFor(); assert.match((await waiting.textContent()) ?? "", /1 of 2 replies in/u); const waitingBlock = page.locator("section").filter({ has: waiting }); const research = waitingBlock.getByRole("listitem").filter({ hasText: "Research" }); assert.match((await research.textContent()) ?? "", new RegExp(t("chat.awaiting.state.replied"))); assert.match((await research.textContent()) ?? "", /All four sources check out/u); assert.doesNotMatch( (await research.textContent()) ?? "", new RegExp(t("chat.awaiting.readsNext", { name: "Chief" })), ); const sales = waitingBlock.getByRole("listitem").filter({ hasText: "Sales Outbound" }); assert.match((await sales.textContent()) ?? "", new RegExp(t("chat.awaiting.state.working"))); const queue = page.getByRole("region", { name: t("queue.label") }); await queue.waitFor(); assert.equal(await queue.getByText("All four sources check out", { exact: false }).count(), 0); const waitingAboveQueue = await waitingBlock.evaluate( (block, queueNode) => Boolean(queueNode && block.compareDocumentPosition(queueNode) & Node.DOCUMENT_POSITION_FOLLOWING), await queue.elementHandle(), ); assert.equal(waitingAboveQueue, true, "The waiting block is above the queue."); await page.screenshot({ path: resolve(OUT, "conversation-waiting.png") }); report.waitingForReplies = { planSteps: 4, waitingRows: 2, agentReplyInQueue: false, waitingAboveQueue }; await openStory(page, "conversation-conversation--plan-in-progress"); const live = header(page, t("chat.taskList.title")); await live.waitFor(); assert.equal(await live.getAttribute("aria-expanded"), "true", "The plan of the running turn is open."); assert.match((await live.textContent()) ?? "", /1 of 3 tasks done/u); const livePanel = page.locator(`[id="${await live.getAttribute("aria-controls")}"]`); const active = livePanel .getByRole("listitem") .filter({ has: page.getByRole("img", { name: t("chat.taskList.state.active") }) }); assert.match((await active.textContent()) ?? "", /Adding the column for the saved provider/u); await page.screenshot({ path: resolve(OUT, "conversation-plan-live.png") }); report.planInProgress = { expanded: true, done: 1, total: 3 }; await openStory(page, "conversation-awaiting-replies--waiting"); const rows = page.getByRole("listitem"); assert.equal(await rows.count(), 3); for (const [name, state] of [ ["Research", "chat.awaiting.state.replied"], ["Sales Outbound", "chat.awaiting.state.working"], ["Support", "chat.awaiting.state.asked"], ] as const) { assert.match((await rows.filter({ hasText: name }).textContent()) ?? "", new RegExp(t(state))); } const opened = header(page, t("chat.awaiting.title")); const block = page.locator(`[aria-controls="${await opened.getAttribute("aria-controls")}"]`); await block.click(); assert.equal(await block.getAttribute("aria-expanded"), "false"); await page.getByText(t("chat.awaiting.working", { name: "Sales Outbound" })).waitFor(); await openStory(page, "conversation-awaiting-replies--one-failed"); assert.match( (await page.getByRole("listitem").first().textContent()) ?? "", new RegExp(t("chat.awaiting.state.failed")), ); report.awaitingStates = ["replied", "working", "asked", "failed"]; report.stacks = {}; for (const id of [ "conversation-conversation--waiting-for-replies", "conversation-conversation--waiting-for-replies-with-usage-limit", "conversation-conversation--plan-in-progress", "conversation-conversation--provider-sign-in-required", "conversation-channel--waiting-subtasks-above-stopped-task", ]) { await openStory(page, id); await page.locator(".composer-wrap .awaiting-replies").waitFor(); const stack = await composerStack(page); const { seams } = stack; assert.ok(seams.length > 0, `${id}: no panel above the input.`); for (const seam of seams) { assert.ok(seam.overlap > 0, `${id}: ${seam.pair} does not go under the next panel.`); assert.ok(seam.lowerPaintsOver, `${id}: ${seam.pair} covers the next panel.`); assert.ok(seam.upperTopVisible, `${id}: the top of ${seam.pair} is hidden.`); } for (const card of stack.cards) { assert.equal(card.overlap, 1, `${id}: ${card.pair} does not sit flush on the card above.`); } assert.ok(stack.cardsFillStack, `${id}: a card in the stack does not fill its width.`); await page.locator(".composer-wrap").screenshot({ path: resolve(OUT, `stack-${id.split("--")[1]}.png`) }); report.stacks[id] = stack; } report.passed = true; writeFileSync(resolve(OUT, "report.json"), `${JSON.stringify(report, null, 2)}\n`); process.stdout.write(`${JSON.stringify(report)}\n`); } finally { await browser.close(); }