|
| 1 | +const INPUT = 'clients.json'; |
| 2 | +const BAD_FIELDS = [ |
| 3 | + ['name', 42], |
| 4 | + ['month', 8], |
| 5 | + ['topPost', null], |
| 6 | + ['brandColor', { toString: null }] |
| 7 | +]; |
| 8 | + |
| 9 | +import assert from 'node:assert/strict'; |
| 10 | +import { spawnSync } from 'node:child_process'; |
| 11 | +import { |
| 12 | + copyFileSync, |
| 13 | + mkdtempSync, |
| 14 | + readFileSync, |
| 15 | + readdirSync, |
| 16 | + rmSync, |
| 17 | + writeFileSync |
| 18 | +} from 'node:fs'; |
| 19 | +import { tmpdir } from 'node:os'; |
| 20 | +import { join } from 'node:path'; |
| 21 | + |
| 22 | +// Only fetch is replaced; each check runs the actual CLI and file handling. |
| 23 | +const source = new URL('.', import.meta.url); |
| 24 | +const directory = mkdtempSync(join(tmpdir(), 'cookbook-check-')); |
| 25 | +const log = join(directory, 'requests.log'); |
| 26 | +let passed = 0; |
| 27 | + |
| 28 | +try { |
| 29 | + for (const file of readdirSync(source).filter(name => |
| 30 | + /\.(mjs|json)$/.test(name) |
| 31 | + )) { |
| 32 | + copyFileSync(new URL(file, source), join(directory, file)); |
| 33 | + } |
| 34 | + const sample = JSON.parse(readFileSync(join(directory, INPUT), 'utf8')); |
| 35 | + const loader = join(directory, 'fetch.mjs'); |
| 36 | + writeFileSync( |
| 37 | + loader, |
| 38 | + ` |
| 39 | +import { appendFileSync } from 'node:fs'; |
| 40 | +globalThis.fetch = async () => { |
| 41 | + appendFileSync(process.env.REQUEST_LOG, 'request\\n'); |
| 42 | + if (process.env.CHECK_CASE === 'network') throw new TypeError('fetch failed'); |
| 43 | + if (process.env.CHECK_CASE.startsWith('body')) { |
| 44 | + return new Response(new ReadableStream({ |
| 45 | + start(controller) { controller.error(new TypeError('terminated')); } |
| 46 | + }), { status: process.env.CHECK_CASE === 'body-error' ? 500 : 200 }); |
| 47 | + } |
| 48 | + if (process.env.CHECK_CASE === 'auth') return Response.json({}, { status: 403 }); |
| 49 | + if (process.env.CHECK_CASE === 'invalid-response') return new Response('not json'); |
| 50 | + return Response.json({ response: { id: 'test-render', status: 'done', url: 'https://example.com/video.mp4' } }); |
| 51 | +}; |
| 52 | +` |
| 53 | + ); |
| 54 | + |
| 55 | + function run(mode, data = sample, script = 'render.mjs') { |
| 56 | + writeFileSync(join(directory, INPUT), JSON.stringify(data)); |
| 57 | + writeFileSync(log, ''); |
| 58 | + const result = spawnSync( |
| 59 | + process.execPath, |
| 60 | + ['--import', loader, join(directory, script)], |
| 61 | + { |
| 62 | + encoding: 'utf8', |
| 63 | + env: { |
| 64 | + PATH: process.env.PATH, |
| 65 | + SHOTSTACK_API_KEY: mode === 'missing' ? '' : 'test-key', |
| 66 | + SHOTSTACK_ENV: mode === 'environment' ? 'invalid' : 'stage', |
| 67 | + CHECK_CASE: mode, |
| 68 | + REQUEST_LOG: log |
| 69 | + } |
| 70 | + } |
| 71 | + ); |
| 72 | + const requests = readFileSync(log, 'utf8') |
| 73 | + .trim() |
| 74 | + .split('\n') |
| 75 | + .filter(Boolean).length; |
| 76 | + return { ...result, requests }; |
| 77 | + } |
| 78 | + |
| 79 | + for (const mode of [ |
| 80 | + 'missing', |
| 81 | + 'environment', |
| 82 | + 'auth', |
| 83 | + 'network', |
| 84 | + 'body', |
| 85 | + 'body-error', |
| 86 | + 'invalid-response' |
| 87 | + ]) { |
| 88 | + const result = run(mode); |
| 89 | + assert.equal(result.status, 1, mode); |
| 90 | + assert.equal(result.stderr.trim().split('\n').length, 1, mode); |
| 91 | + assert.match(result.stderr, /Set |Check |must be|Try again/i, mode); |
| 92 | + assert.equal( |
| 93 | + result.requests, |
| 94 | + ['missing', 'environment'].includes(mode) ? 0 : 1, |
| 95 | + mode |
| 96 | + ); |
| 97 | + passed++; |
| 98 | + } |
| 99 | + |
| 100 | + const invalid = [null, ...(Array.isArray(sample) ? [[null]] : [])]; |
| 101 | + for (const [field, value] of BAD_FIELDS) { |
| 102 | + const data = structuredClone(sample); |
| 103 | + (Array.isArray(data) ? data[0] : data)[field] = value; |
| 104 | + invalid.push(data); |
| 105 | + } |
| 106 | + for (const data of invalid) { |
| 107 | + const result = run('valid', data); |
| 108 | + assert.equal(result.status, 1); |
| 109 | + assert.equal( |
| 110 | + result.requests, |
| 111 | + 0, |
| 112 | + 'Invalid input must fail before submission' |
| 113 | + ); |
| 114 | + assert.doesNotMatch(result.stderr, /TypeError|at file:|at ModuleJob/); |
| 115 | + passed++; |
| 116 | + } |
| 117 | + |
| 118 | + const result = run('valid'); |
| 119 | + assert.equal(result.status, 0, result.stderr); |
| 120 | + const count = Array.isArray(sample) ? sample.length : 1; |
| 121 | + assert.equal(result.requests, count); |
| 122 | + assert.equal( |
| 123 | + readFileSync(join(directory, 'renders.jsonl'), 'utf8').trim().split('\n') |
| 124 | + .length, |
| 125 | + count |
| 126 | + ); |
| 127 | + passed++; |
| 128 | + |
| 129 | + writeFileSync(join(directory, 'renders.jsonl'), 'null\n'); |
| 130 | + const status = run('valid', sample, 'status.mjs'); |
| 131 | + assert.equal(status.status, 1); |
| 132 | + assert.equal( |
| 133 | + status.requests, |
| 134 | + 0, |
| 135 | + 'Invalid tracking rows must fail before requests' |
| 136 | + ); |
| 137 | + assert.doesNotMatch(status.stderr, /TypeError|at file:|at ModuleJob/); |
| 138 | + assert.match(status.stderr, /renders\.jsonl.*(Fix|Check)/); |
| 139 | + passed++; |
| 140 | + |
| 141 | + console.log(passed + ' checks passed'); |
| 142 | +} finally { |
| 143 | + rmSync(directory, { recursive: true, force: true }); |
| 144 | +} |
0 commit comments