/**
* ── syndicate — ONE IDEA, EVERY FORMAT IT HAS TO EXIST IN ───────────────────
*
* ⚠️⚠️ NOTHING IN THIS FILE GENERATES ANYTHING, AND THAT IS NOT A TESTING
* TRICK — IT IS THE VERB'S DESIGN. `syndicate` calls no model, no GPU and no
* network by construction: the caller composes the copy and the verb assembles
* it. So there is no producer to inject, no mock to drift, and every assertion
* below runs the REAL code path onto a REAL temporary directory for free.
*
* ⭐ THE ASSERTIONS THAT MATTER MOST ARE THE ARITHMETIC ONES. "Does the blog
* read well" is not this file's business and never could be. "Is post 4 of 9
* over 280 characters ONCE THE COUNTER IS APPENDED" is the defect that ships,
* and it is checked here from both sides.
*/
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, writeFileSync, readFileSync, existsSync, readdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
syndicate, syndicateToolSchemas, syndicateToolNames, runSyndicateTool,
slugify, charCount, wordCount, yamlScalar, splitPost,
buildThread, renderThread, renderBlog, renderInfographic, wrapLabel,
PLATFORM_LIMITS, MAX_THREAD_POSTS, SYNDICATE_TOOL_NAMES,
} from '../lib/syndicate.mjs';
import { createLocalExecutor } from '../lib/workspace.mjs';
import { TOOL_NAMES, toolNamesForRounds, executeToolCall } from '../lib/tools.mjs';
import { REFUSED_TOOL_REASONS } from '../lib/mcp-server.mjs';
function workspace(t) {
const root = mkdtempSync(join(tmpdir(), 'acuvo-syndicate-'));
t.after(() => { try { rmSync(root, { recursive: true, force: true }); } catch { /* windows handle lag */ } });
return root;
}
const FIXED = new Date('2026-08-25T02:00:00Z');
const IDEA = 'Why we price per outcome';
const BLOG = {
title: 'Why we price per outcome: the whole argument',
dek: 'Seats measure attendance. Outcomes measure work.',
tags: ['pricing', 'saas'],
sections: [
{ heading: 'The problem with seats', body: 'A seat is a proxy for value and a bad one. It rewards headcount growth and punishes automation.' },
{ heading: 'What we changed', body: 'We bill per confirmed booking. If nothing is booked, nothing is billed.' },
],
};
const THREAD = {
platform: 'x',
posts: [
'Seat pricing rewards the wrong thing: it goes up when you hire and down when you automate.',
'So we bill per confirmed booking. Nothing booked, nothing billed.',
'That means we lose money when the product is bad. Which is the point.',
],
};
const INFO = {
title: 'Outcome pricing',
subtitle: 'What changed and why',
stats: [
{ label: 'Billed per confirmed booking', value: '1', note: 'not per seat' },
{ label: 'Charged when nothing books', value: '$0' },
{ label: 'Contracts required', value: 'None' },
],
steps: [
{ label: 'Connect the calendar', body: 'Read-only, one click, no migration.' },
{ label: 'Watch bookings land', body: 'Every one carries the receipt that attributes it.' },
],
source: 'XXIautomate, 2026',
};
/* ────────────────────────────────────────────────────────────────────────────
* 1. THE PIECES
* ──────────────────────────────────────────────────────────────────────────── */
test('slugify produces something safe to use as a directory name', () => {
assert.equal(slugify('Why we price per outcome'), 'why-we-price-per-outcome');
assert.equal(slugify('Café pricing: what changed?'), 'cafe-pricing-what-changed');
// ⚠️ THE SECURITY-SHAPED CASE: nothing that could traverse may survive.
const nasty = slugify('../../etc/passwd');
assert.ok(!nasty.includes('/') && !nasty.includes('\\') && !nasty.includes('..'), `a traversal survived: ${nasty}`);
assert.equal(slugify(''), 'untitled');
assert.equal(slugify(' '), 'untitled');
assert.ok(slugify('a'.repeat(200)).length <= 60);
assert.ok(!slugify('trailing punctuation!!!').endsWith('-'));
});
test('⭐ character counts are CODE POINTS, which is what a platform counts', () => {
assert.equal(charCount('hello'), 5);
// '👍'.length is 2 in JavaScript and 1 everywhere a post is measured.
assert.equal(charCount('👍'), 1);
assert.equal('👍'.length, 2, 'the premise of this test changed');
assert.equal(charCount('a👍b'), 3);
assert.equal(charCount(null), 0);
});
test('yamlScalar survives the characters that break front matter', () => {
assert.equal(yamlScalar('Why we price: the argument'), '"Why we price: the argument"');
assert.equal(yamlScalar('he said "hi"'), '"he said \\"hi\\""');
assert.equal(yamlScalar('line\nbreak'), '"line break"');
assert.equal(yamlScalar(7), '7');
assert.equal(yamlScalar(true), 'true');
});
/* ── the thread ─────────────────────────────────────────────────────────── */
test('⭐⭐ the length check counts the COUNTER, because the platform does', () => {
// 277 characters of body. Fits 280 alone; does NOT fit once " 1/2" (4 more) is appended.
const long = 'x'.repeat(277);
assert.equal(charCount(`${long} 1/2`), 281, 'the arithmetic this test rests on changed');
const alone = buildThread([long], { platform: 'x' });
assert.equal(alone.ok, true, 'a lone post with no counter should fit');
const numbered = buildThread([long, 'second post'], { platform: 'x' });
assert.equal(numbered.ok, false, 'the counter was not counted — this is the bug that ships');
assert.match(numbered.error, /post 1 of 2/);
assert.match(numbered.error, /280/);
assert.match(numbered.error, /counter is included/);
});
test('the refusal says WHICH post and BY HOW MUCH, so the fix is one sentence', () => {
const r = buildThread(['ok', 'y'.repeat(400), 'ok'], { platform: 'x' });
assert.equal(r.ok, false);
assert.match(r.error, /post 2 of 3 is \d+ characters and x allows 280/);
assert.match(r.error, /\d+ too many/);
assert.match(r.error, /split_long_posts/);
});
test('every platform limit is honoured, and an unknown platform falls back rather than refusing', () => {
const post = 'z'.repeat(320);
assert.equal(buildThread([post], { platform: 'x' }).ok, false);
assert.equal(buildThread([post], { platform: 'linkedin' }).ok, true);
assert.equal(buildThread([post], { platform: 'bluesky' }).ok, false, `bluesky is ${PLATFORM_LIMITS.bluesky}`);
const unknown = buildThread([post], { platform: 'some-new-network' });
assert.equal(unknown.ok, true, 'an unlisted platform blocked the whole call');
assert.equal(unknown.platform, 'generic');
assert.equal(unknown.limit, PLATFORM_LIMITS.generic);
});
test('numbering is n/total and can be switched off', () => {
const on = buildThread(['a', 'b', 'c'], { platform: 'x' });
assert.deepEqual(on.posts.map((p) => p.text), ['a 1/3', 'b 2/3', 'c 3/3']);
const off = buildThread(['a', 'b'], { platform: 'x', number: false });
assert.deepEqual(off.posts.map((p) => p.text), ['a', 'b']);
// A single post gets no counter — " 1/1" is noise.
assert.deepEqual(buildThread(['only'], { platform: 'x' }).posts.map((p) => p.text), ['only']);
});
test('⭐ splitting is OPT-IN, splits on a sentence boundary, and never mid-word', () => {
const text = `${'A sentence that is reasonably long. '.repeat(12)}End.`;
const refused = buildThread([text], { platform: 'x' });
assert.equal(refused.ok, false, 'copy was rewritten without being asked');
const split = buildThread([text], { platform: 'x', split: true });
assert.equal(split.ok, true, split.error);
assert.ok(split.posts.length > 1);
for (const p of split.posts) assert.ok(p.chars <= 280, `a split post is still ${p.chars} characters`);
// ⚠️ Nothing was lost and nothing was cut through a word.
const rejoined = split.posts.map((p) => p.text.replace(/ \d+\/\d+$/, '')).join(' ');
assert.ok(rejoined.includes('End.'), 'the tail of the post disappeared in the split');
for (const p of split.posts) assert.ok(!/\w-$/.test(p.text.replace(/ \d+\/\d+$/, '')), 'a word was cut in half');
});
test('splitPost makes progress even on one enormous unbroken word', () => {
const parts = splitPost('q'.repeat(900), 100);
assert.ok(parts.length > 1);
assert.equal(parts.join('').length, 900, 'characters were lost in a hard cut');
});
test('a thread that is really a blog post is refused', () => {
const many = Array.from({ length: MAX_THREAD_POSTS + 1 }, (_, i) => `post ${i}`);
const r = buildThread(many, { platform: 'x' });
assert.equal(r.ok, false);
assert.match(r.error, /that is a blog post, not a thread/);
assert.equal(buildThread([], {}).ok, false);
assert.equal(buildThread(['', ' '], {}).ok, false, 'blank posts counted as posts');
});
test('the rendered thread carries the counts a human needs to check it', () => {
const t = buildThread(THREAD.posts, { platform: 'x' });
const md = renderThread(t, { title: IDEA });
assert.ok(md.includes('/280'), 'the limit is not shown beside each post');
assert.ok(md.includes('1/3') && md.includes('3/3'));
assert.ok(md.includes(THREAD.posts[2]));
});
/* ── the blog ───────────────────────────────────────────────────────────── */
test('⭐ the blog gets front matter, or nothing will render it', () => {
const r = renderBlog(BLOG, { slug: 'why', date: '2026-08-25' });
assert.equal(r.ok, true, r.error);
assert.ok(r.markdown.startsWith('---\n'));
assert.ok(r.markdown.includes('title: "Why we price per outcome: the whole argument"'), 'the colon in the title was not quoted');
assert.ok(r.markdown.includes('slug: "why"'));
assert.ok(r.markdown.includes('date: "2026-08-25"'));
assert.ok(r.markdown.includes('tags: ["pricing", "saas"]'));
assert.ok(r.markdown.includes('draft: true'), 'the post does not say it is a draft');
assert.ok(r.markdown.includes('## The problem with seats'));
/**
* ⭐ THE NUMBER IN THE FRONT MATTER IS THE NUMBER IN THE RESULT. Two counts of
* the same thing is how a manifest starts disagreeing with the file it
* describes, and the disagreement is invisible until somebody checks by hand.
*/
assert.ok(r.words > 25, `only ${r.words} words counted for a two-section post`);
assert.ok(r.markdown.includes(`word_count: ${r.words}`), 'the front matter and the result disagree about the length');
assert.equal(r.minutes, Math.max(1, Math.round(r.words / 225)));
assert.ok(r.markdown.includes(`reading_time: ${r.minutes}`));
});
test('the blog accepts raw markdown instead of sections', () => {
const r = renderBlog({ title: 'T', markdown: '# already written\n\nbody text here' }, { slug: 's', date: 'd' });
assert.equal(r.ok, true, r.error);
assert.ok(r.markdown.includes('already written'));
});
test('an empty blog refuses with the thing that is missing', () => {
assert.match(renderBlog({}, { slug: 's', date: 'd' }).error, /needs a title/);
assert.match(renderBlog({ title: 'T' }, { slug: 's', date: 'd' }).error, /markdown.*or at least one section/);
assert.match(renderBlog({ title: 'T', sections: [{ heading: 'H' }] }, { slug: 's', date: 'd' }).error, /section 1 \("H"\) has no body/);
});
/* ── the infographic ────────────────────────────────────────────────────── */
test('the infographic is vector, self-contained, and escapes its labels', () => {
const r = renderInfographic({ ...INFO, stats: [{ label: '', value: '1' }] }, { title: IDEA });
assert.equal(r.ok, true, r.error);
assert.ok(r.svg.startsWith('