mirror of
https://github.com/citeworthyio/seo-agent.git
synced 2026-08-11 13:22:39 +02:00
The homepage-crawl fallback (v1.18.0) let a sitemap-less site be audited. This closes the loop: the crawl it did is turned into the sitemap the site never had, published through the ordinary review lane and served at the edge. - src/sitemap.ts: buildSitemap (pure) + generateSitemap + createSitemapProposal, on a new fixed resource field `sitemap_xml` → /sitemap.xml. - ORIGIN WINS, enforced upstream: the proposal is refused unless the `sitemap_missing` finding is open — the crawler's own verdict that discovery had to fall back. The first run a real sitemap parses, the finding resolves and the offer goes with it, so a working sitemap is never shadowed. - NOTHING INVENTED: only pages the crawl fetched 200 and that aren't noindexed. No <priority>, no <changefreq>. <lastmod> only where one was OBSERVED — a crawl timestamp is when we looked, not when the page changed. - Too big to publish is refused loudly rather than silently truncated: a URL missing from a sitemap is invisible in a way a short llms.txt is not. - injector: /sitemap.xml joins RESOURCE_PATHS on the same presence terms as the rest — no key, no override, the origin's own file is proxied untouched. - rules: the sitemap_missing copy now names the fix. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
262 lines
12 KiB
TypeScript
262 lines
12 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import worker from '../injector/src/index';
|
|
|
|
// Minimal fake KVNamespace — only `get` is exercised by the resource path.
|
|
function fakeKv(entries: Record<string, string>) {
|
|
return {
|
|
get: vi.fn(async (key: string) => (key in entries ? entries[key] : null)),
|
|
};
|
|
}
|
|
|
|
function fakeCtx(): ExecutionContext {
|
|
return {
|
|
waitUntil: vi.fn(),
|
|
passThroughOnException: vi.fn(),
|
|
} as unknown as ExecutionContext;
|
|
}
|
|
|
|
const baseEnv = { ORIGIN_HOST: 'origin.example.com' };
|
|
|
|
describe('injector resource overrides (llms.txt / llms-full.txt)', () => {
|
|
let fetchMock: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
fetchMock = vi.fn(async () => new Response('origin body', { status: 200, headers: { 'content-type': 'text/plain' } }));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it('serves a KV resource override on an allowlisted path without touching the origin', async () => {
|
|
const kv = fakeKv({ 'resource:/llms.txt': JSON.stringify({ contentType: 'text/plain; charset=utf-8', body: '# hello' }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const req = new Request('https://example.com/llms.txt');
|
|
const res = await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8');
|
|
expect(res.headers.get('cache-control')).toBe('public, max-age=300');
|
|
expect(await res.text()).toBe('# hello');
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('proxies to the origin when the request asks to bypass resource overrides', async () => {
|
|
// The agent's generators send this header: they must read the ORIGIN's file
|
|
// to append to it, not the copy we published over it last time.
|
|
const kv = fakeKv({ 'resource:/robots.txt': JSON.stringify({ contentType: 'text/plain; charset=utf-8', body: 'ours' }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const req = new Request('https://example.com/robots.txt', { headers: { 'x-seo-agent-bypass': 'resource' } });
|
|
const res = await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(await res.text()).toBe('origin body');
|
|
expect(fetchMock).toHaveBeenCalled();
|
|
expect(kv.get).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('ignores an unrelated bypass value and still serves the override', async () => {
|
|
const kv = fakeKv({ 'resource:/robots.txt': JSON.stringify({ contentType: 'text/plain; charset=utf-8', body: 'ours' }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const req = new Request('https://example.com/robots.txt', { headers: { 'x-seo-agent-bypass': 'nonsense' } });
|
|
const res = await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(await res.text()).toBe('ours');
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('proxies to origin when no override exists for an allowlisted path', async () => {
|
|
const kv = fakeKv({});
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const req = new Request('https://example.com/llms-full.txt');
|
|
const res = await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(fetchMock).toHaveBeenCalled();
|
|
expect(await res.text()).toBe('origin body');
|
|
});
|
|
|
|
it('never performs a resource lookup for a non-allowlisted path', async () => {
|
|
const kv = fakeKv({ 'resource:/llms.txt': JSON.stringify({ contentType: 'text/plain', body: 'x' }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
// Was /sitemap.xml until v1.19.0, which made that path a real resource —
|
|
// an ordinary page is the honest stand-in for "not in the allowlist".
|
|
const req = new Request('https://example.com/some/page');
|
|
await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(kv.get).not.toHaveBeenCalled();
|
|
expect(fetchMock).toHaveBeenCalled();
|
|
});
|
|
|
|
it('serves a robots.txt override, replacing the origin response entirely', async () => {
|
|
const body = 'User-agent: *\nAllow: /\n';
|
|
const kv = fakeKv({ 'resource:/robots.txt': JSON.stringify({ contentType: 'text/plain; charset=utf-8', body }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const res = await worker.fetch(new Request('https://example.com/robots.txt'), env, fakeCtx());
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8');
|
|
expect(await res.text()).toBe(body);
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// /sitemap.xml joins the allowlist in v1.19.0. The origin-wins guarantee for
|
|
// it lives UPSTREAM (the agent only ever offers to generate one for a site
|
|
// whose own sitemap could not serve), so here it behaves exactly like the
|
|
// other resources: a published key wins, and no key means the origin's own
|
|
// sitemap is proxied untouched.
|
|
it('serves a published sitemap.xml override with its XML content type', async () => {
|
|
const body = '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n</urlset>\n';
|
|
const kv = fakeKv({ 'resource:/sitemap.xml': JSON.stringify({ contentType: 'application/xml; charset=utf-8', body }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const res = await worker.fetch(new Request('https://example.com/sitemap.xml'), env, fakeCtx());
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get('content-type')).toBe('application/xml; charset=utf-8');
|
|
expect(await res.text()).toBe(body);
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// THE ORIGIN-WINS REGRESSION TEST. A site that serves its own sitemap has no
|
|
// published key, and must keep serving its own bytes.
|
|
it('proxies sitemap.xml to the origin when nothing is published', async () => {
|
|
const kv = fakeKv({});
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const res = await worker.fetch(new Request('https://example.com/sitemap.xml'), env, fakeCtx());
|
|
expect(await res.text()).toBe('origin body');
|
|
expect(fetchMock).toHaveBeenCalled();
|
|
});
|
|
|
|
it('proxies robots.txt to the origin when no override is published', async () => {
|
|
const kv = fakeKv({});
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const res = await worker.fetch(new Request('https://example.com/robots.txt'), env, fakeCtx());
|
|
|
|
expect(fetchMock).toHaveBeenCalled();
|
|
expect(await res.text()).toBe('origin body');
|
|
});
|
|
|
|
it('fails open and proxies to origin on malformed override JSON', async () => {
|
|
const kv = fakeKv({ 'resource:/llms.txt': '{ not valid json' });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const req = new Request('https://example.com/llms.txt');
|
|
const res = await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(fetchMock).toHaveBeenCalled();
|
|
expect(await res.text()).toBe('origin body');
|
|
});
|
|
|
|
it('returns headers only (no body) for a HEAD request', async () => {
|
|
const kv = fakeKv({ 'resource:/llms.txt': JSON.stringify({ contentType: 'text/plain', body: 'full body text' }) });
|
|
const env = { ...baseEnv, SEO_OVERRIDES: kv } as unknown as Env;
|
|
const req = new Request('https://example.com/llms.txt', { method: 'HEAD' });
|
|
const res = await worker.fetch(req, env, fakeCtx());
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get('content-type')).toBe('text/plain');
|
|
expect(await res.text()).toBe('');
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Remote mode — the mode a hosted service actually runs (no KV binding;
|
|
// OVERRIDES_URL + EDGE_TOKEN). Stubs caches.default alongside fetch.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function fakeCaches(prefilled: Record<string, Response> = {}) {
|
|
const store = new Map<string, Response>(Object.entries(prefilled));
|
|
const cache = {
|
|
match: vi.fn(async (req: Request) => store.get(req.url)),
|
|
put: vi.fn(async (req: Request, res: Response) => void store.set(req.url, res)),
|
|
};
|
|
return { cache, store };
|
|
}
|
|
|
|
const remoteEnv = {
|
|
ORIGIN_HOST: 'origin.example.com',
|
|
OVERRIDES_URL: 'https://cloud.example.com/edge/site-1',
|
|
EDGE_TOKEN: 'tok-1',
|
|
} as unknown as Env;
|
|
|
|
describe('injector resource overrides — remote mode', () => {
|
|
afterEach(() => vi.unstubAllGlobals());
|
|
|
|
it('fetches the /resource endpoint with the bearer and serves the body', async () => {
|
|
const { cache } = fakeCaches();
|
|
vi.stubGlobal('caches', { default: cache });
|
|
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = String(input);
|
|
if (url.includes('/resource?path=')) {
|
|
return new Response(JSON.stringify({ contentType: 'text/markdown; charset=utf-8', body: '# remote' }), { status: 200 });
|
|
}
|
|
return new Response('origin body', { status: 200 });
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const res = await worker.fetch(new Request('https://example.com/llms.txt'), remoteEnv, fakeCtx());
|
|
expect(res.status).toBe(200);
|
|
expect(await res.text()).toBe('# remote');
|
|
expect(res.headers.get('content-type')).toBe('text/markdown; charset=utf-8');
|
|
const resourceCall = fetchMock.mock.calls.find(([i]) => String(i).includes('/resource?path=%2Fllms.txt'));
|
|
expect(resourceCall).toBeTruthy();
|
|
const headers = (resourceCall![1] as RequestInit | undefined)?.headers as Record<string, string> | undefined;
|
|
expect(headers?.authorization).toBe('Bearer tok-1');
|
|
});
|
|
|
|
it('KV binding wins over remote mode when both are configured', async () => {
|
|
const { cache } = fakeCaches();
|
|
vi.stubGlobal('caches', { default: cache });
|
|
const fetchMock = vi.fn(async () => new Response('origin body', { status: 200 }));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
const kv = fakeKv({ 'resource:/llms.txt': JSON.stringify({ contentType: 'text/plain', body: 'kv wins' }) });
|
|
const env = { ...(remoteEnv as object), SEO_OVERRIDES: kv } as unknown as Env;
|
|
|
|
const res = await worker.fetch(new Request('https://example.com/llms.txt'), env, fakeCtx());
|
|
expect(await res.text()).toBe('kv wins');
|
|
// No remote /resource call was made.
|
|
expect(fetchMock.mock.calls.some(([i]) => String(i).includes('/resource'))).toBe(false);
|
|
});
|
|
|
|
it('a 404 from the cloud falls through to origin proxying (no shadowing)', async () => {
|
|
const { cache } = fakeCaches();
|
|
vi.stubGlobal('caches', { default: cache });
|
|
const fetchMock = vi.fn(async (input: RequestInfo | URL) =>
|
|
String(input).includes('/resource?path=')
|
|
? new Response(null, { status: 404 })
|
|
: new Response('origin body', { status: 200 }),
|
|
);
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const res = await worker.fetch(new Request('https://example.com/llms.txt'), remoteEnv, fakeCtx());
|
|
expect(await res.text()).toBe('origin body');
|
|
});
|
|
|
|
it('a 500 from the cloud fails open to origin proxying (tracked plan risk)', async () => {
|
|
const { cache } = fakeCaches();
|
|
vi.stubGlobal('caches', { default: cache });
|
|
const fetchMock = vi.fn(async (input: RequestInfo | URL) =>
|
|
String(input).includes('/resource?path=')
|
|
? new Response('boom', { status: 500 })
|
|
: new Response('origin body', { status: 200 }),
|
|
);
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const res = await worker.fetch(new Request('https://example.com/llms.txt'), remoteEnv, fakeCtx());
|
|
expect(res.status).toBe(200);
|
|
expect(await res.text()).toBe('origin body');
|
|
});
|
|
|
|
it('a cached hit is served without refetching the cloud', async () => {
|
|
const cachedUrl = 'https://cloud.example.com/edge/site-1/resource?path=%2Fllms.txt';
|
|
const { cache } = fakeCaches({
|
|
[cachedUrl]: new Response(JSON.stringify({ contentType: 'text/plain', body: 'cached' }), { status: 200 }),
|
|
});
|
|
vi.stubGlobal('caches', { default: cache });
|
|
const fetchMock = vi.fn(async () => new Response('origin body', { status: 200 }));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const res = await worker.fetch(new Request('https://example.com/llms.txt'), remoteEnv, fakeCtx());
|
|
expect(await res.text()).toBe('cached');
|
|
expect(fetchMock.mock.calls.some(([i]) => String(i).includes('/resource'))).toBe(false);
|
|
});
|
|
});
|