Public: anyone can read this; the crew can change it.
The renderers nothing tests by name get a test: toml-config, yaml-config, html_preview, gcode-css, model3d-css, model3d-client #12708
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12678-the-renderers-nothing-tests-by-name-get → main 1 commit fork
3 files
changed +222
−0
test/render-css-client.test.ts added +67
| @@ -0,0 +1,67 @@ | ||
| 1 | /** | |
| 2 | * The renderers' styles and the 3D viewer by name (#12678): `gcode-css.ts` | |
| 3 | * and `model3d-css.ts` are static text that loads nothing from anywhere, and | |
| 4 | * `model3d-client.ts`, bundled with three.js, stays within the size the | |
| 5 | * feature's page states for it (`docs/features/file-media.md`). | |
| 6 | */ | |
| 7 | ||
| 8 | import { describe, expect, test } from "bun:test"; | |
| 9 | import { readFileSync } from "node:fs"; | |
| 10 | import { gcodeCss } from "../src/web/render/gcode-css.ts"; | |
| 11 | import { model3dCss } from "../src/web/render/model3d-css.ts"; | |
| 12 | import { model3dAsset } from "../src/web/render/model3d.ts"; | |
| 13 | ||
| 14 | const SHEETS = { gcode: gcodeCss, model3d: model3dCss } as const; | |
| 15 | ||
| 16 | /** Every selector of a sheet, outside at-rules. */ | |
| 17 | const selectors = (css: string): string[] => | |
| 18 | [...css.matchAll(/([^{}]+)\{/g)] | |
| 19 | .map((m) => m[1]!.trim()) | |
| 20 | .filter((s) => !s.startsWith("@")) | |
| 21 | .flatMap((s) => s.split(",").map((x) => x.trim())); | |
| 22 | ||
| 23 | describe.each(Object.entries(SHEETS))("%s-css", (name, css) => { | |
| 24 | test("is static text: no url(, no @import, no interpolation left over", () => { | |
| 25 | expect(typeof css).toBe("string"); | |
| 26 | expect(css).not.toMatch(/url\s*\(/i); | |
| 27 | expect(css).not.toMatch(/@import/i); | |
| 28 | expect(css).not.toMatch(/\/\/[a-z0-9.-]+\.[a-z]{2,}/i); | |
| 29 | expect(css).not.toContain("${"); | |
| 30 | }); | |
| 31 | ||
| 32 | test("sits in the content layer and styles only its own classes", () => { | |
| 33 | expect(css.trim()).toStartWith("@layer content {"); | |
| 34 | const sel = selectors(css); | |
| 35 | expect(sel.length).toBeGreaterThan(3); | |
| 36 | for (const s of sel) expect([s, s.startsWith(`.${name}`)]).toEqual([s, true]); | |
| 37 | }); | |
| 38 | ||
| 39 | test("its braces balance", () => { | |
| 40 | expect(css.split("{").length).toBe(css.split("}").length); | |
| 41 | }); | |
| 42 | }); | |
| 43 | ||
| 44 | describe("model3d-client", () => { | |
| 45 | /** "(648 KB, 166 KB gzipped" in the feature page: the stated size is the budget. */ | |
| 46 | const stated = (): { raw: number; gzip: number } => { | |
| 47 | const doc = readFileSync(new URL("../docs/features/file-media.md", import.meta.url), "utf8").replace(/\s+/g, " "); | |
| 48 | const m = /model3d-<hash>\.js` \((\d+) KB, (\d+) KB gzipped/.exec(doc); | |
| 49 | expect(m).not.toBeNull(); | |
| 50 | return { raw: Number(m![1]) * 1024, gzip: Number(m![2]) * 1024 }; | |
| 51 | }; | |
| 52 | ||
| 53 | test("the bundle stays within the size its page states", async () => { | |
| 54 | const asset = await model3dAsset(); | |
| 55 | expect(asset).not.toBeNull(); | |
| 56 | const bytes = new TextEncoder().encode(asset!.code); | |
| 57 | const budget = stated(); | |
| 58 | expect(bytes.length).toBeLessThanOrEqual(budget.raw); | |
| 59 | expect(Bun.gzipSync(bytes).length).toBeLessThanOrEqual(budget.gzip); | |
| 60 | }); | |
| 61 | ||
| 62 | test("the bundle loads nothing from another origin", async () => { | |
| 63 | const code = (await model3dAsset())!.code; | |
| 64 | expect(code).not.toMatch(/import\s*\(?\s*["']https?:/); | |
| 65 | expect(code).not.toMatch(/from\s*["']https?:/); | |
| 66 | }); | |
| 67 | }); |
test/render-html-preview.test.ts added +67
| @@ -0,0 +1,67 @@ | ||
| 1 | /** | |
| 2 | * `html_preview.ts` by name (#12678): the frame it draws is sandboxed with no | |
| 3 | * `allow-*` token, points at the media route, and never carries the file's | |
| 4 | * bytes; the media route's policy for html (`htmlPolicy`) is a sandbox with | |
| 5 | * no script source, so the file's script runs neither on the forge's origin | |
| 6 | * nor in the frame. The route end to end is `test/html_preview.test.ts`. | |
| 7 | */ | |
| 8 | ||
| 9 | import { describe, expect, test } from "bun:test"; | |
| 10 | import { htmlPolicy } from "../src/web/pages/media.ts"; | |
| 11 | import { htmlPreview } from "../src/web/render/html_preview.ts"; | |
| 12 | ||
| 13 | const PAGE = '<!doctype html><p>hi</p><script>alert("xss-marker")</script><img src=x onerror="alert(1)">'; | |
| 14 | ||
| 15 | const draw = (path = "web/index.html", body = PAGE) => { | |
| 16 | const bytes = new TextEncoder().encode(body); | |
| 17 | return htmlPreview({ path, size: bytes.length, body: bytes, media: `/alice/site/media/main/${path}`, download: "/d" }); | |
| 18 | }; | |
| 19 | ||
| 20 | describe("the frame", () => { | |
| 21 | test("sandboxed with no allow token, onto the media route, no referrer", () => { | |
| 22 | const html = String(draw().content); | |
| 23 | expect(html).toContain('sandbox=""'); | |
| 24 | expect(html).not.toContain("allow-"); | |
| 25 | expect(html).toContain('src="/alice/site/media/main/web/index.html"'); | |
| 26 | expect(html).toContain('referrerpolicy="no-referrer"'); | |
| 27 | expect(html.match(/<iframe/g)?.length).toBe(1); | |
| 28 | }); | |
| 29 | ||
| 30 | test("the file's script text is never in the page's markup", () => { | |
| 31 | const html = String(draw().content); | |
| 32 | expect(html).not.toContain("xss-marker"); | |
| 33 | expect(html).not.toContain("<script"); | |
| 34 | expect(html).not.toContain("onerror"); | |
| 35 | }); | |
| 36 | ||
| 37 | test("a hostile path is escaped in the title and the address", () => { | |
| 38 | const html = String(draw('x"><script>alert(2)</script>.html').content); | |
| 39 | expect(html).not.toContain("<script>"); | |
| 40 | expect(html).toContain("<script>"); | |
| 41 | expect(html.match(/<iframe/g)?.length).toBe(1); | |
| 42 | }); | |
| 43 | ||
| 44 | test("the summary names the size and the kind", () => { | |
| 45 | expect(draw().summary).toMatch(/· HTML$/); | |
| 46 | expect(draw().lines).toBe(0); | |
| 47 | }); | |
| 48 | }); | |
| 49 | ||
| 50 | describe("the policy the media route serves it under", () => { | |
| 51 | const policy = htmlPolicy("https://forge.example/alice/site/media/main/"); | |
| 52 | const directives = policy.split(";").map((d) => d.trim()); | |
| 53 | ||
| 54 | test("a bare sandbox: an opaque origin, no scripts, no same-origin", () => { | |
| 55 | expect(directives[0]).toBe("sandbox"); | |
| 56 | expect(policy).not.toContain("allow-"); | |
| 57 | expect(directives).toContain("default-src 'none'"); | |
| 58 | expect(policy).not.toContain("script-src"); | |
| 59 | }); | |
| 60 | ||
| 61 | test("loads only from the file's own ref, framed only by the forge", () => { | |
| 62 | expect(directives).toContain("img-src https://forge.example/alice/site/media/main/ data:"); | |
| 63 | expect(directives).toContain("style-src 'unsafe-inline' https://forge.example/alice/site/media/main/"); | |
| 64 | expect(directives).toContain("frame-ancestors 'self'"); | |
| 65 | expect(policy).not.toMatch(/(?:img|style)-src[^;]*'self'/); | |
| 66 | }); | |
| 67 | }); |
test/render-toml-yaml.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * The two config readers by name (#12678): `toml-config.ts` and | |
| 3 | * `yaml-config.ts` read a file into the form's `Json`, and a file they cannot | |
| 4 | * read comes back as an error with its line, so the page falls back to text. | |
| 5 | */ | |
| 6 | ||
| 7 | import { describe, expect, test } from "bun:test"; | |
| 8 | import { parseToml } from "../src/web/render/toml-config.ts"; | |
| 9 | import { parseYaml } from "../src/web/render/yaml-config.ts"; | |
| 10 | import type { Json } from "../src/web/render/json-doc.ts"; | |
| 11 | ||
| 12 | /** A `Json` as plain data, so a test reads like the file. */ | |
| 13 | const plain = (v: Json): unknown => | |
| 14 | v.t === "object" | |
| 15 | ? Object.fromEntries(v.entries.map(([k, x]) => [k, plain(x)])) | |
| 16 | : v.t === "array" | |
| 17 | ? v.items.map(plain) | |
| 18 | : v.t === "null" | |
| 19 | ? null | |
| 20 | : v.v; | |
| 21 | ||
| 22 | const okValue = (r: { ok: true; value: Json } | { ok: false }): unknown => { | |
| 23 | expect(r.ok).toBe(true); | |
| 24 | return r.ok ? plain(r.value) : undefined; | |
| 25 | }; | |
| 26 | ||
| 27 | describe("toml-config", () => { | |
| 28 | test("a table reads as an object, in the file's order", () => { | |
| 29 | const r = parseToml('title = "site"\n\n[server]\nport = 8080\nhost = "0.0.0.0"\ndebug = false\n'); | |
| 30 | expect(okValue(r)).toEqual({ title: "site", server: { port: "8080", host: "0.0.0.0", debug: false } }); | |
| 31 | if (r.ok && r.value.t === "object") expect(r.value.entries.map(([k]) => k)).toEqual(["title", "server"]); | |
| 32 | }); | |
| 33 | ||
| 34 | test("an array of tables reads as a list of objects", () => { | |
| 35 | const r = parseToml('[[route]]\npath = "/"\n\n[[route]]\npath = "/api"\nauth = true\n'); | |
| 36 | expect(okValue(r)).toEqual({ route: [{ path: "/" }, { path: "/api", auth: true }] }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("a malformed file is an error with its line, not a value", () => { | |
| 40 | const r = parseToml("ok = 1\nx = = 2\nport = 3\n"); | |
| 41 | expect(r.ok).toBe(false); | |
| 42 | if (!r.ok) { | |
| 43 | expect(r.error.line).toBe(2); | |
| 44 | expect(r.error.column).toBe(5); | |
| 45 | expect(r.error.message.length).toBeGreaterThan(0); | |
| 46 | } | |
| 47 | expect(parseToml("a = 1\na = 2\n").ok).toBe(false); | |
| 48 | }); | |
| 49 | }); | |
| 50 | ||
| 51 | describe("yaml-config", () => { | |
| 52 | test("a nested map reads as nested objects", () => { | |
| 53 | expect(okValue(parseYaml("server:\n http:\n port: 80\n tls: true\n name: web\n"))).toEqual({ | |
| 54 | server: { http: { port: "80", tls: true }, name: "web" }, | |
| 55 | }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a list reads as an array", () => { | |
| 59 | expect(okValue(parseYaml("steps:\n - build\n - test\n - { run: deploy }\n"))).toEqual({ | |
| 60 | steps: ["build", "test", { run: "deploy" }], | |
| 61 | }); | |
| 62 | }); | |
| 63 | ||
| 64 | test("an anchor is shown plainly: an alias reads as the value it points at", () => { | |
| 65 | const r = parseYaml("base: &b\n image: bun\n retries: 2\njob:\n setup: *b\n"); | |
| 66 | expect(okValue(r)).toEqual({ base: { image: "bun", retries: "2" }, job: { setup: { image: "bun", retries: "2" } } }); | |
| 67 | }); | |
| 68 | ||
| 69 | test("an alias bomb is refused, not expanded", () => { | |
| 70 | const lines = ['a: &a ["x","x","x","x","x","x","x","x","x","x"]']; | |
| 71 | for (let i = 1; i < 8; i++) { | |
| 72 | const prev = String.fromCharCode(96 + i); | |
| 73 | const name = String.fromCharCode(97 + i); | |
| 74 | lines.push(`${name}: &${name} [${Array(10).fill(`*${prev}`).join(",")}]`); | |
| 75 | } | |
| 76 | const r = parseYaml(lines.join("\n")); | |
| 77 | expect(r.ok).toBe(false); | |
| 78 | if (!r.ok) expect(r.error.message).toStartWith("The form cannot hold more than"); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a malformed file is an error with its line, not a value", () => { | |
| 82 | const r = parseYaml("ok: 1\nlist: [a, b\nnext: 2\n"); | |
| 83 | expect(r.ok).toBe(false); | |
| 84 | if (!r.ok) expect(r.error.line).toBeGreaterThanOrEqual(2); | |
| 85 | const tab = parseYaml("a:\n b: 1\n c: 2\n"); | |
| 86 | expect(tab.ok).toBe(false); | |
| 87 | }); | |
| 88 | }); |
new above
offline