|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +// Type-only tests for the AssetInstance discriminated union and the |
| 3 | +// SyncAccountsResponseRow / SyncGovernanceResponseRow row types. |
| 4 | +// |
| 5 | +// The "test" is whether this file compiles. Each `// @ts-expect-error` |
| 6 | +// comment claims the next line WILL fail typechecking. If TypeScript ever |
| 7 | +// stops flagging that line — e.g., because the discriminator was loosened |
| 8 | +// or a required field became optional — the `@ts-expect-error` itself |
| 9 | +// becomes an error and the project's `tsc --noEmit` fails. That's the |
| 10 | +// regression alarm. |
| 11 | +// |
| 12 | +// Pattern: each negative test uses an arrow function returning the value. |
| 13 | +// TS reports the error on the `return` line, so the directive directly |
| 14 | +// above is what catches it. Bare-const-assignment placement is fragile — |
| 15 | +// TS reports object-literal-required-property errors at varying line/col |
| 16 | +// positions depending on the type structure. |
| 17 | +// |
| 18 | +// Run with `npm run typecheck`. |
| 19 | + |
| 20 | +import type { AssetInstance, AssetInstanceType, SyncAccountsResponseRow, SyncGovernanceResponseRow } from './index'; |
| 21 | + |
| 22 | +// ── AssetInstance: discriminator narrowing + exhaustiveness ────────────── |
| 23 | + |
| 24 | +function describeAsset(asset: AssetInstance): string { |
| 25 | + switch (asset.asset_type) { |
| 26 | + case 'image': |
| 27 | + return `${asset.width}x${asset.height} @ ${asset.url}`; |
| 28 | + case 'video': |
| 29 | + return `video ${asset.container_format ?? ''} ${asset.duration_ms ?? 0}ms`; |
| 30 | + case 'audio': |
| 31 | + return `audio ${asset.codec}`; |
| 32 | + case 'text': |
| 33 | + return asset.content; |
| 34 | + case 'html': |
| 35 | + return asset.content; |
| 36 | + case 'url': |
| 37 | + return asset.url; |
| 38 | + case 'css': |
| 39 | + case 'javascript': |
| 40 | + case 'markdown': |
| 41 | + case 'vast': |
| 42 | + case 'daast': |
| 43 | + case 'brief': |
| 44 | + case 'catalog': |
| 45 | + case 'webhook': |
| 46 | + return asset.asset_type; |
| 47 | + default: { |
| 48 | + // Exhaustiveness rail: if a new asset_type lands in AssetInstance |
| 49 | + // without a case above, `asset` here is no longer `never` and this |
| 50 | + // line fails compilation. Stronger than `noImplicitReturns` — |
| 51 | + // survives refactors that move returns out of switch arms. |
| 52 | + const _exhaustive: never = asset; |
| 53 | + return _exhaustive; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// ── AssetInstance: omitting asset_type is rejected ─────────────────────── |
| 59 | + |
| 60 | +function _assetInstance_missingDiscriminator(): AssetInstance { |
| 61 | + // @ts-expect-error — `asset_type` is the discriminator and required. |
| 62 | + return { url: 'https://x.test/img.png', width: 300, height: 250 }; |
| 63 | +} |
| 64 | + |
| 65 | +// ── AssetInstance: image variant requires width and height ─────────────── |
| 66 | + |
| 67 | +function _imageAsset_missingWidth(): AssetInstance { |
| 68 | + // @ts-expect-error — ImageAsset requires `width` (per AdCP 3.0 GA). |
| 69 | + return { asset_type: 'image', url: 'https://x.test/img.png', height: 250 }; |
| 70 | +} |
| 71 | + |
| 72 | +// ── AssetInstance: html instance carries `content`, not `html` ─────────── |
| 73 | + |
| 74 | +function _htmlAsset_wrongFieldName(): AssetInstance { |
| 75 | + // @ts-expect-error — HTMLAsset has `content`, not `html`. Common drift. |
| 76 | + return { asset_type: 'html', html: '<div>...</div>' }; |
| 77 | +} |
| 78 | + |
| 79 | +// ── AssetInstanceType: enumerates every variant's discriminator ────────── |
| 80 | + |
| 81 | +const _all_types: AssetInstanceType[] = [ |
| 82 | + 'image', |
| 83 | + 'video', |
| 84 | + 'audio', |
| 85 | + 'text', |
| 86 | + 'html', |
| 87 | + 'url', |
| 88 | + 'css', |
| 89 | + 'javascript', |
| 90 | + 'markdown', |
| 91 | + 'vast', |
| 92 | + 'daast', |
| 93 | + 'brief', |
| 94 | + 'catalog', |
| 95 | + 'webhook', |
| 96 | +]; |
| 97 | + |
| 98 | +function _assetType_bogusValue(): AssetInstanceType { |
| 99 | + // @ts-expect-error — 'banner' is not a valid asset_type. |
| 100 | + return 'banner'; |
| 101 | +} |
| 102 | + |
| 103 | +// ── SyncAccountsResponseRow: action discriminator is required ──────────── |
| 104 | + |
| 105 | +const _row_ok: SyncAccountsResponseRow = { |
| 106 | + account_id: 'acct_1', |
| 107 | + brand: { domain: 'example.com' }, |
| 108 | + operator: 'agency.example', |
| 109 | + action: 'created', |
| 110 | + status: 'active', |
| 111 | +}; |
| 112 | + |
| 113 | +function _row_missingAction(): SyncAccountsResponseRow { |
| 114 | + // @ts-expect-error — `action` is required on every row. |
| 115 | + return { |
| 116 | + account_id: 'acct_1', |
| 117 | + brand: { domain: 'example.com' }, |
| 118 | + operator: 'agency.example', |
| 119 | + status: 'active', |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +function _row_badAction(): SyncAccountsResponseRow { |
| 124 | + return { |
| 125 | + account_id: 'acct_1', |
| 126 | + brand: { domain: 'example.com' }, |
| 127 | + operator: 'agency.example', |
| 128 | + // @ts-expect-error — 'archived' is not a valid action enum value. |
| 129 | + action: 'archived', |
| 130 | + status: 'active', |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +// ── SyncGovernanceResponseRow: status discriminator is required ────────── |
| 135 | + |
| 136 | +const _gov_row_ok: SyncGovernanceResponseRow = { |
| 137 | + account: { account_id: 'acct_1' }, |
| 138 | + status: 'synced', |
| 139 | +}; |
| 140 | + |
| 141 | +function _gov_row_missingStatus(): SyncGovernanceResponseRow { |
| 142 | + // @ts-expect-error — `status` is required. |
| 143 | + return { account: { account_id: 'acct_1' } }; |
| 144 | +} |
| 145 | + |
| 146 | +// Reference all symbols once to keep the file's intent visible to readers |
| 147 | +// even though they're never executed. The file-level eslint-disable above |
| 148 | +// is what actually silences the unused-vars lint. |
| 149 | +export const _references = [ |
| 150 | + describeAsset, |
| 151 | + _assetInstance_missingDiscriminator, |
| 152 | + _imageAsset_missingWidth, |
| 153 | + _htmlAsset_wrongFieldName, |
| 154 | + _all_types, |
| 155 | + _assetType_bogusValue, |
| 156 | + _row_ok, |
| 157 | + _row_missingAction, |
| 158 | + _row_badAction, |
| 159 | + _gov_row_ok, |
| 160 | + _gov_row_missingStatus, |
| 161 | +] as const; |
0 commit comments