|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import test from 'node:test' |
| 3 | +import { spawnSync } from 'node:child_process' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { checkSitemapPage, getSitemapUrls } from '../scripts/check-sitemap' |
| 6 | + |
| 7 | +const url = 'https://tanstack.com/charts/catalog' |
| 8 | +const canonical = `<link rel="canonical" href="${url}">` |
| 9 | +function check(body = canonical, status = 200, headers = new Headers()) { |
| 10 | + return checkSitemapPage(url, { body, status, headers }) |
| 11 | +} |
| 12 | + |
| 13 | +test('sitemap parser decodes XML entities and ignores unrelated links', () => { |
| 14 | + assert.deepEqual( |
| 15 | + getSitemapUrls( |
| 16 | + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://tanstack.com/partners?a=1&b=2</loc></url></urlset>', |
| 17 | + ), |
| 18 | + ['https://tanstack.com/partners?a=1&b=2'], |
| 19 | + ) |
| 20 | + assert.deepEqual(getSitemapUrls('<html><a href="/">Home</a></html>'), []) |
| 21 | +}) |
| 22 | + |
| 23 | +test('contract accepts a healthy self-canonical page and rejects status/canonical failures', () => { |
| 24 | + assert.deepEqual(check(), []) |
| 25 | + assert.ok( |
| 26 | + check(`<html><head></head><body>${canonical}</body></html>`).includes( |
| 27 | + 'expected one canonical link, found 0', |
| 28 | + ), |
| 29 | + ) |
| 30 | + assert.ok(check(canonical, 307).includes('HTTP 307')) |
| 31 | + assert.ok(check(canonical, 404).includes('HTTP 404')) |
| 32 | + assert.ok(check('').includes('expected one canonical link, found 0')) |
| 33 | + assert.ok( |
| 34 | + check(canonical + canonical).includes( |
| 35 | + 'expected one canonical link, found 2', |
| 36 | + ), |
| 37 | + ) |
| 38 | + assert.ok( |
| 39 | + check('<link rel="canonical" href="/elsewhere">').includes( |
| 40 | + 'canonical does not match sitemap URL', |
| 41 | + ), |
| 42 | + ) |
| 43 | + assert.ok( |
| 44 | + check('<link rel="canonical" href="http://[">').includes( |
| 45 | + 'invalid canonical URL', |
| 46 | + ), |
| 47 | + ) |
| 48 | +}) |
| 49 | + |
| 50 | +test('contract rejects robots directives, placeholders and forbidden paths', () => { |
| 51 | + assert.ok( |
| 52 | + check( |
| 53 | + canonical + '<meta name="ROBOTS" content="noindex, follow">', |
| 54 | + ).includes('page is noindex'), |
| 55 | + ) |
| 56 | + assert.ok( |
| 57 | + check( |
| 58 | + canonical, |
| 59 | + 200, |
| 60 | + new Headers({ 'X-Robots-Tag': 'googlebot: none' }), |
| 61 | + ).includes('page is noindex'), |
| 62 | + ) |
| 63 | + assert.ok( |
| 64 | + check(canonical + '<h1>Content temporarily unavailable</h1>').includes( |
| 65 | + 'error or temporary-unavailable heading', |
| 66 | + ), |
| 67 | + ) |
| 68 | + for (const path of [ |
| 69 | + '/router/latest/docs/how-to/drafts/test', |
| 70 | + '/db/latest/docs/README', |
| 71 | + '/db/latest/docs/README.md', |
| 72 | + '/router/latest/docs/test.draft', |
| 73 | + '/admin/users', |
| 74 | + ]) { |
| 75 | + const expected = `https://tanstack.com${path}` |
| 76 | + assert.ok( |
| 77 | + checkSitemapPage(expected, { |
| 78 | + body: `<link rel="canonical" href="${expected}">`, |
| 79 | + status: 200, |
| 80 | + headers: new Headers(), |
| 81 | + }).includes('disallowed sitemap path'), |
| 82 | + ) |
| 83 | + } |
| 84 | +}) |
| 85 | + |
| 86 | +test('image-preview restrictions do not prevent page indexing', () => { |
| 87 | + for (const directive of [ |
| 88 | + 'max-image-preview:none', |
| 89 | + 'index, MAX-IMAGE-PREVIEW: none', |
| 90 | + 'googlebot: max-image-preview: none', |
| 91 | + ]) { |
| 92 | + assert.deepEqual( |
| 93 | + check(canonical + `<meta name="robots" content="${directive}">`), |
| 94 | + [], |
| 95 | + ) |
| 96 | + assert.deepEqual( |
| 97 | + check(canonical, 200, new Headers({ 'X-Robots-Tag': directive })), |
| 98 | + [], |
| 99 | + ) |
| 100 | + assert.ok( |
| 101 | + check( |
| 102 | + canonical, |
| 103 | + 200, |
| 104 | + new Headers({ 'X-Robots-Tag': `${directive}, noindex` }), |
| 105 | + ).includes('page is noindex'), |
| 106 | + ) |
| 107 | + assert.ok( |
| 108 | + check( |
| 109 | + canonical, |
| 110 | + 200, |
| 111 | + new Headers({ 'X-Robots-Tag': `${directive}, none` }), |
| 112 | + ).includes('page is noindex'), |
| 113 | + ) |
| 114 | + } |
| 115 | +}) |
| 116 | + |
| 117 | +test('CLI rejects an explicitly empty preview origin before fetching', () => { |
| 118 | + const result = spawnSync( |
| 119 | + process.execPath, |
| 120 | + [ |
| 121 | + '--import', |
| 122 | + 'tsx', |
| 123 | + fileURLToPath(new URL('../scripts/check-sitemap.ts', import.meta.url)), |
| 124 | + '--sitemap', |
| 125 | + 'http://127.0.0.1:1/sitemap.xml', |
| 126 | + '--base-url', |
| 127 | + '', |
| 128 | + ], |
| 129 | + { encoding: 'utf8' }, |
| 130 | + ) |
| 131 | + assert.notEqual(result.status, 0) |
| 132 | + assert.match(result.stderr, /ERR_INVALID_URL/) |
| 133 | +}) |
0 commit comments