|
| 1 | +/** |
| 2 | + * Unit tests for the README shared-block synchroniser's marker validation. |
| 3 | + * |
| 4 | + * `assertWellFormed` is the gate that decides whether a package README is |
| 5 | + * syncable. Every bug found here so far has been a case it accepted and then |
| 6 | + * silently failed to replace, letting `--check` report success while a stale |
| 7 | + * block shipped to npm — so the tests below are all about REJECTION. |
| 8 | + */ |
| 9 | +import { strict as assert } from "node:assert"; |
| 10 | +import test from "node:test"; |
| 11 | +import { assertWellFormed } from "./sync-package-readmes.mjs"; |
| 12 | + |
| 13 | +const S = (n) => `<!-- polycss:shared:${n}:start -->`; |
| 14 | +const E = (n) => `<!-- polycss:shared:${n}:end -->`; |
| 15 | +const ALLOWED = new Set(["links", "packages", "license"]); |
| 16 | + |
| 17 | +/** Collects the error instead of exiting, so a rejection is observable. */ |
| 18 | +const check = (text) => { |
| 19 | + let error = null; |
| 20 | + const names = assertWellFormed(text, "fixture", ALLOWED, (m) => { |
| 21 | + error = m; |
| 22 | + return null; |
| 23 | + }); |
| 24 | + return { error, names }; |
| 25 | +}; |
| 26 | + |
| 27 | +test("accepts a well-formed file and reports its blocks", () => { |
| 28 | + const { error, names } = check(`# Pkg\n${S("links")}\nx\n${E("links")}\nbody\n${S("license")}\nMIT\n${E("license")}\n`); |
| 29 | + assert.equal(error, null); |
| 30 | + assert.deepEqual([...names], ["links", "license"]); |
| 31 | +}); |
| 32 | + |
| 33 | +test("accepts a file with no markers at all (opted out)", () => { |
| 34 | + const { error, names } = check("# Pkg\n\nNo shared blocks here.\n"); |
| 35 | + assert.equal(error, null); |
| 36 | + assert.equal(names.size, 0); |
| 37 | +}); |
| 38 | + |
| 39 | +test("rejects an end marker that precedes its start", () => { |
| 40 | + const { error } = check(`${E("links")}\nx\n${S("links")}\n`); |
| 41 | + assert.match(error, /end marker before its start/); |
| 42 | +}); |
| 43 | + |
| 44 | +test("rejects a duplicated block", () => { |
| 45 | + const { error } = check(`${S("links")}a${E("links")}\n${S("links")}b${E("links")}`); |
| 46 | + assert.match(error, /appears more than once/); |
| 47 | +}); |
| 48 | + |
| 49 | +test("rejects nested blocks", () => { |
| 50 | + const { error } = check(`${S("links")}\n${S("license")}\nx\n${E("license")}\n${E("links")}`); |
| 51 | + assert.match(error, /may not nest or cross/); |
| 52 | +}); |
| 53 | + |
| 54 | +test("rejects crossing blocks", () => { |
| 55 | + const { error } = check(`${S("links")}\n${S("license")}\n${E("links")}\n${E("license")}`); |
| 56 | + assert.match(error, /may not nest or cross/); |
| 57 | +}); |
| 58 | + |
| 59 | +test("rejects an unclosed block", () => { |
| 60 | + const { error } = check(`${S("links")}\nx\n`); |
| 61 | + assert.match(error, /is never closed/); |
| 62 | +}); |
| 63 | + |
| 64 | +test("rejects a block name the root README does not define", () => { |
| 65 | + const { error } = check(`${S("bogus")}\nx\n${E("bogus")}`); |
| 66 | + assert.match(error, /not defined in the root README/); |
| 67 | +}); |
| 68 | + |
| 69 | +test("replacement copies content literally, including $ sequences", () => { |
| 70 | + // Regression: passing the block as a replacement STRING expands `$&`, |
| 71 | + // "$`" and `$'`, corrupting any README containing them. |
| 72 | + const blockRe = /<!-- polycss:shared:links:start -->[\s\S]*?<!-- polycss:shared:links:end -->/; |
| 73 | + const replacement = `${S("links")}\ncost: $5 — see $& and $\` and $'\n${E("links")}`; |
| 74 | + const target = `head\n${S("links")}\nold\n${E("links")}\ntail`; |
| 75 | + |
| 76 | + const viaString = target.replace(blockRe, replacement); |
| 77 | + const viaCallback = target.replace(blockRe, () => replacement); |
| 78 | + |
| 79 | + assert.ok(viaCallback.includes("$& and $` and $'"), "callback must copy verbatim"); |
| 80 | + assert.notEqual(viaString, viaCallback, "string form is the buggy path this guards against"); |
| 81 | +}); |
0 commit comments