|
| 1 | +/** |
| 2 | + * Feature flag registry completeness (issue #316). |
| 3 | + * |
| 4 | + * The framework itself — resolution, precedence, source metadata, typed |
| 5 | + * disabled-feature errors, diagnostics integration — landed in #355. What it |
| 6 | + * had no defence against is **drift**: a flag can gate a code path without ever |
| 7 | + * being registered, and nothing notices. |
| 8 | + * |
| 9 | + * That is not hypothetical. `experimentalVaultLocks` gates the vault lock |
| 10 | + * actions through `readiness.featureFlag` in `src/vault/intents.ts`, a variable |
| 11 | + * rather than a literal, so it never appeared in a grep of call sites and was |
| 12 | + * absent from `DEFAULT_FEATURE_FLAGS`, `FeatureFlagKey` and the docs table. |
| 13 | + * |
| 14 | + * These tests are the canary. Adding a flag without registering it now fails |
| 15 | + * here instead of silently shipping a flag no consumer can discover. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect } from 'vitest'; |
| 19 | +import fs from 'node:fs'; |
| 20 | +import path from 'node:path'; |
| 21 | +import { DEFAULT_FEATURE_FLAGS, isFeatureEnabled } from '../src/config'; |
| 22 | + |
| 23 | +const SRC = path.resolve(__dirname, '..', 'src'); |
| 24 | + |
| 25 | +/** Every `.ts` file under `src/`. */ |
| 26 | +function sourceFiles(dir: string, acc: string[] = []): string[] { |
| 27 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 28 | + const full = path.join(dir, entry.name); |
| 29 | + if (entry.isDirectory()) sourceFiles(full, acc); |
| 30 | + else if (entry.name.endsWith('.ts')) acc.push(full); |
| 31 | + } |
| 32 | + return acc; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Flag keys referenced anywhere in `src/`. |
| 37 | + * |
| 38 | + * Matches the `'experimental…'` string literal itself rather than the call |
| 39 | + * site, because a flag can be passed as a variable — which is exactly how the |
| 40 | + * one drifting flag escaped detection. |
| 41 | + */ |
| 42 | +function referencedFlagKeys(): Map<string, string[]> { |
| 43 | + const found = new Map<string, string[]>(); |
| 44 | + for (const file of sourceFiles(SRC)) { |
| 45 | + const contents = fs.readFileSync(file, 'utf8'); |
| 46 | + for (const match of contents.matchAll(/['"](experimental[A-Za-z0-9]+)['"]/g)) { |
| 47 | + const key = match[1]!; |
| 48 | + const rel = path.relative(SRC, file).replace(/\\/g, '/'); |
| 49 | + const files = found.get(key) ?? []; |
| 50 | + if (!files.includes(rel)) files.push(rel); |
| 51 | + found.set(key, files); |
| 52 | + } |
| 53 | + } |
| 54 | + return found; |
| 55 | +} |
| 56 | + |
| 57 | +describe('registry completeness', () => { |
| 58 | + it('registers every experimental flag the source code references', () => { |
| 59 | + const referenced = referencedFlagKeys(); |
| 60 | + const registered = new Set(Object.keys(DEFAULT_FEATURE_FLAGS)); |
| 61 | + |
| 62 | + const unregistered = [...referenced.entries()] |
| 63 | + .filter(([key]) => !registered.has(key)) |
| 64 | + .map(([key, files]) => `${key} (referenced in ${files.join(', ')})`); |
| 65 | + |
| 66 | + expect( |
| 67 | + unregistered, |
| 68 | + 'Flags gate code but are missing from DEFAULT_FEATURE_FLAGS. ' + |
| 69 | + 'An unregistered flag still defaults to false, but no consumer can ' + |
| 70 | + 'discover it and diagnostics will not report its state.', |
| 71 | + ).toEqual([]); |
| 72 | + }); |
| 73 | + |
| 74 | + it('includes the vault lock flag that previously drifted', () => { |
| 75 | + // Regression guard for the specific gap this issue closes. |
| 76 | + expect(DEFAULT_FEATURE_FLAGS).toHaveProperty('experimentalVaultLocks'); |
| 77 | + expect(referencedFlagKeys().has('experimentalVaultLocks')).toBe(true); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('safe defaults', () => { |
| 82 | + it('defaults every registered flag to false', () => { |
| 83 | + for (const [key, value] of Object.entries(DEFAULT_FEATURE_FLAGS)) { |
| 84 | + expect(value, `${key} must be disabled by default`).toBe(false); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it('reports an unregistered flag as disabled rather than throwing', () => { |
| 89 | + // An unknown key resolves to false, which is why the drift was silent. |
| 90 | + expect(isFeatureEnabled('experimentalSomethingNobodyRegistered')).toBe(false); |
| 91 | + }); |
| 92 | + |
| 93 | + it('resolves a registered flag as disabled unless enabled explicitly', () => { |
| 94 | + expect(isFeatureEnabled('experimentalVaultLocks')).toBe(false); |
| 95 | + expect( |
| 96 | + isFeatureEnabled('experimentalVaultLocks', { |
| 97 | + featureFlags: { experimentalVaultLocks: true }, |
| 98 | + }), |
| 99 | + ).toBe(true); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('documentation matches the registry', () => { |
| 104 | + it('documents every registered flag in docs/feature-flags.md', () => { |
| 105 | + const docs = fs.readFileSync( |
| 106 | + path.resolve(__dirname, '..', 'docs', 'feature-flags.md'), |
| 107 | + 'utf8', |
| 108 | + ); |
| 109 | + |
| 110 | + const undocumented = Object.keys(DEFAULT_FEATURE_FLAGS).filter( |
| 111 | + (key) => !docs.includes(`\`${key}\``), |
| 112 | + ); |
| 113 | + |
| 114 | + expect( |
| 115 | + undocumented, |
| 116 | + 'Registered flags missing from the documented table.', |
| 117 | + ).toEqual([]); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('registered flags that gate nothing are declared as reserved', () => { |
| 122 | + it('lists which registered flags have no reference in src/', () => { |
| 123 | + // Not a failure: a reserved key is a legitimate placeholder, and removing |
| 124 | + // one would be a breaking change to a published union. It is documented |
| 125 | + // rather than silently implying a capability that does not exist — the same |
| 126 | + // reasoning the capability registry applies to `planned` entries. |
| 127 | + const referenced = referencedFlagKeys(); |
| 128 | + const reserved = Object.keys(DEFAULT_FEATURE_FLAGS).filter( |
| 129 | + (key) => !referenced.has(key), |
| 130 | + ); |
| 131 | + |
| 132 | + const docs = fs.readFileSync( |
| 133 | + path.resolve(__dirname, '..', 'docs', 'feature-flags.md'), |
| 134 | + 'utf8', |
| 135 | + ); |
| 136 | + |
| 137 | + for (const key of reserved) { |
| 138 | + expect( |
| 139 | + docs.includes(`\`${key}\``) && /Reserved/i.test(docs), |
| 140 | + `${key} gates no code path and must be marked Reserved in the docs`, |
| 141 | + ).toBe(true); |
| 142 | + } |
| 143 | + }); |
| 144 | +}); |
0 commit comments