import { type Page } from "@playwright/test"; import path from "node:path"; import { test, expect } from "../../fixtures/test-base"; import type { SeedData } from "../../fixtures/test-base"; import type { ApiClient } from "../../helpers/api-client"; import type { BackendContext } from "../../fixtures/backend"; import { assertNoDocumentHorizontalOverflow } from "../../helpers/layout-assertions"; import { GitHelper, makeGitEnv, createStandardProfile } from "../../helpers/git-helper"; import { routeMainWebSocketWithPreviewFeedbackCreateFailure } from "../../helpers/ws-drop"; import { SessionPage } from "../../pages/session-page"; const SVG_ASSET = ''; function mobilePreviewHtml(assetDirectory: string): string { return `
CSS asset pending
Browser script pending
`; } function mobilePreviewScript(entryName: string): string { return `(() => { const image = document.querySelector("#asset-image"); const status = document.querySelector("#native-status"); const render = () => { status.textContent = [ typeof ResizeObserver === "function" ? "api:available" : "api:missing", image.complete && image.naturalWidth > 0 ? "image:loaded" : "image:pending", location.pathname.endsWith("/${entryName}") ? "path:entry" : "path:wrong", ].join(" | "); }; image.addEventListener("load", render); render(); })();`; } async function setupMobileHtmlPreviewTest({ testPage, apiClient, seedData, backend, }: { testPage: Page; apiClient: ApiClient; seedData: SeedData; backend: BackendContext; }): Promise<{ session: SessionPage; filePath: string; taskId: string; sessionId: string }> { const suffix = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; const filePath = `mobile-preview-${suffix}.html`; const assetDirectory = `mobile-preview-assets-${suffix}`; const git = new GitHelper( path.join(backend.tmpDir, "repos", "e2e-repo"), makeGitEnv(backend.tmpDir), ); git.createFile(filePath, mobilePreviewHtml(assetDirectory)); git.createFile(`${assetDirectory}/preview.css`, "#css-status { font-weight: 700; }"); git.createFile(`${assetDirectory}/preview.js`, mobilePreviewScript(filePath)); git.createFile(`${assetDirectory}/logo.svg`, SVG_ASSET); git.stageAll(); git.commit(`add ${filePath}`); const profile = await createStandardProfile(apiClient, `mobile-html-${Date.now()}`); const task = await apiClient.createTaskWithAgent( seedData.workspaceId, "Mobile HTML Preview", profile.id, { description: "/e2e:simple-message", workflow_id: seedData.workflowId, workflow_step_id: seedData.startStepId, repository_ids: [seedData.repositoryId], }, ); await testPage.goto(`/t/${task.id}`); const session = new SessionPage(testPage); await session.waitForLoad(); await session.waitForChatIdle({ timeout: 45_000 }); if (!task.session_id) throw new Error("Mobile HTML preview task has no session"); return { session, filePath, taskId: task.id, sessionId: task.session_id }; } test.describe("Mobile HTML preview", () => { test.describe.configure({ retries: 1, timeout: 120_000 }); test("renders native scripts and relative assets in the focused viewer", async ({ testPage, apiClient, seedData, backend, prCapture, }) => { const { filePath } = await setupMobileHtmlPreviewTest({ testPage, apiClient, seedData, backend, }); await testPage.getByRole("button", { name: "Files", exact: true }).tap(); const fileNode = testPage.locator(`[data-testid="file-tree-node"][data-path="${filePath}"]`); await expect(fileNode).toBeVisible({ timeout: 15_000 }); await fileNode.tap(); const viewer = testPage.getByTestId("mobile-file-viewer-panel"); await expect(viewer).toBeVisible({ timeout: 5_000 }); const previewToggle = viewer.getByTestId("html-preview-toggle"); await expect(previewToggle).toBeVisible(); const toggleBox = await previewToggle.boundingBox(); expect(toggleBox?.width).toBeGreaterThanOrEqual(44); expect(toggleBox?.height).toBeGreaterThanOrEqual(44); await expect(previewToggle).toHaveAttribute("title", /Previewing runs workspace code/); await previewToggle.tap(); const preview = viewer.getByTestId("html-preview"); await expect(preview).toBeVisible(); await expect(preview.getByTestId("html-preview-trust-warning")).toBeVisible(); const frame = preview.frameLocator("iframe"); await expect(preview.locator("iframe")).toHaveAttribute("src", /\/port-proxy\/.*\/\d+\//); await expect(frame.locator("h1")).toHaveText("Mobile native preview", { timeout: 15_000 }); await expect(frame.locator("#native-status")).toHaveText( "api:available | image:loaded | path:entry", ); await expect(frame.locator("#css-status")).toHaveCSS("font-weight", "700"); await expect(frame.locator("#asset-image")).toBeVisible(); await assertNoDocumentHorizontalOverflow(testPage, "mobile HTML preview"); await prCapture.screenshot("html-preview-mobile", { caption: "Native HTML preview in the focused mobile viewer", }); await preview.getByRole("button", { name: "Show code" }).tap(); await expect(viewer.locator(".cm-editor:visible")).toBeVisible(); await expect(viewer.locator(".cm-content")).toContainText("Mobile native preview"); await expect(preview).toBeHidden(); }); test("captures touch feedback in the native HTML preview drawer", async ({ testPage, apiClient, seedData, backend, }) => { const createFailure = await routeMainWebSocketWithPreviewFeedbackCreateFailure(testPage); const { filePath, session, taskId, sessionId } = await setupMobileHtmlPreviewTest({ testPage, apiClient, seedData, backend, }); await testPage.getByRole("button", { name: "Files" }).tap(); const fileNode = testPage.locator(`[data-testid="file-tree-node"][data-path="${filePath}"]`); await expect(fileNode).toBeVisible({ timeout: 15_000 }); await fileNode.tap(); const viewer = testPage.getByTestId("mobile-file-viewer-panel"); await expect(viewer).toBeVisible({ timeout: 5_000 }); await viewer.getByTestId("html-preview-toggle").tap(); const preview = viewer.getByTestId("html-preview"); const frame = preview.frameLocator("iframe"); await expect(frame.locator("#capture-button")).toBeVisible({ timeout: 15_000 }); const trigger = preview.getByTestId("preview-feedback-trigger"); const triggerBox = await trigger.boundingBox(); expect(triggerBox?.width).toBeGreaterThanOrEqual(44); expect(triggerBox?.height).toBeGreaterThanOrEqual(44); await trigger.tap(); const drawer = testPage.getByTestId("preview-feedback-drawer"); await expect(drawer).toBeVisible(); const viewport = testPage.viewportSize(); expect(viewport).not.toBeNull(); await expect .poll(async () => { const box = await drawer.boundingBox(); return box ? box.y + box.height : Number.POSITIVE_INFINITY; }) .toBeLessThanOrEqual(viewport!.height); const drawerBox = await drawer.boundingBox(); expect(drawerBox).not.toBeNull(); expect(drawerBox!.x).toBeGreaterThanOrEqual(0); expect(drawerBox!.x + drawerBox!.width).toBeLessThanOrEqual(viewport!.width); expect(drawerBox!.y + drawerBox!.height).toBeLessThanOrEqual(viewport!.height); const captureChoices = [ { name: "Select text", label: "Text" }, { name: "Select element", label: "Element" }, { name: "Select screenshot region", label: "Screenshot" }, ]; const captureChoiceBoxes = []; for (const choice of captureChoices) { const button = drawer.getByRole("button", { name: choice.name, exact: true }); await expect(button).toHaveText(choice.label); captureChoiceBoxes.push(await button.boundingBox()); } expect(new Set(captureChoiceBoxes.map((box) => Math.round(box?.y ?? -1))).size).toBe(1); const elementChoice = drawer.getByRole("button", { name: "Select element", exact: true }); const elementChoiceBox = await elementChoice.boundingBox(); expect(elementChoiceBox?.height).toBeGreaterThanOrEqual(44); await elementChoice.tap(); await frame.locator("#capture-button").tap(); const elementDraft = testPage.getByTestId("preview-feedback-draft"); await expect(elementDraft).toContainText("