Files
Alan WizemannandClaude Fable 5 03e1b427fa feat: the crawl reads what AI reads, and a twin answers for its page — body-text capture + deps.twinned (v1.27.0)
Two host-facing senses grow up together. The crawl now stores each page's
JS-free body text (PageSnapshot.text, capped at MAX_SNAPSHOT_TEXT=4000) and
an uncapped word_count — exactly what GPTBot/ClaudeBot see, persisted so
downstream fact-extraction and thin-page senses read stored truth instead of
refetching. schema.sql gains the two nullable columns; existing installs run
the two ALTERs documented in README's 'Upgrading an existing install'.

And AgentDeps gains twinned — a set of page paths the host currently serves
a readable twin for, read only by ai_page_body_empty. The measurement (raw
HTML has no visible body text) stays true of a twinned page; the claim ('AI
crawlers see an empty page here') does not, and only the host knows that. The
path stays sampled — blocked/error senses keep measuring — but the body-empty
finding is withheld at trigger time, so a previously open row auto-resolves
via runRules' existing lifecycle instead of being re-opened and host-resolved
every audit. Absent (every self-hosted deployment) nothing changes anywhere.

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

133 lines
5.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { parseRobots, robotsDecision, hasExplicitAiPolicy, pickSample, dayOfYear } from '../src/aeo';
describe('parseRobots + robotsDecision', () => {
const txt = `
User-agent: *
Disallow: /admin
User-agent: GPTBot
Disallow: /
User-agent: Googlebot
Allow: /
Disallow: /private
`;
const groups = parseRobots(txt);
it('blocks a bot disallowed at /', () => {
expect(robotsDecision(groups, 'GPTBot', '/')).toBe('block');
});
it('is case-insensitive on the agent token', () => {
expect(robotsDecision(groups, 'gptbot', '/')).toBe('block');
});
it('allows a bot with an explicit Allow: / (longest match wins)', () => {
expect(robotsDecision(groups, 'Googlebot', '/')).toBe('allow');
expect(robotsDecision(groups, 'Googlebot', '/private')).toBe('block');
});
it('falls back to the * group for unnamed bots', () => {
expect(robotsDecision(groups, 'RandomBot', '/')).toBe('allow');
expect(robotsDecision(groups, 'RandomBot', '/admin')).toBe('block');
});
it('allows everything when robots names no matching group', () => {
expect(robotsDecision(parseRobots('Sitemap: https://x/s.xml'), 'GPTBot', '/')).toBe('allow');
});
it('treats an empty Disallow: as allow-all', () => {
expect(robotsDecision(parseRobots('User-agent: *\nDisallow:'), 'AnyBot', '/')).toBe('allow');
});
});
describe('hasExplicitAiPolicy', () => {
it('is true when a known AI bot is named', () => {
expect(hasExplicitAiPolicy(parseRobots('User-agent: PerplexityBot\nDisallow:'))).toBe(true);
});
it('is false when only generic agents appear', () => {
expect(hasExplicitAiPolicy(parseRobots('User-agent: *\nDisallow: /'))).toBe(false);
});
});
describe('pickSample (deliverability rotation)', () => {
const pool = ['a', 'b', 'c', 'd'];
it('picks count items from the offset, wrapping around', () => {
expect(pickSample(pool, 3, 0)).toEqual(['a', 'b', 'c']);
expect(pickSample(pool, 3, 1)).toEqual(['b', 'c', 'd']);
expect(pickSample(pool, 3, 3)).toEqual(['d', 'a', 'b']);
});
it('normalizes offsets larger than the pool', () => {
expect(pickSample(pool, 2, 4)).toEqual(['a', 'b']); // 4 % 4 === 0
expect(pickSample(pool, 2, 5)).toEqual(['b', 'c']);
});
it('never returns more than the pool size, and handles empty', () => {
expect(pickSample(['x', 'y'], 3, 0)).toEqual(['x', 'y']);
expect(pickSample([], 3, 0)).toEqual([]);
});
});
describe('dayOfYear', () => {
it('is 1 on Jan 1 and 365 on Dec 31 (non-leap)', () => {
expect(dayOfYear(new Date('2026-01-01T12:00:00Z'))).toBe(1);
expect(dayOfYear(new Date('2026-12-31T00:00:00Z'))).toBe(365);
});
});
// deps.twinned suppression: a page the host serves a live twin for must not
// file ai_page_body_empty (the claim is answered), while every other sense on
// that same page — blocked, errors — keeps measuring. Exercises the REAL
// aeoChecks pipeline over a stubbed fetch, not a re-implementation.
import { vi, afterEach } from 'vitest';
import { aeoChecks } from '../src/aeo';
import { resolveSiteConfig } from '../src/config';
const SHELL = '<html><body><div id="app"></div><script src="/a.js"></script></body></html>';
function snap(path: string) {
return {
path, status: 200, title: 't', description: null, canonical: null,
ogImage: null, ogType: null, jsonldTypes: [], noindex: false,
lastmod: null, error: null, text: null, wordCount: 0,
};
}
function serveAeo(pages: Record<string, { status?: number; body?: string }>) {
const fn = vi.fn(async (input: RequestInfo | URL) => {
const path = new URL(String(input)).pathname;
if (path === '/robots.txt') return new Response('User-agent: GPTBot\nDisallow:', { headers: { 'content-type': 'text/plain' } });
if (path === '/llms.txt' || path === '/llms-full.txt') return new Response('# index', { headers: { 'content-type': 'text/plain' } });
const p = pages[path] ?? {};
return new Response(p.body ?? SHELL, { status: p.status ?? 200, headers: { 'content-type': 'text/html' } });
});
vi.stubGlobal('fetch', fn);
return fn;
}
describe('aeoChecks deps.twinned suppression', () => {
const cfg = resolveSiteConfig({ SITE_URL: 'https://example.com' });
afterEach(() => vi.unstubAllGlobals());
it('suppresses ai_page_body_empty for twinned paths and keeps it for the rest', async () => {
serveAeo({});
const out = await aeoChecks(cfg, [snap('/a'), snap('/b')], new Set(['/a']));
const empty = out.filter((f) => f.rule === 'ai_page_body_empty').map((f) => f.path);
expect(empty).toEqual(['/b']);
});
it('changes nothing when twinned is absent (the self-hosted case)', async () => {
serveAeo({});
const out = await aeoChecks(cfg, [snap('/a'), snap('/b')]);
expect(out.filter((f) => f.rule === 'ai_page_body_empty')).toHaveLength(2);
});
it('still files ai_page_blocked on a twinned page — only the body-empty claim is answered', async () => {
serveAeo({ '/a': { status: 403 } });
const out = await aeoChecks(cfg, [snap('/a')], new Set(['/a']));
expect(out.some((f) => f.rule === 'ai_page_blocked' && f.path === '/a')).toBe(true);
expect(out.some((f) => f.rule === 'ai_page_body_empty')).toBe(false);
});
it('does not suppress a readable page it never would have flagged (no false accounting)', async () => {
serveAeo({ '/a': { body: `<html><body><article>${'real words here '.repeat(60)}</article></body></html>` } });
const out = await aeoChecks(cfg, [snap('/a')], new Set(['/a']));
expect(out.some((f) => f.rule === 'ai_page_body_empty')).toBe(false);
});
});