// The server's displayers over a TEMP settings file and TEMP private rule // files (never the operator's): live settings, the useSourceRules opt-in, a // marker planted in the rule files that never reaches the client rules, a // warning or an error, and the memo following a change. // // Run with: node_modules/.bin/tsx --test common/lib/privacy/privacy-server.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync, writeFileSync, utimesSync } from "node:fs"; import os, { tmpdir } from "node:os"; import path from "node:path"; const DIR = mkdtempSync(path.join(tmpdir(), "privacy-server-")); const SETTINGS = path.join(DIR, "settings.json"); const SCRUB = path.join(DIR, "scrub.txt"); const DENY = path.join(DIR, "deny.txt"); Object.assign(process.env, { SETTINGS_FILE: SETTINGS, SOURCE_SCRUB_FILE: SCRUB, SOURCE_DENYLIST_FILE: DENY }); const { currentPrivacy, displayPath, displayText, clientPrivacyRules, hidePrivateSites, resetPrivacyMemo } = await import("./privacy-server"); let tick = 0; const write = (file: string, text: string) => { writeFileSync(file, text); // A distinct mtime per write, so the memo key moves even within a second. tick += 10; const t = new Date(Date.UTC(2026, 0, 1) + tick * 1000); utimesSync(file, t, t); resetPrivacyMemo(); }; const settings = (privacy: unknown) => write(SETTINGS, JSON.stringify(privacy === undefined ? {} : { privacy })); const HOME = os.homedir(); test("absent: identity, no client rules, nothing hidden", () => { settings(undefined); const s = `${HOME}/x on ${os.hostname()}`; assert.equal(displayPath(s), s); assert.equal(displayText(s), s); assert.equal(clientPrivacyRules(), null); assert.equal(hidePrivateSites(), false); // At its defaults, explicitly: the same. settings({ displayPaths: "full", hideHost: false, hidePrivateSites: false, aliases: [], maskEmail: false, useSourceRules: false }); assert.equal(displayText(s), s); assert.equal(clientPrivacyRules(), null); }); test("live settings: home mode, hidePrivateSites", () => { settings({ displayPaths: "home", hidePrivateSites: true }); assert.equal(displayPath(`${HOME}/a/b`), "~/a/b"); assert.equal(hidePrivateSites(), true); assert.ok(clientPrivacyRules()); settings({ hidePrivateSites: true }); assert.equal(clientPrivacyRules(), null, "a filter flag alone sends no rules"); }); test("useSourceRules: the files are applied on the server, never handed to the client, never quoted", () => { const warnings: string[] = []; write(SCRUB, "MARKER-SCRUB-7==>stand-in\nregex:MARKER-RX-(==>x\n"); write(DENY, "MARKER-DENY-7\n"); settings({ useSourceRules: true, displayPaths: "home" }); const c = currentPrivacy({ warn: (l) => warnings.push(l) }); assert.equal(displayText("a MARKER-SCRUB-7 b MARKER-DENY-7"), "a stand-in b [hidden]"); const client = JSON.stringify(clientPrivacyRules()); assert.ok(!client.includes("MARKER"), "the client rules carry no private text"); assert.ok(!JSON.stringify(c.problems).includes("MARKER")); assert.deepEqual(warnings, ["[privacy] scrub line 2: does not compile (regex); it runs in fallback: on every line holding one of its 1 certain literal fragment, the text from that fragment (or the whole line, where the fragment does not start the match) is shown as [hidden]"]); for (const w of warnings) assert.ok(!w.includes("MARKER")); }); test("useSourceRules with the files missing: a warning naming the path, nothing else", () => { rmSync(SCRUB, { force: true }); rmSync(DENY, { force: true }); settings({ useSourceRules: true }); const warnings: string[] = []; currentPrivacy({ warn: (l) => warnings.push(l) }); assert.equal(warnings.length, 2); assert.match(warnings[0], /^\[privacy\] useSourceRules: no scrub rules at .*scrub\.txt \(ENOENT\)/); assert.equal(displayText("plain"), "plain"); }); test("the memo follows a settings change", () => { settings({ displayPaths: "home" }); assert.equal(displayPath(HOME), "~"); settings({ displayPaths: "masked" }); assert.equal(displayPath(HOME), HOME === "/home/user" ? HOME : "/home/user"); }); test("a settings.json that does not parse: strict at boot, the last good rules after", () => { // At boot (no good compile yet): strict, warned once. write(SETTINGS, "{ not json"); const warnings: string[] = []; const c = currentPrivacy({ warn: (l) => warnings.push(l) }); assert.equal(c.settings.displayPaths, "masked"); assert.equal(c.settings.hideHost, true); assert.match(warnings.join("\n"), /does not parse: privacy runs strict/); assert.equal(displayPath(`${HOME}/x`), HOME === "/home/user" ? `${HOME}/x` : "/home/user/x"); // After a good compile with privacy on, a broken file keeps it. settings({ displayPaths: "home" }); assert.equal(displayPath(`${HOME}/x`), "~/x"); writeFileSync(SETTINGS, "{ broken"); tick += 10; const t = new Date(Date.UTC(2026, 0, 1) + tick * 1000); utimesSync(SETTINGS, t, t); // Let the one-second recheck pass without dropping the memo. const until = Date.now() + 1100; while (Date.now() < until) { /* wait */ } assert.equal(displayPath(`${HOME}/x`), "~/x"); // A missing file is the defaults: off. rmSync(SETTINGS, { force: true }); resetPrivacyMemo(); assert.equal(displayPath(`${HOME}/x`), `${HOME}/x`); }); test.after(() => rmSync(DIR, { recursive: true, force: true }));