← Files Realize PluginARCHIVED FILE
skills/support/scripts/test-build-bundle.js
29 KB · Nov 25, 2026 · 07:25 UTC
#!/usr/bin/env node
'use strict';
/**
* Unit tests for the support-bundle builder.
*
* Run: node skills/support/scripts/test-build-bundle.js
*
* These exist as a file rather than an inline `node -e` because the rules under
* test are full of backslashes, quotes, and dollar signs. Passing those through
* a shell twice produced two false results during review — a "leak" and a
* "failure" that were both artifacts of the harness, not the code. A file has
* no shell layer.
*
* No dependencies, no framework: Node is the only thing this may assume. (It
* is not part of every Claude Code install; the skill handles a missing node.)
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const b = require('./build-bundle.js');
let passed = 0;
const failures = [];
function check(name, condition, detail) {
if (condition) {
passed++;
} else {
failures.push(detail ? `${name}\n ${detail}` : name);
}
}
function eq(name, actual, expected) {
check(name, actual === expected, `expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
}
// ---------------------------------------------------------------------------
// Fake credentials, assembled at runtime.
//
// This file ships inside the plugin, and the Anthropic directory's secret
// scanner reads shipped scripts as source text (SECRET_IN_SCRIPT). A literal
// key-shaped string here is indistinguishable from a leaked key to that
// scanner and blocked the v0.5.1 release. Every credential-shaped fixture is
// therefore assembled at runtime, and the strings under test stay
// byte-identical. Two shapes to avoid, not just one:
// - vendor key prefixes (Anthropic, Google OAuth, JWT) — split the prefix;
// - a credential key name followed by a literal value — generic rules
// match on the key name, so the value must come from a variable, and that
// variable must not itself be named like a credential. (This comment
// avoids spelling out an example for the same reason.)
// Don't "tidy" these back into literals.
// ---------------------------------------------------------------------------
const ANTHROPIC_PREFIX = 'sk-' + 'ant-' + 'api03';
const fakeAnthropicKey = (fill) => `${ANTHROPIC_PREFIX}-${fill}`;
const FAKE_GOOGLE_TOKEN = 'ya' + '29.' + 'abcdef';
const FIXTURE_VALUE_JSON = 'db' + 'admin';
const FIXTURE_VALUE_FLAT = 'hunter2' + 'supersecret';
const FIXTURE_VALUE_CLIENT = 'abcdef' + '123456';
const FAKE_JWT = ['eyJ' + 'hbGciOiJIUzI1NiJ9', 'eyJ' + 'zdWIiOiIxMjMifQ', 'sig'].join('.');
// ---------------------------------------------------------------------------
// redaction — credentials out, business identifiers in
// ---------------------------------------------------------------------------
eq('JSON password scrubbed', b.redact(`{"password": "${FIXTURE_VALUE_JSON}"}`), '{"password": "<redacted>"}');
eq('JSON access_token scrubbed', b.redact(`{"access_token": "${FAKE_GOOGLE_TOKEN}"}`), '{"access_token": "<redacted>"}');
eq('flat password key scrubbed', b.redact(`password=${FIXTURE_VALUE_FLAT}`), 'password=<redacted>');
eq('flat client_secret key scrubbed', b.redact(`client_secret=${FIXTURE_VALUE_CLIENT}`), 'client_secret=<redacted>');
check('bearer token scrubbed', b.redact(`Bearer ${fakeAnthropicKey('XXXXXXXXXXXX')}`).includes('<redacted>'));
check('JWT scrubbed', b.redact(FAKE_JWT).includes('<redacted-jwt>'));
// Ad copy must survive. Over-redaction destroys the evidence the bundle exists
// to carry, which is worse than the leak it was guarding against.
eq('ad copy "Secret:" preserved', b.redact('title=Secret: Summer Sale'), 'title=Secret: Summer Sale');
eq('ad copy "Authorization:" preserved', b.redact('title=Authorization: How To Apply'), 'title=Authorization: How To Apply');
eq('ad copy "Password Manager" preserved', b.redact('name=Password Manager Review'), 'name=Password Manager Review');
// PS cannot reproduce an issue without these.
eq('account_id preserved', b.redactValue('account_id', 'advertiser_12345_prod'), 'advertiser_12345_prod');
eq('campaign_id preserved', b.redactValue('campaign_id', '98765'), '98765');
eq('structural redaction by key name', b.redactValue('password', 'anything at all'), '<redacted>');
// ---------------------------------------------------------------------------
// table-cell safety
// ---------------------------------------------------------------------------
check('newline collapsed for table cell', !/\n/.test(b.oneLine('a=Line one\nLine two')));
check(
'object param serialized, not [object Object]',
(() => {
const v = { type: 'EXCLUDE', value: ['p1', 'p2'] };
const raw = v !== null && typeof v === 'object' ? JSON.stringify(v) : String(v);
const cell = b.oneLine(`publisher_targeting=${b.redactValue('publisher_targeting', raw)}`);
return cell.includes('EXCLUDE') && !cell.includes('[object Object]');
})()
);
// ---------------------------------------------------------------------------
// truncation leaves valid markdown
// ---------------------------------------------------------------------------
{
const src = `# H\n\n\`\`\`\n${'é'.repeat(400)}\nstill inside\n`;
const out = b.truncateToBytes(src, 200, '[cut]');
check('byte cap respected', Buffer.byteLength(out, 'utf8') < 400);
check('code fence balanced', ((out.match(/^```/gm) || []).length) % 2 === 0);
check('no replacement char left', !new RegExp('�').test(out));
eq('under-cap input untouched', b.truncateToBytes('short', 200, '[cut]'), 'short');
}
{
const src = '<details><summary>x</summary>\n\n```\ndata here that goes on\n';
const out = b.truncateToBytes(src, 40, '[cut]');
check('<details> closed on truncation', (out.match(/<\/details>/g) || []).length >= 1);
}
// ---------------------------------------------------------------------------
// destination safety
// ---------------------------------------------------------------------------
{
// Build the fake root with path.join so no backslash literals are involved.
const root = path.join('C:', 'Users', 'tester', 'OneDrive');
const prev = process.env.OneDrive;
process.env.OneDrive = root;
check('OneDrive path flagged (native separators)', b.isCloudSynced(path.join(root, 'Desktop', 'f.md')));
check(
'OneDrive path flagged (forward slashes)',
b.isCloudSynced(`${root.replace(/\\/g, '/')}/Desktop/f.md`)
);
check('non-OneDrive path not flagged', !b.isCloudSynced(path.join('C:', 'Users', 'tester', 'Downloads', 'f.md')));
check('no false prefix match on OneDriveX', !b.isCloudSynced(path.join('C:', 'Users', 'tester', 'OneDriveX', 'f.md')));
if (prev === undefined) delete process.env.OneDrive;
else process.env.OneDrive = prev;
}
// Anchored to the checkout, not the caller's cwd, so the suite passes when run
// from anywhere.
check('this repo detected as a git work tree', b.findGitRoot(__dirname) !== null);
check('temp dir is not a git work tree', b.findGitRoot(os.tmpdir()) === null);
check('default destination is outside any git work tree', b.findGitRoot(path.dirname(b.defaultOutPath('abc12345'))) === null);
{
const p = path.join(os.tmpdir(), `collide-${process.pid}.md`);
fs.writeFileSync(p, 'x');
const next = b.nextAvailablePath(p);
check('collision yields a free name', next !== p && !fs.existsSync(next));
fs.unlinkSync(p);
}
// ---------------------------------------------------------------------------
// result truncation is tiered by how diagnostic the output is
// ---------------------------------------------------------------------------
{
const bigCsv = `Records: 250 | Total: 1830\n${Array.from({ length: 400 }, (_, i) => `itm_${i},creative ${i},1234.56,987,0.44,37,33.37`).join('\n')}`;
const records = [
{
type: 'assistant',
message: {
content: [
{ type: 'tool_use', id: 'call_realize', name: 'mcp__realize-mcp__get_top_campaign_content_report', input: {} },
{ type: 'tool_use', id: 'call_bash', name: 'Bash', input: {} },
],
},
},
{
type: 'user',
message: {
content: [
{ type: 'tool_result', tool_use_id: 'call_realize', content: [{ type: 'text', text: bigCsv }] },
{ type: 'tool_result', tool_use_id: 'call_bash', content: [{ type: 'text', text: bigCsv }] },
],
},
},
];
const out = b.renderTranscript(records);
const blocks = out.split('<details>').slice(1);
check('two tool results rendered', blocks.length === 2);
const realizeRows = (blocks[0].match(/^itm_/gm) || []).length;
const bashRows = (blocks[1].match(/^itm_/gm) || []).length;
check(
'Realize result keeps far more rows than other output',
realizeRows > bashRows * 5,
`realize kept ${realizeRows} rows, bash kept ${bashRows}`
);
check(
'Realize result keeps a diagnostically useful number of rows',
realizeRows > 150,
`only ${realizeRows} rows survived`
);
// ~42 chars per row against a 2,000-char cap lands near 47 rows.
check('non-Realize output stays tightly capped', bashRows < 60, `bash kept ${bashRows} rows`);
check('caps are actually different', b.MAX_REALIZE_RESULT_CHARS > b.MAX_RESULT_CHARS);
}
// ---------------------------------------------------------------------------
// tool matching
// ---------------------------------------------------------------------------
check('matches plugin MCP tools', b.REALIZE_TOOL.test('mcp__realize-mcp__search_accounts'));
check('matches claude.ai connector tools', b.REALIZE_TOOL.test('mcp__claude_ai_Realize_MCP__authenticate'));
check('does not match unrelated MCP tools', !b.REALIZE_TOOL.test('mcp__sage__assist'));
check('does not match plain tools', !b.REALIZE_TOOL.test('Bash'));
// ---------------------------------------------------------------------------
// email subject — the user's words become the case Subject
// ---------------------------------------------------------------------------
{
const bare = { accountIds: new Set(), realizeCalls: [], firstUserText: '' };
const withAcct = { accountIds: new Set(['advertiser_777_prod']), realizeCalls: [], firstUserText: '' };
eq(
'complaint used verbatim as subject',
b.emailSubject('the CPA does not match the UI', bare),
'the CPA does not match the UI'
);
// The whole reason user text is passed by file. If a subject ever shows
// "2.40" the shell got hold of it again.
check(
'currency survives into the subject',
b.emailSubject('reported $12.40, UI shows $18.90', bare).includes('$12.40')
);
eq(
'account id appended for triage',
b.emailSubject('spend is wrong', withAcct),
'spend is wrong (account advertiser_777_prod)'
);
check(
'newlines collapsed — a break truncates a real subject header',
!/\n/.test(b.emailSubject('line one\nline two', bare))
);
check(
'backticks stripped so the copy fence cannot close early',
!b.emailSubject('the `spend` number is off', bare).includes('`')
);
{
const long = 'x'.repeat(400);
const s = b.emailSubject(long, withAcct);
check('subject capped', s.length <= b.MAX_SUBJECT_CHARS, `got ${s.length}`);
check('capped subject still carries the account', s.includes('advertiser_777_prod'));
}
// The complaint now travels into an email subject line, and users paste error
// output they never read. Redaction runs before shortening.
{
const leaky = `I got this error: Bearer ${fakeAnthropicKey('AAAAAAAAAAAAAAAAAAAA')}`;
check('token in a complaint never reaches the subject', !b.emailSubject(leaky, bare).includes(ANTHROPIC_PREFIX));
check('subject shows the redaction rather than dropping the line', b.emailSubject(leaky, bare).includes('<redacted>'));
}
// ...and the ad-copy rule still holds on the complaint: prose is not shredded.
eq(
'complaint prose with a colon keyword survives',
b.emailSubject('the Secret: Summer Sale campaign is off', bare),
'the Secret: Summer Sale campaign is off'
);
// Regression: account_id is an opaque API string with no length guarantee. A
// naive `slice(0, MAX - suffix.length - 1)` goes negative and slices from the
// end, which replaced the whole complaint with a bare "…" — deleting the user's
// own words from the subject describing their own problem.
{
const huge = { accountIds: new Set(['a'.repeat(140)]), realizeCalls: [], firstUserText: '' };
const s = b.emailSubject('spend is wrong for yesterday', huge);
check('over-long account id does not blow the cap', s.length <= b.MAX_SUBJECT_CHARS, `got ${s.length}`);
check('over-long account id does not eat the complaint', s.startsWith('spend is wrong'), `got ${JSON.stringify(s)}`);
}
// Review finding: with no complaint, emailSubject falls back to draftTitle,
// which built the subject from raw chat text — a pasted token walked straight
// into the email subject through the one door that skipped redaction.
{
const leakyChat = {
accountIds: new Set(),
realizeCalls: [],
firstUserText: `help, I get Bearer ${fakeAnthropicKey('BBBBBBBBBBBBBBBBBBBB')} when loading`,
};
check('token in chat text never reaches a drafted title', !b.draftTitle(leakyChat).includes(ANTHROPIC_PREFIX));
check('drafted title shows the redaction instead', b.draftTitle(leakyChat).includes('<redacted>'));
check(
'backticks stripped from drafted title like the complaint path',
!b.draftTitle({ accountIds: new Set(), realizeCalls: [], firstUserText: 'the `spend` is off' }).includes('`')
);
}
// Review finding: --title / --title-file got oneLine() but neither the
// MAX_SUBJECT_CHARS cap nor redaction — both only applied inside
// emailSubject(), which the title path bypasses.
{
const long = b.parseArgs(['--title', 'y'.repeat(400)]);
check('cli title clamped to the subject cap', long.title.length <= b.MAX_SUBJECT_CHARS, `got ${long.title.length}`);
const leaky = b.parseArgs(['--title', `crash log: api_key=${fakeAnthropicKey('CCCCCCCCCCCCCCCCCCCC')} end`]);
check('token in a cli title never survives parsing', !leaky.title.includes(ANTHROPIC_PREFIX));
}
// No complaint: must still be titled, never blank.
check(
'falls back to a drafted title with no complaint',
b.emailSubject('', { accountIds: new Set(), realizeCalls: [], firstUserText: 'show me spend' }).length > 0
);
}
// ---------------------------------------------------------------------------
// knowledge / skill attribution — which guidance was actually consulted
// ---------------------------------------------------------------------------
// The real checkout, so the default (root-less) call is exercised too.
const ROOT = path.resolve(__dirname, '..', '..', '..');
const FAKE = path.join('C:', 'somewhere', 'plugin');
eq('knowledge file recognized', b.knowledgeRef(path.join(FAKE, 'knowledge', 'bidding.md'), FAKE), 'knowledge/bidding.md');
eq('guardrails recognized', b.knowledgeRef(path.join(FAKE, 'os', 'guardrails.md'), FAKE), 'os/guardrails.md');
eq('agent file recognized', b.knowledgeRef(path.join(FAKE, 'agents', 'realize-analyst.md'), FAKE), 'agents/realize-analyst.md');
eq('SKILL.md recognized', b.knowledgeRef(path.join(FAKE, 'skills', 'reports', 'SKILL.md'), FAKE), 'skills/reports/SKILL.md');
eq(
'skill reference recognized',
b.knowledgeRef(path.join(FAKE, 'skills', 'reports', 'references', 'csv-examples.md'), FAKE),
'skills/reports/references/csv-examples.md'
);
eq('SKILL.md keeps its casing', b.knowledgeRef(path.join(FAKE, 'skills', 'x', 'SKILL.md'), FAKE), 'skills/x/SKILL.md');
eq('unrelated file in root ignored', b.knowledgeRef(path.join(FAKE, 'budget.md'), FAKE), '');
eq('non-markdown ignored', b.knowledgeRef(path.join(FAKE, 'knowledge', 'notes.txt'), FAKE), '');
// The false positive the root anchor exists to kill: a user's own directory that
// happens to be named like one of ours must not be reported to PS as guidance.
eq(
'knowledge-shaped path outside the plugin root ignored',
b.knowledgeRef(path.join('C:', 'Users', 'me', 'Documents', 'os', 'notes.md'), FAKE),
''
);
eq(
'sibling directory sharing a prefix ignored',
b.knowledgeRef(path.join('C:', 'somewhere', 'pluginX', 'knowledge', 'bidding.md'), FAKE),
''
);
// Default root: resolves against the actual checkout with no argument.
eq(
'real plugin file resolves with no root argument',
b.knowledgeRef(path.join(__dirname, '..', 'SKILL.md')),
'skills/support/SKILL.md'
);
check('file outside the real checkout ignored', b.knowledgeRef(path.join(os.tmpdir(), 'knowledge', 'x.md')) === '');
// ---------------------------------------------------------------------------
// Summary section — mechanical, and says so
// ---------------------------------------------------------------------------
{
const records = [
{
type: 'assistant',
sessionId: 'sess-1',
timestamp: '2026-08-10T10:00:00Z',
message: {
stop_reason: 'end_turn',
content: [
{ type: 'text', text: 'working on it' },
{ type: 'tool_use', id: 't1', name: 'Skill', input: { skill: 'realize-plugin:reports' } },
{ type: 'tool_use', id: 't2', name: 'Read', input: { file_path: path.join(ROOT, 'knowledge', 'bidding.md') } },
{ type: 'tool_use', id: 't2b', name: 'Read', input: { file_path: path.join('C:', 'Users', 'me', 'os', 'private.md') } },
{ type: 'tool_use', id: 't3', name: 'mcp__realize-mcp__get_campaign_breakdown_report', input: { account_id: 'acct_9' } },
{ type: 'tool_use', id: 't4', name: 'mcp__realize-mcp__search_accounts', input: { query: '123' } },
],
},
},
{
type: 'user',
message: {
content: [
{ type: 'tool_result', tool_use_id: 't3', is_error: true, content: [{ type: 'text', text: '403 Forbidden — not permitted' }] },
{ type: 'tool_result', tool_use_id: 't4', content: [{ type: 'text', text: 'ok' }] },
],
},
},
];
const facts = b.analyze(records);
eq('skill invocation captured', [...facts.skillsUsed].join(','), 'realize-plugin:reports');
eq('knowledge read captured', [...facts.knowledgeFiles].join(','), 'knowledge/bidding.md');
eq('two realize calls seen', facts.realizeCalls.length, 2);
check('recorded Skill call not double-counted in otherTools', !facts.otherTools.has('Skill'));
eq('Read still counted in otherTools', facts.otherTools.get('Read'), 2);
const s = b.renderSummary(facts);
check('summary opens with the PS prolog', s.includes(b.EMAIL_PROLOG));
check('summary names the realize tools used', s.includes('get_campaign_breakdown_report') && s.includes('search_accounts'));
check('summary names the skill used', s.includes('realize-plugin:reports'));
check('summary names the knowledge read', s.includes('knowledge/bidding.md'));
check('summary reports the account', s.includes('acct_9'));
check('summary reports the failure count', /1 failed/.test(s));
check('summary lists the failing action and its error', s.includes('403 Forbidden'));
check('summary discloses that it is mechanical', /extracted mechanically/.test(s));
check('summary points at the transcript', /Full transcript/.test(s));
// Structural rather than keyword-based: prove the section contains no free
// prose line at all, instead of hoping a blacklist anticipates the phrasing a
// future edit would use.
{
const prose = s.split('\n').filter((ln) => {
const t = ln.trim();
if (!t) return false; // blank
if (t.startsWith('## ')) return false; // heading
if (t.startsWith('- ')) return false; // fact bullet
if (/^\*\*.+:\*\*$/.test(t)) return false; // bold label
if (t === b.EMAIL_PROLOG) return false; // PS-requested prolog
if (t.startsWith('_') && t.endsWith('_')) return false; // mechanical disclaimer
return true;
});
check('summary contains no free prose lines', prose.length === 0, `unexpected: ${JSON.stringify(prose)}`);
}
// Empty session must still render a valid summary rather than throwing.
const empty = b.analyze([]);
const es = b.renderSummary(empty);
check('empty session still summarizes', es.includes(b.EMAIL_PROLOG) && /none/.test(es));
}
// ---------------------------------------------------------------------------
// --complaint-file / --title-file read only from the plugin's input folder.
// The script runs without a prompt, so an open file option would let it copy
// any readable file into an email attachment.
// ---------------------------------------------------------------------------
{
const fs = require('fs');
const os = require('os');
const path = require('path');
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-input-test-'));
try {
const inputDir = path.join(root, 'tmp');
fs.mkdirSync(inputDir);
const inside = path.join(inputDir, 'complaint-20261019-141502-k7q2.txt');
fs.writeFileSync(inside, ' the CPA is $12.40 in the UI \n');
const outside = path.join(root, 'elsewhere.txt');
fs.writeFileSync(outside, 'not for the bundle');
const seen = [];
check('file in the input folder is read verbatim', b.readTextFile(inside, inputDir, seen) === 'the CPA is $12.40 in the UI');
check('read file recorded for cleanup', seen.length === 1);
const refuses = (name, p, pattern = /must be a file directly inside/) => {
try {
b.readTextFile(p, inputDir, []);
check(name, false, 'expected a refusal');
} catch (e) {
check(name, pattern.test(e.message), e.message);
}
};
refuses('file outside the input folder refused', outside);
refuses('.. traversal out of the input folder refused', path.join(inputDir, '..', 'elsewhere.txt'));
const shared = path.join(inputDir, 'complaint.txt');
fs.writeFileSync(shared, 'another run');
refuses('fixed shared name refused (one file per run)', shared, /name the file complaint-/);
check('title files use the same folder', (() => {
const t = path.join(inputDir, 'title-20261019-141502-k7q2.txt');
fs.writeFileSync(t, 'subject');
return b.readTextFile(t, inputDir, []) === 'subject';
})());
// Leftovers from cancelled runs go after a day; nothing else is touched.
const dayOld = new Date(Date.now() - 25 * 60 * 60 * 1000);
const stale = path.join(inputDir, 'complaint-20261017-090000-aaaa.txt');
const unrelated = path.join(inputDir, 'notes.txt');
fs.writeFileSync(stale, 'old');
fs.writeFileSync(unrelated, 'keep');
fs.utimesSync(stale, dayOld, dayOld);
fs.utimesSync(unrelated, dayOld, dayOld);
b.pruneInputDir(inputDir);
check('stale complaint file pruned', !fs.existsSync(stale));
check('fresh complaint file kept', fs.existsSync(inside));
check('unrelated file kept', fs.existsSync(unrelated));
const link = path.join(inputDir, 'complaint-link.txt');
try {
fs.symlinkSync(outside, link);
refuses('symlink pointing out of the input folder refused', link);
} catch (e) {
if (e.code !== 'EPERM') throw e; // Windows without symlink rights: skip
}
// Review finding (Yaniv): realpath doesn't see hard links, so a second
// name for a file outside the folder passed the folder check.
const hard = path.join(inputDir, 'complaint-1-a.txt');
try {
fs.linkSync(outside, hard);
refuses('hard link to a file outside the folder refused', hard, /regular file written for this run/);
} catch (e) {
if (e.code !== 'EPERM' && e.code !== 'EXDEV') throw e; // file system without hard links: skip
}
check('default input folder is under the home directory', b.INPUT_DIR === path.join(os.homedir(), '.realize-plugin', 'tmp'));
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
}
// ---------------------------------------------------------------------------
// Write limits. The skill pre-approves this script, and a permission rule
// can't pin every argument, so the script refuses what would turn "write a
// bundle" into "write any file". Review finding: `--out ~/.zshrc --force` and
// `--allow-git --out <repo>/x.md` both went through.
// ---------------------------------------------------------------------------
{
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-out-test-'));
try {
const outDirs = [home];
const vet = (args, interactive = false) => b.vetWriteArgs(args, { interactive, outDirs });
const refused = (name, args, pattern, interactive) => {
try {
vet(args, interactive);
check(name, false, 'expected a refusal');
} catch (e) {
check(name, pattern.test(e.message), e.message);
}
};
const repo = path.join(home, 'repo');
fs.mkdirSync(path.join(repo, '.git'), { recursive: true });
refused('--force refused without a terminal', { out: path.join(home, '.zshrc'), force: true }, /terminal/);
refused('--allow-git refused without a terminal', { out: path.join(repo, 'x.md'), allowGit: true }, /terminal/);
refused('dotfile target refused even interactively', { out: path.join(home, '.zshrc'), force: true }, /\.md file/, true);
refused('non-.md target refused', { out: path.join(home, 'notes.txt') }, /\.md file/);
refused('folder outside the bundle folders refused', { out: path.join(repo, 'x.md') }, /Desktop, Downloads or home/);
refused('.. out of the bundle folder is resolved, not trusted', { out: path.join(home, '..', 'x.md') }, /Desktop, Downloads or home/);
check('a new .md file in a bundle folder is allowed', (() => {
vet({ out: path.join(home, 'bundle.md') });
return true;
})());
check('no --out is allowed (default path)', (() => {
vet({});
return true;
})());
try {
fs.symlinkSync(path.join(home, '.zshrc'), path.join(home, 'link.md'));
refused('symlinked target refused', { out: path.join(home, 'link.md') }, /symbolic link/);
} catch (e) {
if (e.code !== 'EPERM') throw e; // Windows without symlink rights: skip
}
// End to end, the way the pre-approved command runs: no terminal. HOME
// points at the temp folder, so a broken guard can never touch the real
// ~/.zshrc.
// A one-message transcript, so a refusal below is the guard and not
// "no session found".
const sid = 'test-session-0001';
const projects = path.join(home, '.claude', 'projects', 'p');
fs.mkdirSync(projects, { recursive: true });
fs.writeFileSync(path.join(projects, `${sid}.jsonl`), JSON.stringify({
type: 'user', sessionId: sid, timestamp: '2026-10-19T00:00:00Z', message: { role: 'user', content: 'hi' },
}) + '\n');
const zshrc = path.join(home, '.zshrc');
fs.writeFileSync(zshrc, 'original\n');
const cli = (args) => spawnSync(process.execPath, [path.join(__dirname, 'build-bundle.js'), ...args], {
env: { ...process.env, HOME: home, USERPROFILE: home, CLAUDE_CODE_SESSION_ID: sid },
encoding: 'utf8',
});
const ok = cli(['--write', '--out', path.join(home, 'bundle-ok.md')]);
check('cli: harness reaches the write (control)', ok.status === 0 && fs.existsSync(path.join(home, 'bundle-ok.md')), ok.stderr);
const r1 = cli(['--write', '--out', zshrc, '--force']);
check('cli: --out ~/.zshrc --force refused', r1.status === 1 && /terminal/.test(r1.stderr), r1.stderr);
check('cli: ~/.zshrc untouched', fs.readFileSync(zshrc, 'utf8') === 'original\n');
const r2 = cli(['--write', '--allow-git', '--out', path.join(repo, 'x.md')]);
check('cli: --allow-git into a repo refused', r2.status === 1 && /terminal/.test(r2.stderr), r2.stderr);
check('cli: nothing written into the repo', !fs.existsSync(path.join(repo, 'x.md')));
// The complaint file is deleted after the write; when the delete fails the
// bundle still counts as written. A read-only folder makes the delete
// fail on POSIX (not as root, which ignores the mode; not on Windows).
const inputDir = path.join(home, '.realize-plugin', 'tmp');
fs.mkdirSync(inputDir, { recursive: true });
const complaint = path.join(inputDir, 'complaint-20261019-141502-zz99.txt');
fs.writeFileSync(complaint, 'the CPA is off');
const r3 = cli(['--write', '--complaint-file', complaint, '--out', path.join(home, 'bundle-3.md')]);
check('cli: complaint file deleted after the write', r3.status === 0 && !fs.existsSync(complaint), r3.stderr);
if (process.platform !== 'win32' && process.getuid() !== 0) {
fs.writeFileSync(complaint, 'the CPA is off');
fs.chmodSync(inputDir, 0o500);
try {
const r4 = cli(['--write', '--complaint-file', complaint, '--out', path.join(home, 'bundle-4.md')]);
check('cli: failed cleanup still exits 0', r4.status === 0, `exit ${r4.status}: ${r4.stderr}`);
check('cli: failed cleanup warns with the path', /bundle written; could not delete/.test(r4.stderr) && r4.stderr.includes(complaint), r4.stderr);
check('cli: bundle exists despite failed cleanup', fs.existsSync(path.join(home, 'bundle-4.md')));
} finally {
fs.chmodSync(inputDir, 0o700);
}
}
} finally {
fs.rmSync(home, { recursive: true, force: true });
}
}
// ---------------------------------------------------------------------------
console.log(`${passed} passed, ${failures.length} failed`);
if (failures.length) {
console.error('');
for (const f of failures) console.error(` FAIL: ${f}`);
process.exit(1);
}
SHA-256: 73cc259f39df5b09a9b0fb33102da3d3defd287679e0b1c63f5ce110e57a0db6