|
3 | 3 |
|
4 | 4 | import { test } from 'node:test'; |
5 | 5 | import assert from 'node:assert/strict'; |
| 6 | +import { readFile } from 'node:fs/promises'; |
6 | 7 | import { CUSTOMER_API_ROUTES } from '@searchcode/core'; |
7 | 8 | import { COMMANDS } from '../src/commands.js'; |
8 | 9 |
|
@@ -56,3 +57,32 @@ test('every parameter carries a description', () => { |
56 | 57 | } |
57 | 58 | } |
58 | 59 | }); |
| 60 | + |
| 61 | +// The renderer picks the row array by name, and the API names it after what it returns: |
| 62 | +// "sites" from a technology query, "domains" from the domain index, "results" from a search. |
| 63 | +// These payloads are the real gateway shapes. If the renderer stops recognising one, output |
| 64 | +// silently degrades to raw JSON — which is exactly the regression these pin down. The live |
| 65 | +// smoke test (scripts/smoke-public-clients.mjs) checks the same thing against production. |
| 66 | +const GATEWAY_SHAPES = [ |
| 67 | + ['technology query', { technology: 'React', total_sites: 1234567, sites: [{ domain: 'a.com', rank: 1 }], offset: 0 }], |
| 68 | + ['domain index', { total: 42, total_is_exact: true, domains: [{ domain: 'b.com' }], limit: 10 }], |
| 69 | + ['facet count', { kind: 'tech', signal: 'React', sites: 1234567 }], |
| 70 | + ['search results', { results: [{ domain: 'c.com', blob_hash: 'x' }], next_cursor: null }], |
| 71 | +]; |
| 72 | + |
| 73 | +for (const [name, payload] of GATEWAY_SHAPES) { |
| 74 | + test(`the renderer reads the ${name} response shape`, async () => { |
| 75 | + const source = await readFile(new URL('../src/index.js', import.meta.url), 'utf8'); |
| 76 | + const match = source.match(/const rows = ([^;]+);/); |
| 77 | + assert.ok(match, 'the renderer no longer looks up rows the way this test expects'); |
| 78 | + const known = [...match[1].matchAll(/payload\.([a-z_]+)/g)].map((m) => m[1]); |
| 79 | + const arrayKey = Object.keys(payload).find((k) => Array.isArray(payload[k])); |
| 80 | + if (arrayKey) { |
| 81 | + assert.ok( |
| 82 | + known.includes(arrayKey), |
| 83 | + `the renderer would print raw JSON for a ${name}: it returns "${arrayKey}", ` + |
| 84 | + `and the renderer only knows ${known.join(', ')}`, |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | +} |
0 commit comments