diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc99e4d9..29ed5004 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -206,7 +206,10 @@ jobs: s3-prefix: visualci s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }} s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }} - - run: cargo build --locked -p visualci_server -p visualci-cli + # The CLI (root workspace) and the app the screenshots are taken against (app/, its own workspace). + - run: cargo build --locked -p visualci-cli + - run: cargo build --locked --bin visualci-app + working-directory: app - uses: pnpm/action-setup@v4 with: package_json_file: server/frontend/package.json diff --git a/scripts/ci/visual.sh b/scripts/ci/visual.sh index e60af73f..15df6306 100755 --- a/scripts/ci/visual.sh +++ b/scripts/ci/visual.sh @@ -1,49 +1,106 @@ #!/usr/bin/env bash -# VisualCI tests itself: seed a database, start the server against it, take screenshots of the landing page, the docs -# home and the app, and send them to VisualCI in one build with the Vitest component snapshots, the request tests' JSON -# snapshots and a few files the site serves to agents. CI runs it in the `visual` job (.github/workflows/ci.yml); it -# also runs on a dev machine with Postgres and S3 storage (MinIO or RustFS) up. +# VisualCI tests itself: seed a database, start the app (app/, the Laravel Rust app production runs) against it, take +# screenshots of the landing page, the docs home and the app, and send them to VisualCI in one build with the Vitest +# component snapshots, the request tests' JSON snapshots and a few files the site serves to agents. CI runs it in the +# `visual` job (.github/workflows/ci.yml); it also runs on a build box with Postgres and S3 storage (RustFS) up. # # scripts/ci/visual.sh capture seed, serve, take screenshots and component snapshots into tmp/visual # scripts/ci/visual.sh upload send tmp/visual (and $VISUAL_JSON_DIR) to VisualCI as one build, and finalize it # -# Needs: target/debug/visualci-server and visualci (cargo build), server/frontend/dist (pnpm build), and +# Needs: app/target/debug/visualci-app and target/debug/visualci (cargo build), server/frontend/dist (pnpm build), and # scripts/screenshots/node_modules with Playwright's Chromium. Settings: PORT (5151), DATABASE_URL (a database this -# script drops and recreates), S3_BUCKET, VISUAL_JSON_DIR (the request tests' JSON snapshots), VISUALCI_TOKEN. +# script drops and recreates), S3_ENDPOINT and S3_BUCKET (made if missing), VISUAL_JSON_DIR (the request tests' JSON +# snapshots), VISUALCI_TOKEN. set -euo pipefail + root=$(cd "$(dirname "$0")/../.." && pwd) out=$root/tmp/visual +run=$root/tmp/visual-app port=${PORT:-5151} export DATABASE_URL=${DATABASE_URL:-postgres://loco:loco@127.0.0.1:55433/visualci_screenshots} -export S3_BUCKET=${S3_BUCKET:-visualci-screenshots} -export LOCO_ENV=development -# The month the seeded credit usage falls in (server/src/tasks/screenshots_seed.rs NOW). -export VISUALCI_NOW=2026-11-20T15:00:00Z -server=$root/target/debug/visualci-server +s3=${S3_ENDPOINT:-http://127.0.0.1:59000} +bucket=${S3_BUCKET:-visualci-screenshots} +app=${VISUALCI_APP_BIN:-$root/app/target/debug/visualci-app} cli=$root/target/debug/visualci +# The bucket, as production's R2 bucket exists (the app makes none): the AWS CLI where it's installed (GitHub's +# runners), else its image. +make_bucket() { + local keys=(-e AWS_ACCESS_KEY_ID=visualci -e AWS_SECRET_ACCESS_KEY=visualci-dev-secret -e AWS_DEFAULT_REGION=us-east-1) + for _ in $(seq 1 10); do + if command -v aws >/dev/null; then + if AWS_ACCESS_KEY_ID=visualci AWS_SECRET_ACCESS_KEY=visualci-dev-secret AWS_DEFAULT_REGION=us-east-1 \ + aws --endpoint-url "$s3" s3api head-bucket --bucket "$bucket" >/dev/null 2>&1 || + AWS_ACCESS_KEY_ID=visualci AWS_SECRET_ACCESS_KEY=visualci-dev-secret AWS_DEFAULT_REGION=us-east-1 \ + aws --endpoint-url "$s3" s3 mb "s3://$bucket" >/dev/null 2>&1; then + return 0 + fi + elif docker run --rm --network host "${keys[@]}" amazon/aws-cli:2.17.0 --endpoint-url "$s3" \ + s3api head-bucket --bucket "$bucket" >/dev/null 2>&1 || + docker run --rm --network host "${keys[@]}" amazon/aws-cli:2.17.0 --endpoint-url "$s3" \ + s3 mb "s3://$bucket" >/dev/null 2>&1; then + return 0 + fi + sleep 2 + done + echo "couldn't make the bucket $bucket at $s3" >&2 + exit 1 +} + capture() { - rm -rf "$out" - mkdir -p "$out" - cd "$root/server" + rm -rf "$out" "$run" + mkdir -p "$out" "$run/storage/app" "$run/storage/framework" "$run/storage/logs" + # The app serves the built React app and the site from its public folder. + ln -sfn "$root/server/frontend/dist" "$run/public" # A fresh database every time, so the seeded ids are the same. local db=${DATABASE_URL##*/} - psql "${DATABASE_URL%/*}/postgres" -tAc "SELECT 1 FROM pg_database WHERE datname = '$db'" | grep -q 1 || - psql "${DATABASE_URL%/*}/postgres" -qc "CREATE DATABASE $db" - "$server" db reset >"$out/seed.log" 2>&1 || { cat "$out/seed.log"; exit 1; } - "$server" task screenshots:seed >>"$out/seed.log" 2>&1 || { cat "$out/seed.log"; exit 1; } - PORT=$port "$server" start --binding 127.0.0.1 --no-banner >"$out/server.log" 2>&1 & + psql "${DATABASE_URL%/*}/postgres" -qc "DROP DATABASE IF EXISTS $db WITH (FORCE)" + psql "${DATABASE_URL%/*}/postgres" -qc "CREATE DATABASE $db" + make_bucket + cat >"$run/env" </dev/null | tail -1)" >>"$run/env" + set -a + . "$run/env" + set +a + "$app" migrate --force >"$out/seed.log" 2>&1 || { cat "$out/seed.log"; exit 1; } + "$app" screenshots:seed >>"$out/seed.log" 2>&1 || { cat "$out/seed.log"; exit 1; } + (cd "$run" && exec "$app" serve >"$out/server.log" 2>&1) & server_pid=$! - trap 'kill $server_pid 2>/dev/null || true' EXIT + (cd "$run" && exec "$app" queue:work >"$out/worker.log" 2>&1) & + worker_pid=$! + trap 'kill $server_pid $worker_pid 2>/dev/null || true' EXIT for _ in $(seq 1 60); do - curl -fsS "http://127.0.0.1:$port/_ping" >/dev/null 2>&1 && break + curl -fsS "http://127.0.0.1:$port/up" >/dev/null 2>&1 && break sleep 1 done - curl -fsS "http://127.0.0.1:$port/_ping" >/dev/null || { tail -50 "$out/server.log"; exit 1; } - + curl -fsS "http://127.0.0.1:$port/up" >/dev/null || { tail -50 "$out/server.log"; exit 1; } (cd "$root/scripts/screenshots" && node capture.mjs --base-url "http://127.0.0.1:$port" --out "$out/screenshots") (cd "$root/scripts/screenshots" && node checks.mjs --base-url "http://127.0.0.1:$port") - # Files the site serves to agents. mkdir -p "$out/site/docs" curl -fsS "http://127.0.0.1:$port/llms.txt" -o "$out/site/llms.txt" @@ -51,7 +108,6 @@ capture() { if curl -fsS "http://127.0.0.1:$port/visualci.schema.json" -o "$out/site/visualci.schema.json" 2>/dev/null; then :; else rm -f "$out/site/visualci.schema.json" fi - # Component snapshots, written fresh: the baselines are on VisualCI. cd "$root/server/frontend" find src -name __snapshots__ -type d -prune -exec rm -rf {} + diff --git a/scripts/screenshots/capture.mjs b/scripts/screenshots/capture.mjs index 30498185..33f10b5b 100644 --- a/scripts/screenshots/capture.mjs +++ b/scripts/screenshots/capture.mjs @@ -1,17 +1,21 @@ -// VisualCI's own screenshots: the landing page, the docs home and the app, against a server seeded with -// `visualci-server task screenshots:seed` (server/src/tasks/screenshots_seed.rs). Each page is taken at 1440 and 390 px +// VisualCI's own screenshots: the landing page, the docs home and the app, against app/ seeded with +// `visualci-app screenshots:seed` (app/src/domain/demo.rs). Each page is taken at 1440 and 390 px // wide, light and dark, as `/-.png` under the output folder, for `visualci upload`. // // node capture.mjs [--base-url http://127.0.0.1:5151] [--out out/shots] [--only ] // // Pages must come out the same on every run of unchanged code (`node stable.mjs` checks): the browser's clock is frozen // at the seed's NOW, animations and transitions are off, and each shot waits for the network, fonts and images. -import { mkdir } from 'node:fs/promises' +import { mkdir, readFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { chromium } from 'playwright' import { EMAIL, NOW, pages } from './pages.mjs' +/** VisualCI's production gallery, as server/frontend/src/components/GalleryImage.tsx names it. */ +const GALLERY = 'https://app.visualci.com/g/docspring/visualci' +const GALLERY_PATH = new URL(GALLERY).pathname + const NO_MOTION = `*, *::before, *::after { animation: none !important; transition: none !important; caret-color: transparent !important; scroll-behavior: auto !important; @@ -60,6 +64,26 @@ async function settle(page) { ) } +/** A loaded image below the first screen (lazy, decoding="async") isn't painted in a full-page shot until it has been + * on screen: the landing page's gallery figure came out an empty frame. Each image is brought on screen, given a + * moment and decoded, then the page goes back to the top. Run last, after `unstick`. */ +async function paint(page) { + await page.evaluate(async () => { + for (const img of document.images) { + // Hidden ones (the other theme's copy) and those on the first screen need nothing + if (!img.getClientRects().length || img.getBoundingClientRect().bottom <= window.innerHeight) continue + img.decoding = 'sync' + // The window only: scrollIntoView also scrolls the image's own frame (a thumbnail's overflow: hidden box), and the + // shot then showed another part of the picture + window.scrollTo(0, img.getBoundingClientRect().top + window.scrollY - window.innerHeight / 2) + await new Promise((r) => setTimeout(r, 250)) + await img.decode().catch(() => {}) + } + window.scrollTo(0, 0) + }) + await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) +} + /** A full-page shot stitches the page as if the viewport were the whole page tall, so a sticky bar (the settings page's * Save bar, the review bar) is drawn wherever the first viewport ended, over the content there. Sticky elements become * static (they stay in the flow, at their own place in the page) and fixed ones absolute, so nothing covers anything. */ @@ -91,6 +115,7 @@ async function main() { ], }) const contexts = new Map() + const missing = new Set() async function context(variant, signedIn, scale = 1) { const key = `${variant}/${signedIn}/${scale}` if (contexts.has(key)) return contexts.get(key) @@ -104,10 +129,17 @@ async function main() { locale: 'en-US', timezoneId: 'UTC', }) - // The site's figures come from VisualCI's own production gallery, which changes when its screenshots are approved: - // a screenshot of them would follow production. Each is refused here, so the page shows its description in their - // place, the same every run. - await ctx.route('https://app.visualci.com/g/**', (route) => route.fulfill({ status: 404, body: '' })) + // The site's figures come from VisualCI's own production gallery (GalleryImage), which changes when its + // screenshots are approved: a screenshot of them would follow production. Never production here: each gallery + // image is this run's own screenshot of the same page, taken first (the app's pages go before the site's), from + // the seeded app. One this run hasn't taken (a renamed page) fails the capture. + await ctx.route(`${GALLERY}/**`, async (route) => { + const name = decodeURIComponent(new URL(route.request().url()).pathname.slice(GALLERY_PATH.length + 1)) + const file = name.startsWith('screenshots/') ? join(opts.out, name.slice('screenshots/'.length)) : null + const body = file ? await readFile(file).catch(() => null) : null + if (!body) missing.add(name) + await (body ? route.fulfill({ status: 200, contentType: 'image/png', body }) : route.fulfill({ status: 404, body: '' })) + }) await ctx.addInitScript((css) => { document.addEventListener('DOMContentLoaded', () => { const style = document.createElement('style') @@ -139,7 +171,9 @@ async function main() { } let count = 0 - for (const [name, target, signedIn, variants] of pages()) { + // The app's pages first: the site's figures show this run's screenshots of them (the gallery route above). + const ordered = pages().sort(([a], [b]) => Number(!a.startsWith('app/')) - Number(!b.startsWith('app/'))) + for (const [name, target, signedIn, variants] of ordered) { if (opts.only && !name.includes(opts.only)) continue // Each screenshot at 1x (the baseline VisualCI compares). The app's, which the public gallery publishes, again at 2x // (`-2x`) for high-density screens: visualci.com's own pages use it in their srcset. @@ -157,6 +191,7 @@ async function main() { if (!response || response.status() >= 400) throw new Error(`${name}: ${path} gave HTTP ${response?.status()}`) await settle(page) await unstick(page) + await paint(page) const file = join(opts.out, `${name}/${variant}${scale === 2 ? '-2x' : ''}.png`) await mkdir(dirname(file), { recursive: true }) await page.screenshot({ path: file, fullPage: true, animations: 'disabled', caret: 'hide' }) @@ -165,6 +200,8 @@ async function main() { } } await browser.close() + // Only a full run takes every page the site's figures show + if (missing.size && !opts.only) throw new Error(`the site shows gallery images this run didn't take: ${[...missing].join(', ')}`) console.log(`${count} screenshots in ${opts.out}`) }