// post-redact.mjs: words blanked from what is published -- the matcher, the
// marks, the read without them, the card that never holds them. Made-up
// terms only ("Quentin Example").
import assert from "node:assert/strict";
import { test } from "node:test";
import {
REDACT_MARK, barsHtml, dropMarks, redactEntry, redactPattern, redactText, sayHandles, spokenRead, spokenWithout, termsFor, validatePostsMarkup, validatePostsRender,
} from "./post-redact.mjs";
import { postCardHtml, validatePostItems } from "./post-item.mjs";
const NAME = [{ term: "Quentin Example" }];
const marks = (n) => REDACT_MARK.repeat(n);
test("a term matches without case, as whole words; --substring inside words too", () => {
assert.equal(redactText("quentin is here", [{ term: "Quentin" }]).text, `${marks(7)} is here`);
// Whole word: not inside another word.
assert.equal(redactText("Quentinesque", [{ term: "Quentin" }]).text, "Quentinesque");
assert.equal(redactText("Quentinesque", [{ term: "Quentin", wholeWord: false }]).text, `${marks(7)}esque`);
// A possessive goes with it.
assert.equal(redactText("QUENTIN's mic", [{ term: "Quentin" }]).text, `${marks(9)} mic`);
// A bare string is a term.
assert.equal(redactText("hi Quentin", ["quentin"]).spans.length, 1);
assert.equal(redactPattern([]), null);
});
test("a phrase matches across any spaces or punctuation; its length and offsets are kept", () => {
const s = "Signed, quentin.\n EXAMPLE -- for real";
const r = redactText(s, NAME);
assert.equal(r.text.length, s.length);
assert.deepEqual(r.spans, [{ start: 8, end: 25 }]);
// Its spaces kept: as many card words as before (a word zoom's indices hold).
assert.equal(r.text, `Signed, ${marks(8)}\n ${marks(7)} -- for real`);
assert.equal(redactText("Quentin alone", NAME).spans.length, 0, "a phrase is all its words");
assert.equal(redactText("@QuentinExample", NAME).text, `@${marks(14)}`);
// A span of the archived text (markup), merged with a match it overlaps.
assert.deepEqual(redactText("ab Quentin Example cd", NAME, { spans: [{ start: 0, end: 2 }, { start: 10, end: 20 }] }).spans, [{ start: 0, end: 2 }, { start: 3, end: 20 }]);
});
test("the read drops the match: spaces collapsed, dangling punctuation and an empty bracket taken away", () => {
assert.equal(spokenWithout("My name is Quentin Example.", NAME), "My name is.");
assert.equal(spokenWithout("Quentin Example, the author, said so", NAME), "the author, said so");
assert.equal(spokenWithout("Thanks, Quentin Example!", NAME), "Thanks!");
assert.equal(spokenWithout("hello (Quentin Example) there", NAME), "hello there");
assert.equal(spokenWithout("I, Quentin Example, swear", NAME), "I, swear");
// Paragraphs kept; nothing else touched.
assert.equal(spokenWithout("One , two.\n\nQuentin Example: three", NAME), "One , two.\n\nthree");
assert.equal(spokenWithout("no match , here", NAME), "no match , here");
assert.equal(dropMarks(`a ${marks(3)} b`), "a b");
});
test("the card AS PUBLISHED never holds a term: words, names, handles, context, paraphrase -- solid bars instead", () => {
const entry = {
type: "post", id: "p1", post: "subj-x/1", platform: "x", author: "Quentin Example", handle: "QuentinExample", date: "2026-10-30T04:00:00.000Z",
text: "Hi, I'm QUENTIN example and this is my channel", url: "https://x.com/s/status/1",
parents: [{ author: "Fan", handle: "fan", badge: "gold", text: "is that quentin, example?", date: "2026-10-30T03:00:00.000Z" }],
quoted: { author: "Other", handle: "other", badge: "gold", text: "long words", paraphrase: "He said Quentin Example out loud" },
redact: false,
};
// The phrase alone covers the handle that runs its words together.
const red = { terms: NAME };
const html = postCardHtml(entry, { width: 1280, height: 720, redaction: red });
assert.doesNotMatch(html, /quentin/i, "not even hidden");
assert.doesNotMatch(html, new RegExp(REDACT_MARK));
assert.ok((html.match(/data-post-blanked=""/g) ?? []).length >= 5, "the post, the parent, the paraphrase, the name and the handle");
assert.match(html, /\.rbar \{ display: inline-block/);
// Without a redaction, the card is the words.
const plain = postCardHtml(entry, { width: 1280, height: 720 });
assert.match(plain, />QUENTIN);
assert.match(plain, />example);
// The entry itself is never rewritten.
assert.equal(entry.text, "Hi, I'm QUENTIN example and this is my channel");
assert.equal(redactEntry(entry, { terms: [] }), entry);
assert.equal(barsHtml(`x${marks(4)}y`), `xy`);
});
test("a post's markup: a term in one scope, or a span of its archived text", () => {
const entry = { type: "post", id: "p1", post: "subj-x/7", text: "abc Zed def", parents: [{ text: "Zed in a parent" }], quoted: { text: "Zed quoted" } };
const red = { terms: [], markup: { "subj-x/7": { redact: [{ term: "Zed", scope: "parent" }, { start: 8, end: 11, scope: "text" }] } } };
const out = redactEntry(entry, red);
assert.equal(out.text, `abc Zed ${marks(3)}`);
assert.equal(out.parents[0].text, `${marks(3)} in a parent`);
assert.equal(out.quoted.text, "Zed quoted");
assert.deepEqual(termsFor(entry, red).map((t) => t.term), ["Zed"]);
assert.deepEqual(termsFor({ post: "other/1" }, red), []);
assert.deepEqual(validatePostsMarkup({ "7": { redact: [{ term: "Zed" }, { start: 1, end: 3, scope: "quoted" }] } }), []);
const bad = validatePostsMarkup({ "7": { redact: [{ start: 3, end: 1 }, { term: "Zed", scope: "elsewhere" }] }, "a/b": {}, "8": { other: 1 } });
assert.equal(bad.length, 4, bad.join("; "));
});
test("render.posts.redact is validated with the build's check", () => {
assert.deepEqual(validatePostsRender({ posts: { redact: [{ term: "Quentin Example" }, { term: "Q", wholeWord: false, note: "a note" }] } }), []);
const bad = validatePostsRender({ posts: { redact: [{ term: " !! " }, { term: "x", wholeWord: "yes" }, { term: "y", extra: 1 }, "bare"] } });
assert.equal(bad.length, 4, bad.join("; "));
assert.ok(validatePostItems({ timeline: [], render: { posts: { redact: "nope" } } }).some((e) => e.includes("render.posts.redact must be a list")));
});
test("with a word zoom: the redacted text keeps its card words (the zoom's indices hold), a zoom on a redacted word is refused", async () => {
const { fxRedactionErrors, validatePostItems: vpi } = await import("./post-item.mjs");
const { cardWordsOf } = await import("./word-zoom.mjs");
const text = "So Quentin Example said barbecue twice, barbecue!";
assert.equal(cardWordsOf(redactText(text, NAME).text).length, cardWordsOf(text).length);
const e = { type: "post", id: "z1", post: "c-x/1", platform: "x", text, date: "2026-10-30T04:00:00Z", fx: [{ kind: "word-zoom", match: "barbecue", occurrence: 2, at: 1 }] };
assert.deepEqual(fxRedactionErrors(e, { terms: NAME }), []);
// The published card: the bars in the redacted words' slots, the zoom's word where it was.
const html = postCardHtml(e, { width: 1280, height: 720, redaction: { terms: NAME }, highlight: { scope: "post", from: 6, to: 6, color: "#ffd84d" } });
assert.match(html, /data-w="post:6" data-w-hl="">barbecue!/);
assert.match(html, /data-w="post:1"> x.includes("is a redacted word")));
assert.deepEqual(vpi({ timeline: [onName], render: {} }), []);
});
test("a handle left inside the words is said \"at \"; the lexicon's own way wins", () => {
assert.equal(sayHandles("its good advice @grok don't you agree?"), "its good advice at grok don't you agree?");
assert.equal(sayHandles("ask @Some_One (or @x)"), "ask at Some One (or at x)");
assert.equal(sayHandles("a@b.com"), "a@b.com");
assert.equal(spokenRead("advice @grok don't you agree?", { id: "p-1" }, null).text, "advice at grok don't you agree?");
assert.equal(spokenRead("advice @grok ok", { id: "p-1" }, { say: [{ word: "@grok", say: "at Grok A I" }] }).text, "advice at Grok A I ok");
});