|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#5674] No `getDiscovery` test double in this package may spell `endpoints`. |
| 5 | + * |
| 6 | + * ## What was wrong |
| 7 | + * |
| 8 | + * 25 test files in `packages/rest/src` mocked the protocol like this: |
| 9 | + * |
| 10 | + * ```ts |
| 11 | + * getDiscovery: vi.fn().mockResolvedValue({ |
| 12 | + * version: 'v0', |
| 13 | + * endpoints: { data: '', metadata: '', ui: '', auth: '/auth' }, |
| 14 | + * }) |
| 15 | + * ``` |
| 16 | + * |
| 17 | + * The real producer (`ObjectStackProtocolImplementation.getDiscovery()` in |
| 18 | + * `packages/metadata-protocol/src/protocol.ts`) emits `routes` — declared as |
| 19 | + * `ApiRoutesSchema` and REQUIRED by `DiscoverySchema` — and has never emitted |
| 20 | + * `endpoints`. That key existed only on the dispatcher path, as a verbatim |
| 21 | + * copy of `routes`, and #4828 removed it under ADR-0049. The producer side is |
| 22 | + * already pinned next door: `discovery-schema-conformance.test.ts` asserts the |
| 23 | + * live `/discovery` body has no `endpoints` property. |
| 24 | + * |
| 25 | + * Those doubles were inert — `rest-server`'s discovery handler reads |
| 26 | + * `discovery.routes`, so `if (discovery.routes)` was simply false and the |
| 27 | + * route-augmentation block was skipped — which is exactly why they survived |
| 28 | + * the retirement: nothing they asserted depended on the key at all. |
| 29 | + * |
| 30 | + * ## Why a pin, and why THIS pin |
| 31 | + * |
| 32 | + * The harm is authoring-time, not runtime: these were the last places in the |
| 33 | + * repo still spelling a retired key, and a test double is the most-copied |
| 34 | + * artifact there is. Copy one and the key is back in the fixture layer, still |
| 35 | + * inert, still teaching a producer shape that never existed. A conformance |
| 36 | + * test over the live producer cannot see that — no producer is involved. |
| 37 | + * |
| 38 | + * Scope, stated honestly: |
| 39 | + * |
| 40 | + * - It scans the `mockResolvedValue({ … })` form only, which is the form all |
| 41 | + * 26 doubles in this package use and therefore the form that gets copied. A |
| 42 | + * double written some other way (`vi.fn(async () => ({ … }))`) is not read. |
| 43 | + * - It asserts only the NEGATIVE — no `endpoints` key. It deliberately does |
| 44 | + * not require `routes`, because `rest-server` guards with |
| 45 | + * `if (discovery.routes)` and a future test is entitled to drive that guard's |
| 46 | + * falsy branch with a double that omits it. |
| 47 | + * - The floor below is anti-vacuity, in the sense of #4642: a scanner that |
| 48 | + * silently stops matching would otherwise pass by finding nothing. |
| 49 | + */ |
| 50 | + |
| 51 | +import { describe, it, expect } from 'vitest'; |
| 52 | +import { readdirSync, readFileSync } from 'node:fs'; |
| 53 | +import { join } from 'node:path'; |
| 54 | +import { fileURLToPath } from 'node:url'; |
| 55 | + |
| 56 | +const SRC_DIR = fileURLToPath(new URL('.', import.meta.url)); |
| 57 | + |
| 58 | +/** |
| 59 | + * This file is excluded from its own scan, EXPLICITLY. |
| 60 | + * |
| 61 | + * The docblock above quotes the defective double verbatim — that is the point |
| 62 | + * of it — so the scanner does match a "double" here. It happens to escape the |
| 63 | + * key check only because a docblock line prefixes the key with `* `, which the |
| 64 | + * `[{,]\s*` lead-in does not accept. Depending on that is depending on comment |
| 65 | + * formatting: re-wrap the docblock and this pin fails on its own prose. Name |
| 66 | + * the exclusion instead of inheriting the luck. |
| 67 | + */ |
| 68 | +const SELF = 'discovery-double-retired-key.test.ts'; |
| 69 | + |
| 70 | +/** Matches an `endpoints` key at the start of a line or after `{` / `,`. */ |
| 71 | +const ENDPOINTS_KEY = /(^|[{,])\s*endpoints\s*:/; |
| 72 | + |
| 73 | +interface Double { |
| 74 | + file: string; |
| 75 | + /** The source text of the object the double resolves to, braces included. */ |
| 76 | + literal: string; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Capture the balanced `{ … }` that follows `getDiscovery: …mockResolvedValue(`. |
| 81 | + * |
| 82 | + * Brace counting is enough here: the doubles are plain data literals, and a |
| 83 | + * `{` inside a string would only ever make this capture MORE text, i.e. fail |
| 84 | + * loud rather than pass quiet. |
| 85 | + */ |
| 86 | +function collectDoubles(file: string, source: string): Double[] { |
| 87 | + const found: Double[] = []; |
| 88 | + const opener = /getDiscovery\s*:[\s\S]{0,80}?mockResolvedValue\(\s*\{/g; |
| 89 | + let m: RegExpExecArray | null; |
| 90 | + while ((m = opener.exec(source)) !== null) { |
| 91 | + const start = m.index + m[0].length - 1; // index of the `{` |
| 92 | + let depth = 0; |
| 93 | + let end = -1; |
| 94 | + for (let i = start; i < source.length; i++) { |
| 95 | + const ch = source[i]; |
| 96 | + if (ch === '{') depth++; |
| 97 | + else if (ch === '}') { |
| 98 | + depth--; |
| 99 | + if (depth === 0) { end = i; break; } |
| 100 | + } |
| 101 | + } |
| 102 | + if (end === -1) throw new Error(`${file}: unbalanced getDiscovery double literal`); |
| 103 | + found.push({ file, literal: source.slice(start, end + 1) }); |
| 104 | + } |
| 105 | + return found; |
| 106 | +} |
| 107 | + |
| 108 | +const DOUBLES: Double[] = readdirSync(SRC_DIR) |
| 109 | + .filter((f) => f.endsWith('.test.ts') && f !== SELF) |
| 110 | + .flatMap((f) => collectDoubles(f, readFileSync(join(SRC_DIR, f), 'utf8'))); |
| 111 | + |
| 112 | +describe('[#5674] getDiscovery doubles carry the producer key, not the retired one', () => { |
| 113 | + it('finds the doubles it claims to police (anti-vacuity)', () => { |
| 114 | + // 27 doubles across 26 files when this pin was written (the 26 #5674 |
| 115 | + // corrected, plus `rest-route-ledger.conformance.test.ts`, which |
| 116 | + // resolves to a bare `{}` and never spelled the retired key). The floor |
| 117 | + // is deliberately slack — it exists so a scanner that matches NOTHING |
| 118 | + // fails instead of reporting a clean sweep of an empty set. |
| 119 | + expect( |
| 120 | + DOUBLES.length, |
| 121 | + 'the scanner found (almost) no getDiscovery doubles — it has drifted from how this package writes them, so its verdict below means nothing', |
| 122 | + ).toBeGreaterThanOrEqual(20); |
| 123 | + }); |
| 124 | + |
| 125 | + it('no double resolves to an object carrying `endpoints` (retired in #4828)', () => { |
| 126 | + const offenders = DOUBLES |
| 127 | + .filter((d) => ENDPOINTS_KEY.test(d.literal)) |
| 128 | + .map((d) => d.file); |
| 129 | + |
| 130 | + expect( |
| 131 | + Array.from(new Set(offenders)), |
| 132 | + 'the discovery producer emits `routes` (ApiRoutesSchema) and never emitted `endpoints`; ' |
| 133 | + + '`endpoints` was the dispatcher-only copy retired by #4828 (ADR-0049). A double that spells ' |
| 134 | + + 'it teaches a shape no producer ever had, and is the seed the next copy-paste grows from (#5674).', |
| 135 | + ).toEqual([]); |
| 136 | + }); |
| 137 | +}); |
0 commit comments