|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | + |
| 4 | +const root = resolve(import.meta.dirname, '..'); |
| 5 | +const contractArg = process.argv.indexOf('--contract-dir'); |
| 6 | +if (contractArg < 0 || !process.argv[contractArg + 1]) { |
| 7 | + throw new Error('--contract-dir is required'); |
| 8 | +} |
| 9 | +const contractDir = resolve(process.argv[contractArg + 1]); |
| 10 | + |
| 11 | +function readJson(path) { |
| 12 | + const value = JSON.parse(readFileSync(path, 'utf8')); |
| 13 | + if (!value || typeof value !== 'object' || Array.isArray(value)) { |
| 14 | + throw new Error(`${path} must contain a JSON object`); |
| 15 | + } |
| 16 | + return value; |
| 17 | +} |
| 18 | + |
| 19 | +function semanticVersion(value) { |
| 20 | + const parts = String(value).split('.'); |
| 21 | + if (parts.length !== 3 || parts.some((part) => !/^\d+$/.test(part))) { |
| 22 | + throw new Error(`invalid semantic version: ${value}`); |
| 23 | + } |
| 24 | + return parts.map(Number); |
| 25 | +} |
| 26 | + |
| 27 | +function compareVersions(left, right) { |
| 28 | + for (let index = 0; index < 3; index += 1) { |
| 29 | + if (left[index] !== right[index]) return left[index] - right[index]; |
| 30 | + } |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +function schemaFields(schema, key) { |
| 35 | + const value = schema[key] ?? (key === 'required' ? [] : undefined); |
| 36 | + if (key === 'required') { |
| 37 | + if (!Array.isArray(value) || value.some((item) => typeof item !== 'string')) { |
| 38 | + throw new Error('schema required must be an array of strings'); |
| 39 | + } |
| 40 | + return new Set(value); |
| 41 | + } |
| 42 | + if (!value || typeof value !== 'object' || Array.isArray(value)) { |
| 43 | + throw new Error('schema properties must be an object'); |
| 44 | + } |
| 45 | + return new Set(Object.keys(value)); |
| 46 | +} |
| 47 | + |
| 48 | +function propertyMaxLength(schema, field) { |
| 49 | + const value = schema.properties?.[field]; |
| 50 | + const maximum = value?.maxLength; |
| 51 | + if (!Number.isInteger(maximum) || maximum < 1) { |
| 52 | + throw new Error(`${field} must declare a positive maxLength`); |
| 53 | + } |
| 54 | + return maximum; |
| 55 | +} |
| 56 | + |
| 57 | +const declaration = readJson(resolve(root, 'compatibility/client-api.json')); |
| 58 | +const manifest = readJson(resolve(contractDir, 'manifest.json')); |
| 59 | +const packageJson = readJson(resolve(root, 'package.json')); |
| 60 | +const vectors = readJson(resolve(contractDir, manifest.vectors ?? 'vectors.json')); |
| 61 | + |
| 62 | +if (manifest.contract !== declaration.contract) { |
| 63 | + throw new Error('contract id does not match the adapter declaration'); |
| 64 | +} |
| 65 | +if (manifest.major !== declaration.contract_major) { |
| 66 | + throw new Error('Client API major version is incompatible'); |
| 67 | +} |
| 68 | +const currentVersion = semanticVersion(manifest.version); |
| 69 | +const testedVersion = semanticVersion(declaration.tested_contract_version); |
| 70 | +if ( |
| 71 | + currentVersion[0] !== testedVersion[0] || |
| 72 | + compareVersions(currentVersion, testedVersion) < 0 |
| 73 | +) { |
| 74 | + throw new Error('current Client API version predates the adapter compatibility floor'); |
| 75 | +} |
| 76 | +if (declaration.adapter_version !== packageJson.version) { |
| 77 | + throw new Error('adapter_version must match package.json'); |
| 78 | +} |
| 79 | + |
| 80 | +const endpoints = new Map(); |
| 81 | +for (const endpoint of manifest.endpoints ?? []) { |
| 82 | + if (!endpoint || typeof endpoint.id !== 'string') { |
| 83 | + throw new Error('every manifest endpoint must have an id'); |
| 84 | + } |
| 85 | + endpoints.set(endpoint.id, endpoint); |
| 86 | +} |
| 87 | +const endpointContracts = declaration.endpoint_contracts; |
| 88 | +if (!endpointContracts || typeof endpointContracts !== 'object' || Array.isArray(endpointContracts)) { |
| 89 | + throw new Error('adapter must declare endpoint_contracts'); |
| 90 | +} |
| 91 | +for (const [endpointId, claim] of Object.entries(endpointContracts)) { |
| 92 | + const endpoint = endpoints.get(endpointId); |
| 93 | + if (!endpoint || !claim || typeof claim !== 'object' || Array.isArray(claim)) { |
| 94 | + throw new Error(`missing required endpoint: ${endpointId}`); |
| 95 | + } |
| 96 | + for (const key of ['method', 'path', 'authentication']) { |
| 97 | + if (claim[key] !== endpoint[key]) { |
| 98 | + throw new Error( |
| 99 | + `${endpointId}: ${key} changed from ${JSON.stringify(claim[key])} ` + |
| 100 | + `to ${JSON.stringify(endpoint[key])}`, |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +for (const [endpointId, claim] of Object.entries(declaration.requests ?? {})) { |
| 107 | + const endpoint = endpoints.get(endpointId); |
| 108 | + const schema = readJson(resolve(contractDir, endpoint.request_schema)); |
| 109 | + const sent = new Set(claim.fields_sent ?? []); |
| 110 | + const required = schemaFields(schema, 'required'); |
| 111 | + const properties = schemaFields(schema, 'properties'); |
| 112 | + const missing = [...required].filter((field) => !sent.has(field)); |
| 113 | + const unknown = [...sent].filter((field) => !properties.has(field)); |
| 114 | + if (missing.length) throw new Error(`${endpointId}: adapter omits ${missing.join(', ')}`); |
| 115 | + if (unknown.length) throw new Error(`${endpointId}: adapter sends unknown ${unknown.join(', ')}`); |
| 116 | +} |
| 117 | + |
| 118 | +const runRequest = readJson( |
| 119 | + resolve(contractDir, endpoints.get('run.submit').request_schema), |
| 120 | +); |
| 121 | +const limits = declaration.limits; |
| 122 | +if (!limits || typeof limits !== 'object' || Array.isArray(limits)) { |
| 123 | + throw new Error('adapter limits must be an object'); |
| 124 | +} |
| 125 | +for (const [limitName, field] of [ |
| 126 | + ['request_id_max_length', 'request_id'], |
| 127 | + ['route_max_length', 'route'], |
| 128 | + ['input_max_length', 'input'], |
| 129 | +]) { |
| 130 | + const adapterLimit = limits[limitName]; |
| 131 | + const contractLimit = propertyMaxLength(runRequest, field); |
| 132 | + if (!Number.isInteger(adapterLimit) || adapterLimit < 1) { |
| 133 | + throw new Error(`${limitName} must be a positive integer`); |
| 134 | + } |
| 135 | + if (adapterLimit > contractLimit) { |
| 136 | + throw new Error( |
| 137 | + `${limitName} allows ${adapterLimit}, above contract maximum ${contractLimit}`, |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +for (const [endpointId, claim] of Object.entries(declaration.responses ?? {})) { |
| 143 | + const endpoint = endpoints.get(endpointId); |
| 144 | + const schema = readJson(resolve(contractDir, endpoint.response_schema)); |
| 145 | + const guaranteed = schemaFields(schema, 'required'); |
| 146 | + const missing = (claim.required_fields ?? []).filter((field) => !guaranteed.has(field)); |
| 147 | + if (missing.length) { |
| 148 | + throw new Error(`${endpointId}: contract no longer guarantees ${missing.join(', ')}`); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +if ( |
| 153 | + JSON.stringify(declaration.known_job_statuses) !== |
| 154 | + JSON.stringify(manifest.job_statuses?.known) |
| 155 | +) { |
| 156 | + throw new Error('adapter known statuses differ from the Client API contract'); |
| 157 | +} |
| 158 | +if ( |
| 159 | + JSON.stringify(declaration.terminal_job_statuses) !== |
| 160 | + JSON.stringify(manifest.job_statuses?.terminal) |
| 161 | +) { |
| 162 | + throw new Error('adapter terminal statuses differ from the Client API contract'); |
| 163 | +} |
| 164 | + |
| 165 | +const handledErrors = new Set(declaration.handled_http_errors ?? []); |
| 166 | +const missingErrors = Object.keys(manifest.http_errors ?? {}) |
| 167 | + .map(Number) |
| 168 | + .filter((status) => !handledErrors.has(status)); |
| 169 | +if (missingErrors.length) { |
| 170 | + throw new Error(`adapter does not classify HTTP errors ${missingErrors.join(', ')}`); |
| 171 | +} |
| 172 | + |
| 173 | +if (!Array.isArray(vectors.cases) || vectors.cases.length === 0) { |
| 174 | + throw new Error('Client API vectors must contain cases'); |
| 175 | +} |
| 176 | +for (const vector of vectors.cases) { |
| 177 | + if (typeof vector.schema !== 'string') throw new Error('vector schema must be a string'); |
| 178 | + readFileSync(resolve(contractDir, vector.schema)); |
| 179 | +} |
| 180 | + |
| 181 | +console.log( |
| 182 | + `PASS: ${declaration.consumer} ${declaration.adapter_version} ` + |
| 183 | + `accepts ${manifest.contract} ${manifest.version}`, |
| 184 | +); |
0 commit comments