Implement deterministic website build engine
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { BuildEngine, renderMarkdown } from "./buildEngine";
|
||||
import { BuildEngineInput } from "./buildEngine";
|
||||
import { ContentItem, SiteConfig, ThemeConfig } from "../types";
|
||||
|
||||
const siteConfig: SiteConfig = {
|
||||
protocol: "labyricorn-site/v1",
|
||||
site: {
|
||||
id: "test-site",
|
||||
title: "Test Site",
|
||||
baseUrl: "https://example.test",
|
||||
language: "en-US",
|
||||
},
|
||||
navigation: [
|
||||
{ id: "home", label: "Home", route: "/", iconName: "Home", contentModel: "page" },
|
||||
{ id: "articles", label: "Articles", route: "/articles/", iconName: "FileText", contentModel: "article" },
|
||||
],
|
||||
contentModels: [],
|
||||
sourcesFile: "./sources.yml",
|
||||
styleInstancesPath: "./styles",
|
||||
pagesPath: "./pages",
|
||||
navigationFile: "./navigation.yml",
|
||||
pushIntegrationsPath: "./push",
|
||||
theme: { source: "site-definition", path: "/.theme" },
|
||||
markdown: {
|
||||
dialect: "commonmark",
|
||||
rawHtmlPolicy: "disabled",
|
||||
rawHtmlEnabled: false,
|
||||
extensions: {
|
||||
tables: true,
|
||||
taskLists: true,
|
||||
footnotes: true,
|
||||
definitionLists: true,
|
||||
headingAnchors: true,
|
||||
fencedCode: true,
|
||||
syntaxHighlighting: true,
|
||||
callouts: true,
|
||||
youtube: true,
|
||||
wikipediaLinks: true,
|
||||
},
|
||||
},
|
||||
hosting: {
|
||||
engine: "nginx",
|
||||
production: { enabled: true, hostname: "example.test", listen: 80 },
|
||||
staging: { enabled: true, hostname: "preview.example.test", listen: 8080 },
|
||||
releases: { retainCount: 5 },
|
||||
},
|
||||
buildPolicy: {
|
||||
staging: { enabled: true, requireApproval: true },
|
||||
localActivation: { automatic: false },
|
||||
push: { automatic: false },
|
||||
},
|
||||
rawYaml: "",
|
||||
};
|
||||
|
||||
const themeConfig: ThemeConfig = {
|
||||
id: "test-theme",
|
||||
name: "Test Theme",
|
||||
version: "1.0.0",
|
||||
path: "/.theme",
|
||||
templates: {},
|
||||
styles: [],
|
||||
scripts: [],
|
||||
supportsPackages: [],
|
||||
isValidated: true,
|
||||
validationErrors: [],
|
||||
};
|
||||
|
||||
const content = (overrides: Partial<ContentItem> = {}): ContentItem => ({
|
||||
id: "article-one",
|
||||
title: "Article One",
|
||||
slug: "article-one",
|
||||
published: "2026-01-01T00:00:00.000Z",
|
||||
status: "published",
|
||||
artifactType: "article",
|
||||
summary: "A deterministic article.",
|
||||
tags: ["test"],
|
||||
aliases: [],
|
||||
sourceRepo: "content",
|
||||
path: "articles/article-one.md",
|
||||
contentMarkdown: "# Hello\n\nThis is **safe** markdown.",
|
||||
mediaReferences: [],
|
||||
youtubeDirectives: [],
|
||||
wikipediaLinks: [],
|
||||
validationStatus: "valid",
|
||||
validationMessages: [],
|
||||
route: "/articles/article-one/",
|
||||
styleInstanceId: "default",
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const input = (buildId: string, items: ContentItem[]): BuildEngineInput => ({
|
||||
buildId,
|
||||
siteConfig,
|
||||
contentItems: items,
|
||||
mediaAssets: [],
|
||||
sourceCommits: { content: "abc123" },
|
||||
themeConfig,
|
||||
});
|
||||
|
||||
test("build output is deterministic for equivalent inputs", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "labyricorn-engine-test-"));
|
||||
try {
|
||||
const engine = new BuildEngine(root);
|
||||
const firstItem = content();
|
||||
const secondItem = content({
|
||||
id: "article-two",
|
||||
title: "Article Two",
|
||||
slug: "article-two",
|
||||
path: "articles/article-two.md",
|
||||
route: "/articles/article-two/",
|
||||
});
|
||||
const first = engine.build(input("build-one", [firstItem, secondItem]));
|
||||
const second = engine.build(input("build-two", [secondItem, firstItem]));
|
||||
|
||||
assert.equal(first.success, true);
|
||||
assert.equal(second.success, true);
|
||||
assert.equal(first.artifactChecksum, second.artifactChecksum);
|
||||
assert.deepEqual(first.generatedFiles, second.generatedFiles);
|
||||
assert.ok(fs.existsSync(path.join(first.outputDirectory, "build-manifest.json")));
|
||||
assert.ok(fs.existsSync(path.join(first.outputDirectory, "checksums.json")));
|
||||
assert.match(
|
||||
fs.readFileSync(path.join(first.outputDirectory, "articles/article-one/index.html"), "utf8"),
|
||||
/<h1>Hello<\/h1>/,
|
||||
);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("route collisions fail validation without writing a release", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "labyricorn-engine-test-"));
|
||||
try {
|
||||
const engine = new BuildEngine(root);
|
||||
const result = engine.build(
|
||||
input("collision", [content(), content({ id: "article-two", route: "/articles/article-one/" })]),
|
||||
);
|
||||
|
||||
assert.equal(result.success, false);
|
||||
assert.equal(result.validationReport.summary.passed, false);
|
||||
assert.deepEqual(result.validationReport.routeCollisions, ["/articles/article-one/"]);
|
||||
assert.equal(fs.existsSync(result.outputDirectory), false);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("raw HTML is rejected when the site policy disables it", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "labyricorn-engine-test-"));
|
||||
try {
|
||||
const engine = new BuildEngine(root);
|
||||
const result = engine.build(input("raw-html", [content({ contentMarkdown: "<script>alert(1)</script>" })]));
|
||||
|
||||
assert.equal(result.success, false);
|
||||
assert.equal(result.validationReport.htmlPolicyViolations.length, 1);
|
||||
assert.equal(fs.existsSync(result.outputDirectory), false);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("raw HTML inside fenced code remains a valid code example", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "labyricorn-engine-test-"));
|
||||
try {
|
||||
const engine = new BuildEngine(root);
|
||||
const result = engine.build(
|
||||
input("html-code-example", [content({ contentMarkdown: "```html\n<div>example</div>\n```" })]),
|
||||
);
|
||||
assert.equal(result.success, true);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("markdown renderer escapes source HTML", () => {
|
||||
assert.equal(renderMarkdown("Text <img src=x>"), "<p>Text <img src=x></p>");
|
||||
});
|
||||
Reference in New Issue
Block a user