Skip to content

Commit ef8a7a9

Browse files
authored
Gallery: the Astro component, visualci-astro-gallery (sc-9306) (#193)
* Gallery: the Astro component visualci-astro-gallery (sc-9306) <Gallery manifest=… /> fetches the manifest when the site builds, renders the contract's markup with visualci-gallery's render(), and brings the default stylesheet and a module script that hydrates every gallery on the page. A manifest that can't be read or has nothing to show fails the build. The props are the library's options and load()'s. clients/gallery is now a pnpm workspace (js, astro). Tests: every golden fixture through Astro's container API, the two failures, and a real astro build of a small site whose page carries the golden markup, a stylesheet and the script as files (no inline scripts). * Gallery: release workflow for the npm packages Publishes visualci-gallery and visualci-astro-gallery from CI with provenance for a gallery-js-v* tag, with the NPM_TOKEN secret until npm trusted publishing is set up. The component depends on visualci-gallery with workspace:^, so it's published as a caret range. * Gallery: the Astro build test passes the fixture's options too The fleetlayer fixture's Mac approver tab needs its groups option, so the test site reads the whole options.json rather than copying some of it. * Gallery: the Astro component hydrates again after a view-transition navigation Under Astro's ClientRouter the component's script runs once per document, so a gallery on the next page stayed dead. It now hydrates on astro:page-load too (hydrate ignores a gallery it already has) and destroys the old page's galleries on astro:before-swap, so their listeners don't leak.
1 parent 8d38af4 commit ef8a7a9

19 files changed

Lines changed: 3595 additions & 1437 deletions

‎.github/workflows/ci.yml‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ jobs:
227227
working-directory: frontend
228228
env:
229229
GITHUB_TOKEN: ${{ github.token }} # the docs' CLI release (scripts/cli-version.mjs)
230-
- run: pnpm install --frozen-lockfile && pnpm build
231-
working-directory: clients/gallery/js
230+
- run: pnpm install --frozen-lockfile && pnpm --filter visualci-gallery build
231+
working-directory: clients/gallery
232232
- uses: ./.github/actions/playwright
233233
with:
234234
save: ${{ github.ref == 'refs/heads/main' }}
@@ -432,10 +432,11 @@ jobs:
432432
- uses: actions/setup-node@v4
433433
with:
434434
node-version: "24"
435-
# The gallery library (clients/gallery): its type check, its build (the ES5 file's test reads it) and its tests,
436-
# which check the contract's golden fixtures.
437-
- run: pnpm install --frozen-lockfile && pnpm typecheck && pnpm build && pnpm test
438-
working-directory: clients/gallery/js
435+
# The gallery (clients/gallery, a pnpm workspace): the library's type check and build (the ES5 file's test reads
436+
# it), then every package's tests, which check the contract's golden fixtures (the Astro component's include a
437+
# real astro build).
438+
- run: pnpm install --frozen-lockfile && pnpm --filter visualci-gallery typecheck && pnpm build && pnpm test
439+
working-directory: clients/gallery
439440
# The Compare two PDFs page runs crates/visualci-wasm in the browser (scripts/wasm.mjs builds it).
440441
- run: rustup target add wasm32-unknown-unknown
441442
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Publish the gallery's npm packages (clients/gallery): visualci-gallery (the browser library) and
2+
# visualci-astro-gallery (the Astro component), with npm provenance. Runs for a gallery-js-v* tag, which must match
3+
# both packages' versions, or by hand. pnpm turns the component's workspace:^ dependency into ^<version>.
4+
#
5+
# The token is temporary (Nathan's Tasks sc-9327). The first publish of both packages uses the NPM_TOKEN repository
6+
# secret, a short-lived granular token also kept in 1Password (visualci-npm-publish-token). After that, both packages
7+
# switch to npm trusted publishing (OIDC from this workflow, as RubyGems and PyPI do), NPM_TOKEN is deleted, and the
8+
# token is revoked on npm. Without the secret, npm publishes through OIDC.
9+
name: Release gallery npm packages
10+
11+
on:
12+
push:
13+
tags: ["gallery-js-v*"]
14+
workflow_dispatch:
15+
16+
permissions:
17+
contents: read
18+
id-token: write
19+
20+
jobs:
21+
publish:
22+
runs-on: ubuntu-latest
23+
timeout-minutes: 10
24+
defaults:
25+
run:
26+
working-directory: clients/gallery
27+
steps:
28+
- uses: actions/checkout@v4
29+
- uses: pnpm/action-setup@v4
30+
with:
31+
package_json_file: clients/gallery/package.json
32+
- uses: actions/setup-node@v4
33+
with:
34+
node-version: "24"
35+
registry-url: "https://registry.npmjs.org"
36+
- name: The tag matches both packages' versions
37+
if: startsWith(github.ref, 'refs/tags/gallery-js-v')
38+
run: |
39+
for p in js astro; do
40+
version=$(node -p "require('./$p/package.json').version")
41+
test "gallery-js-v$version" = "$GITHUB_REF_NAME" || { echo "tag $GITHUB_REF_NAME, $p $version"; exit 1; }
42+
done
43+
- run: pnpm install --frozen-lockfile && pnpm build && pnpm test
44+
- run: pnpm -r publish --access public --provenance --no-git-checks
45+
env:
46+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

‎clients/gallery/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ variables.
77
- [`contract/`](contract/SPEC.md): how a manifest becomes a gallery (pairing, defaults, the exact HTML) and the golden
88
fixtures every renderer's tests check.
99
- [`js/`](js/README.md): `visualci-gallery`, the browser library and reference renderer.
10+
- [`astro/`](astro/README.md): `visualci-astro-gallery`, rendered when the site builds and hydrated by the library.
1011

11-
Coming: the Astro component, the Ruby gem, the Python package and the Rust crate, each emitting the contract's markup
12-
for the JS library to hydrate.
12+
A pnpm workspace: `pnpm install && pnpm build && pnpm test` here. Coming: the Ruby gem, the Python package and the Rust
13+
crate, each emitting the contract's markup for the JS library to hydrate.

‎clients/gallery/astro/.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/site/dist/
2+
test/site/.astro/

‎clients/gallery/astro/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# visualci-astro-gallery
2+
3+
An Astro component for a screenshots page from a VisualCI [public gallery](https://visualci.com/docs/gallery/)
4+
manifest. It fetches the manifest when the site builds, renders the [gallery contract](../contract/SPEC.md)'s markup,
5+
and ships `visualci-gallery`'s stylesheet and the script that hydrates it (tabs, theme and size switches, the
6+
full-size view). A manifest that can't be read, or that has nothing to show, fails the build.
7+
8+
```astro
9+
---
10+
import Gallery from "visualci-astro-gallery";
11+
---
12+
<Gallery
13+
manifest="https://app.visualci.com/g/acme/web.json"
14+
title={null}
15+
screens={{ overview: { title: "Overview", caption: "Everything at a glance." } }}
16+
order={["overview"]}
17+
/>
18+
```
19+
20+
The props are `visualci-gallery`'s options (`title`, `headingLevel`, `id`, `screens`, `order`, `groups`,
21+
`groupOrder`, `credit`, `layout`, `themeSwitch`, `seeAll`) and `load()`'s (`manifest`, `timeout`, `attempts`,
22+
`fetch`). Give the gallery your site's look with the `--vg-*` CSS variables on `.vg` or an ancestor:
23+
24+
```css
25+
.vg {
26+
--vg-ink: var(--color-ink);
27+
--vg-muted: var(--color-muted);
28+
--vg-paper: var(--color-paper);
29+
--vg-paper-2: var(--color-paper-2);
30+
--vg-rule: var(--color-rule);
31+
--vg-accent: var(--color-accent);
32+
}
33+
```

‎clients/gallery/astro/package.json‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "visualci-astro-gallery",
3+
"version": "0.1.0",
4+
"description": "An Astro component for a screenshots page from VisualCI's public gallery: rendered at build time, hydrated by visualci-gallery.",
5+
"license": "Apache-2.0",
6+
"homepage": "https://visualci.com/docs/gallery/",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/DocSpring/visualci.git",
10+
"directory": "clients/gallery/astro"
11+
},
12+
"type": "module",
13+
"packageManager": "pnpm@10.12.4",
14+
"exports": {
15+
".": "./src/index.ts",
16+
"./Gallery.astro": "./src/Gallery.astro"
17+
},
18+
"files": [
19+
"src",
20+
"README.md"
21+
],
22+
"keywords": [
23+
"astro-component",
24+
"screenshots",
25+
"gallery",
26+
"visualci"
27+
],
28+
"scripts": {
29+
"test": "vitest run"
30+
},
31+
"peerDependencies": {
32+
"astro": ">=5"
33+
},
34+
"dependencies": {
35+
"visualci-gallery": "workspace:^"
36+
},
37+
"devDependencies": {
38+
"astro": "^7.4.14",
39+
"vitest": "^4.1.12"
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
// A screenshots page from a VisualCI public gallery manifest (https://visualci.com/docs/gallery/): fetched and
3+
// rendered when the site builds, as the gallery contract's markup (clients/gallery/contract/SPEC.md), and hydrated by
4+
// visualci-gallery in the browser. A manifest that can't be read, or that has nothing to show, fails the build.
5+
//
6+
// <Gallery manifest="https://app.visualci.com/g/acme/web.json" screens={{ overview: { title: "Overview" } }} />
7+
//
8+
// The props are the library's options (title, headingLevel, id, screens, order, groups, groupOrder, credit, layout,
9+
// themeSwitch, seeAll) and load()'s (manifest, timeout, attempts, fetch). The default stylesheet comes with it; set the
10+
// --vg-* variables on .vg (or an ancestor) to give it the site's look.
11+
import { load, render, type LoadOptions } from "visualci-gallery/render";
12+
import "visualci-gallery/gallery.css";
13+
14+
export type Props = LoadOptions;
15+
16+
const html = render(await load(Astro.props));
17+
---
18+
19+
<Fragment set:html={html} />
20+
21+
<script>
22+
// Every gallery on the page, once (hydrate does nothing on one it already has). With Astro's view transitions the
23+
// script runs once per document, so it hydrates again after each navigation and lets the old page's galleries go.
24+
import { hydrate, type Gallery } from "visualci-gallery";
25+
26+
const live = new Set<Gallery>();
27+
const all = () => {
28+
for (const el of document.querySelectorAll<HTMLElement>("[data-vg]")) live.add(hydrate(el));
29+
};
30+
all();
31+
document.addEventListener("astro:page-load", all);
32+
document.addEventListener("astro:before-swap", () => {
33+
for (const g of live) g.destroy();
34+
live.clear();
35+
});
36+
</script>

‎clients/gallery/astro/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// visualci-astro-gallery: `import Gallery from "visualci-astro-gallery"`.
2+
export { default } from "./Gallery.astro";
3+
export type { Props } from "./Gallery.astro";
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// A real `astro build` of a site using the component (test/site): the page has the contract's markup, the default
2+
// stylesheet and the script that hydrates it, as files (no inline script, for sites with a strict CSP).
3+
import { execFileSync } from "node:child_process";
4+
import { readdirSync, readFileSync } from "node:fs";
5+
import { join } from "node:path";
6+
import { expect, it } from "vitest";
7+
8+
const site = join(import.meta.dirname, "site");
9+
const fixture = join(import.meta.dirname, "../../contract/fixtures/fleetlayer");
10+
11+
it("builds a page with the markup, the stylesheet and the hydrating script", () => {
12+
// The CLI in its own process: Astro's build doesn't run inside Vitest's Vite.
13+
execFileSync(join(import.meta.dirname, "../node_modules/.bin/astro"), ["build", "--root", site], {
14+
stdio: "pipe",
15+
env: { ...process.env, GALLERY_MANIFEST: join(fixture, "manifest.json"), GALLERY_OPTIONS: join(fixture, "options.json") },
16+
});
17+
const dist = join(site, "dist");
18+
const page = readFileSync(join(dist, "index.html"), "utf8");
19+
const golden = readFileSync(join(fixture, "gallery.html"), "utf8");
20+
expect(page).toContain(golden.trim());
21+
const assets = readdirSync(join(dist, "_astro"));
22+
const css = assets.filter((f) => f.endsWith(".css")).map((f) => readFileSync(join(dist, "_astro", f), "utf8")).join("");
23+
expect(css).toContain(".vg-grid");
24+
expect(page).toMatch(/<link rel="stylesheet" href="\/_astro\/[^"]+\.css"/);
25+
const script = /<script type="module" src="(\/_astro\/[^"]+\.js)"><\/script>/.exec(page);
26+
expect(script, "the hydrating script, as a file").not.toBeNull();
27+
expect(readFileSync(join(dist, script![1]), "utf8")).toContain("data-vg");
28+
expect(page).not.toMatch(/<script(?![^>]*\bsrc=)[^>]*>/);
29+
}, 60_000);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// The component's markup is the contract's: for each golden fixture, rendering <Gallery> with the fixture's manifest
2+
// (served by a stub fetch) and options gives exactly its gallery.html. A manifest that can't be read fails the render,
3+
// so the site's build fails.
4+
import { experimental_AstroContainer as AstroContainer } from "astro/container";
5+
import { readdirSync, readFileSync } from "node:fs";
6+
import { join } from "node:path";
7+
import { describe, expect, it } from "vitest";
8+
import Gallery from "../src/Gallery.astro";
9+
10+
const fixtures = join(import.meta.dirname, "../../contract/fixtures");
11+
const serve = (body: string, status = 200) => async () => new Response(body, { status });
12+
13+
describe("Gallery", () => {
14+
for (const name of readdirSync(fixtures).sort()) {
15+
it(`renders the ${name} fixture's markup`, async () => {
16+
const dir = join(fixtures, name);
17+
const container = await AstroContainer.create();
18+
const html = await container.renderToString(Gallery, {
19+
props: {
20+
...JSON.parse(readFileSync(join(dir, "options.json"), "utf8")),
21+
manifest: "https://app.visualci.com/g/acme/web.json",
22+
fetch: serve(readFileSync(join(dir, "manifest.json"), "utf8")),
23+
},
24+
});
25+
expect(html).toContain(readFileSync(join(dir, "gallery.html"), "utf8"));
26+
});
27+
}
28+
29+
it("fails when the manifest can't be read", async () => {
30+
const container = await AstroContainer.create();
31+
await expect(
32+
container.renderToString(Gallery, {
33+
props: { manifest: "https://app.visualci.com/g/acme/web.json", attempts: 1, fetch: serve("", 503) },
34+
}),
35+
).rejects.toThrow("The screenshots gallery couldn't read https://app.visualci.com/g/acme/web.json: HTTP 503");
36+
});
37+
38+
it("fails when the manifest has nothing to show", async () => {
39+
const container = await AstroContainer.create();
40+
await expect(
41+
container.renderToString(Gallery, {
42+
props: { manifest: "https://app.visualci.com/g/acme/web.json", fetch: serve('{"groups":[]}') },
43+
}),
44+
).rejects.toThrow(/can't show/);
45+
});
46+
});

0 commit comments

Comments
 (0)