|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { createServer } from "node:http"; |
| 3 | +import { extname, join, normalize, relative, resolve } from "node:path"; |
| 4 | +import { readFile, stat } from "node:fs/promises"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const STARTER_ROOT = resolve(fileURLToPath(new URL("../..", import.meta.url))); |
| 8 | +const CONTENT_TYPES = new Map([ |
| 9 | + [".css", "text/css; charset=utf-8"], |
| 10 | + [".html", "text/html; charset=utf-8"], |
| 11 | + [".js", "text/javascript; charset=utf-8"] |
| 12 | +]); |
| 13 | + |
| 14 | +let server; |
| 15 | +let starterUrl; |
| 16 | + |
| 17 | +async function startStaticServer() { |
| 18 | + server = createServer(async (request, response) => { |
| 19 | + try { |
| 20 | + const requestPath = request.url === "/" ? "/index.html" : new URL(request.url, "http://127.0.0.1").pathname; |
| 21 | + const filePath = normalize(join(STARTER_ROOT, decodeURIComponent(requestPath))); |
| 22 | + const relativePath = relative(STARTER_ROOT, filePath); |
| 23 | + if (relativePath.startsWith("..")) { |
| 24 | + response.writeHead(403); |
| 25 | + response.end("Forbidden"); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const fileStat = await stat(filePath); |
| 30 | + if (!fileStat.isFile()) { |
| 31 | + response.writeHead(404); |
| 32 | + response.end("Not found"); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const contentType = CONTENT_TYPES.get(extname(filePath)) || "application/octet-stream"; |
| 37 | + response.writeHead(200, { "content-type": contentType }); |
| 38 | + response.end(await readFile(filePath)); |
| 39 | + } catch { |
| 40 | + response.writeHead(404); |
| 41 | + response.end("Not found"); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + await new Promise((resolveServer) => { |
| 46 | + server.listen(0, "127.0.0.1", resolveServer); |
| 47 | + }); |
| 48 | + const address = server.address(); |
| 49 | + starterUrl = `http://127.0.0.1:${address.port}/index.html`; |
| 50 | +} |
| 51 | + |
| 52 | +test.describe("First-Class Tool Starter", () => { |
| 53 | + test.beforeAll(async () => { |
| 54 | + await startStaticServer(); |
| 55 | + }); |
| 56 | + |
| 57 | + test.afterAll(async () => { |
| 58 | + await new Promise((resolveServer) => server.close(resolveServer)); |
| 59 | + }); |
| 60 | + |
| 61 | + test.beforeEach(async ({ page }) => { |
| 62 | + await page.goto(starterUrl); |
| 63 | + }); |
| 64 | + |
| 65 | + test("launches with Palette Manager-style header shell", async ({ page }) => { |
| 66 | + await expect(page.locator("body.tools-platform-tool-page[data-tool-id='first-class-tool-starter']")).toBeVisible(); |
| 67 | + await expect(page.locator(".is-collapsible")).toBeVisible(); |
| 68 | + await expect(page.locator("#shared-theme-header")).toBeAttached(); |
| 69 | + await expect(page.locator("[data-tool-starter-header]")).toContainText("First-Class Tool Starter"); |
| 70 | + await expect(page.locator("[data-tool-starter-summary]")).toContainText("Hide Header and Details"); |
| 71 | + }); |
| 72 | + |
| 73 | + test("accordion sections collapse and expand", async ({ page }) => { |
| 74 | + const sourceHeader = page.getByRole("button", { name: "Input Source" }); |
| 75 | + const sourceContent = page.locator("#sourceInputContent"); |
| 76 | + |
| 77 | + await expect(sourceContent).toBeVisible(); |
| 78 | + await sourceHeader.click(); |
| 79 | + await expect(sourceContent).toBeHidden(); |
| 80 | + await expect(sourceHeader).toHaveAttribute("aria-expanded", "false"); |
| 81 | + await sourceHeader.click(); |
| 82 | + await expect(sourceContent).toBeVisible(); |
| 83 | + await expect(sourceHeader).toHaveAttribute("aria-expanded", "true"); |
| 84 | + }); |
| 85 | + |
| 86 | + test("primary action state changes when required input is valid", async ({ page }) => { |
| 87 | + const runButton = page.locator("#runToolButton"); |
| 88 | + const exportButton = page.locator("#exportToolStateButton"); |
| 89 | + const sourceInput = page.locator("#sourceInput"); |
| 90 | + |
| 91 | + await expect(runButton).toBeDisabled(); |
| 92 | + await expect(exportButton).toBeDisabled(); |
| 93 | + await sourceInput.fill("starter value"); |
| 94 | + await expect(runButton).toBeEnabled(); |
| 95 | + await expect(exportButton).toBeEnabled(); |
| 96 | + }); |
| 97 | + |
| 98 | + test("missing input failure state is visible", async ({ page }) => { |
| 99 | + const sourceInput = page.locator("#sourceInput"); |
| 100 | + const runButton = page.locator("#runToolButton"); |
| 101 | + |
| 102 | + await sourceInput.fill("starter value"); |
| 103 | + await sourceInput.fill(""); |
| 104 | + await expect(runButton).toBeDisabled(); |
| 105 | + await expect(page.locator("#sourceValidationMessage")).toContainText("Input is required"); |
| 106 | + }); |
| 107 | + |
| 108 | + test("status log clear behavior works after primary action", async ({ page }) => { |
| 109 | + await page.locator("#sourceInput").fill("starter value"); |
| 110 | + await page.locator("#runToolButton").click(); |
| 111 | + await expect(page.locator("#statusLog")).toHaveValue(/Processed source value/); |
| 112 | + |
| 113 | + await page.locator("#clearStatusButton").click(); |
| 114 | + await expect(page.locator("#statusLog")).toHaveValue(""); |
| 115 | + }); |
| 116 | +}); |
0 commit comments