mirror of
https://github.com/citeworthyio/seo-agent.git
synced 2026-08-11 13:22:39 +02:00
missing_canonical and canonical_mismatch are now proposable. The draft is computed, never modeled: expectedCanonicalUrl(siteOrigin, path) is the same expression the rule checks against, exported from rules.ts so the fix can never fail its own verifying recrawl. invalidCanonicalReason (overrides.ts) is the one gate — manual proposals, the drafter, and applyOverride's fail-closed edge all run it; the injector's serve-time belt enforces the same rules (http(s) only, attribute-safe charset, no query/fragment). The injector gains its first rewrite-or-insert lane: patch link[rel=canonical] href when present, insert via head onEndTag when absent. verify.ts now checks stored canonicals against the crawl (verbatim — the validator charset makes entity asymmetry impossible). Attribute-hostile path characters are percent-encoded inside expectedCanonicalUrl so such pages are fixable rather than re-enqueued forever. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
345 lines
17 KiB
TypeScript
345 lines
17 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { invalidReason, buildSystemPrompt, draftWithTrace } from '../src/propose';
|
|
import { approvalBlockedReason, createProposal, decideProposal, ApiError } from '../src/actions';
|
|
import { resolveSiteConfig } from '../src/config';
|
|
|
|
const cfg = (vars: Record<string, string> = {}) => resolveSiteConfig({ SITE_URL: 'https://example.com', ...vars });
|
|
|
|
describe('invalidReason (description validation)', () => {
|
|
const ok = 'This is a plainly written meta description that lands comfortably within the seventy to one-sixty character band.';
|
|
|
|
it('accepts a well-formed description', () => {
|
|
expect(invalidReason(ok)).toBeNull();
|
|
});
|
|
it('rejects too-short copy (< 70 chars)', () => {
|
|
expect(invalidReason('Too short.')).toMatch(/too short/);
|
|
});
|
|
it('rejects too-long copy (> 160 chars)', () => {
|
|
expect(invalidReason('a'.repeat(170) + '.')).toMatch(/too long/);
|
|
});
|
|
it('rejects copy that does not end in sentence punctuation', () => {
|
|
expect(invalidReason('a'.repeat(100))).toMatch(/complete sentence/);
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Banned terms — the deterministic half of the vocabulary feature.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('invalidReason — banned terms', () => {
|
|
// Long enough to clear the 70-char floor, so only the term check can fire.
|
|
const withTerm = (t: string) => `We build ${t} tooling for teams that need a dependable answer engine presence.`;
|
|
|
|
it('is inert without a config — the pre-1.13 signature keeps its old behavior', () => {
|
|
expect(invalidReason(withTerm('Widgetron'))).toBeNull();
|
|
expect(invalidReason(withTerm('Widgetron'), {} as any)).toBeNull();
|
|
expect(invalidReason(withTerm('Widgetron'), { bannedTerms: [] })).toBeNull();
|
|
});
|
|
|
|
it('flags a banned term and names it in the reason', () => {
|
|
expect(invalidReason(withTerm('Widgetron'), { bannedTerms: ['Widgetron'] })).toBe('contains banned term "Widgetron"');
|
|
});
|
|
|
|
it('matches case-insensitively', () => {
|
|
expect(invalidReason(withTerm('WIDGETRON'), { bannedTerms: ['widgetron'] })).toMatch(/banned term/);
|
|
expect(invalidReason(withTerm('widgetron'), { bannedTerms: ['WidgetRon'] })).toMatch(/banned term/);
|
|
});
|
|
|
|
it('respects word boundaries — banning "AI" does not flag "maintain"', () => {
|
|
const text = 'We maintain a plainly written index so that answer engines can find the pages that matter to readers.';
|
|
expect(invalidReason(text, { bannedTerms: ['AI'] })).toBeNull();
|
|
expect(
|
|
invalidReason('We use AI to maintain a plainly written index for the answer engines that read this site.', { bannedTerms: ['AI'] })
|
|
).toMatch(/banned term "AI"/);
|
|
});
|
|
|
|
it('treats a term with regex metacharacters as literal text', () => {
|
|
const cpp = 'Our team writes C++ tooling for teams that need a dependable answer engine presence today.';
|
|
expect(invalidReason(cpp, { bannedTerms: ['C++'] })).toBe('contains banned term "C++"');
|
|
// The metacharacters must not be interpreted: a stray "." must not become
|
|
// "any character", and a lone "(" must not throw or match.
|
|
expect(invalidReason(cpp, { bannedTerms: ['C.tooling'] })).toBeNull();
|
|
expect(invalidReason(cpp, { bannedTerms: ['('] })).toBeNull();
|
|
});
|
|
|
|
it('anchors a punctuation-ending term without swallowing longer words', () => {
|
|
const inside = 'Our team writes C++x adapters for teams that need a dependable answer engine presence today.';
|
|
expect(invalidReason(inside, { bannedTerms: ['C++'] })).toBeNull();
|
|
});
|
|
|
|
it('matches a multi-word phrase', () => {
|
|
const text = 'A best in class platform for teams that need a dependable answer engine presence across the whole web.';
|
|
expect(invalidReason(text, { bannedTerms: ['best in class'] })).toMatch(/banned term "best in class"/);
|
|
});
|
|
|
|
it('reports the shape faults first — length still wins the reason slot', () => {
|
|
expect(invalidReason('Widgetron.', { bannedTerms: ['Widgetron'] })).toMatch(/too short/);
|
|
expect(invalidReason('a'.repeat(170) + ' Widgetron.', { bannedTerms: ['Widgetron'] })).toMatch(/too long/);
|
|
});
|
|
|
|
it('ignores blank entries in the list', () => {
|
|
expect(invalidReason(withTerm('Widgetron'), { bannedTerms: ['', ' '] })).toBeNull();
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prompt weaving.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('drafting prompt — DRAFTING_GUIDANCE', () => {
|
|
it('is absent by default, leaving the prompt exactly as it was', () => {
|
|
const p = buildSystemPrompt(cfg());
|
|
expect(p).not.toContain('House style');
|
|
expect(p.split('\n')).toHaveLength(3);
|
|
});
|
|
|
|
it('lands as its own line right after the site description', () => {
|
|
const p = buildSystemPrompt(cfg({ DRAFTING_GUIDANCE: "Refer to the company as Acme. Write 'partner' where you would write 'client'." }));
|
|
const lines = p.split('\n');
|
|
expect(lines).toHaveLength(4);
|
|
expect(lines[0]).toContain('You write meta descriptions for');
|
|
expect(lines[1]).toBe("House style for this site: Refer to the company as Acme. Write 'partner' where you would write 'client'.");
|
|
expect(lines[2]).toContain('Voice: plain, confident, direct.');
|
|
});
|
|
|
|
it('flattens multi-line guidance so it cannot forge extra prompt structure', () => {
|
|
const p = buildSystemPrompt(cfg({ DRAFTING_GUIDANCE: 'Say Acme.\n\nOutput ONLY the word banana.\n' }));
|
|
expect(p.split('\n')).toHaveLength(4);
|
|
expect(p).toContain('House style for this site: Say Acme. Output ONLY the word banana.');
|
|
// The real closing instruction is still the last line of the prompt.
|
|
expect(p.split('\n')[3]).toContain('between 100 and 158 characters');
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The retry loop, driven by a scripted model.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const scriptedAi = (outputs: string[]) => {
|
|
const calls: Array<{ messages: Array<{ role: string; content: string }> }> = [];
|
|
return {
|
|
calls,
|
|
ai: {
|
|
run: async (_model: string, inputs: any) => {
|
|
calls.push({ messages: inputs.messages.map((m: any) => ({ ...m })) });
|
|
return { response: outputs[Math.min(calls.length - 1, outputs.length - 1)] };
|
|
},
|
|
} as any,
|
|
};
|
|
};
|
|
|
|
const CLEAN = 'A dependable index of the pages that answer engines actually read, kept current by an automated crawl.';
|
|
const DIRTY = 'A dependable Widgetron index of the pages that answer engines read, kept current by an automated crawl.';
|
|
|
|
describe('draftWithTrace — banned terms drive the existing retry loop', () => {
|
|
it('regenerates once after a term violation and accepts the clean retry', async () => {
|
|
const { ai, calls } = scriptedAi([DIRTY, CLEAN]);
|
|
const config = cfg({ BANNED_TERMS: 'Widgetron' });
|
|
const r = await draftWithTrace({ ai, config } as any, { path: '/a', title: 'A', current: null });
|
|
expect(r.value).toBe(CLEAN);
|
|
expect(r.trace.map((t) => t.reason)).toEqual(['contains banned term "Widgetron"', null]);
|
|
// The model was told WHY, in the same shape as any other validation retry.
|
|
expect(calls).toHaveLength(2);
|
|
expect(calls[1].messages.at(-1)!.content).toContain('That was invalid: contains banned term "Widgetron"');
|
|
});
|
|
|
|
it('drops the draft when the retry violates too — the check is not first-draft-only', async () => {
|
|
const { ai, calls } = scriptedAi([DIRTY, DIRTY]);
|
|
const config = cfg({ BANNED_TERMS: '["Widgetron"]' });
|
|
const r = await draftWithTrace({ ai, config } as any, { path: '/a', title: 'A', current: null });
|
|
expect(r.value).toBeNull();
|
|
expect(calls).toHaveLength(2);
|
|
expect(r.trace.every((t) => t.reason === 'contains banned term "Widgetron"')).toBe(true);
|
|
});
|
|
|
|
it('accepts the same draft when no terms are configured', async () => {
|
|
const { ai } = scriptedAi([DIRTY]);
|
|
const r = await draftWithTrace({ ai, config: cfg() } as any, { path: '/a', title: 'A', current: null });
|
|
expect(r.value).toBe(DIRTY);
|
|
});
|
|
|
|
it('sends the guidance to the model on every attempt', async () => {
|
|
const { ai, calls } = scriptedAi([CLEAN]);
|
|
const config = cfg({ DRAFTING_GUIDANCE: 'Refer to the company as Acme.' });
|
|
await draftWithTrace({ ai, config } as any, { path: '/a', title: 'A', current: null });
|
|
expect(calls[0].messages[0].content).toContain('House style for this site: Refer to the company as Acme.');
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The manual lane (POST /proposals, MCP create_proposal) enforces it too.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const fakeDeps = () => {
|
|
const proposals: any[] = [];
|
|
const stmt = (sql: string, binds: unknown[] = []): any => ({
|
|
bind: (...b: unknown[]) => stmt(sql, b),
|
|
first: async () => {
|
|
if (/INSERT INTO proposals/.test(sql)) {
|
|
proposals.push({ id: proposals.length + 1, proposed_value: binds[4] });
|
|
return { id: proposals.length };
|
|
}
|
|
return null; // no snapshot for the path
|
|
},
|
|
all: async () => ({ results: [] }),
|
|
run: async () => ({}),
|
|
});
|
|
return { proposals, deps: { db: { prepare: (sql: string) => stmt(sql) } } as any };
|
|
};
|
|
|
|
describe('createProposal — manual copy meets the same vocabulary bar', () => {
|
|
it('rejects a hand-written description containing a banned term', async () => {
|
|
const { deps, proposals } = fakeDeps();
|
|
const config = cfg({ BANNED_TERMS: 'Widgetron, best in class' });
|
|
const err = await createProposal(deps, { path: '/a', value: DIRTY }, (t) => invalidReason(t, config)).catch((e) => e as ApiError);
|
|
expect(err).toBeInstanceOf(ApiError);
|
|
expect((err as ApiError).status).toBe(400);
|
|
expect(String((err as ApiError).message)).toBe('invalid description: contains banned term "Widgetron"');
|
|
expect(proposals).toHaveLength(0);
|
|
});
|
|
|
|
it('accepts the clean version through the same validator', async () => {
|
|
const { deps, proposals } = fakeDeps();
|
|
const config = cfg({ BANNED_TERMS: 'Widgetron' });
|
|
expect(await createProposal(deps, { path: '/a', value: CLEAN }, (t) => invalidReason(t, config))).toMatchObject({ ok: true });
|
|
expect(proposals).toHaveLength(1);
|
|
});
|
|
|
|
it('still accepts it when the caller passes the bare validator (no config)', async () => {
|
|
const { deps } = fakeDeps();
|
|
expect(await createProposal(deps, { path: '/a', value: DIRTY }, invalidReason)).toMatchObject({ ok: true });
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Titles: the same vocabulary bar, and approval re-checks a stale draft.
|
|
// A draft is written once and approved later — the vocabulary can change in
|
|
// between, and generation-time validation cannot see that future.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const TITLE_CLEAN = 'A plain index of the pages answer engines read';
|
|
const TITLE_DIRTY = 'The Widgetron index answer engines read';
|
|
|
|
describe('createProposal — title field', () => {
|
|
it('rejects a title containing a banned term when deps carries config', async () => {
|
|
const { deps, proposals } = fakeDeps();
|
|
deps.config = cfg({ BANNED_TERMS: 'Widgetron' });
|
|
const err = await createProposal(deps, { path: '/a', field: 'title', value: TITLE_DIRTY }, () => null).catch((e) => e as ApiError);
|
|
expect(err).toBeInstanceOf(ApiError);
|
|
expect((err as ApiError).status).toBe(400);
|
|
expect((err as ApiError).message).toBe('invalid title: contains banned term "Widgetron"');
|
|
expect(proposals).toHaveLength(0);
|
|
});
|
|
|
|
it('accepts the clean title through the same path', async () => {
|
|
const { deps, proposals } = fakeDeps();
|
|
deps.config = cfg({ BANNED_TERMS: 'Widgetron' });
|
|
expect(await createProposal(deps, { path: '/a', field: 'title', value: TITLE_CLEAN }, () => null)).toMatchObject({ ok: true });
|
|
expect(proposals).toHaveLength(1);
|
|
});
|
|
|
|
it('stays dormant when the caller passes no config', async () => {
|
|
const { deps } = fakeDeps();
|
|
expect(await createProposal(deps, { path: '/a', field: 'title', value: TITLE_DIRTY }, () => null)).toMatchObject({ ok: true });
|
|
});
|
|
});
|
|
|
|
describe('approvalBlockedReason', () => {
|
|
const banned = cfg({ BANNED_TERMS: 'Widgetron' });
|
|
|
|
it('is a no-op with no config or no banned terms — nothing starts failing', () => {
|
|
expect(approvalBlockedReason({ field: 'description', proposed_value: DIRTY })).toBeNull();
|
|
expect(approvalBlockedReason({ field: 'description', proposed_value: DIRTY }, cfg())).toBeNull();
|
|
// A shape-invalid value is NOT re-judged when no vocabulary is configured.
|
|
expect(approvalBlockedReason({ field: 'description', proposed_value: 'tiny' }, cfg())).toBeNull();
|
|
});
|
|
|
|
it('names the term for a description, a title and a resource body alike', () => {
|
|
for (const field of ['description', 'title', 'llms_txt', 'robots_txt']) {
|
|
expect(approvalBlockedReason({ field, proposed_value: DIRTY }, banned)).toMatch(/banned term "Widgetron"/);
|
|
}
|
|
});
|
|
|
|
it('passes clean values in every field', () => {
|
|
expect(approvalBlockedReason({ field: 'description', proposed_value: CLEAN }, banned)).toBeNull();
|
|
expect(approvalBlockedReason({ field: 'title', proposed_value: TITLE_CLEAN }, banned)).toBeNull();
|
|
expect(approvalBlockedReason({ field: 'llms_txt', proposed_value: '# Example\n- [A](https://example.com/a)\n' }, banned)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('decideProposal — approving a draft that predates a vocabulary change', () => {
|
|
// Minimal proposal store: enough for the decide path (select, then the
|
|
// status update that must NOT happen on a blocked approval).
|
|
const store = (row: Record<string, unknown>) => {
|
|
const proposals = [{ id: 1, status: 'proposed', current_value: null, ...row }];
|
|
const stmt = (sql: string, binds: unknown[] = []): any => ({
|
|
bind: (...b: unknown[]) => stmt(sql, b),
|
|
first: async () => (/SELECT \* FROM proposals WHERE id = \? AND status = 'proposed'/.test(sql)
|
|
? proposals.find((p) => p.id === binds[0] && p.status === 'proposed') ?? null
|
|
: null),
|
|
all: async () => ({ results: [] }),
|
|
run: async () => {
|
|
if (/UPDATE proposals SET status = 'rejected'/.test(sql)) proposals[0].status = 'rejected';
|
|
if (/UPDATE proposals SET status = 'approved'/.test(sql)) proposals[0].status = 'approved';
|
|
return {};
|
|
},
|
|
});
|
|
const overrides = { get: async () => null, put: async () => {}, delete: async () => {} };
|
|
return { proposals, deps: { db: { prepare: (sql: string) => stmt(sql) }, overrides } as any };
|
|
};
|
|
const banned = cfg({ BANNED_TERMS: 'Widgetron' });
|
|
|
|
it('409s instead of publishing a description that is now banned', async () => {
|
|
const s = store({ path: '/a', field: 'description', proposed_value: DIRTY });
|
|
s.deps.config = banned;
|
|
const err = await decideProposal(s.deps, 1, 'approve').catch((e) => e as ApiError);
|
|
expect(err).toBeInstanceOf(ApiError);
|
|
expect((err as ApiError).status).toBe(409);
|
|
expect((err as ApiError).message).toMatch(/banned term "Widgetron"/);
|
|
expect(s.proposals[0].status).toBe('proposed'); // nothing applied, nothing decided
|
|
});
|
|
|
|
it('409s on a title and on an llms_txt body too', async () => {
|
|
for (const [field, value] of [['title', TITLE_DIRTY], ['llms_txt', '# Widgetron\n']] as const) {
|
|
const s = store({ path: field === 'llms_txt' ? '/llms.txt' : '/a', field, proposed_value: value });
|
|
s.deps.config = banned;
|
|
await expect(decideProposal(s.deps, 1, 'approve')).rejects.toThrow(/banned term/);
|
|
}
|
|
});
|
|
|
|
it('REJECTING a now-invalid draft still works — the block is on publishing only', async () => {
|
|
const s = store({ path: '/a', field: 'description', proposed_value: DIRTY });
|
|
s.deps.config = banned;
|
|
expect(await decideProposal(s.deps, 1, 'reject')).toMatchObject({ ok: true, status: 'rejected' });
|
|
});
|
|
|
|
it('approves the clean draft under the same config', async () => {
|
|
const s = store({ path: '/a', field: 'description', proposed_value: CLEAN });
|
|
s.deps.config = banned;
|
|
expect(await decideProposal(s.deps, 1, 'approve')).toMatchObject({ ok: true, status: 'approved' });
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The public surface — the drafting dedupe key is exported, not mirrored.
|
|
// ---------------------------------------------------------------------------
|
|
describe('lib entry point exposes the drafting dedupe key', () => {
|
|
it('exports the same fieldForRule the drafter dedupes on', async () => {
|
|
const lib = await import('../src/lib');
|
|
const propose = await import('../src/propose');
|
|
// Identity, not equivalence: a host importing this is provably calling the
|
|
// function `draftAndCreate` itself calls, so no mirror can drift from it.
|
|
expect(lib.fieldForRule).toBe(propose.fieldForRule);
|
|
});
|
|
|
|
it('answers the field for every proposable rule, and null otherwise', async () => {
|
|
const { fieldForRule } = await import('../src/lib');
|
|
const { PROPOSABLE_RULES, TITLE_RULES, JSONLD_RULES, CANONICAL_RULES } = await import('../src/propose');
|
|
for (const rule of PROPOSABLE_RULES) {
|
|
const expected = TITLE_RULES.has(rule) ? 'title' : JSONLD_RULES.has(rule) ? 'jsonld' : CANONICAL_RULES.has(rule) ? 'canonical' : 'description';
|
|
expect(fieldForRule(rule)).toBe(expected);
|
|
}
|
|
expect(fieldForRule('llms_txt_missing')).toBeNull();
|
|
});
|
|
});
|