mirror of
https://github.com/citeworthyio/seo-agent.git
synced 2026-08-11 13:22:39 +02:00
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>
207 lines
9.5 KiB
TypeScript
207 lines
9.5 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { parseSitemap, dedupeAndCap, extractBodyText, MAX_SNAPSHOT_TEXT } from '../src/crawl';
|
|
|
|
const ORIGIN = 'https://example.com';
|
|
|
|
describe('parseSitemap', () => {
|
|
it('parses a <urlset> into entries (same-origin only)', () => {
|
|
const xml = `<?xml version="1.0"?><urlset>
|
|
<url><loc>https://example.com/a</loc><lastmod>2026-01-01</lastmod></url>
|
|
<url><loc>https://example.com/b</loc></url>
|
|
<url><loc>https://other.com/c</loc></url>
|
|
</urlset>`;
|
|
const { entries, sitemaps } = parseSitemap(xml, ORIGIN);
|
|
expect(entries).toEqual([
|
|
{ loc: 'https://example.com/a', lastmod: '2026-01-01' },
|
|
{ loc: 'https://example.com/b', lastmod: null },
|
|
]);
|
|
expect(sitemaps).toEqual([]);
|
|
});
|
|
|
|
it('parses a <sitemapindex> into child sitemap locations', () => {
|
|
const xml = `<?xml version="1.0"?><sitemapindex>
|
|
<sitemap><loc>https://example.com/sitemap-1.xml</loc></sitemap>
|
|
<sitemap><loc>https://example.com/sitemap-2.xml</loc></sitemap>
|
|
<sitemap><loc>https://cdn.other.com/s.xml</loc></sitemap>
|
|
</sitemapindex>`;
|
|
const { entries, sitemaps } = parseSitemap(xml, ORIGIN);
|
|
expect(entries).toEqual([]);
|
|
expect(sitemaps).toEqual(['https://example.com/sitemap-1.xml', 'https://example.com/sitemap-2.xml']);
|
|
});
|
|
|
|
it('rejects prefix-lookalike origins: userinfo tricks, lookalike hosts, unparseable locs', () => {
|
|
const xml = `<?xml version="1.0"?><urlset>
|
|
<url><loc>https://example.com@evil.net/steal</loc></url>
|
|
<url><loc>https://example.com.evil.net/steal</loc></url>
|
|
<url><loc>https://example.com:8443/other-port</loc></url>
|
|
<url><loc>not a url at all</loc></url>
|
|
<url><loc>https://example.com/legit</loc></url>
|
|
</urlset>`;
|
|
const { entries } = parseSitemap(xml, ORIGIN);
|
|
expect(entries.map((e) => e.loc)).toEqual(['https://example.com/legit']);
|
|
});
|
|
|
|
it('rejects lookalike child sitemaps in a <sitemapindex>', () => {
|
|
const xml = `<sitemapindex>
|
|
<sitemap><loc>https://example.com@evil.net/s.xml</loc></sitemap>
|
|
<sitemap><loc>https://example.com/s.xml</loc></sitemap>
|
|
</sitemapindex>`;
|
|
expect(parseSitemap(xml, ORIGIN).sitemaps).toEqual(['https://example.com/s.xml']);
|
|
});
|
|
});
|
|
|
|
describe('dedupeAndCap', () => {
|
|
it('dedupes by loc, keeping first occurrence', () => {
|
|
const { entries, truncated } = dedupeAndCap(
|
|
[
|
|
{ loc: 'https://example.com/a', lastmod: '1' },
|
|
{ loc: 'https://example.com/a', lastmod: '2' },
|
|
{ loc: 'https://example.com/b', lastmod: null },
|
|
],
|
|
2000
|
|
);
|
|
expect(entries.map((e) => e.loc)).toEqual(['https://example.com/a', 'https://example.com/b']);
|
|
expect(entries[0].lastmod).toBe('1');
|
|
expect(truncated).toBe(false);
|
|
});
|
|
|
|
it('caps the total and flags truncation', () => {
|
|
const many = Array.from({ length: 5 }, (_, i) => ({ loc: `https://example.com/${i}`, lastmod: null }));
|
|
const { entries, truncated } = dedupeAndCap(many, 3);
|
|
expect(entries).toHaveLength(3);
|
|
expect(truncated).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- body-text capture -------------------------------------------------------
|
|
// The snapshot's `text` is what an AI crawler reads: a plain fetch, no JS. These
|
|
// tests are the proof that code never leaks into it — an HTMLRewriter `text`
|
|
// handler on `body` DOES receive <script>/<style>/<noscript>/<template>/<svg>
|
|
// content (measured on workerd), and `el.remove()` does not suppress it, which
|
|
// is why extraction is a pure pass over the buffered HTML instead.
|
|
describe('extractBodyText', () => {
|
|
it('captures readable copy and counts its words', () => {
|
|
const html = `<html><head><title>T</title><meta name="description" content="D"></head>
|
|
<body><h1>Caching explained</h1><p>Edge caches store <strong>copies</strong> near users.</p></body></html>`;
|
|
expect(extractBodyText(html)).toEqual({
|
|
text: 'Caching explained Edge caches store copies near users.',
|
|
wordCount: 8,
|
|
});
|
|
});
|
|
|
|
it('excludes script, style, noscript, template, svg and iframe content', () => {
|
|
const html = `<html><head><style>.a{color:red}</style></head><body>
|
|
<h1>Real</h1>
|
|
<script>var LEAK_JS = 1; console.log("LEAK_STRING");</script>
|
|
<script type="application/ld+json">{"@type":"LEAK_JSONLD"}</script>
|
|
<style>.b{content:"LEAK_CSS"}</style>
|
|
<noscript>LEAK_NOSCRIPT</noscript>
|
|
<template><p>LEAK_TEMPLATE</p></template>
|
|
<svg viewBox="0 0 1 1"><title>LEAK_SVG_TITLE</title><text>LEAK_SVG_TEXT</text></svg>
|
|
<iframe src="/x"><p>LEAK_IFRAME</p></iframe>
|
|
<p>Copy</p></body></html>`;
|
|
const { text } = extractBodyText(html);
|
|
expect(text).toBe('Real Copy');
|
|
expect(text).not.toMatch(/LEAK/);
|
|
});
|
|
|
|
it('gives an unclosed script the rest of the document (as the parser does)', () => {
|
|
const html = '<body><p>Before</p><script>var a = 1; <p>After</p>';
|
|
expect(extractBodyText(html)).toEqual({ text: 'Before', wordCount: 1 });
|
|
});
|
|
|
|
it('ends a script at the first </script>, even inside a JS string', () => {
|
|
const html = `<body><p>A</p><script>var s = "</script>";</script><p>B</p></body>`;
|
|
// Everything after the first close tag is markup again — including the
|
|
// stray `";` the browser also renders. What matters: no JS identifiers leak.
|
|
const { text } = extractBodyText(html);
|
|
expect(text).toContain('A');
|
|
expect(text).toContain('B');
|
|
expect(text).not.toContain('var s');
|
|
});
|
|
|
|
it('drops head metadata and HTML comments but keeps stray copy before <body>', () => {
|
|
const html = `<!doctype html><html><head><title>Title</title></head>Stray<!-- HIDDEN --><body><p>Copy</p></body></html>`;
|
|
expect(extractBodyText(html).text).toBe('Stray Copy');
|
|
});
|
|
|
|
it('puts a word boundary at every tag, so adjacent blocks never fuse', () => {
|
|
expect(extractBodyText('<body><p>Hello</p><p>World</p></body>').wordCount).toBe(2);
|
|
});
|
|
|
|
it('decodes entities and collapses whitespace, including ', () => {
|
|
const html = '<body><p>Tom &\n\n Jerry <tag> — —</p></body>';
|
|
// 3 words: `&` and the two em dashes are punctuation, not words.
|
|
expect(extractBodyText(html)).toEqual({ text: 'Tom & Jerry <tag> — —', wordCount: 3 });
|
|
});
|
|
|
|
it('does not double-decode an already-escaped entity', () => {
|
|
expect(extractBodyText('<body>&lt;p&gt;</body>').text).toBe('<p>');
|
|
});
|
|
|
|
it('does not let a SELF-closing <svg/> swallow the rest of the page', () => {
|
|
expect(extractBodyText('<body><p>A</p><svg viewBox="0 0 1 1"/><p>B</p></body>').text).toBe('A B');
|
|
});
|
|
|
|
it('keeps a tag whole when a quoted attribute contains ">"', () => {
|
|
expect(extractBodyText('<body><a title="a > b" href="/x">Link</a> here</body>').text).toBe('Link here');
|
|
});
|
|
|
|
it('leaves comparison signs in prose alone', () => {
|
|
expect(extractBodyText('<body><p>5 < 10 and 20 > 3</p></body>').text).toBe('5 < 10 and 20 > 3');
|
|
});
|
|
|
|
it('still strips a tag with an unbalanced quote', () => {
|
|
expect(extractBodyText('<body><a href="/x>Link</a> here</body>').text).toBe('Link here');
|
|
});
|
|
|
|
it('tolerates an unterminated final tag', () => {
|
|
expect(extractBodyText('<body><p>Copy</p><div class="x').text).toBe('Copy');
|
|
});
|
|
|
|
it('reads a JS shell as no text at all', () => {
|
|
const html = `<html><head><title>App</title></head><body><div id="root"></div>
|
|
<script src="/app.js"></script><script>window.__DATA__={"a":1};</script></body></html>`;
|
|
expect(extractBodyText(html)).toEqual({ text: null, wordCount: 0 });
|
|
});
|
|
|
|
// A commented-out or quoted open tag is NOT an element. Reading one as an
|
|
// unclosed raw-text element deletes the rest of the page and records a real
|
|
// page as a JS shell — the exact false signal this snapshot exists to catch.
|
|
it('keeps copy after a commented-out <script> open tag', () => {
|
|
const html = '<body><!-- <script> --><p>REAL COPY</p></body>';
|
|
expect(extractBodyText(html)).toEqual({ text: 'REAL COPY', wordCount: 2 });
|
|
});
|
|
|
|
it('keeps copy after a commented-out <noscript> open tag', () => {
|
|
const html = '<body><!-- <noscript> --><p>REAL COPY</p></body>';
|
|
expect(extractBodyText(html)).toEqual({ text: 'REAL COPY', wordCount: 2 });
|
|
});
|
|
|
|
it('keeps copy after an open tag written inside an attribute value', () => {
|
|
expect(extractBodyText('<p data-x="<script>">REAL COPY</p>').text).toBe('REAL COPY');
|
|
});
|
|
|
|
it('does not let a nested <svg> or <template> end its parent early', () => {
|
|
expect(extractBodyText('<body><svg><svg></svg>LEAK</svg><p>Copy</p></body>').text).toBe('Copy');
|
|
expect(extractBodyText('<body><template><template></template>LEAK</template><p>Copy</p></body>').text).toBe('Copy');
|
|
});
|
|
|
|
// Same answer as the cloud's shared/md-extract extractPage: with no </head>
|
|
// and no <body> there is no boundary to trust, so the document is read whole.
|
|
it('reads a head-only document whole when neither </head> nor <body> exists', () => {
|
|
expect(extractBodyText('<html><head><title>TitleWord</title><p>Copy</p>').text).toBe('TitleWord Copy');
|
|
});
|
|
|
|
it('caps stored text at a word boundary but counts the whole page', () => {
|
|
const words = 2000; // "word " * 2000 = 10000 chars, well past the cap
|
|
const html = `<body><p>${Array.from({ length: words }, () => 'word').join(' ')}</p></body>`;
|
|
const { text, wordCount } = extractBodyText(html);
|
|
expect(wordCount).toBe(words); // honest: counted BEFORE the cap
|
|
expect(text!.length).toBeLessThanOrEqual(MAX_SNAPSHOT_TEXT);
|
|
expect(text!.length).toBeGreaterThan(MAX_SNAPSHOT_TEXT - 10);
|
|
expect(text!.endsWith('word')).toBe(true); // never cut mid-word
|
|
expect(text).not.toMatch(/\s$/);
|
|
});
|
|
});
|