|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | + |
| 3 | +const BASE = process.env.BASE_URL || 'http://localhost:3000'; |
| 4 | +let failed = 0; |
| 5 | + |
| 6 | +console.log(`Smoke tests against ${BASE}\n`); |
| 7 | + |
| 8 | +// index returns 200 or 307 |
| 9 | +{ |
| 10 | + const res = await fetch(`${BASE}/`); |
| 11 | + assert.ok(res.status === 200 || res.status === 307, `index: expected 200 or 307, got ${res.status}`); |
| 12 | + console.log(' ✓ index returns 200 or 307'); |
| 13 | +} |
| 14 | + |
| 15 | +// page API returns frontmatter |
| 16 | +{ |
| 17 | + const res = await fetch(`${BASE}/api/page?slug=docs,getting-started`); |
| 18 | + assert.equal(res.status, 200, `page API: expected 200, got ${res.status}`); |
| 19 | + const data = await res.json(); |
| 20 | + assert.ok(data.frontmatter, 'page API: missing frontmatter'); |
| 21 | + assert.ok(data.frontmatter.title, 'page API: missing title'); |
| 22 | + console.log(' ✓ page API returns frontmatter'); |
| 23 | +} |
| 24 | + |
| 25 | +// search API returns results |
| 26 | +{ |
| 27 | + const res = await fetch(`${BASE}/api/search`); |
| 28 | + assert.equal(res.status, 200, `search API: expected 200, got ${res.status}`); |
| 29 | + const data = await res.json(); |
| 30 | + assert.ok(Array.isArray(data), 'search API: expected array'); |
| 31 | + assert.ok(data.length > 0, 'search API: expected results'); |
| 32 | + assert.ok(data[0].url, 'search API: missing url'); |
| 33 | + console.log(' ✓ search API returns results'); |
| 34 | +} |
| 35 | + |
| 36 | +// search with query returns matches |
| 37 | +{ |
| 38 | + const res = await fetch(`${BASE}/api/search?query=getting`); |
| 39 | + assert.equal(res.status, 200, `search query: expected 200, got ${res.status}`); |
| 40 | + const data = await res.json(); |
| 41 | + assert.ok(data.length > 0, 'search query: expected results'); |
| 42 | + assert.ok(data[0].match, 'search query: missing match field'); |
| 43 | + console.log(' ✓ search with query returns matches'); |
| 44 | +} |
| 45 | + |
| 46 | +// image API resizes PNG |
| 47 | +{ |
| 48 | + const res = await fetch(`${BASE}/api/image?url=${encodeURIComponent('/_content/docs/test-image.png')}&w=320`); |
| 49 | + assert.equal(res.status, 200, `image API: expected 200, got ${res.status}`); |
| 50 | + const ct = res.headers.get('content-type'); |
| 51 | + assert.ok(ct.startsWith('image/'), `image API: expected image content-type, got ${ct}`); |
| 52 | + console.log(' ✓ image API resizes PNG'); |
| 53 | +} |
| 54 | + |
| 55 | +// image API returns 400 for missing params |
| 56 | +{ |
| 57 | + const res = await fetch(`${BASE}/api/image`); |
| 58 | + assert.equal(res.status, 400, `image 400: expected 400, got ${res.status}`); |
| 59 | + console.log(' ✓ image API returns 400 for missing params'); |
| 60 | +} |
| 61 | + |
| 62 | +// image API returns 400 for invalid width |
| 63 | +{ |
| 64 | + const res = await fetch(`${BASE}/api/image?url=${encodeURIComponent('/_content/docs/test-image.png')}&w=999`); |
| 65 | + assert.equal(res.status, 400, `image invalid width: expected 400, got ${res.status}`); |
| 66 | + console.log(' ✓ image API returns 400 for invalid width'); |
| 67 | +} |
| 68 | + |
| 69 | +// image API returns 404 for missing image |
| 70 | +{ |
| 71 | + const res = await fetch(`${BASE}/api/image?url=${encodeURIComponent('/_content/does-not-exist.png')}&w=640`); |
| 72 | + assert.equal(res.status, 404, `image 404: expected 404, got ${res.status}`); |
| 73 | + console.log(' ✓ image API returns 404 for missing image'); |
| 74 | +} |
| 75 | + |
| 76 | +console.log('\nALL PASSED'); |
0 commit comments