|
| 1 | +// Local validation of the headless-api Kody variant (spec §3.1) against a real |
| 2 | +// Postgres via createPostgresStorage + the pg driver — the prod storage path, |
| 3 | +// minus the workerd/U2 question which can't be tested off-platform. |
| 4 | +// |
| 5 | +// Proves: entry.ts `{ fetch }` handler + Postgres storage substitution (D2) + |
| 6 | +// header-role auth (D5) + governance staging/approval + the two jobs (D6). |
| 7 | +// |
| 8 | +// Run from this package dir: CMS_DATABASE_URL=… tsx scripts/local-smoke.mts |
| 9 | +import entry from '../src/entry.ts' |
| 10 | +import { outboxDrain, schedulerTick } from '../src/jobs.ts' |
| 11 | +import { |
| 12 | + approve, |
| 13 | + closeStore, |
| 14 | + getAs, |
| 15 | + listPending, |
| 16 | + systemContext, |
| 17 | +} from '@tanstack/cms-core/server' |
| 18 | + |
| 19 | +const ops = 'http://localhost/api/cms/v1/ops' |
| 20 | +let failures = 0 |
| 21 | +function check(label: string, ok: boolean, detail?: unknown) { |
| 22 | + console.log(`${ok ? '✅' : '❌'} ${label}${detail !== undefined && !ok ? ` — ${JSON.stringify(detail)}` : ''}`) |
| 23 | + if (!ok) failures++ |
| 24 | +} |
| 25 | + |
| 26 | +async function post(op: string, role: string, body: unknown) { |
| 27 | + return entry.fetch( |
| 28 | + new Request(`${ops}/${op}`, { |
| 29 | + method: 'POST', |
| 30 | + headers: { 'content-type': 'application/json', 'x-cms-role': role }, |
| 31 | + body: JSON.stringify(body), |
| 32 | + }), |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +// 1. Editor stages a write over REST (header-role auth) through the fetch entry. |
| 37 | +const writeRes = await post('save_document', 'editor', { |
| 38 | + collection: 'contentItem', |
| 39 | + id: 'item-smoke', |
| 40 | + doc: { title: 'Smoke Item', channel: 'ios', priority: 2 }, |
| 41 | + create: true, |
| 42 | +}) |
| 43 | +check('editor save_document accepted (2xx)', writeRes.ok, writeRes.status) |
| 44 | + |
| 45 | +// 2. Viewer is denied the same write. |
| 46 | +const viewerRes = await post('save_document', 'viewer', { |
| 47 | + collection: 'contentItem', |
| 48 | + id: 'item-denied', |
| 49 | + doc: { title: 'Denied' }, |
| 50 | + create: true, |
| 51 | +}) |
| 52 | +check('viewer write denied (403)', viewerRes.status === 403, viewerRes.status) |
| 53 | + |
| 54 | +// 3. Governance: the editor write is staged, invisible before approval. |
| 55 | +const pending = await listPending() |
| 56 | +check('write is staged in governance (>=1 pending)', pending.length >= 1, pending.length) |
| 57 | +const beforeApprove = |
| 58 | + (await getAs(systemContext('smoke'), { |
| 59 | + collection: 'contentItem', |
| 60 | + id: 'item-smoke', |
| 61 | + staged: false, |
| 62 | + populate: false, |
| 63 | + }))?.doc ?? null |
| 64 | +check('staged doc invisible before approval', beforeApprove === null) |
| 65 | + |
| 66 | +// 4. Approve, then the doc is readable from Postgres. |
| 67 | +for (const p of pending) await approve(p.id) |
| 68 | +const committed = |
| 69 | + (await getAs(systemContext('smoke'), { |
| 70 | + collection: 'contentItem', |
| 71 | + id: 'item-smoke', |
| 72 | + staged: false, |
| 73 | + populate: false, |
| 74 | + }))?.doc ?? null |
| 75 | +check('doc readable after approval', committed?.title === 'Smoke Item', committed) |
| 76 | + |
| 77 | +// 5. The two Kody jobs run without throwing (they resolve storage internally). |
| 78 | +try { |
| 79 | + await outboxDrain() |
| 80 | + await schedulerTick() |
| 81 | + check('cms-outbox-drain + cms-scheduler-tick ran', true) |
| 82 | +} catch (err) { |
| 83 | + check('cms-outbox-drain + cms-scheduler-tick ran', false, String(err)) |
| 84 | +} |
| 85 | + |
| 86 | +await closeStore() |
| 87 | +console.log(failures === 0 ? '\nSMOKE PASS' : `\nSMOKE FAIL (${failures})`) |
| 88 | +process.exit(failures === 0 ? 0 : 1) |
0 commit comments