Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ jobs:
working-directory: frontend
env:
GITHUB_TOKEN: ${{ github.token }} # the docs' CLI release (scripts/cli-version.mjs)
- run: pnpm install --frozen-lockfile && pnpm build
working-directory: clients/gallery/js
- run: pnpm install --frozen-lockfile && pnpm --filter visualci-gallery build
working-directory: clients/gallery
- uses: ./.github/actions/playwright
with:
save: ${{ github.ref == 'refs/heads/main' }}
Expand Down Expand Up @@ -432,10 +432,11 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: "24"
# The gallery library (clients/gallery): its type check, its build (the ES5 file's test reads it) and its tests,
# which check the contract's golden fixtures.
- run: pnpm install --frozen-lockfile && pnpm typecheck && pnpm build && pnpm test
working-directory: clients/gallery/js
# The gallery (clients/gallery, a pnpm workspace): the library's type check and build (the ES5 file's test reads
# it), then every package's tests, which check the contract's golden fixtures (the Astro component's include a
# real astro build).
- run: pnpm install --frozen-lockfile && pnpm --filter visualci-gallery typecheck && pnpm build && pnpm test
working-directory: clients/gallery
# The Compare two PDFs page runs crates/visualci-wasm in the browser (scripts/wasm.mjs builds it).
- run: rustup target add wasm32-unknown-unknown
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/release-gallery-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Publish the gallery's npm packages (clients/gallery): visualci-gallery (the browser library) and
# visualci-astro-gallery (the Astro component), with npm provenance. Runs for a gallery-js-v* tag, which must match
# both packages' versions, or by hand. pnpm turns the component's workspace:^ dependency into ^<version>.
#
# The token is temporary (Nathan's Tasks sc-9327). The first publish of both packages uses the NPM_TOKEN repository
# secret, a short-lived granular token also kept in 1Password (visualci-npm-publish-token). After that, both packages
# switch to npm trusted publishing (OIDC from this workflow, as RubyGems and PyPI do), NPM_TOKEN is deleted, and the
# token is revoked on npm. Without the secret, npm publishes through OIDC.
name: Release gallery npm packages

on:
push:
tags: ["gallery-js-v*"]
workflow_dispatch:

permissions:
contents: read
id-token: write

jobs:
publish:
runs-on: ubuntu-latest
timeout-minutes: 10
defaults:
run:
working-directory: clients/gallery
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
package_json_file: clients/gallery/package.json
- uses: actions/setup-node@v4
with:
node-version: "24"
registry-url: "https://registry.npmjs.org"
- name: The tag matches both packages' versions
if: startsWith(github.ref, 'refs/tags/gallery-js-v')
run: |
for p in js astro; do
version=$(node -p "require('./$p/package.json').version")
test "gallery-js-v$version" = "$GITHUB_REF_NAME" || { echo "tag $GITHUB_REF_NAME, $p $version"; exit 1; }
done
- run: pnpm install --frozen-lockfile && pnpm build && pnpm test
- run: pnpm -r publish --access public --provenance --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
5 changes: 3 additions & 2 deletions clients/gallery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ variables.
- [`contract/`](contract/SPEC.md): how a manifest becomes a gallery (pairing, defaults, the exact HTML) and the golden
fixtures every renderer's tests check.
- [`js/`](js/README.md): `visualci-gallery`, the browser library and reference renderer.
- [`astro/`](astro/README.md): `visualci-astro-gallery`, rendered when the site builds and hydrated by the library.

Coming: the Astro component, the Ruby gem, the Python package and the Rust crate, each emitting the contract's markup
for the JS library to hydrate.
A pnpm workspace: `pnpm install && pnpm build && pnpm test` here. Coming: the Ruby gem, the Python package and the Rust
crate, each emitting the contract's markup for the JS library to hydrate.
2 changes: 2 additions & 0 deletions clients/gallery/astro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/site/dist/
test/site/.astro/
33 changes: 33 additions & 0 deletions clients/gallery/astro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# visualci-astro-gallery

An Astro component for a screenshots page from a VisualCI [public gallery](https://visualci.com/docs/gallery/)
manifest. It fetches the manifest when the site builds, renders the [gallery contract](../contract/SPEC.md)'s markup,
and ships `visualci-gallery`'s stylesheet and the script that hydrates it (tabs, theme and size switches, the
full-size view). A manifest that can't be read, or that has nothing to show, fails the build.

```astro
---
import Gallery from "visualci-astro-gallery";
---
<Gallery
manifest="https://app.visualci.com/g/acme/web.json"
title={null}
screens={{ overview: { title: "Overview", caption: "Everything at a glance." } }}
order={["overview"]}
/>
```

The props are `visualci-gallery`'s options (`title`, `headingLevel`, `id`, `screens`, `order`, `groups`,
`groupOrder`, `credit`, `layout`, `themeSwitch`, `seeAll`) and `load()`'s (`manifest`, `timeout`, `attempts`,
`fetch`). Give the gallery your site's look with the `--vg-*` CSS variables on `.vg` or an ancestor:

```css
.vg {
--vg-ink: var(--color-ink);
--vg-muted: var(--color-muted);
--vg-paper: var(--color-paper);
--vg-paper-2: var(--color-paper-2);
--vg-rule: var(--color-rule);
--vg-accent: var(--color-accent);
}
```
41 changes: 41 additions & 0 deletions clients/gallery/astro/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "visualci-astro-gallery",
"version": "0.1.0",
"description": "An Astro component for a screenshots page from VisualCI's public gallery: rendered at build time, hydrated by visualci-gallery.",
"license": "Apache-2.0",
"homepage": "https://visualci.com/docs/gallery/",
"repository": {
"type": "git",
"url": "git+https://github.com/DocSpring/visualci.git",
"directory": "clients/gallery/astro"
},
"type": "module",
"packageManager": "pnpm@10.12.4",
"exports": {
".": "./src/index.ts",
"./Gallery.astro": "./src/Gallery.astro"
},
"files": [
"src",
"README.md"
],
"keywords": [
"astro-component",
"screenshots",
"gallery",
"visualci"
],
"scripts": {
"test": "vitest run"
},
"peerDependencies": {
"astro": ">=5"
},
"dependencies": {
"visualci-gallery": "workspace:^"
},
"devDependencies": {
"astro": "^7.4.14",
"vitest": "^4.1.12"
}
}
36 changes: 36 additions & 0 deletions clients/gallery/astro/src/Gallery.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
// A screenshots page from a VisualCI public gallery manifest (https://visualci.com/docs/gallery/): fetched and
// rendered when the site builds, as the gallery contract's markup (clients/gallery/contract/SPEC.md), and hydrated by
// visualci-gallery in the browser. A manifest that can't be read, or that has nothing to show, fails the build.
//
// <Gallery manifest="https://app.visualci.com/g/acme/web.json" screens={{ overview: { title: "Overview" } }} />
//
// The props are the library's options (title, headingLevel, id, screens, order, groups, groupOrder, credit, layout,
// themeSwitch, seeAll) and load()'s (manifest, timeout, attempts, fetch). The default stylesheet comes with it; set the
// --vg-* variables on .vg (or an ancestor) to give it the site's look.
import { load, render, type LoadOptions } from "visualci-gallery/render";
import "visualci-gallery/gallery.css";

export type Props = LoadOptions;

const html = render(await load(Astro.props));
---

<Fragment set:html={html} />

<script>
// Every gallery on the page, once (hydrate does nothing on one it already has). With Astro's view transitions the
// script runs once per document, so it hydrates again after each navigation and lets the old page's galleries go.
import { hydrate, type Gallery } from "visualci-gallery";

const live = new Set<Gallery>();
const all = () => {
for (const el of document.querySelectorAll<HTMLElement>("[data-vg]")) live.add(hydrate(el));
};
all();
document.addEventListener("astro:page-load", all);
document.addEventListener("astro:before-swap", () => {
for (const g of live) g.destroy();
live.clear();
});
</script>
3 changes: 3 additions & 0 deletions clients/gallery/astro/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// visualci-astro-gallery: `import Gallery from "visualci-astro-gallery"`.
export { default } from "./Gallery.astro";
export type { Props } from "./Gallery.astro";
29 changes: 29 additions & 0 deletions clients/gallery/astro/test/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// A real `astro build` of a site using the component (test/site): the page has the contract's markup, the default
// stylesheet and the script that hydrates it, as files (no inline script, for sites with a strict CSP).
import { execFileSync } from "node:child_process";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { expect, it } from "vitest";

const site = join(import.meta.dirname, "site");
const fixture = join(import.meta.dirname, "../../contract/fixtures/fleetlayer");

it("builds a page with the markup, the stylesheet and the hydrating script", () => {
// The CLI in its own process: Astro's build doesn't run inside Vitest's Vite.
execFileSync(join(import.meta.dirname, "../node_modules/.bin/astro"), ["build", "--root", site], {
stdio: "pipe",
env: { ...process.env, GALLERY_MANIFEST: join(fixture, "manifest.json"), GALLERY_OPTIONS: join(fixture, "options.json") },
});
const dist = join(site, "dist");
const page = readFileSync(join(dist, "index.html"), "utf8");
const golden = readFileSync(join(fixture, "gallery.html"), "utf8");
expect(page).toContain(golden.trim());
const assets = readdirSync(join(dist, "_astro"));
const css = assets.filter((f) => f.endsWith(".css")).map((f) => readFileSync(join(dist, "_astro", f), "utf8")).join("");
expect(css).toContain(".vg-grid");
expect(page).toMatch(/<link rel="stylesheet" href="\/_astro\/[^"]+\.css"/);
const script = /<script type="module" src="(\/_astro\/[^"]+\.js)"><\/script>/.exec(page);
expect(script, "the hydrating script, as a file").not.toBeNull();
expect(readFileSync(join(dist, script![1]), "utf8")).toContain("data-vg");
expect(page).not.toMatch(/<script(?![^>]*\bsrc=)[^>]*>/);
}, 60_000);
46 changes: 46 additions & 0 deletions clients/gallery/astro/test/gallery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// The component's markup is the contract's: for each golden fixture, rendering <Gallery> with the fixture's manifest
// (served by a stub fetch) and options gives exactly its gallery.html. A manifest that can't be read fails the render,
// so the site's build fails.
import { experimental_AstroContainer as AstroContainer } from "astro/container";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import Gallery from "../src/Gallery.astro";

const fixtures = join(import.meta.dirname, "../../contract/fixtures");
const serve = (body: string, status = 200) => async () => new Response(body, { status });

describe("Gallery", () => {
for (const name of readdirSync(fixtures).sort()) {
it(`renders the ${name} fixture's markup`, async () => {
const dir = join(fixtures, name);
const container = await AstroContainer.create();
const html = await container.renderToString(Gallery, {
props: {
...JSON.parse(readFileSync(join(dir, "options.json"), "utf8")),
manifest: "https://app.visualci.com/g/acme/web.json",
fetch: serve(readFileSync(join(dir, "manifest.json"), "utf8")),
},
});
expect(html).toContain(readFileSync(join(dir, "gallery.html"), "utf8"));
});
}

it("fails when the manifest can't be read", async () => {
const container = await AstroContainer.create();
await expect(
container.renderToString(Gallery, {
props: { manifest: "https://app.visualci.com/g/acme/web.json", attempts: 1, fetch: serve("", 503) },
}),
).rejects.toThrow("The screenshots gallery couldn't read https://app.visualci.com/g/acme/web.json: HTTP 503");
});

it("fails when the manifest has nothing to show", async () => {
const container = await AstroContainer.create();
await expect(
container.renderToString(Gallery, {
props: { manifest: "https://app.visualci.com/g/acme/web.json", fetch: serve('{"groups":[]}') },
}),
).rejects.toThrow(/can't show/);
});
});
3 changes: 3 additions & 0 deletions clients/gallery/astro/test/site/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "astro/config";

export default defineConfig({ logLevel: "warn" });
17 changes: 17 additions & 0 deletions clients/gallery/astro/test/site/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
// A site that uses the component, built by test/build.test.ts: the manifest and options are the contract's fleetlayer
// fixture (the test names the files in GALLERY_MANIFEST and GALLERY_OPTIONS), the manifest served by a stub fetch.
import { readFileSync } from "node:fs";
import Gallery from "../../../../src/Gallery.astro";

const manifest = readFileSync(process.env.GALLERY_MANIFEST!, "utf8");
const options = JSON.parse(readFileSync(process.env.GALLERY_OPTIONS!, "utf8"));
const fetch = async () => new Response(manifest);
---

<html lang="en">
<head><meta charset="utf-8" /><title>Screenshots</title></head>
<body>
<Gallery manifest="https://app.visualci.com/g/acme/web.json" fetch={fetch} {...options} />
</body>
</html>
4 changes: 4 additions & 0 deletions clients/gallery/astro/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// <reference types="vitest/config" />
import { getViteConfig } from "astro/config";

export default getViteConfig({ test: { include: ["test/**/*.test.ts"] } });
2 changes: 1 addition & 1 deletion clients/gallery/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ your site.

## Development

`pnpm build && pnpm test`. The tests check the contract's golden fixtures; `UPDATE_GOLDEN=1 pnpm test` rewrites them
`pnpm build && pnpm test` (after `pnpm install` in `clients/gallery`, the workspace). The tests check the contract's golden fixtures; `UPDATE_GOLDEN=1 pnpm test` rewrites them
(review the diff).
Loading
Loading