Files
Alan WizemannandClaude Fable 5 c89e9ae514 fix(rules): the inferred article section only flags pages shaped like its articles
Fresh-eyes audit of v1.29.0's inference: plain startsWith flagged the
section index itself (a CollectionPage, not an Article), pagination,
tag indexes, and — in sitemap mode, where non-HTML locs are snapshotted
too — even /blog/rss.xml. An inferred section now additionally requires
the same path depth as a declaring Article page, no file extension, and
not the section index. A CONFIGURED prefix keeps the plain startsWith
the config has always meant — the operator's word is not second-guessed.

Also: inferredArticlePrefix exported from the package root (hosts can
say what was inferred), the og-image test shim learns the canonical
link selector, and the three head lanes (jsonld + canonical + og) are
pinned together — the handler-coexistence assumption the one-slot bug
lived in is now tested rather than assumed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-10 23:58:03 -04:00

161 lines
6.3 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { decodeEntities, validateTitle } from '../src/rules';
describe('decodeEntities', () => {
it('decodes the common entities', () => {
expect(decodeEntities('a &amp; b')).toBe('a & b');
expect(decodeEntities('&lt;tag&gt;')).toBe('<tag>');
expect(decodeEntities('say &quot;hi&quot;')).toBe('say "hi"');
expect(decodeEntities('it&#39;s')).toBe("it's");
expect(decodeEntities('it&apos;s')).toBe("it's");
});
it('decodes &amp; LAST so already-escaped entities are not double-decoded', () => {
// "&amp;lt;" is a literal "&lt;" — decoding &amp; first would wrongly yield "<".
expect(decodeEntities('&amp;lt;')).toBe('&lt;');
expect(decodeEntities('&amp;amp;')).toBe('&amp;');
expect(decodeEntities('Tom &amp; Jerry &lt; Spike')).toBe('Tom & Jerry < Spike');
});
});
describe('validateTitle', () => {
it('rejects empty / whitespace-only titles', () => {
expect(validateTitle('')).toBeTruthy();
expect(validateTitle(' ')).toBeTruthy();
});
it('rejects over-long titles (max 80)', () => {
expect(validateTitle('x'.repeat(81))).toBeTruthy();
expect(validateTitle('x'.repeat(80))).toBeNull();
});
it('accepts a sensible title', () => {
expect(validateTitle('About Alan Wizemann | Alan Wizemann')).toBeNull();
});
});
// ---------------------------------------------------------------------------
// The article prefix, inferred from the crawl (v1.29.0). The check used to be
// exactly as good as the operator's memory of ARTICLE_PATH_PREFIX; now the
// crawl's own Article declarations name the section, conservatively.
import { inferredArticlePrefix } from '../src/rules';
describe('inferredArticlePrefix', () => {
const page = (path: string, types: string[] = []) => ({ path, jsonldTypes: types });
it('names the section when two or more pages under one segment declare Article', () => {
expect(inferredArticlePrefix([
page('/blog/a', ['Article']),
page('/blog/b', ['Article']),
page('/blog/c'),
page('/pricing'),
])).toBe('/blog/');
});
it('one declaring page names itself, not a section', () => {
expect(inferredArticlePrefix([
page('/blog/a', ['Article']),
page('/blog/b'),
])).toBeNull();
});
it('root-level Article pages carry no section — never infers "/"', () => {
expect(inferredArticlePrefix([
page('/a', ['Article']),
page('/b', ['Article']),
])).toBeNull();
});
it('the busiest section wins; ties break alphabetically, so runs never churn', () => {
expect(inferredArticlePrefix([
page('/news/a', ['Article']), page('/news/b', ['Article']), page('/news/c', ['Article']),
page('/blog/a', ['Article']), page('/blog/b', ['Article']),
])).toBe('/news/');
expect(inferredArticlePrefix([
page('/news/a', ['Article']), page('/news/b', ['Article']),
page('/blog/a', ['Article']), page('/blog/b', ['Article']),
])).toBe('/blog/');
});
it('nested paths still cluster under the FIRST segment', () => {
expect(inferredArticlePrefix([
page('/blog/2026/a', ['Article']),
page('/blog/2026/b', ['Article']),
])).toBe('/blog/');
});
it('non-Article types do not count toward a section', () => {
expect(inferredArticlePrefix([
page('/blog/a', ['Product']),
page('/blog/b', ['FAQPage']),
])).toBeNull();
});
});
// The inferred section flags only pages SHAPED like the declaring ones
// (fresh-eyes audit 2026-08-11): the runRules-side filter is exercised through
// the same pure helpers, so pin the shapes at the inference boundary here and
// the behavioral cut in the integration below.
import { runRules } from '../src/rules';
describe('inferred article section — the shape filter', () => {
const snap = (path: string, types: string[] = []) => ({
path, status: 200, title: 't', description: 'd'.repeat(80), canonical: null,
ogImage: 'https://x.test/i.png', ogType: null, jsonldTypes: types, noindex: false,
lastmod: null, error: null, text: null, wordCount: null,
}) as any;
const db = () => ({
prepare: () => ({
bind: () => ({
first: async () => null, all: async () => ({ results: [] }), run: async () => ({}),
}),
first: async () => null, all: async () => ({ results: [] }), run: async () => ({}),
}),
batch: async () => [],
}) as any;
const config = { siteUrl: 'https://x.test', articlePathPrefix: '' } as any;
async function fired(snaps: any[], cfg = config): Promise<string[]> {
const paths: string[] = [];
const deps = { db: db(), config: cfg };
// runRules writes findings through db; intercept via the triggered list is
// internal, so read the upsert instead: simplest honest observation is the
// db above recording nothing — so we re-run the check through the public
// seam: runRules resolves with counts, and the paths are recovered from the
// INSERT bindings.
const rows: any[] = [];
deps.db.prepare = (sql: string) => ({
bind: (...b: any[]) => {
if (/INSERT INTO findings/i.test(sql) && b[3] === 'missing_article_jsonld') rows.push(b[2]);
return { first: async () => null, all: async () => ({ results: [] }), run: async () => ({}) };
},
first: async () => null, all: async () => ({ results: [] }), run: async () => ({}),
});
await runRules(deps, 1, snaps);
paths.push(...rows);
return paths;
}
it('flags same-depth siblings; spares the index, files, pagination and tag indexes', async () => {
const paths = await fired([
snap('/blog/a', ['Article']),
snap('/blog/b', ['Article']),
snap('/blog/c'), // same shape, no Article → flagged
snap('/blog/'), // section index → spared
snap('/blog/rss.xml'), // file → spared
snap('/blog/page/2'), // deeper than the articles → spared
snap('/blog/tags/x'), // deeper → spared
snap('/pricing'), // outside the section → spared
]);
expect(paths).toEqual(['/blog/c']);
});
it('a CONFIGURED prefix keeps the plain startsWith the config always meant', async () => {
const paths = await fired(
[snap('/blog/'), snap('/blog/page/2'), snap('/blog/a', ['Article']), snap('/blog/b', ['Article'])],
{ ...config, articlePathPrefix: '/blog/' },
);
expect(paths.sort()).toEqual(['/blog/', '/blog/page/2']);
});
});