|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * `_test:conformance-presence` — every first-party Flowthru extension that implements a |
| 4 | + * Core extension surface (`IStorageAdapter<T>`, `IFormatSerializer<TRow>`, `IStorageMedium`, |
| 5 | + * `IStorageMediumProvider`, `IMetadataProvider`) has at least one corresponding `*Conformance` |
| 6 | + * subclass in its sibling `tests/extensions/<Ext>.Tests/` project. |
| 7 | + * |
| 8 | + * The check is at the *extension level*, not per implementor: an extension passes if any |
| 9 | + * conformance subclass for the relevant surface kind exists, regardless of which specific |
| 10 | + * implementor it covers. This admits patterns like Gql, where `GqlQueryStorageAdapter` |
| 11 | + * doesn't fit the kit's contract (read-only deferred handle) but its sibling |
| 12 | + * `GqlStorageAdapter` does and has a conformance subclass. The extension-level |
| 13 | + * aggregation also lets multi-shape conformance (e.g., EFCore's flat + nested entities) |
| 14 | + * cover multiple implementors with one parameterized base. |
| 15 | + * |
| 16 | + * Container-adapter conformance (`IContainerAdapter<TContainer, TRow>`) is intentionally |
| 17 | + * excluded: the kit does not yet codify a `ContainerAdapterConformance` base, and the only |
| 18 | + * first-party container adapter outside Core (MLNet's `DataViewContainerAdapter`) was |
| 19 | + * descoped from the conformance initiative because ONNX is structurally different from |
| 20 | + * the row-oriented surfaces the kit targets. Restore it to the SURFACES list when |
| 21 | + * container conformance is added. |
| 22 | + * |
| 23 | + * Exits with non-zero on any failure. Extracted from the previous monolithic |
| 24 | + * `verify-test-coverage.mjs` Pass 2. |
| 25 | + * |
| 26 | + * Usage: |
| 27 | + * node scripts/_test/conformance-presence.mjs |
| 28 | + */ |
| 29 | + |
| 30 | +import { readFileSync, existsSync, readdirSync } from 'node:fs'; |
| 31 | +import { join } from 'node:path'; |
| 32 | +import { findCs, rel, SRC_DIR, TESTS_DIR } from './_lib.mjs'; |
| 33 | + |
| 34 | +// ── Surfaces in scope for conformance enforcement ──────────────────────────── |
| 35 | +// |
| 36 | +// Each surface maps from the Core interface name (matched on the impl side) to the |
| 37 | +// kit base class name (matched on the test side). |
| 38 | +// |
| 39 | +const SURFACES = [ |
| 40 | + { iface: 'IStorageAdapter', base: 'StorageAdapterConformance' }, |
| 41 | + { iface: 'IFormatSerializer', base: 'FormatSerializerConformance' }, |
| 42 | + { iface: 'IStorageMedium', base: 'StorageMediumConformance' }, |
| 43 | + { iface: 'IStorageMediumProvider', base: 'StorageMediumProviderConformance' }, |
| 44 | + { iface: 'IMetadataProvider', base: 'MetadataProviderConformance' }, |
| 45 | +]; |
| 46 | + |
| 47 | +/** |
| 48 | + * Returns the set of surface keys (e.g., 'IStorageAdapter') the file declares as base |
| 49 | + * types or implemented interfaces. Matches `: IStorageAdapter<` and similar across multi- |
| 50 | + * line declarations by collapsing whitespace before regex matching. |
| 51 | + */ |
| 52 | +function detectSurfaceImpls(filePath) { |
| 53 | + const text = readFileSync(filePath, 'utf8'); |
| 54 | + const collapsed = text.replace(/\s+/g, ' '); |
| 55 | + const found = new Set(); |
| 56 | + for (const { iface } of SURFACES) { |
| 57 | + // Match `: IStorageAdapter<` (with or without surrounding whitespace) anywhere a base |
| 58 | + // list could appear. Also match `, IStorageAdapter<` (additional interfaces). |
| 59 | + const re = new RegExp(`[:,]\\s*${iface}(?:<|\\s)`, 'g'); |
| 60 | + if (re.test(collapsed)) { |
| 61 | + found.add(iface); |
| 62 | + } |
| 63 | + } |
| 64 | + return found; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Returns the set of conformance-base keys (e.g., 'StorageAdapterConformance') the file |
| 69 | + * declares as a base class. |
| 70 | + */ |
| 71 | +function detectConformanceBases(filePath) { |
| 72 | + const text = readFileSync(filePath, 'utf8'); |
| 73 | + const collapsed = text.replace(/\s+/g, ' '); |
| 74 | + const found = new Set(); |
| 75 | + for (const { base } of SURFACES) { |
| 76 | + // Match `: StorageAdapterConformance<` or `: StorageAdapterConformance ` (no generics). |
| 77 | + const re = new RegExp(`[:,]\\s*${base}(?:<|\\s|$)`, 'g'); |
| 78 | + if (re.test(collapsed)) { |
| 79 | + found.add(base); |
| 80 | + } |
| 81 | + } |
| 82 | + return found; |
| 83 | +} |
| 84 | + |
| 85 | +const EXTENSIONS_SRC = join(SRC_DIR, 'extensions'); |
| 86 | +const EXTENSIONS_TESTS = join(TESTS_DIR, 'extensions'); |
| 87 | + |
| 88 | +const conformanceFailures = []; |
| 89 | + |
| 90 | +if (existsSync(EXTENSIONS_SRC)) { |
| 91 | + for (const entry of readdirSync(EXTENSIONS_SRC, { withFileTypes: true })) { |
| 92 | + if (!entry.isDirectory()) continue; |
| 93 | + if (!entry.name.startsWith('Flowthru.Extensions.')) continue; |
| 94 | + |
| 95 | + const extName = entry.name; |
| 96 | + const extSrcDir = join(EXTENSIONS_SRC, extName); |
| 97 | + const extTestsDir = join(EXTENSIONS_TESTS, `${extName}.Tests`); |
| 98 | + |
| 99 | + // Collect all surface kinds this extension implements. |
| 100 | + const implementedSurfaces = new Set(); |
| 101 | + for (const csFile of findCs(extSrcDir)) { |
| 102 | + const impls = detectSurfaceImpls(csFile); |
| 103 | + for (const surface of impls) { |
| 104 | + implementedSurfaces.add(surface); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (implementedSurfaces.size === 0) { |
| 109 | + // Extension implements no kit-tracked surface (e.g., EFCore.Bulk supplies saveFunc |
| 110 | + // delegates only). Nothing to enforce. |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + // Collect all conformance bases the test project covers. |
| 115 | + const coveredBases = new Set(); |
| 116 | + if (existsSync(extTestsDir)) { |
| 117 | + for (const csFile of findCs(extTestsDir)) { |
| 118 | + const bases = detectConformanceBases(csFile); |
| 119 | + for (const base of bases) { |
| 120 | + coveredBases.add(base); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // Map each implemented surface to its expected conformance base, then check coverage. |
| 126 | + const missingBases = []; |
| 127 | + for (const surface of implementedSurfaces) { |
| 128 | + const entry = SURFACES.find((s) => s.iface === surface); |
| 129 | + if (!entry) continue; |
| 130 | + if (!coveredBases.has(entry.base)) { |
| 131 | + missingBases.push({ surface, base: entry.base }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if (missingBases.length > 0) { |
| 136 | + conformanceFailures.push({ |
| 137 | + extension: extName, |
| 138 | + testsDir: existsSync(extTestsDir) ? rel(extTestsDir) : '<missing>', |
| 139 | + missing: missingBases, |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +let exitCode = 0; |
| 146 | + |
| 147 | +if (conformanceFailures.length > 0) { |
| 148 | + exitCode = 1; |
| 149 | + console.error( |
| 150 | + `\n${conformanceFailures.length} extension(s) implement Core surfaces but lack conformance coverage:\n` |
| 151 | + ); |
| 152 | + for (const { extension, testsDir, missing } of conformanceFailures) { |
| 153 | + console.error(` extension: ${extension}`); |
| 154 | + console.error(` tests: ${testsDir}`); |
| 155 | + for (const { surface, base } of missing) { |
| 156 | + console.error(` - implements ${surface} but no ${base} subclass found`); |
| 157 | + } |
| 158 | + console.error(''); |
| 159 | + } |
| 160 | + console.error( |
| 161 | + 'See `tests/README.md` (Extension Conformance Kits) and ' + |
| 162 | + '`docs/scratch/extension-conformance-kits.md` for the kit pattern.\n' |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +if (exitCode === 0) { |
| 167 | + console.log( |
| 168 | + '_test:conformance-presence — every first-party extension surface implementor has a conformance subclass.' |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +process.exit(exitCode); |
0 commit comments