Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

56.0 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

import { copyProblems, weightedLength } from '../api/_lib/x-content/quality.js';
import { SPEED_OUTPUT_FPS, attachmentProblems, mediaProblems, parseFfmpegProbe, videoFilterChain } from '../api/_lib/x-content/media.js';
import { markdownToContentState, attachArticleMedia } from '../api/_lib/x-content/articles.js';
import { DEFAULT_SLOTS, currentSlot, inQuietHours, jitterMinutes, pickDue, slotLabel, slotOpenings, tierOrder } from '../api/_lib/x-content/schedule.js';
import { linkProbeUrl } from '../api/_lib/x-content/verify.js';
import { engagementSignals, loadLifts, loadVolumeModel, rankItems, scoreItem, volumeScore } from '../api/_lib/x-content/priority.js';
import { accountRefusal, activeHolds, inventory, isPostSpecific, placeHold, runTick } from '../api/_lib/x-content/runner.js';
import { memoryStore } from '../api/_lib/x-content/state.js';
import { validateItem, validateQueue, loadQueue } from '../api/_lib/x-content/queue.js';
import { previewClient, publishItem } from '../api/_lib/x-content/publisher.js';

const root = process.cwd();
const HOUR = 3_600_000;

function sandbox() {
	const dir = mkdtempSync(join(tmpdir(), 'x-content-'));
	mkdirSync(join(dir, 'public/x-media/t'), { recursive: true });
	mkdirSync(join(dir, 'data/x-content/articles'), { recursive: true });
	writeFileSync(join(dir, 'public/x-media/t/a.png'), Buffer.alloc(64));
	writeFileSync(join(dir, 'public/x-media/t/b.png'), Buffer.alloc(64));
	writeFileSync(join(dir, 'public/x-media/t/clip.mp4'), Buffer.alloc(64));
	return dir;
}

const HEAD = 'Rig Doctor names the skeleton convention of any humanoid GLB, then lists which bones animate and which stay put: three.ws/rig-doctor';

describe('quality', () => {
	it('weights URLs as 23 characters', () => {
		expect(weightedLength('see https://three.ws/a/very/long/path/that/keeps/going')).toBe(4 + 23);
	});

	it('flags the tells of an automated feed', () => {
		const problems = copyProblems('Introducing our REVOLUTIONARY AMAZING tool!!! #ai', { minimum: 1 });
		expect(problems.join('\n')).toMatch(/banned opening/);
		expect(problems.join('\n')).toMatch(/hashtags/);
		expect(problems.join('\n')).toMatch(/exclamation/);
		expect(problems.join('\n')).toMatch(/shouting/);
		expect(problems.join('\n')).toMatch(/must link/);
	});

	it('allows tickers and acronyms in capitals', () => {
		expect(copyProblems('$THREE holders get GLTF exports over HTTP on three.ws/pay', { minimum: 1 })).toEqual([]);
	});

	it('does not require a link when the item links elsewhere', () => {
		expect(copyProblems('The rig runs on our own GPU workers.', { minimum: 1, requireUrl: false })).toEqual([]);
	});
});

describe('media', () => {
	it('allows four images, or one video or GIF alone', () => {
		const img = (n) => ({ path: `public/x-media/t/${n}.png` });
		expect(attachmentProblems([img(1), img(2), img(3), img(4)])).toEqual([]);
		expect(attachmentProblems([img(1), img(2), img(3), img(4), img(5)])).toHaveLength(1);
		expect(attachmentProblems([{ path: 'public/x-media/t/clip.mp4' }, img(1)])).toHaveLength(1);
	});

	it('requires alt text on images and media inside the image', () => {
		const dir = sandbox();
		expect(mediaProblems({ path: 'public/x-media/t/a.png' }, dir)).toEqual(['public/x-media/t/a.png: needs alt text']);
		expect(mediaProblems({ path: 'public/x-media/t/a.png', alt: 'a hero shot' }, dir)).toEqual([]);
		expect(mediaProblems({ path: 'docs/a.png', alt: 'x' }, dir).join('\n')).toMatch(/must live under/);
	});

	it('requires a probe within X video limits', () => {
		const dir = sandbox();
		const path = 'public/x-media/t/clip.mp4';
		expect(mediaProblems({ path }, dir).join('\n')).toMatch(/no probe/);
		const good = { durationSec: 12, width: 1280, height: 720, fps: 30, videoCodec: 'h264', pixFmt: 'yuv420p', audioCodec: 'aac' };
		expect(mediaProblems({ path, probe: good }, dir)).toEqual([]);
		const bad = mediaProblems({ path, probe: { ...good, durationSec: 200, width: 3840, videoCodec: 'hevc' } }, dir).join('\n');
		expect(bad).toMatch(/duration/);
		expect(bad).toMatch(/3840x720/);
		expect(bad).toMatch(/h264/);
	});

	it('parses ffmpeg stream info into a probe', () => {
		const stderr = [
			'  Duration: 00:00:06.03, start: 0.000000, bitrate: 6112 kb/s',
			'  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 5972 kb/s, 60 fps, 60 tbr, 15360 tbn (default)',
			'  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)',
		].join('\n');
		expect(parseFfmpegProbe(stderr)).toEqual({ durationSec: 6.03, width: 1920, height: 1080, fps: 60, videoCodec: 'h264', pixFmt: 'yuv420p', audioCodec: 'aac' });
	});
});

describe('articles', () => {
	it('maps inline styles and links onto correct offsets', () => {
		const { content_state } = markdownToContentState('Drop one `<agent-3d>` tag &amp; go. **Bold [link](https://three.ws) text** and ~~old~~.');
		const [block] = content_state.blocks;
		expect(block.text).toBe('Drop one <agent-3d> tag & go. Bold link text and old.');
		const bold = block.inline_style_ranges.find((range) => range.style === 'bold');
		expect(block.text.substr(bold.offset, bold.length)).toBe('Bold link text');
		const strike = block.inline_style_ranges.find((range) => range.style === 'strikethrough');
		expect(block.text.substr(strike.offset, strike.length)).toBe('old');
		const [link] = block.entity_ranges;
		expect(block.text.substr(link.offset, link.length)).toBe('link');
		expect(content_state.entities[link.key].value).toEqual({ type: 'link', mutability: 'mutable', data: { url: 'https://three.ws' } });
	});

	it('turns structure into block types and atomic embeds', () => {
		const md = '# One\n\n## Two\n\n### Three\n\n- a\n- b\n\n1. c\n\n> quote\n\n---\n\n```js\nx()\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |';
		const { content_state, markdownWeight } = markdownToContentState(md);
		expect(content_state.blocks.map((block) => block.type)).toEqual([
			'header-one', 'header-two', 'header-three', 'unordered-list-item', 'unordered-list-item', 'ordered-list-item', 'blockquote', 'atomic', 'atomic', 'atomic',
		]);
		expect(content_state.entities.map((entity) => entity.value.type)).toEqual(['divider', 'markdown', 'markdown']);
		expect(markdownWeight).toBeGreaterThan(0);
	});

	it('resolves local images and attaches uploaded media ids', () => {
		const { content_state, images, warnings } = markdownToContentState('![Hero](../../../public/x-media/t/a.png)\n\n![Remote](https://example.com/x.png)', {
			articlePath: 'data/x-content/articles/demo.md',
		});
		expect(images).toEqual([{ entityIndex: 0, path: 'public/x-media/t/a.png', caption: 'Hero' }]);
		expect(warnings).toHaveLength(1);
		const attached = attachArticleMedia(content_state, images, ['123']);
		expect(attached.entities[0].value.data.media_items).toEqual([{ media_category: 'tweet_image', media_id: '123' }]);
		expect(content_state.entities[0].value.data.media_items).toEqual([]);
	});
});

describe('schedule', () => {
	const cadence = { windowMinutes: 45, minimumMinutesApart: 240, dailyCap: 3, slots: [{ tier: 3, at: '08:00' }, { tier: 1, at: '16:00' }, { tier: 2, at: '22:00' }] };
	const item = (id, over = {}) => ({ id, status: 'approved', kind: 'post', tier: 2, lane: id, pattern: id, notBefore: '2026-09-17T00:00:00Z', posts: [{ text: HEAD }], ...over });
	// 16:50 UTC: the 16:00 T1 slot has opened whatever the jitter (under 45 min).
	const primeTime = Date.parse('2026-09-17T16:50:00Z');

	it('handles quiet hours that wrap midnight', () => {
		expect(inQuietHours(Date.parse('2026-09-17T23:30:00Z'), ['22:00', '06:00'])).toBe(true);
		expect(inQuietHours(Date.parse('2026-09-17T12:00:00Z'), ['22:00', '06:00'])).toBe(false);
	});

	it('opens three tiered slots a day at jittered minutes only the seed reproduces', () => {
		const openings = slotOpenings(primeTime, cadence).filter((slot) => slot.key.startsWith('2026-09-17'));
		expect(openings.map((slot) => slot.tier)).toEqual([3, 1, 2]);
		for (const [slot, hour] of openings.map((slot, index) => [slot, [8, 16, 22][index]])) {
			const minutes = (slot.opensAt - Date.parse(`2026-09-17T${String(hour).padStart(2, '0')}:00:00Z`)) / 60_000;
			expect(minutes).toBeGreaterThanOrEqual(0);
			expect(minutes).toBeLessThan(45);
		}
		const seeded = slotOpenings(primeTime, cadence, 'production-seed');
		expect(slotOpenings(primeTime, cadence, 'production-seed')).toEqual(seeded);
		const bySeed = new Set(['s1', 's2', 's3', 's4', 's5'].map((seed) => slotOpenings(primeTime, cadence, seed)[4].opensAt));
		expect(bySeed.size).toBeGreaterThan(1);
		expect(jitterMinutes('genesis', 60)).toBe(jitterMinutes('genesis', 60, null));
	});

	it('keeps a slot open for three hours, then closes it until the next one', () => {
		expect(currentSlot(primeTime, cadence).slot.key).toBe('2026-09-17#1');
		expect(currentSlot(Date.parse('2026-09-18T00:30:00Z'), cadence).slot.key).toBe('2026-09-17#2');
		const small = currentSlot(Date.parse('2026-09-18T04:00:00Z'), cadence);
		expect(small.slot).toBeNull();
		expect(small.next.key).toBe('2026-09-18#0');
	});

	it('fills a slot from its own tier first, then lower tiers, then a higher tier with surplus', () => {
		expect(tierOrder(1, new Map())).toEqual([1, 2, 3]);
		expect(tierOrder(3, new Map([[1, 1], [2, 1]]))).toEqual([3]);
		expect(tierOrder(3, new Map([[1, 2], [2, 2]]))).toEqual([3, 2, 1]);
		const items = [item('feature', { tier: 2, priority: 40 }), item('flagship', { tier: 1 })];
		const prime = pickDue({ items, state: {}, now: primeTime, cadence });
		expect([prime.item.id, prime.slot.tier, prime.filledDown]).toEqual(['flagship', 1, false]);
		const noFlagship = pickDue({ items: [items[0]], state: {}, now: primeTime, cadence });
		expect([noFlagship.item.id, noFlagship.filledDown]).toEqual(['feature', true]);
		// The last flagship post is kept for prime time, not spent off-peak.
		const morning = Date.parse('2026-09-17T08:50:00Z');
		expect(pickDue({ items: [item('flagship', { tier: 1 })], state: {}, now: morning, cadence }).item).toBeNull();
	});

	it('spends each slot once, and respects embargo, spacing, and the daily cap', () => {
		const used = { published: [{ id: 'z', slot: '2026-09-17#1', publishedAt: '2026-09-17T16:40:00Z' }] };
		expect(pickDue({ items: [item('a', { tier: 1 })], state: used, now: primeTime, cadence }).reason).toMatch(/2026-09-17#1 \(T1\) is used/);
		expect(pickDue({ items: [item('a', { tier: 1, notBefore: '2026-09-18T00:00:00Z' })], state: {}, now: primeTime, cadence }).reason).toMatch(/embargoed/);
		const recent = { published: [{ id: 'z', slot: 'other', publishedAt: new Date(primeTime - HOUR).toISOString() }] };
		expect(pickDue({ items: [item('a', { tier: 1 })], state: recent, now: primeTime, cadence }).reason).toMatch(/spacing/);
		const capped = { published: [5, 11, 17].map((h, i) => ({ id: `p${i}`, slot: `s${i}`, publishedAt: new Date(primeTime - h * HOUR).toISOString() })) };
		expect(pickDue({ items: [item('a', { tier: 1 })], state: capped, now: primeTime, cadence }).reason).toMatch(/daily cap/);
	});

	it('lets hand-sent posts count toward spacing but not toward the daily cap', () => {
		// Four owner-requested posts in one afternoon (no slot) used to fill the
		// rolling window and hold every scheduled slot shut the next morning.
		const manual = [8, 9, 10, 11].map((h, i) => ({ id: `m${i}`, publishedAt: new Date(primeTime - h * HOUR).toISOString() }));
		const pick = pickDue({ items: [item('a', { tier: 1 })], state: { published: manual }, now: primeTime, cadence });
		expect(pick.reason ?? '').not.toMatch(/daily cap/);
		expect(pick.item?.id).toBe('a');

		// The same posts sent minutes ago still block the slot on spacing.
		const justNow = [{ id: 'm', publishedAt: new Date(primeTime - 10 * 60_000).toISOString() }];
		expect(pickDue({ items: [item('a', { tier: 1 })], state: { published: justNow }, now: primeTime, cadence }).reason).toMatch(/spacing/);

		// Scheduled posts still fill the cap exactly as before.
		const scheduled = [5, 11, 17].map((h, i) => ({ id: `s${i}`, slot: `k${i}`, publishedAt: new Date(primeTime - h * HOUR).toISOString() }));
		expect(pickDue({ items: [item('a', { tier: 1 })], state: { published: [...manual, ...scheduled] }, now: primeTime, cadence }).reason).toMatch(/daily cap/);
	});

	it('ranks within a tier and falls to the next post when one is held', () => {
		const items = [item('low', { tier: 1, priority: -10 }), item('high', { tier: 1, priority: 30 }), item('mid', { tier: 1 })];
		const first = pickDue({ items, state: {}, now: primeTime, cadence });
		expect(first.ranking.map((row) => row.id)).toEqual(['high', 'mid', 'low']);
		expect(pickDue({ items, state: {}, now: primeTime, cadence, exclude: new Set(['high']) }).item.id).toBe('mid');
		expect(pickDue({ items, state: {}, now: primeTime, cadence, exclude: new Set(['high', 'mid', 'low']) }).reason).toMatch(/held this tick/);
	});

	it('opens an Article slot every second day, keeps Articles for it, and leaves it outside the daily cap', () => {
		const withArticles = { ...cadence, slots: [...cadence.slots, { kind: 'article', at: '02:00', everyDays: 2, from: '2026-09-17' }] };
		const articleDays = (from, days) => Array.from({ length: days }, (_, i) => from + i * 24 * HOUR)
			.filter((day) => slotOpenings(day, withArticles).some((slot) => slot.kind === 'article' && slot.key.startsWith(new Date(day).toISOString().slice(0, 10))))
			.map((day) => new Date(day).toISOString().slice(0, 10));
		expect(articleDays(Date.parse('2026-09-15T12:00:00Z'), 6)).toEqual(['2026-09-15', '2026-09-17', '2026-09-19']);
		expect(slotLabel(slotOpenings(primeTime, withArticles).find((slot) => slot.kind))).toBe('article');

		const article = item('longread', { kind: 'article', tier: 1, priority: 50, article: { title: 'How it works', body: 'x.md' } });
		const feature = item('feature', { tier: 1 });
		// The daily slot never spends the Article, however high it ranks.
		expect(pickDue({ items: [article, feature], state: {}, now: primeTime, cadence: withArticles }).item.id).toBe('feature');
		expect(pickDue({ items: [article], state: {}, now: primeTime, cadence: withArticles, quota: true }).item).toBeNull();
		// Without an Article slot an Article is an ordinary tier 1 item.
		expect(pickDue({ items: [article, feature], state: {}, now: primeTime, cadence }).item.id).toBe('longread');

		// 02:50 on an Article day: the slot takes only Articles, past a full daily cap.
		const night = Date.parse('2026-09-19T02:50:00Z');
		const capped = { published: [5, 9, 13].map((h, i) => ({ id: `p${i}`, slot: `s${i}`, publishedAt: new Date(night - h * HOUR).toISOString() })) };
		const pick = pickDue({ items: [article, feature], state: capped, now: night, cadence: withArticles, quota: true });
		expect([pick.item.id, pick.slot.kind, pick.tier, pick.filledDown, pick.filledUp]).toEqual(['longread', 'article', 1, false, false]);
		expect(pickDue({ items: [feature], state: capped, now: night, cadence: withArticles, quota: true }).reason).toMatch(/no approved unpublished article for slot 2026-09-19#3/);
		// The Article it sent does not count toward the next morning's cap.
		const sent = { published: [...capped.published.slice(1), { id: 'longread', slot: '2026-09-19#3', slotKind: 'article', publishedAt: new Date(night).toISOString() }] };
		const morning = Date.parse('2026-09-19T08:50:00Z');
		expect(pickDue({ items: [item('demo', { tier: 3 })], state: sent, now: morning, cadence: withArticles }).item.id).toBe('demo');
	});

	it('gives a series its own daily slot, keeps its posts for that slot, and lends the slot to its tier when the series is dry', () => {
		const withSeries = { ...cadence, slots: cadence.slots.map((slot) => (slot.tier === 1 ? { ...slot, series: 'partners' } : slot)) };
		expect(slotLabel(slotOpenings(primeTime, withSeries).find((slot) => slot.series))).toBe('T1 partners');

		const spotlight = item('nvidia-gpu-fleet', { tier: 2, series: 'partners' });
		const flagship = item('flagship', { tier: 1, priority: 50 });
		// The series slot takes the series post ahead of a higher-ranked flagship.
		const pick = pickDue({ items: [spotlight, flagship], state: {}, now: primeTime, cadence: withSeries });
		expect([pick.item.id, pick.slot.series, pick.series]).toEqual(['nvidia-gpu-fleet', 'partners', 'partners']);
		// With no series post ready, the slot is the tier's slot as before.
		expect(pickDue({ items: [flagship], state: {}, now: primeTime, cadence: withSeries }).item.id).toBe('flagship');

		// Every other slot leaves the series post alone, even when it would sit empty.
		const morning = Date.parse('2026-09-17T08:50:00Z');
		expect(pickDue({ items: [spotlight], state: {}, now: morning, cadence: withSeries, quota: true }).item).toBeNull();
		expect(pickDue({ items: [spotlight, item('feature', { tier: 2 })], state: {}, now: morning, cadence: withSeries, quota: true }).item.id).toBe('feature');
		// Without a series slot a series post is an ordinary item of its tier.
		expect(pickDue({ items: [spotlight], state: {}, now: morning, cadence, quota: true }).item.id).toBe('nvidia-gpu-fleet');
	});

	it('counts a series stock on its own line', () => {
		const dir = sandbox();
		const items = [
			{ id: 'a', status: 'approved', tier: 1, kind: 'post', posts: [{ text: 'a' }] },
			{ id: 's1', status: 'approved', tier: 2, kind: 'post', series: 'partners', posts: [{ text: 's1' }] },
			{ id: 's2', status: 'approved', tier: 2, kind: 'post', series: 'partners', posts: [{ text: 's2' }] },
		];
		const cadence = { slots: [{ tier: 1, series: 'partners', at: '15:30' }, { tier: 2, at: '12:00' }] };
		expect(inventory(items, { published: [] }, dir, Date.now(), cadence)).toEqual([
			{ tier: 1, days: 1, low: true },
			{ tier: 2, days: 0, low: true },
			{ tier: 3, days: 0, low: true },
			{ series: 'partners', count: 2, days: 2, low: true },
		]);
	});

	it('resumes a half-published item ahead of pacing and priority', () => {
		const state = { published: [{ id: 'p', publishedAt: new Date(primeTime - 10 * 60_000).toISOString() }], inflight: { a: { media: {}, postIds: ['1'] } } };
		expect(pickDue({ items: [item('a'), item('b', { priority: 50 })], state, now: primeTime, cadence }).resuming).toBe(true);
	});
});

const tierOfPick = (pick) => Number(pick.item.tier);

describe('priority', () => {
	const lifts = loadLifts(root);
	const post = (text, over = {}) => ({ id: 'x', kind: 'post', lane: 'l', pattern: 'p', notBefore: '2026-09-17T00:00:00Z', posts: [{ text, media: [{ path: 'public/x.webp' }] }], ...over });

	it('reads the measured lifts from the engagement report', () => {
		expect(lifts.get('topic:token').lift).toBeGreaterThan(1);
		const keys = engagementSignals(post('The $THREE layer, on @awscloud: three.ws/x'), lifts).map((row) => row.key);
		expect(keys).toEqual(expect.arrayContaining(['format:image', 'format:mention', 'format:cashtag', 'topic:token']));
	});

	it('shrinks small samples and does not stack overlapping signals', () => {
		const scored = scoreItem(post('The $THREE layer, on @awscloud, explained for partners: three.ws/x'), { lifts, now: Date.parse('2026-09-17T00:00:00Z') });
		expect(scored.predictedLift).toBeGreaterThan(1.5);
		expect(scored.predictedLift).toBeLessThan(10);
	});

	it('ranks on the chance of a volume response, not on engagement, when the volume model ships', () => {
		const model = loadVolumeModel(root);
		expect(model.baseRate).toBeGreaterThan(0);
		expect(model.features.map((row) => row.key)).toEqual(expect.arrayContaining(['launch', 'recognition', 'tier1', 'long', 'thread', 'video']));

		const plain = post('A short note about a tool: three.ws/x');
		const partner = post('three.ws has been accepted into the @nvidia Inception program. The rendering pipeline that ships your agent is now live for every creator on the platform, and the partnership opens GPU capacity we could not reach before: three.ws/nvidia', { posts: [{ text: 'three.ws has been accepted into the @nvidia Inception program. The rendering pipeline that ships your agent is now live for every creator on the platform, and the partnership opens GPU capacity we could not reach before: three.ws/nvidia', media: [{ path: 'public/x.webp' }] }, { text: 'What it unlocks, in detail.', media: [] }] });
		expect(volumeScore(partner, model).found).toEqual(expect.arrayContaining(['tier1', 'recognition', 'launch', 'long', 'thread']));
		expect(volumeScore(partner, model).chance).toBeGreaterThan(volumeScore(plain, model).chance * 2);

		const context = { lifts, now: Date.parse('2026-09-17T00:00:00Z') };
		const scored = scoreItem(partner, context);
		expect(scored.parts.volume).toBeGreaterThan(0);
		expect(scored.parts.engagement).toBeUndefined();
		expect(scoreItem(plain, context).parts.volume).toBeLessThan(0);
		expect(rankItems([{ ...plain, id: 'plain' }, { ...partner, id: 'partner' }], context)[0].item.id).toBe('partner');
	});

	it('falls back to the engagement estimate when no volume model is present', () => {
		const withoutModel = Object.assign(new Map(lifts), { volumeModel: null });
		const scored = scoreItem(post('The $THREE layer, on @awscloud: three.ws/x'), { lifts: withoutModel, now: Date.parse('2026-09-17T00:00:00Z') });
		expect(scored.parts.engagement).toBeGreaterThan(0);
		expect(scored.parts.volume).toBeUndefined();
		expect(scored.volumeChance).toBeNull();
	});

	it('keeps the three daily slots inside the volume window, and every slot far enough apart that all of them can post', () => {
		// Owner cadence (2026-10-01): three posts a day inside 12:00 to 20:00 UTC,
		// where the volume study found a post is most often followed by a volume
		// response. Owner directive (2026-10-08): one X Article every second day,
		// in its own slot after the last daily post.
		const queue = loadQueue(root);
		const toMinutes = (at) => Number(at.slice(0, 2)) * 60 + Number(at.slice(3));
		const daily = queue.cadence.slots.filter((slot) => !slot.kind);
		expect(daily).toHaveLength(3);
		for (const slot of daily) {
			expect(toMinutes(slot.at)).toBeGreaterThanOrEqual(12 * 60);
			expect(toMinutes(slot.at) + queue.cadence.windowMinutes).toBeLessThanOrEqual(20 * 60);
		}
		expect(queue.cadence.slots.filter((slot) => slot.kind)).toEqual([{ kind: 'article', at: '22:30', everyDays: 2, from: '2026-10-08' }]);

		// The latest a slot can open, to the earliest the next one can: never closer
		// than the minimum gap, or the later slot would be blocked by the earlier post.
		// The Article evening is the tightest day, so measure every slot together.
		const minutes = queue.cadence.slots.map((slot) => toMinutes(slot.at)).sort((a, b) => a - b);
		for (let index = 1; index <= minutes.length; index++) {
			const next = index < minutes.length ? minutes[index] : minutes[0] + 24 * 60;
			expect(next - (minutes[index - 1] + queue.cadence.windowMinutes)).toBeGreaterThanOrEqual(queue.cadence.minimumMinutesApart);
		}
	});

	it('opens all three slots every day, weekends included, and still lets a flagship take the flagship slot', () => {
		// Owner directive (2026-09-20): three a day, Sundays too. The queue no
		// longer sets flagshipWeekdaysOnly, so no slot is withheld on a weekend.
		const cadence = loadQueue(root).cadence;
		expect(cadence.flagshipWeekdaysOnly).toBeUndefined();
		const saturday = Date.parse('2026-09-19T16:20:00Z');
		const sunday = Date.parse('2026-09-20T16:20:00Z');
		const monday = Date.parse('2026-09-21T16:45:00Z');
		const tiersOn = (day) => slotOpenings(day, cadence, 'seed').filter((slot) => !slot.kind && slot.key.startsWith(new Date(day).toISOString().slice(0, 10))).map((slot) => slot.tier);
		const everyDay = cadence.slots.filter((slot) => !slot.kind).map((slot) => Number(slot.tier));
		for (const day of [saturday, sunday, monday]) expect(tiersOn(day)).toEqual(everyDay);

		// A weekend flagship slot goes to a flagship post, not down to a feature.
		const flagship = (id) => ({ id, status: 'approved', kind: 'post', tier: 1, lane: id, pattern: id, notBefore: '2026-09-01T00:00:00Z', posts: [{ text: `${id} is now live: three.ws/x`, media: [] }] });
		const feature = { id: 'feature', status: 'approved', kind: 'post', tier: 2, lane: 'f', pattern: 'f', notBefore: '2026-09-01T00:00:00Z', posts: [{ text: 'A feature: three.ws/x', media: [] }] };
		const items = [flagship('one'), flagship('two'), feature];
		const pick = (now) => pickDue({ items, state: { published: [] }, now, cadence, quality: {}, seed: 'seed', lifts: null, reviews: new Map(), exclude: new Set() });
		const sundayFlagship = slotOpenings(sunday, cadence, 'seed').find((slot) => slot.key.startsWith('2026-09-20') && slot.tier === 1);
		expect(tierOfPick(pick(sundayFlagship.opensAt + 60_000))).toBe(1);
		const sundayFeature = slotOpenings(sunday, cadence, 'seed').find((slot) => slot.key.startsWith('2026-09-20') && slot.tier === 2);
		expect(pick(sundayFeature.opensAt + 60_000).item.id).toBe('feature');
	});

	it('adds owner boost, timeliness, waiting, and review; penalizes repetition; drops expired posts', () => {
		const now = Date.parse('2026-09-20T00:00:00Z');
		const base = scoreItem(post('A plain update about avatars: three.ws/x'), { lifts, now });
		expect(base.parts.waiting).toBe(3);
		expect(scoreItem(post('A plain update about avatars: three.ws/x', { priority: 20 }), { lifts, now }).score).toBeCloseTo(base.score + 20, 5);
		expect(scoreItem(post('A plain update about avatars: three.ws/x', { expiresAt: '2026-09-20T12:00:00Z' }), { lifts, now }).parts.timely).toBe(15);
		expect(scoreItem(post('A plain update about avatars: three.ws/x', { expiresAt: '2026-09-19T00:00:00Z' }), { lifts, now }).expired).toBe(true);
		const review = { editor: { scores: { a: 5, b: 5, c: 5, d: 5, e: 5, f: 5 } } };
		expect(scoreItem(post('A plain update about avatars: three.ws/x'), { lifts, now, review }).parts.review).toBe(5);
		const published = [{ id: 'q', lane: 'l', pattern: 'p', publishedAt: '2026-09-19T00:00:00Z' }];
		expect(scoreItem(post('A plain update about avatars: three.ws/x'), { lifts, now, published, quality: { maximumSamePatternInARow: 1 } }).parts.variety).toBe(-25);
		const ranked = rankItems([post('A plain update about avatars: three.ws/x', { id: 'old' }), post('A plain update about avatars: three.ws/x', { id: 'gone', expiresAt: '2026-09-19T00:00:00Z' })], { lifts, now });
		expect(ranked.map((row) => row.item.id)).toEqual(['old']);
	});
});

describe('holds and fall-through', () => {
	it('tells a post problem from an account or platform problem', () => {
		expect(isPostSpecific({ code: 403, data: { detail: 'You are not allowed to create a Tweet with duplicate content.' } })).toBe(true);
		expect(isPostSpecific({ code: 400, message: 'invalid media' })).toBe(true);
		expect(isPostSpecific({ code: 403, data: { detail: 'forbidden' } })).toBe(false);
		expect(isPostSpecific({ code: 401 })).toBe(false);
		expect(isPostSpecific({ code: 429 })).toBe(false);
		expect(isPostSpecific({ code: 402, data: { detail: 'credits depleted', type: 'https://api.x.com/2/problems/credits-depleted' } })).toBe(false);
		expect(isPostSpecific({ code: 503 })).toBe(false);
		expect(isPostSpecific(new Error('socket hang up'))).toBe(false);
	});

	it('names an account-level refusal in words the owner can act on', () => {
		expect(accountRefusal({ code: 402, data: { detail: 'credits depleted' } })).toMatch(/run out of credits; top it up/);
		expect(accountRefusal({ code: 402, data: { detail: 'payment required' } })).toMatch(/402 Payment Required/);
		expect(accountRefusal({ code: 401 })).toMatch(/rejected the credentials/);
		expect(accountRefusal({ code: 403, data: { detail: 'forbidden' } })).toMatch(/refused the account/);
		expect(accountRefusal({ code: 403, data: { detail: 'You are not allowed to create a Tweet with duplicate content.' } })).toBe(null);
		expect(accountRefusal({ code: 429 })).toBe(null);
		expect(accountRefusal({ code: 503 })).toBe(null);
	});

	it('stops on an account refusal without holding the post, and keeps it ready for the next tick', async () => {
		const { mkdtempSync: mk, mkdirSync: md, writeFileSync: wf } = await import('node:fs');
		const { contentHash: hashOf, reviewPath: pathOf } = await import('../api/_lib/x-content/review.js');
		const dir = mk(join(tmpdir(), 'x-refusal-'));
		md(join(dir, 'data/x-content/reviews'), { recursive: true });
		const cadence = { slots: [{ tier: 2, at: '12:00' }], windowMinutes: 0, minimumMinutesApart: 1, dailyCap: 3, quietHoursUtc: null };
		const item = { id: 'ready', status: 'approved', kind: 'post', tier: 2, lane: 'a', pattern: 'b', notBefore: '2026-09-17T00:00:00Z', textOnly: true, posts: [{ text: HEAD }] };
		wf(join(dir, pathOf(item.id)), JSON.stringify({ id: item.id, contentHash: hashOf(item, dir), reviewedAt: '2026-09-17T00:00:00Z', passed: true, blockers: [] }));
		wf(join(dir, 'data/x-content/queue.json'), JSON.stringify({ account: 'trythreews', cadence, items: [item] }));
		const store = memoryStore();
		const client = previewClient();
		client.tweet = async () => {
			throw Object.assign(new Error('Request failed with code 402'), { code: 402, data: { detail: 'credits depleted' } });
		};
		const now = Date.parse('2026-09-17T12:30:00Z');
		await expect(runTick({ root: dir, store, dryRun: false, now, env: {}, client, checks: async () => [], outcomes: null })).rejects.toThrow('402');
		const state = await store.load();
		expect(state.holds?.ready).toBeUndefined();
		expect(Object.keys(state.accountAlerted)).toEqual([accountRefusal({ code: 402, data: { detail: 'credits depleted' } })]);
	});

	it('backs off a held post and releases it when the post changes', () => {
		const dir = sandbox();
		const item = { id: 'a', kind: 'post', posts: [{ text: HEAD }] };
		const state = {};
		const now = Date.parse('2026-09-17T00:00:00Z');
		expect(placeHold(state, item, dir, 'link down', now).until).toBe('2026-09-17T02:00:00.000Z');
		expect(placeHold(state, item, dir, 'link down', now).until).toBe('2026-09-17T06:00:00.000Z');
		expect(placeHold(state, item, dir, 'link down', now).until).toBe('2026-09-18T00:00:00.000Z');
		expect([...activeHolds(state, [item], dir, now)]).toEqual(['a']);
		expect([...activeHolds(state, [{ ...item, posts: [{ text: `${HEAD} Now fixed.` }] }], dir, now)]).toEqual([]);
		expect([...activeHolds(state, [item], dir, Date.parse('2026-09-18T00:00:01Z'))]).toEqual([]);
	});

	it('counts days of stock per tier and flags the thin ones', () => {
		const dir = sandbox();
		const items = [
			{ id: 'a', status: 'approved', tier: 1, kind: 'post', posts: [{ text: 'a' }] },
			{ id: 'b', status: 'approved', tier: 2, kind: 'post', posts: [{ text: 'b' }] },
			{ id: 'c', status: 'approved', tier: 2, kind: 'post', posts: [{ text: 'c' }] },
			{ id: 'd', status: 'approved', tier: 2, kind: 'post', posts: [{ text: 'd' }] },
			{ id: 'e', status: 'review', tier: 3, kind: 'post', posts: [{ text: 'e' }] },
		];
		const stock = inventory(items, { published: [{ id: 'a' }] }, dir);
		expect(stock).toEqual([{ tier: 1, days: 0, low: true }, { tier: 2, days: 3, low: false }, { tier: 3, days: 0, low: true }]);

		// An Article slot every two days: Articles are counted for it, not for their tier.
		items.push({ id: 'f', status: 'approved', tier: 1, kind: 'article', article: { title: 'f' } });
		const cadence = { slots: [{ tier: 1, at: '15:30' }, { kind: 'article', at: '22:30', everyDays: 2 }] };
		expect(inventory(items, { published: [{ id: 'a' }] }, dir, Date.now(), cadence)).toEqual([
			{ tier: 1, days: 0, low: true },
			{ tier: 2, days: 3, low: false },
			{ tier: 3, days: 0, low: true },
			{ kind: 'article', count: 1, days: 2, low: true },
		]);
	});

	it('never ends a tick on a failed post: it holds it and publishes the next best', async () => {
		const { contentHash, reviewPath } = await import('../api/_lib/x-content/review.js');
		const dir = sandbox();
		mkdirSync(join(dir, 'data/x-content/reviews'), { recursive: true });
		const texts = {
			broken: 'Rig Doctor names the skeleton convention of a humanoid GLB in the browser and lists the joints that stay frozen: three.ws/broken',
			rejected: 'Genesis turns a sentence or a selfie into a rigged 3D agent with a custodial wallet, a persona, and a voice: three.ws/rejected',
			working: 'Materialize measures the true solid volume of a repaired mesh and prices a physical print from that measurement: three.ws/working',
		};
		const items = Object.entries(texts).map(([id, text], index) => ({
			id, status: 'approved', kind: 'post', tier: 1, lane: id, pattern: id, priority: 40 - index * 20,
			notBefore: '2026-09-17T00:00:00Z', textOnly: true, posts: [{ text }],
			probes: [{ type: 'api', url: `https://three.ws/api/${id}` }],
		}));
		for (const item of items) {
			writeFileSync(join(dir, reviewPath(item.id)), JSON.stringify({ id: item.id, contentHash: contentHash(item, dir), reviewedAt: '2026-09-17T00:00:00Z', passed: true, blockers: [] }));
		}
		const cadence = { windowMinutes: 45, minimumMinutesApart: 240, dailyCap: 3, slots: [{ tier: 3, at: '08:00' }, { tier: 1, at: '16:00' }, { tier: 2, at: '22:00' }] };
		writeFileSync(join(dir, 'data/x-content/queue.json'), JSON.stringify({ account: 'trythreews', cadence, items }));

		const store = memoryStore();
		const client = previewClient();
		const tweet = client.tweet;
		client.tweet = async (payload) => {
			if (payload.text.includes('three.ws/rejected')) throw Object.assign(new Error('duplicate content'), { code: 403, data: { detail: 'You are not allowed to create a Tweet with duplicate content.' } });
			return tweet(payload);
		};
		const checks = async (item) => (item.id === 'broken' ? [{ kind: 'probe:api', target: 'https://three.ws/api/broken', ok: false, detail: 'HTTP 500' }] : []);
		const now = Date.parse('2026-09-17T16:50:00Z');

		const result = await runTick({ root: dir, store, dryRun: false, now, client, checks, env: {} });
		expect(result.blocked).toEqual([]);
		expect(result.held.map((row) => row.id)).toEqual(['broken', 'rejected']);
		expect(result.held[0].reason).toMatch(/probe:api .*HTTP 500/);
		expect(result.held[1].reason).toMatch(/X rejected the post/);
		expect(result.published.id).toBe('working');
		expect(result.published.slot).toBe('2026-09-17#1');

		const state = await store.load();
		expect(Object.keys(state.holds).sort()).toEqual(['broken', 'rejected']);
		expect(state.inflight.rejected).toBeUndefined();

		// An account-wide failure is not the post's fault: it stops the tick
		// instead of burning through the queue, and holds nothing new.
		const outage = previewClient();
		outage.tweet = async () => {
			throw Object.assign(new Error('Service Unavailable'), { code: 503 });
		};
		const nextSlot = Date.parse('2026-09-17T22:50:00Z');
		await expect(runTick({ root: dir, store, dryRun: false, now: nextSlot, client: outage, checks: async () => [], env: {} })).rejects.toThrow('Service Unavailable');
		expect(Object.keys((await store.load()).holds).sort()).toEqual(['broken', 'rejected']);
	});
});

describe('queue', () => {
	it('keeps the committed queue valid for every item headed to X', () => {
		const queue = loadQueue(root);
		const { problems } = validateQueue(queue, root);
		for (const item of queue.items.filter((row) => row.status === 'approved')) expect(problems[item.id]).toEqual([]);
	});

	it('speeds a recording up without desyncing its captions, and pins a constant rate', () => {
		// A headless WebGL capture renders about 3 frames a second and the encoder
		// pads it to 25, so 87%% of the shipped frames were repeats. Speeding up
		// drops the padding rather than the content.
		const plain = videoFilterChain();
		expect(plain.some((f) => f.startsWith('setpts'))).toBe(false);
		expect(plain.some((f) => f.startsWith('fps='))).toBe(false);

		const fast = videoFilterChain({ speed: 3 });
		expect(fast.slice(-2)).toEqual(['setpts=PTS/3', `fps=${SPEED_OUTPUT_FPS}`]);

		// Captions burn in against the original timeline and are sped up with the
		// picture; putting setpts first would render them at a third of their cue.
		const captioned = videoFilterChain({ speed: 2, subtitlesPath: '/tmp/cues.srt' });
		const subtitleAt = captioned.findIndex((f) => f.startsWith('subtitles='));
		const setptsAt = captioned.findIndex((f) => f.startsWith('setpts'));
		expect(subtitleAt).toBeGreaterThan(-1);
		expect(setptsAt).toBeGreaterThan(subtitleAt);
	});

	it('refuses a second cashtag, which X rejects at publish', () => {
		// X answers 403 "Posts are limited to a maximum of one cashtag" and the
		// publisher cannot retry around it, so review has to catch it first.
		const one = 'Forge Max is a $THREE holder perk. You hold $25 in it, you do not spend it, and the 200k-poly lane turns on: three.ws/three';
		expect(copyProblems(one)).toEqual([]);
		const two = 'Forge Max is a $THREE holder perk. You hold $25 in $THREE, you do not spend it, and the 200k lane turns on: three.ws/three';
		expect(copyProblems(two)).toEqual(['2 cashtags ($THREE); X allows one per post, so keep the first and write the rest in plain words']);
		// A bare dollar amount is not a cashtag.
		expect(copyProblems('Bronze at $25 takes 5% off compute and $500 takes 20%, which is the whole ladder: three.ws/three')).toEqual([]);
	});

	it('takes a post id for a quote tweet and refuses a pasted URL', async () => {
		const { publishItem, previewClient } = await import('../api/_lib/x-content/publisher.js');
		const dir = sandbox();
		const base = { id: 'quote-post', status: 'draft', kind: 'post', tier: 2, lane: 'l', pattern: 'p', notBefore: '2026-09-20T00:00:00Z', textOnly: true, posts: [{ text: HEAD }, { text: 'A reply that carries the rest of it.' }] };
		expect(validateItem({ ...base, quotes: '2101524482313920723' }, dir)).toEqual([]);
		expect(validateItem({ ...base, quotes: 'https://x.com/trythreews/status/2101524482313920723' }, dir)).toContain('quotes must be a post id (digits only), not a URL');

		// Only the head quotes; the reply still threads under it.
		const client = previewClient();
		const state = { inflight: {} };
		await publishItem({ item: { ...base, quotes: '2101524482313920723' }, client, root: dir, state, store: { save: async () => {} } });
		const tweets = client.calls.filter((call) => call.call === 'tweets.create');
		expect(tweets[0].quote_tweet_id).toBe('2101524482313920723');
		expect(tweets[1].quote_tweet_id).toBeUndefined();
		expect(tweets[1].reply.in_reply_to_tweet_id).toBe(tweets[0] && 'preview-post-1');
	});

	it('requires media on the head post unless text-only is explicit', () => {
		const dir = sandbox();
		const base = { id: 'demo', status: 'review', kind: 'post', tier: 2, lane: 'l', pattern: 'p', notBefore: '2026-09-17T14:00:00Z' };
		expect(validateItem({ ...base, posts: [{ text: HEAD }] }, dir).join('\n')).toMatch(/no media/);
		expect(validateItem({ ...base, textOnly: true, posts: [{ text: HEAD }] }, dir)).toEqual([]);
	});

	// A story written with the shorthand "media": "reel" threw inside adopt,
	// after adopt had already deleted the stories before it.
	it('reports media that is not a list instead of throwing', () => {
		const dir = sandbox();
		const base = { id: 'demo', status: 'draft', kind: 'post', tier: 2, lane: 'l', pattern: 'p', notBefore: '2026-09-17T14:00:00Z' };
		const problems = () => validateItem({ ...base, posts: [{ text: HEAD, media: 'reel' }] }, dir);
		expect(problems).not.toThrow();
		expect(problems().join('\n')).toMatch(/head: media must be a list/);
	});

	it('validates an Article end to end', () => {
		const dir = sandbox();
		writeFileSync(join(dir, 'data/x-content/articles/demo.md'), '## Why\n\nBecause.\n\n![Hero](../../../public/x-media/t/b.png)\n\nThe end.');
		const item = {
			id: 'demo', status: 'review', kind: 'article', tier: 1, lane: 'article', pattern: 'longform', notBefore: '2026-09-17T14:00:00Z',
			article: { title: 'How rigs get named', body: 'data/x-content/articles/demo.md', cover: { path: 'public/x-media/t/a.png' } },
			posts: [{ text: 'The long version of how Rig Doctor reads a skeleton.' }],
		};
		const draft = { ...item, status: 'draft' };
		expect(validateItem(draft, dir)).toEqual([]);
		expect(validateItem({ ...draft, article: { ...item.article, cover: undefined } }, dir).join('\n')).toMatch(/cover/);
	});

	it('holds an Article for review to the depth the X Article limits allow', () => {
		const dir = sandbox();
		const short = '## Why\n\nBecause.\n\n![Hero](../../../public/x-media/t/b.png)\n\nThe end.';
		writeFileSync(join(dir, 'data/x-content/articles/demo.md'), short);
		const item = {
			id: 'demo', status: 'review', kind: 'article', tier: 1, lane: 'article', pattern: 'longform', notBefore: '2026-09-17T14:00:00Z',
			article: { title: 'How rigs get named', body: 'data/x-content/articles/demo.md', cover: { path: 'public/x-media/t/a.png' } },
			posts: [{ text: 'The long version of how Rig Doctor reads a skeleton.' }],
		};
		const problems = validateItem(item, dir).join('\n');
		for (const pattern of [/title is \d+ characters/, /body is \d+ words/, /\d+ sections/, /code blocks and tables total/, /inline images/, /The partners behind/, /"Try it"/]) expect(problems).toMatch(pattern);

		mkdirSync(join(dir, 'pages'), { recursive: true });
		writeFileSync(join(dir, 'pages/partners.html'), '<h3 class="partner-name">Acme Cloud</h3><h3 class="partner-name">Globex</h3>');
		const sentence = 'Each stage reads the file, names what it found and hands the result to the next stage in plain words. ';
		const section = (title) => `## ${title}\n\n${sentence.repeat(26)}\n\n`;
		const code = '```js\n' + 'const stage = run(input);\n'.repeat(120) + '```\n\n';
		const image = (name) => `![Frame](../../../public/x-media/t/${name}.png)\n\n`;
		const full = (partners) => ['One', 'Two', 'Three', 'Four', 'Five', 'Six'].map(section).join('') + code + image('a') + image('b') + image('a') + image('b')
			+ `## The partners behind the work\n\n${partners}\n\n## Try it\n\nOpen three.ws/rig-doctor.\n`;
		const longTitle = 'How a skeleton gets its name in 41 steps, and what each step tells you about every humanoid rig';
		const deep = { ...item, article: { ...item.article, title: longTitle } };
		writeFileSync(join(dir, 'data/x-content/articles/demo.md'), full('Thanks to Acme Cloud for the compute.'));
		expect(validateItem(deep, dir).join('\n')).toMatch(/does not cover Globex/);
		writeFileSync(join(dir, 'data/x-content/articles/demo.md'), full('Thanks to Acme Cloud for the compute and Globex for the review.'));
		const left = validateItem(deep, dir).filter((problem) => /article (title|body)|code blocks|inline images|partners|Try it/.test(problem));
		expect(left).toEqual([]);
	});
});

describe('publisher', () => {
	const store = (log) => ({ save: async (state) => log.push(structuredClone(state)) });

	it('chains a thread with media and alt text on the head', async () => {
		const dir = sandbox();
		const client = previewClient();
		const state = { published: [], inflight: {} };
		const item = {
			id: 'demo', kind: 'post', lane: 'l', pattern: 'p',
			posts: [{ text: HEAD, media: [{ path: 'public/x-media/t/a.png', alt: 'alt one' }] }, { text: 'second' }],
		};
		const row = await publishItem({ item, client, root: dir, state, store: store([]) });
		expect(client.calls.map((call) => call.call)).toEqual(['media.upload', 'media.metadata', 'tweets.create', 'tweets.create']);
		expect(client.calls[3].reply.in_reply_to_tweet_id).toBe(row.postIds[0]);
		expect(state.inflight).toEqual({});
		expect(state.published[0].url).toBe(`https://x.com/trythreews/status/${row.postIds[0]}`);
	});

	it('resumes after a failure without reposting what already went out', async () => {
		const dir = sandbox();
		const state = { published: [], inflight: {} };
		const item = { id: 'demo', kind: 'post', lane: 'l', pattern: 'p', posts: [{ text: HEAD }, { text: 'two' }, { text: 'three' }] };
		const flaky = previewClient();
		const tweet = flaky.tweet;
		let calls = 0;
		flaky.tweet = async (payload) => {
			if (++calls === 2) throw new Error('X 503');
			return tweet(payload);
		};
		await expect(publishItem({ item, client: flaky, root: dir, state, store: store([]) })).rejects.toThrow('X 503');
		expect(state.inflight.demo.postIds).toHaveLength(1);

		const retry = previewClient();
		await publishItem({ item, client: retry, root: dir, state, store: store([]) });
		expect(retry.calls.map((call) => call.text)).toEqual(['two', 'three']);
		expect(retry.calls[0].reply.in_reply_to_tweet_id).toBe(state.published[0].postIds[0]);
	});

	it('drafts, publishes, then quotes an Article', async () => {
		const dir = sandbox();
		writeFileSync(join(dir, 'data/x-content/articles/demo.md'), '## Why\n\n![Hero](../../../public/x-media/t/b.png)\n\nText.');
		const client = previewClient();
		const state = { published: [], inflight: {} };
		const item = {
			id: 'demo', kind: 'article', lane: 'article', pattern: 'longform',
			article: { title: 'Title', body: 'data/x-content/articles/demo.md', cover: { path: 'public/x-media/t/a.png' } },
			posts: [{ text: 'Quote post' }],
		};
		const row = await publishItem({ item, client, root: dir, state, store: store([]) });
		const draft = client.calls.find((call) => call.call === 'POST /2/articles/draft');
		expect(draft.body.cover_media.media_category).toBe('tweet_image');
		const image = draft.body.content_state.entities.find((entity) => entity.value.type === 'image');
		expect(image.value.data.media_items[0].media_id).toMatch(/^preview-media/);
		const quote = client.calls.at(-1);
		expect(quote.quote_tweet_id).toBe(row.articlePostId);
		expect(row.url).toBe(`https://x.com/trythreews/status/${row.articlePostId}`);
	});
});

describe('editorial', () => {
	it('passes the reviewed Genesis copy untouched', async () => {
		const { languageProblems, assertionsIn } = await import('../api/_lib/x-content/editorial.js');
		const text = 'A sentence or a selfie becomes a rigged 3D agent holding its own custodial @solana wallet, a persona, and a voice. It walks and emotes on arrival: three.ws/genesis';
		expect(languageProblems(text)).toEqual([]);
		expect(assertionsIn(text)).toEqual({ numbers: [], absolutes: [] });
	});

	it('blocks brand errors, financial promotion, slang, pushy asks, and ad-copy structure', async () => {
		const { languageProblems } = await import('../api/_lib/x-content/editorial.js');
		const rules = (text) => languageProblems(text).map((row) => `${row.rule}:${row.severity}`);
		expect(rules('Three.ws ships agents on Github: three.ws/a')).toEqual(['brand:blocking', 'brand:blocking', 'links:blocking']);
		expect(rules('$THREE has 100x potential for holders: three.ws/a')).toContain('compliance:blocking');
		expect(rules('gm frens, agents are live: three.ws/a')).toContain('register:blocking');
		expect(rules('A robust agent runtime: three.ws/a')).toEqual(['register:major']);
		expect(rules('Agents are live, check it out: three.ws/a')).toContain('register:blocking');
		expect(rules('Fast. Simple. Onchain. three.ws/a')).toContain('structure:blocking');
		expect(rules('What if agents could pay? They can: three.ws/a')).toContain('structure:blocking');
		expect(rules('Two links: three.ws/a and three.ws/b')).toContain('links:blocking');
	});

	it('counts a bare domain as a link, the way X renders it', async () => {
		const { urlsIn, weightedLength } = await import('../api/_lib/x-content/quality.js');
		const { languageProblems } = await import('../api/_lib/x-content/editorial.js');
		expect(urlsIn('Agents on three.ws spend budgets: https://builder.aws.com/x')).toEqual(['three.ws', 'https://builder.aws.com/x']);
		expect(urlsIn('A .glb loads in Three.js and Node.js; write to [email protected]')).toEqual([]);
		expect(weightedLength('on three.ws')).toBe(3 + 23);
		expect(languageProblems('Agents on three.ws spend budgets: https://builder.aws.com/x').map((row) => row.rule)).toEqual(['links']);
	});

	it('treats names as names and ordinals as numbers', async () => {
		const { assertionsIn } = await import('../api/_lib/x-content/editorial.js');
		expect(assertionsIn('3D agents on ERC-8004 with $THREE: three.ws/a').numbers).toEqual([]);
		expect(assertionsIn('Rig Doctor knows 11 conventions; teach it the 12th in 40 ms at 86%').numbers).toEqual(['11', '12th', '40 ms', '86%']);
	});

	it('requires every number, absolute, and tag in the copy to be declared', async () => {
		const { claimProblems } = await import('../api/_lib/x-content/editorial.js');
		const item = { posts: [{ text: 'The first viewer with 15 rig conventions, built on @solana: three.ws/a' }] };
		const messages = claimProblems(item).map((row) => row.message).join('\n');
		expect(messages).toMatch(/"15" is an unverified number/);
		expect(messages).toMatch(/"first" is an absolute/);
		expect(messages).toMatch(/@solana has no recorded reason/);

		const declared = {
			...item,
			claims: [
				{ says: 'first viewer', evidence: [{ type: 'file', path: 'x', contains: 'y' }] },
				{ says: '15 rig conventions', evidence: [{ type: 'file', path: 'x', contains: 'y' }] },
			],
			mentions: { '@solana': 'wallets are Solana wallets' },
		};
		expect(claimProblems(declared)).toEqual([]);
		expect(claimProblems({ ...declared, claims: [...declared.claims, { says: 'not in the copy', evidence: [] }] }).map((row) => row.message).join('\n'))
			.toMatch(/does not appear in the copy[\s\S]*has no evidence/);
	});

	it('holds every number and absolute in an Article body to the claims ledger', async () => {
		const { claimProblems } = await import('../api/_lib/x-content/editorial.js');
		const item = { kind: 'article', article: { title: 'How the gate works' }, posts: [{ text: 'The write-up: three.ws/a' }] };
		const articleText = 'The raster is 640 pixels wide.\nIt is the only cutter we ship.';
		const messages = claimProblems(item, { articleText }).map((row) => row.message).join('\n');
		expect(messages).toMatch(/"640" is an unverified number/);
		expect(messages).toMatch(/"only" is an absolute/);
		const declared = {
			...item,
			claims: [
				{ says: 'The raster is 640 pixels wide', evidence: [{ type: 'file', path: 'x', contains: 'y' }] },
				{ says: 'the only cutter we ship', evidence: [{ type: 'file', path: 'x', contains: 'y' }] },
			],
		};
		expect(claimProblems(declared, { articleText })).toEqual([]);
		// Without the body a claim quoting it has nothing to appear in.
		expect(claimProblems(declared).map((row) => row.message).join('\n')).toMatch(/does not appear in the copy/);
	});

	it('lets an Article body link to several pages while a post links one', async () => {
		const { languageProblems } = await import('../api/_lib/x-content/editorial.js');
		const text = 'Try it at three.ws/holo and read the docs at three.ws/docs/holo.';
		expect(languageProblems(text).map((row) => row.rule)).toContain('links');
		expect(languageProblems(text, { body: true }).map((row) => row.rule)).not.toContain('links');
	});

	it('reads an Article body as prose, leaving code blocks out', async () => {
		const { articleProse } = await import('../api/_lib/x-content/queue.js');
		const dir = sandbox();
		mkdirSync(join(dir, 'data/x-content/articles'), { recursive: true });
		writeFileSync(join(dir, 'data/x-content/articles/a.md'), 'Opening line with 3 facts.\n\n## A heading\n\n- a list item\n\n```\nPOST /api 402\n```\n');
		const prose = articleProse(dir, { kind: 'article', article: { body: 'data/x-content/articles/a.md' } });
		expect(prose).toBe('Opening line with 3 facts.\nA heading\na list item');
		expect(articleProse(dir, { kind: 'post' })).toBe('');
		writeFileSync(join(dir, 'data/x-content/articles/b.md'), 'Bones named `mixamorig:LeftArm` map to one set.\n');
		const item = { kind: 'article', article: { body: 'data/x-content/articles/b.md' } };
		expect(articleProse(dir, item)).toBe('Bones named mixamorig:LeftArm map to one set.');
		expect(articleProse(dir, item, { inlineCode: false })).toBe('Bones named   map to one set.');
	});

	it('reads "world\'s" as an absolute and "worlds" as a plain word', async () => {
		const { ABSOLUTES } = await import('../api/_lib/x-content/editorial.js');
		expect('private invite-only worlds'.match(ABSOLUTES)).toEqual(['only']);
		expect("the world's first".match(ABSOLUTES)).toEqual(["world's", 'first']);
	});

	it('flags soft, badly cropped, undescribed, and stale media', async () => {
		const { mediaQualityProblems } = await import('../api/_lib/x-content/editorial.js');
		const { default: sharp } = await import('sharp');
		const dir = sandbox();
		await sharp({ create: { width: 800, height: 200, channels: 3, background: '#000' } }).png().toFile(join(dir, 'public/x-media/t/small.png'));
		await sharp({ create: { width: 1800, height: 1013, channels: 3, background: '#000' } }).png().toFile(join(dir, 'public/x-media/t/hero.png'));
		mkdirSync(join(dir, 'public/announce'), { recursive: true });
		writeFileSync(join(dir, 'public/announce/media-manifest.json'), JSON.stringify({ shots: { hero: { src: '/x-media/t/hero.png', route: '/hero', capturedAt: '2026-08-01T00:00:00Z' } } }));
		const now = Date.parse('2026-09-16T00:00:00Z');

		const small = await mediaQualityProblems({ text: 'x', media: [{ path: 'public/x-media/t/small.png', alt: 'short' }] }, dir, { now });
		const smallText = small.map((row) => row.message).join('\n');
		expect(smallText).toMatch(/800px wide/);
		expect(smallText).toMatch(/4\.00:1/);
		expect(smallText).toMatch(/alt text is 5 characters/);

		const stale = await mediaQualityProblems({ text: 'x', media: [{ path: 'public/x-media/t/hero.png', alt: 'The hero page with its drop zone and the four stats beneath it' }] }, dir, { now });
		expect(stale.map((row) => row.message)).toEqual([expect.stringMatching(/captured 46 days ago from \/hero/)]);
	});
});

describe('review', () => {
	it('binds approval to the exact reviewed content', async () => {
		const { approvalProblems, contentHash, reviewPath } = await import('../api/_lib/x-content/review.js');
		const dir = sandbox();
		const item = { id: 'demo', kind: 'post', posts: [{ text: HEAD, media: [{ path: 'public/x-media/t/a.png', alt: 'alt' }] }] };
		expect(approvalProblems(item, dir).join('\n')).toMatch(/no editorial review/);

		mkdirSync(join(dir, 'data/x-content/reviews'), { recursive: true });
		const record = { id: 'demo', contentHash: contentHash(item, dir), reviewedAt: '2026-09-16T00:00:00Z', passed: true, blockers: [] };
		writeFileSync(join(dir, reviewPath('demo')), JSON.stringify(record));
		const now = Date.parse('2026-09-17T00:00:00Z');
		expect(approvalProblems(item, dir, now)).toEqual([]);

		expect(approvalProblems({ ...item, posts: [{ ...item.posts[0], text: `${HEAD} ` }] }, dir, now)).toEqual([]);
		expect(approvalProblems({ ...item, posts: [{ ...item.posts[0], text: HEAD.replace('Rig', 'The rig') }] }, dir, now).join('\n')).toMatch(/changed after the last review/);
		writeFileSync(join(dir, 'public/x-media/t/a.png'), Buffer.alloc(65));
		expect(approvalProblems(item, dir, now).join('\n')).toMatch(/changed after the last review/);
		writeFileSync(join(dir, 'public/x-media/t/a.png'), Buffer.alloc(64));
		expect(approvalProblems(item, dir, Date.parse('2026-10-16T00:00:00Z')).join('\n')).toMatch(/days old/);
		writeFileSync(join(dir, reviewPath('demo')), JSON.stringify({ ...record, passed: false, blockers: ['link three.ws/a: HTTP 404'] }));
		expect(approvalProblems(item, dir, now).join('\n')).toMatch(/did not pass: link three\.ws\/a: HTTP 404/);
	});

	it('blocks an approved item in the queue validator until it is reviewed', () => {
		const dir = sandbox();
		const item = { id: 'demo', status: 'approved', kind: 'post', tier: 2, lane: 'l', pattern: 'p', notBefore: '2026-09-17T14:00:00Z', posts: [{ text: HEAD, media: [{ path: 'public/x-media/t/a.png', alt: 'The Rig Doctor page' }] }] };
		expect(validateItem(item, dir).join('\n')).toMatch(/review: no editorial review/);
		expect(validateItem({ ...item, status: 'review' }, dir)).toEqual([]);
	});

	it('never lets the editor pass its own blocking issue or a low score', async () => {
		const { parseReview } = await import('../api/_lib/x-content/editor.js');
		const scores = { accuracy: 5, clarity: 5, specificity: 5, voice: 5, professionalism: 5, visual: 5 };
		const clean = parseReview(`Here you go: ${JSON.stringify({ verdict: 'publish', scores, issues: [] })}`);
		expect(clean.verdict).toBe('publish');
		expect(parseReview(JSON.stringify({ verdict: 'publish', scores, issues: [{ severity: 'blocking', area: 'accuracy' }] })).verdict).toBe('revise');
		expect(parseReview(JSON.stringify({ verdict: 'publish', scores: { ...scores, clarity: 3 }, issues: [] })).verdict).toBe('revise');
		expect(() => parseReview(JSON.stringify({ verdict: 'ship', scores, issues: [] }))).toThrow(/verdict/);
		expect(() => parseReview('no json here')).toThrow(/no JSON/);
	});
});



describe('npm package links', () => {
	it('checks an npm package page against the registry, which answers a non-browser client', () => {
		expect(linkProbeUrl('https://npmjs.com/package/@three-ws/activity-mcp')).toBe('https://registry.npmjs.org/@three-ws%2Factivity-mcp');
		expect(linkProbeUrl('https://www.npmjs.com/package/three')).toBe('https://registry.npmjs.org/three');
		expect(linkProbeUrl('https://three.ws/knock')).toBe('https://three.ws/knock');
	});
});