63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import configureEleventy from "../eleventy.config.js";
|
|
import { formatBuildAge } from "../src/assets/js/build-timer.js";
|
|
|
|
test("Build age: formats elapsed time as a compact live footer value", () => {
|
|
assert.equal(
|
|
formatBuildAge("2026-10-20T12:00:00.000Z", "2026-10-20T12:00:00.000Z"),
|
|
"0s ago"
|
|
);
|
|
assert.equal(
|
|
formatBuildAge("2026-10-20T12:00:00.000Z", "2026-10-20T12:01:01.000Z"),
|
|
"1m 1s ago"
|
|
);
|
|
assert.equal(
|
|
formatBuildAge("2026-10-20T12:00:00.000Z", "2026-10-21T14:03:04.000Z"),
|
|
"1d 2h ago"
|
|
);
|
|
assert.equal(
|
|
formatBuildAge("2026-10-20T12:00:00.000Z", "2026-10-20T11:59:59.000Z"),
|
|
"0s ago"
|
|
);
|
|
});
|
|
|
|
test("Build age: Eleventy exposes one ISO timestamp for each rebuild", () => {
|
|
const globalData = new Map();
|
|
const events = new Map();
|
|
configureEleventy({
|
|
setUseGitIgnore() {},
|
|
addPassthroughCopy() {},
|
|
addPlugin() {},
|
|
setLibrary() {},
|
|
addFilter() {},
|
|
addCollection() {},
|
|
addGlobalData(name, value) {
|
|
globalData.set(name, value);
|
|
},
|
|
on(name, callback) {
|
|
events.set(name, callback);
|
|
},
|
|
setServerOptions() {}
|
|
});
|
|
|
|
events.get("eleventy.before")();
|
|
assert.match(
|
|
globalData.get("buildTimestamp")(),
|
|
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
|
|
);
|
|
});
|
|
|
|
test("Build age: footer provides an accessible live timer backed by the build timestamp", () => {
|
|
const footer = fs.readFileSync(path.resolve("src/_includes/partials/footer.njk"), "utf-8");
|
|
const baseLayout = fs.readFileSync(path.resolve("src/_includes/layouts/base.njk"), "utf-8");
|
|
|
|
assert.match(
|
|
footer,
|
|
/<time id="build-age" data-build-timestamp="{{ buildTimestamp }}" aria-live="polite">REBUILT: INITIALIZING<\/time>/
|
|
);
|
|
assert.match(baseLayout, /<script type="module" src="\/assets\/js\/build-timer\.js"><\/script>/);
|
|
});
|