diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b60864..c82aac6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -247,12 +247,13 @@ jobs: - markdown - workflows runs-on: ubuntu-latest - timeout-minutes: 15 + timeout-minutes: 20 steps: - name: Check out repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false - name: Download exchange data uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: @@ -277,6 +278,26 @@ jobs: python -m sklc_maintainer.packaging prebuilt --stage --repo-root . --site-dir products/deployment-kit/operator/site/dist --output-dir "$RUNNER_TEMP/static-site" + - name: Install upstream browser test dependencies + working-directory: maintainer/browser-tests + run: npm ci + - name: Install Chromium headless shell + working-directory: maintainer/browser-tests + run: npm exec -- playwright install --with-deps --only-shell chromium + - name: Exercise the staged website in Chromium + id: browser-test + working-directory: maintainer/browser-tests + env: + SKLC_SITE_DIR: ${{ runner.temp }}/static-site/public + SKLC_SITE_REVISION: ${{ github.event.pull_request.head.sha || github.sha }} + run: npm test + - name: Save browser failure traces + if: failure() && steps.browser-test.outcome == 'failure' + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: browser-test-results + path: maintainer/browser-tests/test-results/ + retention-days: 7 - name: Upload static site uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: diff --git a/CI.md b/CI.md index 5c33690..59ce0a9 100644 --- a/CI.md +++ b/CI.md @@ -66,6 +66,10 @@ Python prepares the website's data and downloads under `products/deployment-kit/ ### Find a result and decide what to do +Before uploading the Prebuilt website, the **Static site artifact** job serves its staged files at the root of one local server and under `/public/` on another. Chromium searches the catalog, opens a skill, reads its assessment reports and downloads its ZIP at both addresses. A failed journey stops the upload. Open the failed step for the assertion and download `browser-test-results` from the run summary for the Playwright trace. + +To repeat the check locally, extract the `static-site` artifact into its own directory. From `maintainer/browser-tests/`, run `npm ci` and `npm exec -- playwright install --only-shell chromium` (on Linux, add `--with-deps`). Set `SKLC_SITE_DIR` to the absolute path of the extracted `public/` directory and `SKLC_SITE_REVISION` to the commit used to build it, then run `npm test`. The test uses Python on PATH and loopback ports 4173 and 4174. + Open the PR's checks, or select the relevant run on the [CI Actions page](https://github.com/Code-dot-mil/SkLC/actions/workflows/ci.yml). Confirm that the run belongs to the commit you want to inspect. Open a failed job and its failed step to read the log, the recorded output of its commands. For a failed test or lint check, use the log to identify whether the affected code or file needs correction, or whether the job could not run its tools. A website-data failure can indicate missing or outdated assessment reports. Read the diagnostics before changing anything; do not edit reports to make them appear current. diff --git a/maintainer/browser-tests/.gitignore b/maintainer/browser-tests/.gitignore new file mode 100644 index 0000000..ae2f532 --- /dev/null +++ b/maintainer/browser-tests/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +test-results/ diff --git a/maintainer/browser-tests/package-lock.json b/maintainer/browser-tests/package-lock.json new file mode 100644 index 0000000..3c6e8d6 --- /dev/null +++ b/maintainer/browser-tests/package-lock.json @@ -0,0 +1,58 @@ +{ + "name": "sklc-browser-tests", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "sklc-browser-tests", + "devDependencies": { + "@playwright/test": "1.65.1" + } + }, + "node_modules/@playwright/test": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.65.1.tgz", + "integrity": "sha512-y+NCIRm2LvY4slUVWvGh1nujJ8RF/hEisIsq2mtUPgdQC4L701bq5hZGMhrpeXjACvwvO9tj5dJqhvM6TP9lbw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.65.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.65.1.tgz", + "integrity": "sha512-vJ1FuoOrNwOKHUIVEvSt3jT1L+wjjDpwhwIstKtfvTh8XIG4FG4bIFuvNdMxi81LGfAhUgUZleVFaOLY4RIUUw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.65.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.65.1.tgz", + "integrity": "sha512-hqPpz7blSMPxWVdoeE1yiBSzXsKiVOd2k+kjCvmUywD2GPO2Dd9HfcWnq9NN0GHbNZDFgZ9JfBFr+X5/BLo0qw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + } + } +} diff --git a/maintainer/browser-tests/package.json b/maintainer/browser-tests/package.json new file mode 100644 index 0000000..57e4588 --- /dev/null +++ b/maintainer/browser-tests/package.json @@ -0,0 +1,10 @@ +{ + "name": "sklc-browser-tests", + "private": true, + "scripts": { + "test": "playwright test" + }, + "devDependencies": { + "@playwright/test": "1.65.1" + } +} diff --git a/maintainer/browser-tests/playwright.config.mjs b/maintainer/browser-tests/playwright.config.mjs new file mode 100644 index 0000000..1d893b8 --- /dev/null +++ b/maintainer/browser-tests/playwright.config.mjs @@ -0,0 +1,27 @@ +import { basename, dirname, resolve } from "node:path"; +import { defineConfig } from "@playwright/test"; + +if (!process.env.SKLC_SITE_DIR || !process.env.SKLC_SITE_REVISION) { + throw new Error("Set SKLC_SITE_DIR to the staged public directory and SKLC_SITE_REVISION to its source commit."); +} +const site = resolve(process.env.SKLC_SITE_DIR); +if (basename(site) !== "public") throw new Error("Expected the Prebuilt artifact's public directory."); + +const mounts = [ + { name: "root", cwd: site, port: 4173, path: "/" }, + { name: "nested", cwd: dirname(site), port: 4174, path: "/public/" }, +]; +const url = mount => `http://127.0.0.1:${mount.port}${mount.path}`; + +export default defineConfig({ + testDir: "./tests", + workers: 1, + globalTimeout: 180_000, + use: { trace: "retain-on-failure" }, + projects: mounts.map(mount => ({ name: mount.name, use: { baseURL: url(mount) } })), + webServer: mounts.map(mount => ({ + command: `python -m http.server ${mount.port} --bind 127.0.0.1`, + cwd: mount.cwd, + url: url(mount) + "index.html", + })), +}); diff --git a/maintainer/browser-tests/tests/catalog.spec.mjs b/maintainer/browser-tests/tests/catalog.spec.mjs new file mode 100644 index 0000000..308189f --- /dev/null +++ b/maintainer/browser-tests/tests/catalog.spec.mjs @@ -0,0 +1,88 @@ +import { execFileSync } from "node:child_process"; +import { readFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect, test } from "@playwright/test"; + +async function followDownload(page, link, filename) { + const target = await link.evaluate(anchor => anchor.href); + const httpFailure = page.waitForResponse(response => response.url() === target && response.status() >= 400).then(response => { + throw new Error(`Download failed with HTTP ${response.status()}: ${target}`); + }); + const [download] = await Promise.all([ + Promise.race([page.waitForEvent("download"), httpFailure]), + link.click(), + ]); + expect(download.suggestedFilename()).toBe(filename); + expect(await download.failure()).toBeNull(); + return download.path(); +} + +test("find a skill, read its evidence and download its package", async ({ page, baseURL }) => { + const errors = []; + page.on("pageerror", error => errors.push(error.message)); + page.on("console", message => { + if (message.type() === "error") errors.push(message.text()); + }); + page.on("response", response => { + if (response.status() >= 400) errors.push(`${response.status()} ${response.url()}`); + }); + + await page.goto("./"); + await expect(page.getByRole("contentinfo")).toContainText(process.env.SKLC_SITE_REVISION.slice(0, 8)); + const skills = page.getByRole("article"); + const firstSkill = skills.first().getByRole("heading").getByRole("link"); + await expect(firstSkill).toBeVisible(); + const skillId = await firstSkill.innerText(); + const initialCount = await skills.count(); + const search = page.getByRole("textbox", { name: "Search" }); + // The catalog renders before React attaches the search handler. + await expect(page.locator("astro-island:not([ssr])").filter({ has: search })).toHaveCount(1); + const unrelatedSkill = skills.filter({ hasNotText: skillId }).first().getByRole("heading").getByRole("link"); + const unrelatedId = await unrelatedSkill.innerText(); + await search.fill("sklc-e2e-no-such-skill-73c92e1a"); + await expect(skills).toHaveCount(0); + await search.fill(""); + await expect(skills).toHaveCount(initialCount); + await search.fill(skillId); + await expect(skills.getByRole("link", { name: unrelatedId, exact: true })).toHaveCount(0); + await skills.getByRole("link", { name: skillId, exact: true }).click(); + await expect(page.locator("header").getByRole("heading", { level: 1, name: skillId, exact: true })).toBeVisible(); + const instructions = page.getByRole("tabpanel", { name: "Instructions", exact: true }).locator(".markdown-body"); + await expect(instructions).toBeVisible(); + await expect(instructions).toHaveText(/\S/, { useInnerText: true }); + + const files = join(process.env.SKLC_SITE_DIR, "downloads"); + const filename = `${skillId}.zip`; + const archive = await followDownload(page, page.getByRole("link", { name: "Download skill package" }), filename); + expect(await readFile(archive)).toEqual(await readFile(join(files, filename))); + // Reading the ZIP entry checks its CRC and UTF-8 text. + const instructionLength = execFileSync("python", [ + "-c", `import sys, zipfile +with zipfile.ZipFile(sys.argv[1]) as package: + print(len(package.read(sys.argv[2]).decode("utf-8").strip()))`, + archive, `${skillId}/SKILL.md`, + ], { encoding: "utf8" }); + expect(Number(instructionLength)).toBeGreaterThan(0); + + await page.getByRole("tab", { name: "Assessment Evidence" }).click(); + const reports = join(files, "assessments", skillId); + const adjudication = await followDownload(page, page.getByRole("link", { name: "Download adjudication.json" }), "adjudication.json"); + const decision = await readFile(adjudication); + expect(JSON.parse(decision).findings).toEqual(expect.any(Array)); + expect(decision).toEqual(await readFile(join(reports, "adjudication.json"))); + + // The scanner link opens its native JSON report in the browser. + const [scannerResponse] = await Promise.all([ + page.waitForResponse(response => response.request().isNavigationRequest()), + page.getByRole("link", { name: /download .*scanner output/i }).click(), + ]); + expect(scannerResponse.ok()).toBe(true); + const scan = await scannerResponse.body(); + expect(JSON.parse(scan).issues).toEqual(expect.any(Array)); + expect(scan).toEqual(await readFile(join(reports, "skillspector.json"))); + await page.goBack(); + await page.getByRole("link", { name: "Back to catalog" }).click(); + await expect(page).toHaveURL(baseURL); + await expect(skills).toHaveCount(initialCount); + expect(errors).toEqual([]); +});