Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
/**
* Start's Agent (Content Editor CE-015): the Content Agent writes a new page
* whole, or leaves suggestions on an existing one, in the background, and
* the draft goes to In review — or says it failed.
*/
const BRAND = '33333333-3333-3333-3333-333333333333';
const DRAFT = '77777777-7777-7777-7777-777777777777';
let rows;
let writes;
let claimed;
let finished;
let rpcCalls;
let reads;
const generateObject = vi.fn();
const reserveAiBudget = vi.fn();
vi.mock('../config/supabase.js', () => ({
default: {
rpc: async (name, args) => (rpcCalls.push([name, args]), { data: 'v1', error: null }),
from: (table) => {
const op = { table, kind: 'select', filters: [] };
const result = () => {
if (op.kind === 'select') {
reads.push(op);
// The organization's other drafts being written.
if (op.filters.some(([col]) => col === 'brands.organization_id')) {
return { data: rows.writingInOrg ?? [], error: null };
}
return { data: rows[table] ?? null, error: null };
}
writes.push(op);
// The claim and the finish read back their row, when they matched one.
const claim = op.kind === 'update' && op.value.agent_state === 'writing';
return { data: claim ? claimed : op.kind === 'update' ? finished : null, error: null };
};
const chain = {
select: () => chain,
eq: (col, v) => (op.filters.push([col, v]), chain),
neq: (col, v) => (op.filters.push([`${col}<>`, v]), chain),
gt: (col, v) => (op.filters.push([`${col}>`, v]), chain),
in: (col, v) => (op.filters.push([col, v]), chain),
is: (col, v) => (op.filters.push([`${col} is`, v]), chain),
or: (v) => (op.filters.push(['or', v]), chain),
order: () => chain,
range: () => chain,
limit: () => chain,
update: (value) => ((op.kind = 'update'), (op.value = value), chain),
insert: (value) => ((op.kind = 'insert'), (op.value = value), chain),
delete: () => ((op.kind = 'delete'), chain),
maybeSingle: async () => result(),
single: async () => result(),
then: (resolve, reject) => Promise.resolve(result()).then(resolve, reject),
};
return chain;
},
},
}));
class NoObjectGeneratedError extends Error {
static isInstance(err) {
return err instanceof NoObjectGeneratedError;
}
}
// The real schema helpers: a Claude call's schema is built with them (structured-output.js).
vi.mock('ai', async (importOriginal) => ({
...(await importOriginal()),
generateObject: (...args) => generateObject(...args),
NoObjectGeneratedError,
}));
const resolveModel = vi.hoisted(() => vi.fn((id) => `model:${id}`));
vi.mock('./ai-provider.js', () => ({ resolveModel }));
vi.mock('./plan-guard.js', async (importOriginal) => ({
...(await importOriginal()),
reserveAiBudget: (...args) => reserveAiBudget(...args),
}));
vi.mock('./logger.js', () => ({ logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() } }));
const readLinkTargets = vi.hoisted(() => vi.fn());
const stopIfStopped = vi.fn();
const fixUnderWay = vi.hoisted(() => vi.fn());
vi.mock('./content-runs.js', async (importOriginal) => ({
...(await importOriginal()),
stopIfStopped: (...args) => stopIfStopped(...args),
fixUnderWay: (...args) => fixUnderWay(...args),
}));
vi.mock('./content-links.js', async (importOriginal) => ({
...(await importOriginal()),
readLinkTargets: (...args) => readLinkTargets(...args),
}));
const hasOpenSuggestions = vi.hoisted(() => vi.fn());
vi.mock('./content-agent.js', async (importOriginal) => ({
...(await importOriginal()),
hasOpenSuggestions: (...args) => hasOpenSuggestions(...args),
}));
const {
WRITER_CHECKS,
WRITE_SYSTEM_PROMPT,
buildWritePrompt,
headingCase,
briefText,
lengthNow,
renderPage,
rewriteSystemPrompt,
runDraftWriting,
sourcedPage,
startDraftWriting,
suggestSystemPrompt,
writeSystemPrompt,
} = await import('./content-writer.js');
const { WRITING_GUARDRAIL } = await import('./content-brief.js');
const { REWRITE_SYSTEM_PROMPT } = await import('./content-rewrite.js');
const { NAME_PLACEHOLDER, QUOTE_PLACEHOLDER, knownPeople } = await import('./content-people.js');
const { FEATURE_CHECKS } = await import('./action-center/page-features.js');
const { textLength } = await import('./content-rewrite.js');
const {
PLACEHOLDER_RULE,
SYSTEM_PROMPT: AGENT_SYSTEM_PROMPT,
passageKey,
} = await import('./content-agent.js');
const { PlanLimitError } = await import('./plan-guard.js');
// The real hold: self-hosted here, so it reserves nothing and writes each charge.
const realGuard = await vi.importActual('./plan-guard.js');
const input = (over = {}) => ({
sources: {
promptIds: ['p1'],
fanouts: [{ query: 'pour over ratio', platforms: ['copilot-web'] }],
pages: [],
queries: [],
headings: [],
length: 3000,
},
rules: [{ key: 'faq', name: 'FAQ with 5 questions', detail: 'aim: 5' }],
textRules: [],
locked: [],
...over,
});
const draftRow = (over = {}) => ({
id: DRAFT,
brand_id: BRAND,
title: 'How to brew pour-over coffee',
page_type: 'guide',
method: 'create',
source_url: null,
body_html: '<h1>How to brew pour-over coffee</h1>',
status: 'draft',
agent_state: null,
agent_state_at: null,
opportunity_id: null,
brief: null,
action_id: null,
...over,
});
const brand = {
name: 'Acme Coffee',
industry: 'Coffee',
description: 'Beans',
organization_id: 'org-1',
language: 'en',
};
const page = {
title: 'How to brew pour-over coffee',
intro: ['Use 15 g of coffee for 250 g of water.'],
sections: [
{ heading: 'What you need', paragraphs: ['A dripper & a filter.'], bullets: ['Kettle', ''] },
{ heading: 'Step by step', paragraphs: ['Rinse, bloom, pour.'], bullets: [] },
],
faqHeading: 'FAQ',
faq: [{ question: 'How long?', answer: '[time] minutes.' }],
};
beforeEach(() => {
readLinkTargets.mockReset().mockResolvedValue({ own: [], sources: [] });
writes = [];
rpcCalls = [];
reads = [];
claimed = { id: DRAFT };
finished = { id: DRAFT };
rows = { content_drafts: draftRow(), brands: brand, prompts: [{ id: 'p1', text: 'ratio?' }] };
generateObject.mockReset().mockResolvedValue({
object: page,
usage: { inputTokens: 10, outputTokens: 20 },
});
reserveAiBudget.mockReset().mockImplementation(realGuard.reserveAiBudget);
hasOpenSuggestions.mockReset().mockResolvedValue(false);
fixUnderWay.mockReset().mockResolvedValue(false);
stopIfStopped.mockReset().mockResolvedValue(undefined);
// The model as a server without an Anthropic key picks it, whatever the shell has.
vi.stubEnv('ANTHROPIC_API_KEY', '');
vi.stubEnv('CONTENT_WRITE_MODEL', '');
});
afterEach(() => vi.unstubAllEnvs());
const SONNET = 'anthropic/claude-sonnet-5-5';
/** A call as Claude 5.5 takes it: structured output, a token cap, no sampling. */
const expectClaudeCall = (call) => {
expect(call.maxOutputTokens).toBe(32_000);
expect(call.providerOptions).toEqual({ anthropic: { structuredOutputMode: 'outputFormat' } });
for (const key of ['temperature', 'topP', 'topK']) expect(call).not.toHaveProperty(key);
expect(JSON.stringify(call.schema.jsonSchema)).not.toMatch(
/"(\$schema|maxItems|maxLength|minLength|minimum|maximum)"/,
);
};
const draftUpdates = () =>
writes.filter((w) => w.table === 'content_drafts' && w.kind === 'update').map((w) => w.value);
describe('renderPage', () => {
it('writes the editor’s HTML from text, escaped', () => {
expect(renderPage({ ...page, title: 'Pour-over <b>101</b>' })).toBe(
'<h1>Pour-over <b>101</b></h1>' +
'<p>Use 15 g of coffee for 250 g of water.</p>' +
'<h2>What you need</h2><p>A dripper & a filter.</p><ul><li><p>Kettle</p></li></ul>' +
'<h2>Step by step</h2><p>Rinse, bloom, pour.</p>' +
'<h2>FAQ</h2><h3>How long?</h3><p>[time] minutes.</p>',
);
});
it('starts each heading with a capital, as a fan-out’s words come lowercase', () => {
const html = renderPage({
...page,
sections: [{ heading: 'latest news ChatGPT', paragraphs: ['News.'], bullets: [] }],
faqHeading: 'questions',
faq: [{ question: 'how long does it take?', answer: 'Four minutes.' }],
});
expect(html).toContain('<h2>Latest news ChatGPT</h2>');
expect(html).toContain('<h2>Questions</h2><h3>How long does it take?</h3>');
});
});
describe('headingCase', () => {
it('capitalizes a lowercase first word, in the page’s language', () => {
expect(headingCase('latest news ChatGPT', 'en')).toBe('Latest news ChatGPT');
expect(headingCase('istanbul’da kahve', 'tr')).toBe('İstanbul’da kahve');
expect(headingCase(' çay rehberi ', 'tr')).toBe('Çay rehberi');
});
it('keeps a name written lowercase on purpose, a number and a heading already capitalized', () => {
expect(headingCase('iPhone tips for writers', 'en')).toBe('iPhone tips for writers');
expect(headingCase('eBay sellers', 'en')).toBe('eBay sellers');
expect(headingCase('2026 guide', 'en')).toBe('2026 guide');
expect(headingCase('Best CRM tools', 'en')).toBe('Best CRM tools');
// A language the runtime can't read still gets a capital.
expect(headingCase('latest news', 'not a locale!')).toBe('Latest news');
});
});
describe('sourcedPage', () => {
it('puts a person or a quote the agent was not given as a placeholder (CE-044)', () => {
const written = sourcedPage(
{
...page,
intro: ['Dr. Jane Doe brews with Ada Lovelace every morning.'],
faq: [
{
question: 'Who says so?',
answer: '“Slow pours taste sweeter and cleaner,” says Ada Lovelace.',
},
],
},
new Set(),
knownPeople('Ada Lovelace'),
);
expect(written.intro).toEqual([`${NAME_PLACEHOLDER} brews with Ada Lovelace every morning.`]);
expect(written.faq[0].answer).toBe(`${QUOTE_PLACEHOLDER} says Ada Lovelace.`);
});
});
describe('briefText', () => {
it('reads the draft’s brief, else its opportunity’s', () => {
const own = briefText(
{
goal: 'Sell beans',
audience: 'Home brewers',
outline: [{ heading: 'Ratio', keyPoints: ['15:250'], fanout: 'pour over ratio' }],
keywords: ['pour over'],
},
{ outline: [{ heading: 'Ignored' }] },
);
expect(own).toContain('Goal: Sell beans');
expect(own).toContain('- Ratio: 15:250 (answers the AI search "pour over ratio")');
expect(own).not.toContain('Ignored');
expect(
briefText(null, {
suggestedTitle: 'Pour-over guide',
outline: [{ heading: 'Gear', keyPoints: [] }],
targetKeywords: [],
callToAction: 'Shop',
}),
).toContain('- Gear');
expect(briefText(null, null)).toMatch(/^No brief/);
});
/** What the pages AI cites have (learning C2), as the brief's rebuild read them. */
it('tells the writer what the cited pages have, seen rather than guessed', () => {
const text = briefText(
{
goal: 'Sell beans',
outline: [{ heading: 'Ratio' }],
citedFeatures: {
pages: 4,
checks: [
{ key: 'statistic-density', label: 'Statistics', pages: 3 },
{ key: 'tables', label: 'Tables', pages: 2 },
],
},
},
null,
);
expect(text).toContain(
"What the pages AI cites for these prompts have (Site Audit's checks, 4 pages read): Statistics on 3; Tables on 2. Give the page these where they serve its readers.",
);
expect(briefText({ goal: 'Sell beans', outline: [] }, null)).not.toContain('pages AI cites');
});
it("doesn't ask the writer for what the page's body can't give: schema, dates, a byline", () => {
const template = [
{ key: 'faq-schema', label: 'FAQ schema', pages: 4 },
{ key: 'json-ld-presence', label: 'JSON-LD', pages: 4 },
{ key: 'date-markup', label: 'Date markup', pages: 3 },
{ key: 'freshness', label: 'Freshness', pages: 3 },
{ key: 'author-byline', label: 'Author byline', pages: 2 },
];
const brief = (checks) => ({
goal: 'Sell beans',
outline: [{ heading: 'Ratio' }],
citedFeatures: { pages: 4, checks },
});
const text = briefText(
brief([...template, { key: 'tables', label: 'Tables', pages: 2 }]),
null,
);
expect(text).toContain("(Site Audit's checks, 4 pages read): Tables on 2.");
for (const { label } of template) expect(text).not.toContain(label);
// Nothing the body can give: no line at all.
expect(briefText(brief(template), null)).not.toContain('pages AI cites');
// Each is a check the cited pages are read for.
expect(WRITER_CHECKS.filter((key) => !FEATURE_CHECKS.includes(key))).toEqual([]);
});
});
describe('startDraftWriting', () => {
it('claims the draft, answers at once, and writes the new page for review', async () => {
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).resolves.toEqual({ started: true });
expect(reserveAiBudget).toHaveBeenCalledWith(
'org-1',
expect.objectContaining({ feature: 'content', modelId: expect.any(String) }),
);
// Which run holds it, for every device (00247).
expect(draftUpdates()[0]).toMatchObject({ agent_state: 'writing', agent_run: 'write' });
// Its run carries the claim, the one a Stop on it lets go of (00249).
expect(
writes.find((w) => w.table === 'content_agent_runs' && w.kind === 'insert').value,
).toMatchObject({ kind: 'write', claimed_at: draftUpdates()[0].agent_state_at });
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
});
it('writes with Claude Sonnet with an Anthropic key', async () => {
vi.stubEnv('ANTHROPIC_API_KEY', 'test-key');
await startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() });
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
expect(resolveModel).toHaveBeenLastCalledWith(SONNET);
expect(reserveAiBudget).toHaveBeenCalledWith(
'org-1',
expect.objectContaining({ feature: 'content', modelId: SONNET }),
);
const call = generateObject.mock.calls[0][0];
expect(call.model).toBe(`model:${SONNET}`);
expectClaudeCall(call);
// Two to twelve sections, told in words: Claude takes minItems 0 or 1 only.
expect(call.schema.jsonSchema.properties.sections).toMatchObject({
minItems: 1,
description: '(at least 2 items, at most 12 items)',
});
});
it('says it reads as many rules as the editor counts, when it says so', async () => {
await startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input({ counted: 7 }) });
const run = writes.find((w) => w.table === 'content_agent_runs' && w.kind === 'insert').value;
expect(run.steps.find((s) => s.key === 'read_rules')).toMatchObject({ count: 7 });
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
});
it('writes the new page for review once claimed', async () => {
await startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() });
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
// It ends only its own claim: a newer run's, after a Stop let go, is left alone.
const updates = writes.filter((w) => w.table === 'content_drafts' && w.kind === 'update');
expect(updates[1].filters).toContainEqual(['agent_state_at', updates[0].value.agent_state_at]);
expect(draftUpdates()[1]).toMatchObject({
title: 'How to brew pour-over coffee',
status: 'in_review',
author: 'agent',
agent_state: null,
});
// No number without a source (CE-033): none of the data has 15 or 250.
expect(draftUpdates()[1].body_html).toMatch(
/^<h1>How to brew pour-over coffee<\/h1><p>Use \[figure, source\] g of coffee for \[figure, source\] g of water\.<\/p>/,
);
const finish = writes.filter((w) => w.table === 'content_drafts')[1];
expect(finish.filters).toContainEqual(['agent_state', 'writing']);
// Only while it is still open to it: one marked ready meanwhile keeps its page.
expect(finish.filters).toContainEqual(['status', ['draft', 'in_review']]);
// A new page holds only its H1: nothing of the person's to keep first.
expect(rpcCalls).toEqual([]);
expect(writes.find((w) => w.table === 'prompt_improve_usage')).toMatchObject({
value: expect.objectContaining({ feature: 'content' }),
});
const { prompt } = generateObject.mock.calls[0][0];
// The data, numbered as the agent cites it.
expect(prompt).toContain('- S1 ratio?');
expect(prompt).toContain('- S2 "pour over ratio"');
expect(prompt).toContain('- faq: FAQ with 5 questions — aim: 5');
expect(prompt).toContain('Page type: How-to guide: numbered steps to get one thing done');
});
it('leaves the page as it was once stopped, and lets go of it (CE-027)', async () => {
const { RunStoppedError } = await import('./content-runs.js');
stopIfStopped.mockRejectedValueOnce(new RunStoppedError());
// A page the person wrote in: stopped, History gets no "before the agent".
rows.content_drafts = draftRow({
body_html: '<h1>How to brew pour-over coffee</h1><p>My notes.</p>',
});
await startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() });
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
// Claimed, then let go: no page written, no 'failed'.
expect(draftUpdates()[1]).toEqual({ agent_state: null, agent_state_at: expect.any(String) });
expect(draftUpdates().some((u) => 'body_html' in u)).toBe(false);
expect(rpcCalls).toEqual([]);
});
// Rewrite from brief: a draft the agent wrote is in review, and is written again.
it('writes a draft in review again, keeping its page in History first', async () => {
rows.content_drafts = draftRow({
status: 'in_review',
author: 'agent',
body_html: '<h1>How to brew pour-over coffee</h1><p>The first run.</p>',
});
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).resolves.toEqual({ started: true });
const claim = writes.find((w) => w.table === 'content_drafts');
expect(claim.filters).toContainEqual(['status', ['draft', 'in_review']]);
await vi.waitFor(() => expect(draftUpdates()).toHaveLength(2));
expect(rpcCalls).toEqual([
['content_draft_snapshot', { p_draft_id: DRAFT, p_origin: 'agent_before' }],
]);
expect(draftUpdates()[1]).toMatchObject({ status: 'in_review', agent_state: null });
});
it('refuses another brand’s draft, one marked ready or published, or one the agent is on — before charging', async () => {
const start = () => startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() });
rows.content_drafts = draftRow({ brand_id: 'other' });
await expect(start()).rejects.toMatchObject({ status: 404 });
for (const status of ['ready', 'published']) {
rows.content_drafts = draftRow({ status });
await expect(start()).rejects.toMatchObject({ status: 409, code: 'not_draft' });
}
rows.content_drafts = draftRow({
agent_state: 'writing',
agent_state_at: new Date().toISOString(),
});
await expect(start()).rejects.toMatchObject({ status: 409, code: 'writing' });
expect(reserveAiBudget).not.toHaveBeenCalled();
expect(writes).toEqual([]);
// A 'writing' left long ago can start again; a start that lost the claim can't.
rows.content_drafts = draftRow({
agent_state: 'writing',
agent_state_at: '2026-01-01T00:00:00Z',
});
claimed = null;
await expect(start()).rejects.toMatchObject({ status: 409, code: 'writing' });
expect(generateObject).not.toHaveBeenCalled();
});
it('waits on an existing page while its suggestions are open, before charging', async () => {
rows.content_drafts = draftRow({ method: 'optimize', source_url: 'https://acme.example/x' });
hasOpenSuggestions.mockResolvedValue(true);
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).rejects.toMatchObject({ status: 409, code: 'open_suggestions' });
expect(hasOpenSuggestions).toHaveBeenCalledWith(DRAFT);
expect(reserveAiBudget).not.toHaveBeenCalled();
expect(writes).toEqual([]);
// A new page is written whole: its run adds no batch.
rows.content_drafts = draftRow();
hasOpenSuggestions.mockClear();
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).resolves.toEqual({ started: true });
expect(hasOpenSuggestions).not.toHaveBeenCalled();
});
it('waits while a Fix open rules runs on the draft, before charging, for a Start or a rewrite', async () => {
fixUnderWay.mockResolvedValue(true);
for (const [row, rewrite] of [
[draftRow(), false],
[draftRow({ method: 'optimize', source_url: 'https://acme.example/x' }), false],
[draftRow({ method: 'optimize', source_url: 'https://acme.example/x' }), true],
]) {
rows.content_drafts = row;
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input(), rewrite }),
).rejects.toMatchObject({ status: 409, code: 'running' });
}
expect(fixUnderWay).toHaveBeenCalledWith(DRAFT);
// Nothing held on the budget, no claim on the draft, no model call.
expect(reserveAiBudget).not.toHaveBeenCalled();
expect(writes).toEqual([]);
expect(generateObject).not.toHaveBeenCalled();
// Once it has finished, the Start goes.
fixUnderWay.mockResolvedValue(false);
rows.content_drafts = draftRow();
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).resolves.toEqual({ started: true });
});
it('writes one of the organization’s drafts at a time', async () => {
rows.writingInOrg = [{ id: 'other-draft' }];
const held = { charge: vi.fn(), release: vi.fn() };
reserveAiBudget.mockResolvedValueOnce(held);
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).rejects.toMatchObject({ status: 409, code: 'org_writing' });
expect(writes).toEqual([]);
expect(generateObject).not.toHaveBeenCalled();
// Not started: the hold on the budget goes.
expect(held.release).toHaveBeenCalled();
expect(held.charge).not.toHaveBeenCalled();
});
it('fails at once when the model isn’t set up, claiming nothing', async () => {
resolveModel.mockImplementationOnce(() => {
throw new Error('Provider "google" is not configured');
});
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).rejects.toThrow('not configured');
expect(writes).toEqual([]);
});
it('stops at the budget', async () => {
reserveAiBudget.mockRejectedValue(new PlanLimitError('used up', 429));
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input() }),
).rejects.toBeInstanceOf(PlanLimitError);
expect(writes).toEqual([]);
});
});
// The hold Start took on the AI budget (reserveAiBudget), handed to the run.
const budget = { charge: vi.fn(), release: vi.fn() };
beforeEach(() => {
budget.charge.mockReset();
budget.release.mockReset();
});
describe('runDraftWriting on a new page', () => {
const run = (draft) =>
runDraftWriting({ draft, brand, model: 'model:m', input: input(), budget });
it('keeps the person’s text in History before writing over it, and reads prompts by their set', async () => {
const tried = draftRow({
agent_state: 'failed',
body_html: '<h1>How to brew pour-over coffee</h1><p>My own notes.</p>',
});
await expect(run(tried)).resolves.toEqual({ written: true });
// The answer is charged to the hold; nothing is left to release after.
expect(budget.charge).toHaveBeenCalledTimes(1);
expect(budget.release).toHaveBeenCalled();
// The fan-outs a rule names, in their words, on a new page too (B).
expect(generateObject.mock.calls[0][0].system).toContain(
'For each fan-out a rule names, use its words, not questions of your own: a section heading for fanoutH2, an FAQ question ending in "?" for fanoutFaq.',
);
expect(rpcCalls).toEqual([
['content_draft_snapshot', { p_draft_id: DRAFT, p_origin: 'agent_before' }],
]);
// A prompt without a topic is the brand's too.
expect(reads.find((r) => r.table === 'prompts').filters).toContainEqual([
'prompt_sets.brand_id',
BRAND,
]);
});
it("writes from Brief › Your input in Quality's second phase, its numbers and people kept (CE-048)", async () => {
rows.content_draft_inputs = {
experience: null,
data: [{ name: 'Survey', rows: [['Teams'], ['1,250']] }],
ansvisor_data: [],
quotes: [
{
name: 'Ada Lovelace',
title: 'Head of Research, Acme',
text: 'We cut our forecast misses in half.',
sourceUrl: 'https://acme.example/talk',
},
],
};
generateObject.mockResolvedValue({
object: {
...page,
intro: [
'1,250 teams brew this way. “We cut our forecast misses in half,” says Ada Lovelace.',
'Marco Rossi said 3,400 cups a day.',
],
},
usage: { inputTokens: 10, outputTokens: 20 },
});
const before = process.env.CONTENT_QUALITY_PHASE;
process.env.CONTENT_QUALITY_PHASE = '2';
try {
await expect(run(draftRow())).resolves.toEqual({ written: true });
} finally {
if (before === undefined) delete process.env.CONTENT_QUALITY_PHASE;
else process.env.CONTENT_QUALITY_PHASE = before;
}
expect(generateObject.mock.calls[0][0].prompt).toContain(
'"Survey" (Their own data, a table: Teams; 1,250)',
);
const written = draftUpdates().find((u) => u.body_html)?.body_html;
expect(written).toContain(
'1,250 teams brew this way. “We cut our forecast misses in half,” says Ada Lovelace.',
);
// What the person didn't give is still a placeholder.
expect(written).not.toContain('Marco Rossi');
expect(written).not.toContain('3,400');
});
it('saves the title as its H1 reads, a capital first, in the brand’s language', async () => {
generateObject.mockResolvedValue({
object: { ...page, title: 'latest news on pour-over coffee' },
usage: { inputTokens: 10, outputTokens: 20 },
});
await expect(run(draftRow())).resolves.toEqual({ written: true });
const saved = draftUpdates().find((u) => u.body_html);
expect(saved.title).toBe('Latest news on pour-over coffee');
expect(saved.body_html).toContain('<h1>Latest news on pour-over coffee</h1>');
// Turkish: "i" is "İ", in the title column as in the H1.
writes = [];
generateObject.mockResolvedValue({
object: { ...page, title: 'istanbul’da filtre kahve' },
usage: { inputTokens: 10, outputTokens: 20 },
});
await expect(
runDraftWriting({
draft: draftRow(),
brand: { ...brand, language: 'tr' },
model: 'model:m',
input: input(),
budget,
}),
).resolves.toEqual({ written: true });
const tr = draftUpdates().find((u) => u.body_html);
expect(tr.title).toBe('İstanbul’da filtre kahve');
expect(tr.body_html).toContain('<h1>İstanbul’da filtre kahve</h1>');
});
/** A draft's shopping path as content_drafts.shopping_path keeps it. */
const shoppingPath = (over = {}) => ({
version: 1,
builtAt: '2026-10-27T10:00:00.000Z',
pageType: 'guide',
categories: [
{ name: 'Hiking Boots', url: 'https://shop.example/collections/hiking-boots', title: null },
],
products: [
{
id: 'p1',
title: 'Summit GTX Boot',
url: 'https://shop.example/products/p1',
price: 189,
salePrice: null,
currency: 'USD',
availability: 'in_stock',
image: null,
issues: [],
score: 9,
},
{
id: 'p2',
title: 'Trail 450 Boot',
url: 'https://shop.example/products/p2',
price: 129,
salePrice: null,
currency: 'USD',
availability: 'in_stock',
image: null,
issues: [],
score: 8,
},
{
id: 'p3',
title: 'Evil Boot',
url: 'https://evil.example/products/p3',
price: 99,
salePrice: null,
currency: 'USD',
availability: 'in_stock',
image: null,
issues: [],
score: 7,
},
],
leftOut: [],
locked: [],
removed: [],
added: [],
...over,
});
it("writes with the draft's shopping path, its prices kept and its names linked (shopping path)", async () => {
generateObject.mockResolvedValue({
object: {
...page,
intro: [
'Good hiking boots keep you dry. The Summit GTX Boot costs $189, or 189.00 USD; the Trail 450 Boot [figure, source].',
'At 450 g it is light, and $199 elsewhere.',
],
},
usage: { inputTokens: 10, outputTokens: 20 },
});
rows.brand_domains = [{ domain: 'shop.example' }];
// Sold out since the path was built: the catalog's stock now.
rows.catalog_variants = [
{ id: 'v1', product_id: 'p1', availability: 'out of stock', price: 189, currency: 'USD' },
];
await expect(run(draftRow({ shopping_path: shoppingPath() }))).resolves.toEqual({
written: true,
});
const { prompt } = generateObject.mock.calls[0][0];
expect(prompt).toContain('- Summit GTX Boot: 189.00 USD, out of stock');
// A page off the brand's site isn't offered.
expect(prompt).not.toContain('Evil Boot');
const written = draftUpdates().find((u) => u.body_html)?.body_html;
expect(written).toContain(
'<a href="https://shop.example/collections/hiking-boots">hiking boots</a>',
);
expect(written).toContain(
'The <a href="https://shop.example/products/p1">Summit GTX Boot</a> costs $189, or 189.00 USD;',
);
// A product's own number where its name is: nowhere else, nor another price.
expect(written).toContain('Trail 450 Boot</a> [figure, source].');
expect(written).toContain('At [figure, source] g it is light, and [figure, source] elsewhere.');
});
it('writes a draft whose shopping path is broken as one with none', async () => {
generateObject.mockResolvedValue({
object: { ...page, intro: ['The Summit GTX Boot costs $189.'] },
usage: { inputTokens: 10, outputTokens: 20 },
});
rows.brand_domains = [{ domain: 'shop.example' }];
for (const broken of [
shoppingPath({ categories: { name: 'Hiking Boots' } }),
shoppingPath({ products: [null] }),
]) {
generateObject.mockClear();
writes = [];
await expect(run(draftRow({ shopping_path: broken }))).resolves.toEqual({ written: true });
expect(generateObject.mock.calls[0][0].prompt).not.toContain('The shopping path');
const written = draftUpdates().find((u) => u.body_html)?.body_html;
expect(written).toContain('<p>The Summit GTX Boot costs [figure, source].</p>');
}
});
it("puts the checked path's products in stock as a grid, not the ones the draft keeps (shopping path, part 5)", async () => {
generateObject.mockResolvedValue({
object: {
...page,
sections: [
{ heading: 'How to choose', paragraphs: ['Fit first.'], bullets: [] },
{
heading: 'Our picks',
paragraphs: ['The Summit GTX Boot, then the Trail 450 Boot.'],
bullets: [],
},
],
},
usage: { inputTokens: 10, outputTokens: 20 },
});
rows.brand_domains = [{ domain: 'shop.example' }];
const variant = (id, productId, price) => ({
id,
product_id: productId,
availability: 'in stock',
price,
sale_price: null,
currency: 'USD',
});
// All in stock now, the first cheaper than when the path was built.
rows.catalog_variants = [
variant('v1', 'p1', 179),
variant('v2', 'p2', 129),
variant('v3', 'p3', 99),
variant('v4', 'p4', 99),
];
const kept = shoppingPath({ pageType: 'buying_guide' });
// Kept as a member saved it: one page off the brand's site (Evil Boot), one over http.
kept.products.push({
...kept.products[1],
id: 'p4',
title: 'Plain Boot',
url: 'http://shop.example/products/p4',
});
await expect(
run(draftRow({ page_type: 'buying_guide', shopping_path: kept })),
).resolves.toEqual({ written: true });
const written = draftUpdates().find((u) => u.body_html)?.body_html;
const encoded = /<div data-html-block="([^"]+)"><\/div>/.exec(written);
const grid = Buffer.from(encoded[1], 'base64').toString('utf8');
expect(grid.match(/class="ansvisor-product"/g)).toHaveLength(2);
expect(grid).toContain('href="https://shop.example/products/p1"');
expect(grid).toContain('href="https://shop.example/products/p2"');
expect(grid).not.toContain('Evil Boot');
expect(grid).not.toContain('Plain Boot');
// The catalog's price now, not the one the path was built with.
expect(grid).toContain('$179.00');
expect(grid).not.toContain('$189.00');
// At the end of the section that links them.
expect(written).toMatch(
/Trail 450 Boot<\/a>\.<\/p><div data-html-block="[^"]+"><\/div><h2>FAQ<\/h2>/,
);
});
it('leaves a draft marked ready during the run as it is', async () => {
finished = null;
await expect(run(draftRow())).resolves.toEqual({ skipped: true });
// The page wasn't written; the agent let go of the draft.
expect(draftUpdates().at(-1)).toEqual({
agent_state: null,
agent_state_at: expect.any(String),
});
});
});
describe('runDraftWriting on an existing page', () => {
const QUOTED = 'Pour-over coffee tastes cleaner than a French press.';
const existing = draftRow({
method: 'optimize',
source_url: 'https://acme.example/pour-over',
body_html: `<h1>Pour-over</h1><p>${QUOTED}</p><p>Use good coffee.</p>`,
action_id: 'act-1',
});
const run = (over = {}) =>
runDraftWriting({
draft: existing,
brand,
model: 'model:m',
input: input({ locked: [passageKey(QUOTED)], ...over }),
budget,
});
it('leaves its suggestions for review, never on a locked passage', async () => {
generateObject.mockResolvedValue({
object: {
suggestions: [
{
kind: 'replace',
passage: 2,
before: 'cleaner',
after: 'brighter',
reason: 'x',
rules: [],
},
{
kind: 'replace',
passage: 3,
before: 'good coffee',
after: 'freshly ground coffee',
reason: 'answer the ratio search',
rules: ['faq'],
// From the fan-out it answers, S2 (CE-033).
sources: ['S2'],
marks: [{ words: 'freshly ground coffee', source: 'S2' }],
},
],
},
usage: {},
});
await expect(run()).resolves.toEqual({ suggestions: 1 });
const batch = writes.find((w) => w.table === 'content_draft_suggestions').value;
expect(batch).toEqual([
expect.objectContaining({
draft_id: DRAFT,
action_id: 'act-1',
block_text: 'Use good coffee.',
before_text: 'good coffee',
position: 0,
sources: [{ kind: 'fanout', ref: 'pour over ratio', label: 'pour over ratio' }],
source_marks: [{ start: 0, end: 21, source: 0 }],
}),
]);
expect(generateObject.mock.calls[0][0].prompt).toContain(`[2] (p, LOCKED) ${QUOTED}`);
// An answer goes after a heading, never into it.
expect(generateObject.mock.calls[0][0].system).toContain(
'Never write an answer, a sentence or a FAQ into a heading',
);
// Links only to the pages and sources listed (A); none asked for here.
expect(generateObject.mock.calls[0][0].system).toContain('Never invent a URL');
expect(generateObject.mock.calls[0][0].system).toContain(
'Call a change a fan-out answer only when it uses a fan-out listed',
);
expect(readLinkTargets).toHaveBeenCalledWith(
expect.objectContaining({ draftId: DRAFT, ruleKeys: new Set(['faq']) }),
);
expect(draftUpdates()).toEqual([
{ status: 'in_review', agent_state: null, agent_state_at: expect.any(String) },
]);
});
it('adds no batch over one that came in during the run', async () => {
generateObject.mockResolvedValue({
object: {
suggestions: [
{ kind: 'replace', passage: 3, before: 'good', after: 'fresh', reason: 'x', rules: [] },
],
},
usage: {},
});
hasOpenSuggestions.mockResolvedValue(true);
await expect(run()).resolves.toEqual({ skipped: true });
expect(writes.some((w) => w.table === 'content_draft_suggestions')).toBe(false);
expect(draftUpdates()).toEqual([{ agent_state: null, agent_state_at: expect.any(String) }]);
});
it('drops its suggestions when the draft was marked ready during the run', async () => {
generateObject.mockResolvedValue({
object: {
suggestions: [
{ kind: 'replace', passage: 3, before: 'good', after: 'fresh', reason: 'x', rules: [] },
],
},
usage: {},
});
finished = null;
await expect(run()).resolves.toEqual({ skipped: true });
const batch = writes.find(
(w) => w.table === 'content_draft_suggestions' && w.kind === 'insert',
);
const drop = writes.find((w) => w.table === 'content_draft_suggestions' && w.kind === 'delete');
expect(drop.filters).toEqual([['batch_id', batch.value[0].batch_id]]);
});
it('says it failed when the model fails or leaves nothing to place', async () => {
generateObject.mockResolvedValue({ object: { suggestions: [] }, usage: {} });
await expect(run()).resolves.toEqual({ failed: true });
generateObject.mockRejectedValue(new Error('timeout'));
await expect(run()).resolves.toEqual({ failed: true });
expect(draftUpdates()).toEqual([
{ agent_state: 'failed', agent_state_at: expect.any(String) },
{ agent_state: 'failed', agent_state_at: expect.any(String) },
]);
// The first answer was paid for; the second never came, and its hold goes.
expect(budget.charge).toHaveBeenCalledTimes(1);
expect(budget.release).toHaveBeenCalledTimes(2);
});
it('charges an answer that missed the schema, as the provider billed it', async () => {
const billed = { inputTokens: 5000, outputTokens: 6000 };
generateObject.mockRejectedValueOnce(
Object.assign(new NoObjectGeneratedError('no object'), { usage: billed }),
);
await expect(run()).resolves.toEqual({ failed: true });
expect(budget.charge).toHaveBeenCalledTimes(1);
expect(budget.charge).toHaveBeenCalledWith(billed);
// The run still ends failed.
expect(draftUpdates()).toEqual([{ agent_state: 'failed', agent_state_at: expect.any(String) }]);
});
});
describe('Rewrite the page to these rules', () => {
const QUOTED = 'Pour-over coffee tastes cleaner than a French press.';
const existing = draftRow({
method: 'optimize',
status: 'in_review',
body_html:
`<h1>Pour-over</h1><p>${QUOTED}</p><p>Use good coffee. ${'Filler. '.repeat(20)}</p>` +
'<table><tbody><tr><td>Ratio</td><td>1:16</td></tr></tbody></table>' +
'<p>See <a href="/pricing">pricing</a>.</p>',
action_id: 'act-1',
});
const locked = [passageKey(QUOTED)];
const run = (over = {}) =>
runDraftWriting({
draft: existing,
brand,
model: 'model:m',
input: input({ locked, ...over }),
budget,
rewrite: true,
});
const answer = (html, reason = 'cut to about 3,000 characters') =>
generateObject.mockResolvedValue({
object: { html, reason },
usage: { inputTokens: 900, outputTokens: 700 },
});
it('starts only on a page with text, small enough to send whole, and no open batch', async () => {
rows.content_drafts = existing;
hasOpenSuggestions.mockResolvedValueOnce(true);
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input(), rewrite: true }),
).rejects.toMatchObject({ status: 409, code: 'open_suggestions' });
rows.content_drafts = { ...existing, method: 'create', body_html: '<p> </p>' };
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input(), rewrite: true }),
).rejects.toMatchObject({ status: 422, code: 'empty' });
rows.content_drafts = { ...existing, body_html: `<p>${'word '.repeat(13_000)}</p>` };
await expect(
startDraftWriting({ draftId: DRAFT, brandId: BRAND, input: input(), rewrite: true }),
).rejects.toMatchObject({ status: 422, code: 'too_long' });
// Refused before anything was held or claimed.
expect(reserveAiBudget).not.toHaveBeenCalled();
expect(draftUpdates()).toEqual([]);
});
it('claims the draft as a rewrite, so every device tries a failed one again as one', async () => {
rows.content_drafts = existing;
answer('<h1>Pour-over</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Shorter.</p>');
await expect(
startDraftWriting({
draftId: DRAFT,
brandId: BRAND,
input: input({ locked }),
rewrite: true,
}),
).resolves.toEqual({ started: true });
expect(draftUpdates()[0]).toMatchObject({ agent_state: 'writing', agent_run: 'rewrite' });
await vi.waitFor(() => expect(draftUpdates().length).toBeGreaterThan(1));
});
it('leaves the whole page as one suggestion, with what it kept put back as it was', async () => {
answer(
'<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>Use good coffee, freshly ground.</p>' +
'<p>[[KEEP 2]]</p><p>See <a href="/pricing">pricing</a> or <a href="https://evil.example">this</a>.</p>',
);
await expect(
run({
rules: [
{ key: 'length', name: 'Length', detail: 'aim: 3,000 characters' },
{ key: 'faq', name: 'FAQ with 5 questions', detail: 'aim: 5' },
],
}),
).resolves.toEqual({ rewritten: true });
const { system, prompt } = generateObject.mock.calls[0][0];
expect(system).toContain('stands for a block kept word for word');
// The quoted passage and the table never reach the model to rewrite:
// they are described, and count toward Length.
expect(prompt).toContain('Length: about 3000 characters.');
expect(prompt).toMatch(
/The page now, as HTML: about \d+ characters of text, \d+ of them in its kept blocks/,
);
expect(prompt).toContain(`- [[KEEP 1]]: a passage AI assistants quote today: "${QUOTED}"`);
expect(prompt).toContain('- [[KEEP 2]]: a table, 1 rows: "Ratio1:16"');
expect(prompt).not.toContain(`<p>${QUOTED}</p>`);
expect(prompt).toContain("Write in the page's own language.");
expect(budget.charge).toHaveBeenCalledWith({ inputTokens: 900, outputTokens: 700 });
const row = writes.find((w) => w.table === 'content_draft_suggestions').value;
expect(row).toMatchObject({
draft_id: DRAFT,
action_id: 'act-1',
position: 0,
kind: 'page',
block_text: 'Pour-over',
before_text: '',
before_html: existing.body_html,
reason: 'cut to about 3,000 characters',
rule_keys: ['length', 'faq'],
// What it was asked for and came to (00285): the kept passage and table count.
length_target: 3000,
kept_chars: textLength(`${QUOTED} Ratio 1:16`),
after_chars: textLength(row.after_text),
});
expect(row.after_text).toBe(
`<h1>Pour-over coffee</h1><p>${QUOTED}</p><p>Use good coffee, freshly ground.</p>` +
'<table><tbody><tr><td>Ratio</td><td>1:16</td></tr></tbody></table>' +
'<p>See <a href="/pricing">pricing</a> or this.</p>',
);
// The agent lets go; the draft's status stays as it was.
expect(draftUpdates().at(-1)).toEqual({
agent_state: null,
agent_state_at: expect.any(String),
});
});
it('keeps the data it wrote from, and no number without a source (CE-033)', async () => {
generateObject.mockResolvedValue({
object: {
html:
'<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p>' +
'<p>Brew it in 4 minutes; 87% of baristas agree, as "pour over ratio" asks.</p>',
reason: 'answered the ratio search',
sources: ['S2', 'S7'],
},
usage: { inputTokens: 900, outputTokens: 700 },
});
await expect(run()).resolves.toEqual({ rewritten: true });
const row = writes.find((w) => w.table === 'content_draft_suggestions').value;
// Length off: it was asked for none (00285).
expect(row.length_target).toBeNull();
// The fan-out it named; S7 isn't on the list.
expect(row.sources).toEqual([
{ kind: 'fanout', ref: 'pour over ratio', label: 'pour over ratio' },
]);
// 87% is in none of the data: a placeholder. A lone count stays; the
// kept table's 1:16 is the page's own.
expect(row.after_text).toContain(
'<p>Brew it in 4 minutes; [figure, source] of baristas agree, as "pour over ratio" asks.</p>',
);
expect(row.after_text).toContain('<td>1:16</td>');
expect(generateObject.mock.calls[0][0].system).toContain('Put the numbers (S1, S2…)');
});
it('puts a person or a quote it was not given as a placeholder, and knows the authors (CE-044)', async () => {
rows.content_authors = [{ name: 'Ada Lovelace', title: null, bio: null }];
answer(
'<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p>' +
'<p>“Pour slowly and you taste every note,” said Marco Rossi.</p>' +
'<p>Ada Lovelace said the bloom matters.</p>',
);
await expect(run()).resolves.toEqual({ rewritten: true });
const row = writes.find((w) => w.table === 'content_draft_suggestions').value;
expect(row.after_text).toContain(`<p>${QUOTE_PLACEHOLDER} said ${NAME_PLACEHOLDER}.</p>`);
expect(row.after_text).toContain('<p>Ada Lovelace said the bloom matters.</p>');
// What it kept is the page's own, as it was.
expect(row.after_text).toContain(`<p>${QUOTED}</p>`);
expect(row.after_text).toContain(
'<table><tbody><tr><td>Ratio</td><td>1:16</td></tr></tbody></table>',
);
});
it('keeps the page’s length when the Length rule is off', async () => {
answer('<h1>Pour-over</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Shorter.</p>');
await run({ rules: [{ key: 'faq', name: 'FAQ', detail: '' }] });
const { prompt } = generateObject.mock.calls[0][0];
expect(prompt).toContain(`Length: about ${textLength(existing.body_html)} characters.`);
});
it('fails the run on an answer that isn’t a page in HTML', async () => {
answer('# Pour-over\n\n[[KEEP 1]]\n\n[[KEEP 2]]');
await expect(run()).resolves.toEqual({ failed: true });
expect(writes.find((w) => w.table === 'content_draft_suggestions')).toBeUndefined();
});
it('drops the rewrite of a page marked ready during the run', async () => {
answer('<h1>Pour-over</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Shorter.</p>');
finished = null;
await expect(run()).resolves.toEqual({ skipped: true });
const insert = writes.find(
(w) => w.table === 'content_draft_suggestions' && w.kind === 'insert',
);
const drop = writes.find((w) => w.table === 'content_draft_suggestions' && w.kind === 'delete');
expect(drop.filters).toContainEqual(['batch_id', insert.value.batch_id]);
});
it('fails the run when the rewrite drops what it had to keep', async () => {
answer('<h1>Pour-over</h1><p>[[KEEP 2]]</p><p>A new page without the quoted passage.</p>');
await expect(run()).resolves.toEqual({ failed: true });
expect(writes.find((w) => w.table === 'content_draft_suggestions')).toBeUndefined();
expect(draftUpdates()).toEqual([{ agent_state: 'failed', agent_state_at: expect.any(String) }]);
// The answer was still paid for.
expect(budget.charge).toHaveBeenCalledTimes(1);
});
it('adds nothing over a batch that came in during the run', async () => {
answer('<h1>Pour-over</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Shorter.</p>');
hasOpenSuggestions.mockResolvedValue(true);
await expect(run()).resolves.toEqual({ skipped: true });
expect(writes.find((w) => w.table === 'content_draft_suggestions')).toBeUndefined();
});
describe('a rewrite well past its Length: one more pass cuts it', () => {
const cutBudget = {
charge: vi.fn(async () => {}),
release: vi.fn(async () => {}),
keep: vi.fn(async () => {}),
};
// A Length of 100 (up to 120): a cut pass past 135.
const toLength = (length = 100) => ({
rules: [{ key: 'length', name: 'Length', detail: `aim: ${length} characters` }],
sources: { ...input().sources, length },
});
const long = `<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>${'Long words. '.repeat(10)}</p>`;
const short = '<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>[[KEEP 2]]</p><p>Short.</p>';
const answers = (...htmls) => {
for (const [i, html] of htmls.entries()) {
generateObject.mockResolvedValueOnce({
object: { html, reason: i ? 'cut it' : 'answered each heading first', sources: [] },
usage: { inputTokens: 900 + i, outputTokens: 700 + i },
});
}
};
const row = () => writes.find((w) => w.table === 'content_draft_suggestions').value;
beforeEach(() => {
cutBudget.charge.mockClear();
cutBudget.release.mockClear();
cutBudget.keep.mockClear();
reserveAiBudget.mockReset().mockResolvedValue(cutBudget);
});
it('cuts its own page to the Length on its own hold, keeping every kept line', async () => {
answers(long, short);
await expect(run(toLength())).resolves.toEqual({ rewritten: true });
expect(generateObject).toHaveBeenCalledTimes(2);
const { prompt } = generateObject.mock.calls[1][0];
expect(prompt).toContain('Its Length is 100 characters, up to 120. Cut it to about 100.');
expect(prompt).toContain('every [[KEEP n]] line exactly as it is');
expect(prompt).toContain('Long words.');
expect(reserveAiBudget).toHaveBeenCalledWith('org-1', {
feature: 'content',
modelId: expect.any(String),
draftId: DRAFT,
});
expect(cutBudget.charge).toHaveBeenCalledWith({ inputTokens: 901, outputTokens: 701 });
// The cut page, what it kept put back, the first pass's reason.
expect(row().after_text).toContain('Short.');
expect(row().after_text).not.toContain('Long words.');
expect(row().after_text).toContain(QUOTED);
expect(row().reason).toBe('answered each heading first');
expect(row().after_chars).toBe(textLength(row().after_text));
});
it('rewrites and cuts with the model it was started with, held at its own id', async () => {
answers(long, short);
await runDraftWriting({
draft: existing,
brand,
model: `model:${SONNET}`,
modelId: SONNET,
input: input({ locked, ...toLength() }),
budget,
rewrite: true,
});
expect(generateObject).toHaveBeenCalledTimes(2);
for (const [call] of generateObject.mock.calls) {
expect(call.model).toBe(`model:${SONNET}`);
expectClaudeCall(call);
}
expect(reserveAiBudget).toHaveBeenCalledWith('org-1', {
feature: 'content',
modelId: SONNET,
draftId: DRAFT,
});
});
it('keeps the first rewrite when the cut is no shorter, drops a kept line, fails or has no budget', async () => {
answers(long, long);
await run(toLength());
expect(row().after_text).toContain('Long words.');
writes = [];
answers(long, '<h1>Pour-over coffee</h1><p>[[KEEP 1]]</p><p>Short.</p>');
await run(toLength());
expect(row().after_text).toContain('Long words.');
// Kept lines written into a paragraph: restoreKept couldn't put them back.
writes = [];
answers(long, '<h1>Pour-over coffee</h1><p>[[KEEP 1]] [[KEEP 2]] Short.</p>');
await expect(run(toLength())).resolves.toEqual({ rewritten: true });
expect(row().after_text).toContain('Long words.');
writes = [];
generateObject.mockResolvedValueOnce({
object: { html: long, reason: 'answered each heading first', sources: [] },
usage: { inputTokens: 900, outputTokens: 700 },
});
generateObject.mockRejectedValueOnce(new Error('provider down'));
cutBudget.charge.mockClear();
cutBudget.release.mockClear();
await expect(run(toLength())).resolves.toEqual({ rewritten: true });
expect(row().after_text).toContain('Long words.');
// Nothing billed for a failed call: its hold goes.
expect(cutBudget.charge).not.toHaveBeenCalled();
expect(cutBudget.release).toHaveBeenCalled();
writes = [];
generateObject.mockReset();
reserveAiBudget.mockRejectedValueOnce(
Object.assign(new Error('used up'), { code: 'plan_limit' }),
);
answers(long);
await run(toLength());
expect(generateObject).toHaveBeenCalledTimes(1);
expect(row().after_text).toContain('Long words.');
});
it('keeps the hold of a cut stopped while it runs, and writes nothing', async () => {
const { RunStoppedError } = await import('./content-runs.js');
answers(long);
generateObject.mockRejectedValueOnce(new RunStoppedError());
cutBudget.keep.mockClear();
await run(toLength());
expect(cutBudget.keep).toHaveBeenCalledTimes(1);
expect(
writes.find((w) => w.table === 'content_draft_suggestions' && w.kind === 'insert'),
).toBeUndefined();
});
it('cuts nothing within reach of the Length, or with Length off', async () => {
answers(long);
await run(toLength(3000));
answers(long);
await run();
expect(generateObject).toHaveBeenCalledTimes(2);
expect(reserveAiBudget).not.toHaveBeenCalled();
});
});
});
describe('the write prompt', () => {
afterEach(() => vi.unstubAllEnvs());
it('names the brand’s site type once Quality’s phase 2 is live (CE-056)', () => {
const write = (phase) => {
vi.stubEnv('CONTENT_QUALITY_PHASE', phase);
return buildWritePrompt({
brand,
draft: draftRow(),
brief: 'No brief.',
input: { sources: { length: 3000 }, rules: [], textRules: [] },
});
};
expect(write('2')).toContain(
'Industry: Coffee\nSite type: B2B SaaS (software or services sold to businesses)\nDescription:',
);
expect(write('')).toContain('Industry: Coffee\nDescription:');
});
it('tells an existing page’s suggestions how long it is now, beside its Length', () => {
const LENGTH = [{ key: 'length', name: 'Length' }];
const prompt = (bodyHtml, passages = [{ tag: 'p', text: 'Brew it slowly.' }], rules = LENGTH) =>
buildWritePrompt({
brand,
draft: draftRow({ body_html: bodyHtml }),
brief: 'No brief.',
input: { sources: { length: 3000 }, rules, textRules: [] },
passages,
lockedKeys: new Set(),
});
// 5,353 characters for a Length of 3,000, as the page that went to Webflow.
expect(prompt(`<p>${'x'.repeat(5353)}</p>`)).toContain(
'Length: about 3000 characters. The page is about 5353 characters now, over its Length: suggest cuts',
);
expect(prompt(`<p>${'x'.repeat(3100)}</p>`)).toContain(
'The page is about 3100 characters now: keep it about this long.',
);
// A new page has no length yet.
expect(prompt('', null)).toContain('Length: about 3000 characters.\n');
// Length off keeps the page's length: no cuts asked for it.
const off = prompt(`<p>${'x'.repeat(5353)}</p>`, undefined, []);
expect(off).toContain('Length: about 3000 characters.\n');
expect(off).not.toContain('suggest cuts');
expect(off).not.toContain('characters now');
});
});
describe('lengthNow', () => {
it('reads a page over, under or within its Length’s band (±20%)', () => {
expect(lengthNow(3601, 3000)).toMatch(/over its Length: suggest cuts/);
expect(lengthNow(3600, 3000)).toMatch(/keep it about this long/);
expect(lengthNow(2400, 3000)).toMatch(/keep it about this long/);
expect(lengthNow(2399, 3000)).toMatch(/under its Length: add what/);
});
});
describe('the brief rules (2026) in the Content Agent', () => {
afterEach(() => vi.unstubAllEnvs());
const planned = {
goal: 'Sell beans',
audience: 'Home brewers',
outline: [
{
heading: 'Ratio',
keyPoints: ['Grams per cup'],
fanout: null,
platform: null,
answer: 'Name the ratio first.',
format: 'table',
facts: [{ claim: 'The ratio cafés use', sourceType: 'study', source: null }],
},
],
keywords: ['pour over'],
version: 2,
tldr: 'A 1:16 ratio suits most beans.',
proof: [{ kind: 'test', what: 'Our cupping notes' }],
};
// A plan whose facts name their sources, and whose links' addresses hold a
// number and a name the agent wasn't given.
const linked = {
...planned,
outline: [
{
...planned.outline[0],
facts: [
{
claim: 'Our grind sizes',
sourceType: 'example',
source: { kind: 'input', ref: 'in-1', label: 'Grind chart' },
},
{
claim: 'The ratio cafés use',
sourceType: 'study',
source: {
kind: 'cited_page',
ref: 'https://news.example/ratio',
label: 'https://news.example/ratio',
},
},
],
},
],
links: {
internal: [
{ anchor: 'our tools', url: 'https://acme.example/top-37-tools', section: 'Ratio' },
],
outbound: [
{
anchor: 'an interview',
url: 'https://news.example/interview-with-john-smith',
section: null,
},
],
},
};
it('keeps placeholders for facts the person must find, never a page, a document or a competitor', () => {
// A page waited on "[About]", "[OpenAI release notes]" and "[competitor]".
for (const prompt of [
writeSystemPrompt(),
suggestSystemPrompt(),
rewriteSystemPrompt(),
AGENT_SYSTEM_PROMPT,
]) {
expect(prompt).toContain(PLACEHOLDER_RULE);
}
expect(PLACEHOLDER_RULE).toContain('Never put a page, a document, a link');
expect(PLACEHOLDER_RULE).toContain('"OpenAI\'s release notes"');
});
it('asks as before while they are off: the same prompts, the brief as version 1', () => {
expect(writeSystemPrompt()).toBe(WRITE_SYSTEM_PROMPT);
expect(rewriteSystemPrompt()).toBe(REWRITE_SYSTEM_PROMPT);
expect(suggestSystemPrompt()).not.toContain(WRITING_GUARDRAIL);
// Word for word what version 1's fields give.
expect(briefText(planned, null)).toBe(
briefText(
{
goal: planned.goal,
audience: planned.audience,
keywords: planned.keywords,
outline: planned.outline.map((s) => ({
heading: s.heading,
keyPoints: s.keyPoints,
fanout: s.fanout,
platform: s.platform,
})),
},
null,
),
);
expect(briefText(planned, null)).not.toContain('TL;DR');
});
it('with them on, gives each prompt the guardrail and the writer the plan and short headings', () => {
vi.stubEnv('CONTENT_BRIEF_RULES', 'true');
for (const prompt of [writeSystemPrompt(), suggestSystemPrompt(), rewriteSystemPrompt()]) {
expect(prompt).toContain(WRITING_GUARDRAIL);
}
expect(writeSystemPrompt()).toContain("Follow the brief's section plans");
expect(writeSystemPrompt()).toContain('each section heading about 8 to 15 words');
const brief = briefText(planned, null);
expect(brief).toContain('Goal: Sell beans');
expect(brief).toContain("TL;DR (what the page's top must say): A 1:16 ratio suits most beans.");
expect(brief).toContain(
'- Ratio — answer first: Name the ratio first.; show as a comparison, the same points for each option in turn; give a fact on: The ratio cafés use (study, a placeholder until a source is found)',
);
expect(brief).toContain(
'Proof to gather (write it only from Your input; else a placeholder): test: Our cupping notes',
);
});
it('writes a new page from the plan, with them on', async () => {
vi.stubEnv('CONTENT_BRIEF_RULES', 'true');
await runDraftWriting({
draft: draftRow({ brief: planned }),
brand,
model: 'model:m',
input: input(),
budget,
});
const call = generateObject.mock.calls[0][0];
expect(call.system).toContain(WRITING_GUARDRAIL);
expect(call.prompt).toContain('Section plans:');
});
it("names a fact's source by its kind, and leaves the planned links out for a new page", async () => {
vi.stubEnv('CONTENT_BRIEF_RULES', 'true');
const brief = briefText(linked, null);
expect(brief).toContain(
'give a fact on: Our grind sizes (example, from Your input "Grind chart")',
);
expect(brief).toContain(
"give a fact on: The ratio cafés use (study, a placeholder: check it against https://news.example/ratio, whose words you don't see)",
);
expect(brief).toContain(
'Links planned (only as the links rule allows): "our tools" → https://acme.example/top-37-tools in "Ratio"',
);
expect(briefText(linked, null, { links: false })).not.toContain('Links planned');
// A new page's writer has no links rule: it isn't told of them.
await runDraftWriting({
draft: draftRow({ brief: linked }),
brand,
model: 'model:m',
input: input(),
budget,
});
expect(generateObject.mock.calls[0][0].prompt).not.toContain('Links planned');
});
it('gives an existing page’s suggestions the guardrail and the links planned', async () => {
vi.stubEnv('CONTENT_BRIEF_RULES', 'true');
generateObject.mockResolvedValue({
object: { suggestions: [] },
usage: { inputTokens: 10, outputTokens: 20 },
});
await runDraftWriting({
draft: draftRow({
method: 'optimize',
source_url: 'https://acme.example/pour-over',
body_html: '<h1>Pour-over</h1><p>Use good coffee.</p>',
brief: linked,
}),
brand,
model: 'model:m',
input: input(),
budget,
});
const call = generateObject.mock.calls[0][0];
expect(call.system).toContain(WRITING_GUARDRAIL);
expect(call.prompt).toContain('Links planned');
});
it("never lets a planned link's address vouch for a number or a name", async () => {
vi.stubEnv('CONTENT_BRIEF_RULES', 'true');
generateObject.mockResolvedValue({
object: {
html:
'<h1>Pour-over</h1><p>Try 37 tools for it.</p>' +
'<p>John Smith said the bloom matters.</p>',
reason: 'followed the plan',
},
usage: { inputTokens: 900, outputTokens: 700 },
});
await expect(
runDraftWriting({
draft: draftRow({
method: 'optimize',
status: 'in_review',
body_html: '<h1>Pour-over</h1><p>Use good coffee.</p>',
brief: linked,
}),
brand,
model: 'model:m',
input: input(),
budget,
rewrite: true,
}),
).resolves.toEqual({ rewritten: true });
const call = generateObject.mock.calls[0][0];
expect(call.system).toContain(WRITING_GUARDRAIL);
// The rewrite is told of the links; its guards never read their addresses.
expect(call.prompt).toContain('https://acme.example/top-37-tools');
const row = writes.find((w) => w.table === 'content_draft_suggestions').value;
expect(row.after_text).toContain('<p>Try [figure, source] tools for it.</p>');
expect(row.after_text).toContain(`<p>${NAME_PLACEHOLDER} said the bloom matters.</p>`);
});
});