Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Add deterministic QA e2e suite

Abi Raja6384692aa97c
diff --git a/frontend/src/components/preview/CodeTab.tsx b/frontend/src/components/preview/CodeTab.tsx
index 378d761f..5ae50410 100644
--- a/frontend/src/components/preview/CodeTab.tsx
+++ b/frontend/src/components/preview/CodeTab.tsx
@@ -60,12 +60,14 @@ function CodeTab({ code, setCode, settings }: Props) {
title="Copy Code"
className="bg-black text-white flex items-center justify-center hover:text-black hover:bg-gray-100 cursor-pointer rounded-lg text-sm p-2.5"
onClick={copyCode}
+ data-testid="copy-code"
>
Copy Code <FaCopy className="ml-2" />
</span>
<Button
onClick={doOpenInCodepenio}
className="bg-gray-100 text-black ml-2 py-2 px-4 border border-black rounded-md hover:bg-gray-400 focus:outline-none"
+ data-testid="open-codepen"
>
Open in{" "}
<img
diff --git a/frontend/src/components/preview/PreviewPane.tsx b/frontend/src/components/preview/PreviewPane.tsx
index c98a952a..0525978b 100644
--- a/frontend/src/components/preview/PreviewPane.tsx
+++ b/frontend/src/components/preview/PreviewPane.tsx
@@ -63,6 +63,7 @@ function PreviewPane({ doUpdate, reset, settings }: Props) {
onClick={() => downloadCode(previewCode)}
variant="secondary"
className="flex items-center gap-x-2 mr-4 dark:text-white dark:bg-gray-700 download-btn"
+ data-testid="download-code"
>
<FaDownload /> Download Code
</Button>
@@ -71,13 +72,13 @@ function PreviewPane({ doUpdate, reset, settings }: Props) {
</div>
<div className="flex items-center">
<TabsList>
- <TabsTrigger value="desktop" title="Desktop">
+ <TabsTrigger value="desktop" title="Desktop" data-testid="tab-desktop">
<FaDesktop />
</TabsTrigger>
- <TabsTrigger value="mobile" title="Mobile">
+ <TabsTrigger value="mobile" title="Mobile" data-testid="tab-mobile">
<FaMobile />
</TabsTrigger>
- <TabsTrigger value="code" title="Code">
+ <TabsTrigger value="code" title="Code" data-testid="tab-code">
<FaCode />
</TabsTrigger>
</TabsList>
diff --git a/frontend/src/components/settings/OutputSettingsSection.tsx b/frontend/src/components/settings/OutputSettingsSection.tsx
index b62ed938..92be12c2 100644
--- a/frontend/src/components/settings/OutputSettingsSection.tsx
+++ b/frontend/src/components/settings/OutputSettingsSection.tsx
@@ -30,7 +30,11 @@ function OutputSettingsSection({
onValueChange={(value: string) => setStack(value as Stack)}
disabled={shouldDisableUpdates}
>
- <SelectTrigger className="col-span-2" id="output-settings-js">
+ <SelectTrigger
+ className="col-span-2"
+ id="output-settings-js"
+ data-testid="stack-select"
+ >
{stack ? <StackLabel stack={stack} /> : "Select a stack"}
</SelectTrigger>
<SelectContent>
diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx
index 6a3d45d8..b3d1b338 100644
--- a/frontend/src/components/sidebar/Sidebar.tsx
+++ b/frontend/src/components/sidebar/Sidebar.tsx
@@ -184,6 +184,7 @@ function Sidebar({
}
}}
value={updateInstruction}
+ data-testid="update-input"
/>
<div className="flex gap-2">
<Button
diff --git a/frontend/src/components/unified-input/UnifiedInputPane.tsx b/frontend/src/components/unified-input/UnifiedInputPane.tsx
index 6fcc2aa9..8e57aae8 100644
--- a/frontend/src/components/unified-input/UnifiedInputPane.tsx
+++ b/frontend/src/components/unified-input/UnifiedInputPane.tsx
@@ -36,19 +36,35 @@ function UnifiedInputPane({
className="w-full"
>
<TabsList className="grid w-full grid-cols-4 mb-6">
- <TabsTrigger value="upload" className="flex items-center gap-2">
+ <TabsTrigger
+ value="upload"
+ className="flex items-center gap-2"
+ data-testid="tab-upload"
+ >
<UploadIcon />
<span className="hidden sm:inline">Upload</span>
</TabsTrigger>
- <TabsTrigger value="url" className="flex items-center gap-2">
+ <TabsTrigger
+ value="url"
+ className="flex items-center gap-2"
+ data-testid="tab-url"
+ >
<UrlIcon />
<span className="hidden sm:inline">URL</span>
</TabsTrigger>
- <TabsTrigger value="text" className="flex items-center gap-2">
+ <TabsTrigger
+ value="text"
+ className="flex items-center gap-2"
+ data-testid="tab-text"
+ >
<TextIcon />
<span className="hidden sm:inline">Text</span>
</TabsTrigger>
- <TabsTrigger value="import" className="flex items-center gap-2">
+ <TabsTrigger
+ value="import"
+ className="flex items-center gap-2"
+ data-testid="tab-import"
+ >
<ImportIcon />
<span className="hidden sm:inline">Import</span>
</TabsTrigger>
diff --git a/frontend/src/components/unified-input/tabs/ImportTab.tsx b/frontend/src/components/unified-input/tabs/ImportTab.tsx
index 13a9c91f..0ec98e25 100644
--- a/frontend/src/components/unified-input/tabs/ImportTab.tsx
+++ b/frontend/src/components/unified-input/tabs/ImportTab.tsx
@@ -104,6 +104,7 @@ function ImportTab({ importFromCode }: Props) {
onKeyDown={handleKeyDown}
className="w-full h-48 font-mono text-sm resize-none"
placeholder="Paste your HTML code here or drag/drop a .html file..."
+ data-testid="import-input"
/>
</div>
@@ -114,7 +115,12 @@ function ImportTab({ importFromCode }: Props) {
shouldDisableUpdates={false}
/>
- <Button onClick={doImport} className="w-full" size="lg">
+ <Button
+ onClick={doImport}
+ className="w-full"
+ size="lg"
+ data-testid="import-submit"
+ >
Import Code
</Button>
diff --git a/frontend/src/components/unified-input/tabs/TextTab.tsx b/frontend/src/components/unified-input/tabs/TextTab.tsx
index 820de840..09c5d75a 100644
--- a/frontend/src/components/unified-input/tabs/TextTab.tsx
+++ b/frontend/src/components/unified-input/tabs/TextTab.tsx
@@ -80,6 +80,7 @@ function TextTab({ doCreateFromText }: Props) {
value={text}
onChange={(e) => setText(e.target.value)}
onKeyDown={handleKeyDown}
+ data-testid="text-input"
/>
<div className="flex flex-col gap-2">
@@ -98,7 +99,12 @@ function TextTab({ doCreateFromText }: Props) {
</div>
</div>
- <Button onClick={handleGenerate} className="w-full" size="lg">
+ <Button
+ onClick={handleGenerate}
+ className="w-full"
+ size="lg"
+ data-testid="text-generate"
+ >
Generate
</Button>
diff --git a/frontend/src/components/unified-input/tabs/UploadTab.tsx b/frontend/src/components/unified-input/tabs/UploadTab.tsx
index ae578125..2e843e09 100644
--- a/frontend/src/components/unified-input/tabs/UploadTab.tsx
+++ b/frontend/src/components/unified-input/tabs/UploadTab.tsx
@@ -183,8 +183,8 @@ function UploadTab({ doCreate }: Props) {
return (
<div className="flex flex-col items-center gap-6">
{screenRecorderState === ScreenRecorderState.INITIAL && !hasUploadedFile && (
- <div {...getRootProps({ style })}>
- <input {...getInputProps()} />
+ <div {...getRootProps({ style })} data-testid="upload-dropzone">
+ <input {...getInputProps({ "data-testid": "upload-input" })} />
<div className="flex flex-col items-center gap-3">
<div className="w-16 h-16 rounded-full bg-gray-100 flex items-center justify-center">
<svg
@@ -277,7 +277,12 @@ function UploadTab({ doCreate }: Props) {
)}
<div className="flex flex-col items-center gap-1 w-full max-w-md">
- <Button onClick={handleGenerate} className="w-full" size="lg">
+ <Button
+ onClick={handleGenerate}
+ className="w-full"
+ size="lg"
+ data-testid="upload-generate"
+ >
Generate Code
</Button>
<p className="text-xs text-gray-400">Press Enter to generate</p>
diff --git a/frontend/src/components/unified-input/tabs/UrlTab.tsx b/frontend/src/components/unified-input/tabs/UrlTab.tsx
index f43aeaa5..09b30e32 100644
--- a/frontend/src/components/unified-input/tabs/UrlTab.tsx
+++ b/frontend/src/components/unified-input/tabs/UrlTab.tsx
@@ -95,12 +95,14 @@ function UrlTab({ doCreate, screenshotOneApiKey }: Props) {
}
}}
className="w-full"
+ data-testid="url-input"
/>
<Button
onClick={takeScreenshot}
disabled={isLoading}
className="w-full"
size="lg"
+ data-testid="url-capture"
>
{isLoading ? (
<span className="flex items-center gap-2">
diff --git a/frontend/src/tests/fixtures/simple_page.html b/frontend/src/tests/fixtures/simple_page.html
new file mode 100644
index 00000000..70192bfc
--- /dev/null
+++ b/frontend/src/tests/fixtures/simple_page.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <title>Simple QA Page</title>
+ <style>
+ body {
+ font-family: Arial, sans-serif;
+ padding: 24px;
+ }
+ .button {
+ background: #2563eb;
+ border: none;
+ border-radius: 8px;
+ color: #fff;
+ padding: 12px 20px;
+ font-size: 16px;
+ }
+ </style>
+ </head>
+ <body>
+ <button class="button">Howdy</button>
+ </body>
+</html>
diff --git a/frontend/src/tests/qa.test.ts b/frontend/src/tests/qa.test.ts
index 88910d72..5288a3f8 100644
--- a/frontend/src/tests/qa.test.ts
+++ b/frontend/src/tests/qa.test.ts
@@ -1,55 +1,75 @@
-import puppeteer, { Browser, Page, ElementHandle } from "puppeteer";
+import fs from "fs";
+import path from "path";
+import puppeteer, { Browser, ElementHandle, Page } from "puppeteer";
import { Stack } from "../lib/stacks";
import { CodeGenerationModel } from "../lib/models";
-const TESTS_ROOT_PATH = process.env.TEST_ROOT_PATH;
+declare global {
+ interface Window {
+ __qaDownloads: Array<string | null>;
+ __qaFormSubmits: Array<string | null>;
+ __qaClipboardCalls: string[];
+ }
+}
+
+const RUN_E2E = process.env.RUN_E2E === "true";
+const describeE2E = RUN_E2E ? describe : describe.skip;
+
+const TESTS_ROOT_PATH =
+ process.env.TEST_ROOT_PATH ?? path.resolve(process.cwd(), "src/tests");
// Fixtures
-const FIXTURES_PATH = `${TESTS_ROOT_PATH}/fixtures`;
-const SIMPLE_SCREENSHOT = FIXTURES_PATH + "/simple_button.png";
-const SCREENSHOT_WITH_IMAGES = `${FIXTURES_PATH}/simple_ui_with_image.png`;
+const FIXTURES_PATH = path.join(TESTS_ROOT_PATH, "fixtures");
+const SIMPLE_SCREENSHOT = path.join(FIXTURES_PATH, "simple_button.png");
+const SCREENSHOT_WITH_IMAGES = path.join(
+ FIXTURES_PATH,
+ "simple_ui_with_image.png"
+);
+const SIMPLE_HTML = path.join(FIXTURES_PATH, "simple_page.html");
// Results
-const RESULTS_DIR = `${TESTS_ROOT_PATH}/results`;
+const RESULTS_DIR = path.join(TESTS_ROOT_PATH, "results");
+
+const defaultStacks = [Stack.HTML_TAILWIND];
+const stacks = process.env.QA_STACKS
+ ? process.env.QA_STACKS.split(",").map((stack) => stack.trim() as Stack)
+ : defaultStacks;
-describe.skip("e2e tests", () => {
+const defaultModels = [CodeGenerationModel.CLAUDE_4_5_SONNET_2025_09_29];
+const models = process.env.QA_MODELS
+ ? process.env.QA_MODELS.split(",").map((model) => model.trim())
+ : defaultModels;
+
+describeE2E("qa e2e flows", () => {
let browser: Browser;
let page: Page;
- const DEBUG = true;
- const IS_HEADLESS = true;
-
- const stacks = Object.values(Stack).slice(0, DEBUG ? 1 : undefined);
- const models = DEBUG
- ? [CodeGenerationModel.CLAUDE_4_5_SONNET_2025_09_29]
- : Object.values(CodeGenerationModel);
+ const IS_HEADLESS = process.env.HEADLESS !== "false";
beforeAll(async () => {
+ fs.mkdirSync(RESULTS_DIR, { recursive: true });
browser = await puppeteer.launch({ headless: IS_HEADLESS });
page = await browser.newPage();
- await page.goto("http://localhost:5173/");
+
+ await installMockWebSocket(page);
+ await installDomTestHooks(page);
+ await setupRequestInterception(page, SCREENSHOT_WITH_IMAGES);
+
+ await page.goto("http://localhost:5173/", { waitUntil: "networkidle0" });
// Set screen size
- await page.setViewport({ width: 1080, height: 1024 });
-
- // TODO: Does this need to be moved?
- // const client = await page.createCDPSession();
- // Set download behavior path
- // await client.send("Page.setDownloadBehavior", {
- // behavior: "allow",
- // downloadPath: DOWNLOAD_PATH,
- // });
+ await page.setViewport({ width: 1280, height: 1024 });
});
afterAll(async () => {
await browser.close();
});
- // Create tests
+ // Create tests (every stack)
models.forEach((model) => {
stacks.forEach((stack) => {
it(
- `Create for : ${model} & ${stack}`,
+ `create from screenshot: ${model} & ${stack}`,
async () => {
const app = new App(
page,
@@ -58,14 +78,14 @@ describe.skip("e2e tests", () => {
`create_screenshot_${model}_${stack}`
);
await app.init();
- // Generate from screenshot
await app.uploadImage(SCREENSHOT_WITH_IMAGES);
+ await app.resetTestHooks();
},
60 * 1000
);
it(
- `Create from URL for : ${model} & ${stack}`,
+ `create from URL: ${model} & ${stack}`,
async () => {
const app = new App(
page,
@@ -74,79 +94,113 @@ describe.skip("e2e tests", () => {
`create_url_${model}_${stack}`
);
await app.init();
- // Generate from screenshot
- await app.generateFromUrl("https://a.picoapps.xyz/design-fear");
+ await app.generateFromUrl("https://example.com");
+ await app.resetTestHooks();
},
60 * 1000
);
});
});
- // Update tests - for every model (doesn’t need to be repeated for each stack - fix to HTML Tailwind only)
+ // Update tests - for every model (HTML Tailwind only)
models.forEach((model) => {
- ["html_tailwind"].forEach((stack) => {
- it(
- `update: ${model}`,
- async () => {
- const app = new App(page, stack, model, `update_${model}_${stack}`);
- await app.init();
-
- // Generate from screenshot
- await app.uploadImage(SIMPLE_SCREENSHOT);
- // Regenerate works for v1
- await app.regenerate();
- // Make an update
- await app.edit("make the button background blue", "v2");
- // Make another update
- await app.edit("make the text italic", "v3");
- // Branch off v2 and make an update
- await app.clickVersion("v2");
- await app.edit("make the text yellow", "v4");
- },
- 90 * 1000
- );
- });
+ it(
+ `update flow: ${model}`,
+ async () => {
+ const app = new App(
+ page,
+ Stack.HTML_TAILWIND,
+ model,
+ `update_${model}`
+ );
+ await app.init();
+
+ await app.uploadImage(SIMPLE_SCREENSHOT);
+ await app.regenerate();
+ await app.edit("make the text underline", "v2");
+ await app.edit("make the text italic", "v3");
+ await app.clickVersion("v2");
+ await app.edit("make the text yellow", "v4");
+ await app.resetTestHooks();
+ },
+ 90 * 1000
+ );
});
// Start from code tests - for every model
models.forEach((model) => {
- ["html_tailwind"].forEach((stack) => {
- it.skip(
- `Start from code: ${model}`,
- async () => {
- const app = new App(
- page,
- stack,
- model,
- `start_from_code_${model}_${stack}`
- );
- await app.init();
+ it(
+ `start from code: ${model}`,
+ async () => {
+ const app = new App(
+ page,
+ Stack.HTML_TAILWIND,
+ model,
+ `start_from_code_${model}`
+ );
+ await app.init();
+ await app.importFromCode(fs.readFileSync(SIMPLE_HTML, "utf8"));
+ await app.edit("make the text underline", "v2");
+ await app.edit("make the text italic", "v3");
+ await app.clickVersion("v2");
+ await app.edit("make the text yellow", "v4");
+ await app.resetTestHooks();
+ },
+ 90 * 1000
+ );
+ });
- await app.importFromCode();
-
- // Regenerate works for v1
- // await app.regenerate();
- // // Make an update
- // await app.edit("make the header blue", "v2");
- // // Make another update
- // await app.edit("make all text italic", "v3");
- // // Branch off v2 and make an update
- // await app.clickVersion("v2");
- // await app.edit("make all text red", "v4");
- },
- 90 * 1000
- );
- });
+ // Start from text tests - for every model
+ models.forEach((model) => {
+ it(
+ `start from text: ${model}`,
+ async () => {
+ const app = new App(
+ page,
+ Stack.HTML_TAILWIND,
+ model,
+ `start_from_text_${model}`
+ );
+ await app.init();
+ await app.generateFromText("a simple button that says howdy");
+ await app.edit("make the text underline", "v2");
+ await app.edit("make the text italic", "v3");
+ await app.clickVersion("v2");
+ await app.edit("make the text yellow", "v4");
+ await app.resetTestHooks();
+ },
+ 90 * 1000
+ );
+ });
+
+ // Buttons (Download Code, Copy Code, Open in Codepen)
+ models.forEach((model) => {
+ it(
+ `code action buttons: ${model}`,
+ async () => {
+ const app = new App(
+ page,
+ Stack.HTML_TAILWIND,
+ model,
+ `code_buttons_${model}`
+ );
+ await app.init();
+ await app.generateFromText("a simple button that says howdy");
+ await app.assertCodeActions();
+ await app.resetTestHooks();
+ },
+ 60 * 1000
+ );
});
});
class App {
private screenshotPathPrefix: string;
private page: Page;
- private stack: string;
+ private stack: Stack;
private model: string;
- constructor(page: Page, stack: string, model: string, testId: string) {
+ constructor(page: Page, stack: Stack, model: string, testId: string) {
this.page = page;
this.stack = stack;
this.model = model;
@@ -155,95 +209,118 @@ class App {
async init() {
await this.setupLocalStorage();
+ await this.resetTestHooks();
}
async setupLocalStorage() {
const setting = {
- openAiApiKey: null,
+ openAiApiKey: "test-openai-key",
openAiBaseURL: null,
- screenshotOneApiKey: process.env.TEST_SCREENSHOTONE_API_KEY,
+ anthropicApiKey: "test-anthropic-key",
+ screenshotOneApiKey: "test-screenshotone-key",
isImageGenerationEnabled: true,
editorTheme: "cobalt",
generatedCodeConfig: this.stack,
codeGenerationModel: this.model,
- isTermOfServiceAccepted: false,
+ isTermOfServiceAccepted: true,
accessCode: null,
};
- await this.page.evaluate((setting) => {
- localStorage.setItem("setting", JSON.stringify(setting));
+ await this.page.evaluate((nextSetting) => {
+ localStorage.setItem("setting", JSON.stringify(nextSetting));
}, setting);
- // Reload the page to apply the local storage
- await this.page.reload();
+ await this.page.reload({ waitUntil: "networkidle0" });
}
- async _screenshot(step: string) {
+ async resetTestHooks() {
+ await this.page.evaluate(() => {
+ window.__qaDownloads = [];
+ window.__qaFormSubmits = [];
+ window.__qaClipboardCalls = [];
+ });
+ }
+
+ async takeScreenshot(step: string) {
await this.page.screenshot({
path: `${this.screenshotPathPrefix}_${step}.png`,
});
}
- async _waitUntilVersionIsReady(version: string) {
- await this.page.waitForNetworkIdle();
+ async waitUntilVersionIsReady(version: string) {
await this.page.waitForFunction(
- (version) => document.body.innerText.includes(version),
+ (versionLabel) => document.body.innerText.includes(versionLabel),
{
timeout: 30000,
},
version
);
- // Wait for 3s so that the HTML and JS has time to render before screenshotting
- await new Promise((resolve) => setTimeout(resolve, 3000));
+ await this.page.waitForSelector('[data-testid="update-input"]', {
+ timeout: 30000,
+ });
+ }
+
+ async switchToTab(testId: string) {
+ await this.page.click(`[data-testid="${testId}"]`);
}
async generateFromUrl(url: string) {
- // Type in the URL
- await this.page.type('input[placeholder="Enter URL"]', url);
- await this._screenshot("typed_url");
-
- // Click the capture button and wait for the code to be generated
- await this.page.click("button.capture-btn");
- await this._waitUntilVersionIsReady("v1");
- await this._screenshot("url_result");
+ await this.switchToTab("tab-url");
+ await this.page.type('[data-testid="url-input"]', url);
+ await this.takeScreenshot("typed_url");
+ await this.page.click('[data-testid="url-capture"]');
+ await this.waitUntilVersionIsReady("v1");
+ await this.takeScreenshot("url_result");
+ }
+
+ async generateFromText(prompt: string) {
+ await this.switchToTab("tab-text");
+ await this.page.type('[data-testid="text-input"]', prompt);
+ await this.takeScreenshot("typed_text");
+ await this.page.click('[data-testid="text-generate"]');
+ await this.waitUntilVersionIsReady("v1");
+ await this.takeScreenshot("text_result");
}
- // Uploads a screenshot and generates the image
async uploadImage(screenshotPath: string) {
- // Upload file
+ await this.switchToTab("tab-upload");
const fileInput = (await this.page.$(
- ".file-input"
+ '[data-testid="upload-input"]'
)) as ElementHandle<HTMLInputElement>;
if (!fileInput) {
- throw new Error("File input element not found");
+ throw new Error("Upload input element not found");
}
await fileInput.uploadFile(screenshotPath);
- await this._screenshot("image_uploaded");
+ await this.page.waitForSelector('[data-testid="upload-generate"]');
+ await this.takeScreenshot("image_uploaded");
- // Click the generate button and wait for the code to be generated
- await this._waitUntilVersionIsReady("v1");
- await this._screenshot("image_results");
+ await this.page.click('[data-testid="upload-generate"]');
+ await this.waitUntilVersionIsReady("v1");
+ await this.takeScreenshot("image_results");
}
- // Makes a text edit and waits for a new version
- async edit(edit: string, version: string) {
- // Type in the edit
- await this.page.type(
- 'textarea[placeholder="Tell the AI what to change..."]',
- edit
- );
- await this._screenshot(`typed_${version}`);
+ async importFromCode(code: string) {
+ await this.switchToTab("tab-import");
+ await this.page.type('[data-testid="import-input"]', code);
+ await this.page.click('[data-testid="stack-select"]');
+ await this.page.click('div[role="option"] >> text="HTML + Tailwind"');
+ await this.takeScreenshot("typed_code");
+ await this.page.click('[data-testid="import-submit"]');
+ await this.waitUntilVersionIsReady("v1");
+ }
- // Click the update button and wait for the code to be generated
+ async edit(edit: string, version: string) {
+ await this.page.type('[data-testid="update-input"]', edit);
+ await this.takeScreenshot(`typed_${version}`);
await this.page.click(".update-btn");
- await this._waitUntilVersionIsReady(version);
- await this._screenshot(`done_${version}`);
+ await this.waitUntilVersionIsReady(version);
+ await this.takeScreenshot(`done_${version}`);
}
async clickVersion(version: string) {
- await this.page.evaluate((version) => {
+ await this.page.evaluate((versionLabel) => {
document.querySelectorAll("div").forEach((div) => {
- if (div.innerText.includes(version)) {
+ if (div.innerText.includes(versionLabel)) {
div.click();
}
});
@@ -252,22 +329,157 @@ class App {
async regenerate() {
await this.page.click(".regenerate-btn");
- await this._waitUntilVersionIsReady("v1");
- await this._screenshot("regenerate_results");
+ await this.waitUntilVersionIsReady("v1");
+ await this.takeScreenshot("regenerate_results");
}
- // Work in progress
- async importFromCode() {
- await this.page.click(".import-from-code-btn");
+ async assertCodeActions() {
+ await this.page.click('[data-testid="tab-code"]');
+ await this.page.click('[data-testid="copy-code"]');
+ await this.page.click('[data-testid="open-codepen"]');
+ await this.page.click('[data-testid="download-code"]');
+
+ const results = await this.page.evaluate(() => ({
+ downloads: window.__qaDownloads,
+ submits: window.__qaFormSubmits,
+ clipboard: window.__qaClipboardCalls,
+ }));
+
+ expect(results.downloads).toContain("index.html");
+ expect(results.submits).toContain("https://codepen.io/pen/define");
+ expect(results.clipboard).toContain("copy");
+ }
+}
- await this.page.type("textarea", "<html>hello world</html>");
+async function setupRequestInterception(
+ page: Page,
+ screenshotFixturePath: string
+) {
+ const screenshotBuffer = fs.readFileSync(screenshotFixturePath);
+ const screenshotDataUrl = `data:image/png;base64,${screenshotBuffer.toString(
+ "base64"
+ )}`;
+
+ await page.setRequestInterception(true);
+ page.on("request", (request) => {
+ const url = request.url();
+ if (url.endsWith("/api/screenshot")) {
+ request.respond({
+ status: 200,
+ contentType: "application/json",
+ body: JSON.stringify({ url: screenshotDataUrl }),
+ });
+ return;
+ }
+ request.continue();
+ });
+}
- await this.page.select("#output-settings-js", "HTML + Tailwind");
+async function installDomTestHooks(page: Page) {
+ await page.addInitScript(() => {
+ window.__qaDownloads = [];
+ window.__qaFormSubmits = [];
+ window.__qaClipboardCalls = [];
- await this._screenshot("typed_code");
+ const originalAnchorClick = HTMLAnchorElement.prototype.click;
+ HTMLAnchorElement.prototype.click = function (...args) {
+ window.__qaDownloads.push(this.getAttribute("download"));
+ return originalAnchorClick.apply(this, args);
+ };
- await this.page.click(".import-btn");
+ const originalFormSubmit = HTMLFormElement.prototype.submit;
+ HTMLFormElement.prototype.submit = function (...args) {
+ window.__qaFormSubmits.push(this.getAttribute("action"));
+ return originalFormSubmit.apply(this, args);
+ };
- await this._waitUntilVersionIsReady("v1");
- }
+ const originalExecCommand = document.execCommand?.bind(document);
+ document.execCommand = (command) => {
+ window.__qaClipboardCalls.push(command);
+ if (!originalExecCommand) {
+ return true;
+ }
+ return originalExecCommand(command);
+ };
+ });
+}
+
+async function installMockWebSocket(page: Page) {
+ await page.addInitScript(() => {
+ class MockWebSocket {
+ static CONNECTING = 0;
+ static OPEN = 1;
+ static CLOSING = 2;
+ static CLOSED = 3;
+
+ readyState = MockWebSocket.CONNECTING;
+ url: string;
+ listeners: Record<string, Array<(event: any) => void>> = {};
+
+ constructor(url: string) {
+ this.url = url;
+ window.setTimeout(() => {
+ this.readyState = MockWebSocket.OPEN;
+ this.emit("open", {});
+ }, 10);
+ }
+
+ addEventListener(type: string, listener: (event: any) => void) {
+ if (!this.listeners[type]) {
+ this.listeners[type] = [];
+ }
+ this.listeners[type].push(listener);
+ }
+
+ removeEventListener(type: string, listener: (event: any) => void) {
+ if (!this.listeners[type]) return;
+ this.listeners[type] = this.listeners[type].filter(
+ (existing) => existing !== listener
+ );
+ }
+
+ send(data: string) {
+ const params = JSON.parse(data);
+ const code = [
+ "<!doctype html>",
+ "<html>",
+ "<head><title>QA</title></head>",
+ "<body>",
+ `<button>${params.generationType ?? "create"}</button>`,
+ "</body>",
+ "</html>",
+ ].join("");
+
+ const events = [
+ { type: "variantCount", value: "1", variantIndex: 0 },
+ { type: "status", value: "Generating", variantIndex: 0 },
+ { type: "setCode", value: code, variantIndex: 0 },
+ { type: "variantComplete", value: "", variantIndex: 0 },
+ ];
+
+ events.forEach((payload, index) => {
+ window.setTimeout(() => {
+ this.emit("message", { data: JSON.stringify(payload) });
+ }, 20 * (index + 1));
+ });
+
+ window.setTimeout(() => {
+ this.close(1000, "OK");
+ }, 120);
+ }
+
+ close(code = 1000, reason = "") {
+ this.readyState = MockWebSocket.CLOSED;
+ this.emit("close", { code, reason });
+ }
+
+ emit(type: string, event: any) {
+ (this.listeners[type] || []).forEach((listener) => {
+ listener(event);
+ });
+ }
+ }
+
+ window.WebSocket = MockWebSocket;
+ });
}