feat: live timer since rebuild
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
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>/);
|
||||
});
|
||||
@@ -95,6 +95,8 @@ test("Linked articles: the Eleventy posts collection rejects invalid frontmatter
|
||||
addCollection(name, callback) {
|
||||
collections.set(name, callback);
|
||||
},
|
||||
addGlobalData() {},
|
||||
on() {},
|
||||
setServerOptions() {}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user