|
| 1 | +/** |
| 2 | + * bash-ts-parity.test.ts — CI sync check for bash/TS shared library parity. |
| 3 | + * |
| 4 | + * Ensures that functions in the bash shared libraries (state-utils.sh, |
| 5 | + * parse-command.sh) have corresponding TypeScript implementations, and |
| 6 | + * vice versa. Drift between the two was undetected until manual comparison |
| 7 | + * (kaizen #347). |
| 8 | + * |
| 9 | + * Naming convention: bash uses snake_case, TS uses camelCase. |
| 10 | + * The test normalizes both to compare. |
| 11 | + */ |
| 12 | + |
| 13 | +import { readFileSync } from 'node:fs'; |
| 14 | +import { join } from 'node:path'; |
| 15 | +import { describe, expect, it } from 'vitest'; |
| 16 | + |
| 17 | +const HOOKS_LIB_DIR = join(__dirname, '../../.claude/kaizen/hooks/lib'); |
| 18 | +const HOOKS_TS_DIR = __dirname; |
| 19 | + |
| 20 | +// Functions intentionally present in only one version. |
| 21 | +// Each entry must have a comment explaining WHY it's excluded. |
| 22 | +const EXCLUSIONS: Record<string, string> = { |
| 23 | + // Requires `gh` CLI calls — intentionally stays bash-only (shell orchestration) |
| 24 | + auto_close_kaizen_issues: 'bash-only: requires gh CLI for PR state checks', |
| 25 | + // Requires `gh` CLI for PR state lookup + auto-clear — bash-only orchestration |
| 26 | + find_needs_review_state: |
| 27 | + 'bash-only: requires gh CLI for merged/closed PR auto-clear', |
| 28 | + |
| 29 | + // TS internal helpers with no bash equivalent (TS uses structured objects) |
| 30 | + parseStateFile: 'ts-only: internal helper, bash uses grep/cut inline', |
| 31 | + serializeStateFile: 'ts-only: internal helper, bash uses printf inline', |
| 32 | + ensureStateDir: 'ts-only: internal helper, bash uses mkdir -p inline', |
| 33 | + writeStateFile: 'ts-only: internal helper for atomic writes', |
| 34 | + |
| 35 | + // TS splits extractPrUrl from reconstructPrUrl; bash inlines the grep |
| 36 | + extractPrUrl: |
| 37 | + 'ts-only: extracted helper, bash inlines grep in reconstruct_pr_url', |
| 38 | +}; |
| 39 | + |
| 40 | +/** Extract function names from a bash script (matches `function_name() {`). */ |
| 41 | +function extractBashFunctions(filePath: string): string[] { |
| 42 | + const content = readFileSync(filePath, 'utf-8'); |
| 43 | + const pattern = /^([a-z_][a-z0-9_]*)\s*\(\)/gm; |
| 44 | + const functions: string[] = []; |
| 45 | + let match; |
| 46 | + while ((match = pattern.exec(content)) !== null) { |
| 47 | + functions.push(match[1]); |
| 48 | + } |
| 49 | + return functions; |
| 50 | +} |
| 51 | + |
| 52 | +/** Extract exported function names from a TypeScript file. */ |
| 53 | +function extractTsFunctions(filePath: string): string[] { |
| 54 | + const content = readFileSync(filePath, 'utf-8'); |
| 55 | + const pattern = /^export\s+function\s+([a-zA-Z_][a-zA-Z0-9_]*)/gm; |
| 56 | + const functions: string[] = []; |
| 57 | + let match; |
| 58 | + while ((match = pattern.exec(content)) !== null) { |
| 59 | + functions.push(match[1]); |
| 60 | + } |
| 61 | + return functions; |
| 62 | +} |
| 63 | + |
| 64 | +/** Convert snake_case to camelCase for comparison. */ |
| 65 | +function snakeToCamel(name: string): string { |
| 66 | + return name.replace(/_([a-z])/g, (_, c) => c.toUpperCase()); |
| 67 | +} |
| 68 | + |
| 69 | +/** Convert camelCase to snake_case for comparison. */ |
| 70 | +function camelToSnake(name: string): string { |
| 71 | + return name.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`); |
| 72 | +} |
| 73 | + |
| 74 | +function checkParity(bashFile: string, tsFile: string) { |
| 75 | + const bashFns = extractBashFunctions(join(HOOKS_LIB_DIR, bashFile)); |
| 76 | + const tsFns = extractTsFunctions(join(HOOKS_TS_DIR, tsFile)); |
| 77 | + |
| 78 | + // Normalize bash names to camelCase for comparison |
| 79 | + const bashCamel = new Map(bashFns.map((fn) => [snakeToCamel(fn), fn])); |
| 80 | + |
| 81 | + // Normalize TS names to snake_case for comparison |
| 82 | + const tsSnake = new Map(tsFns.map((fn) => [camelToSnake(fn), fn])); |
| 83 | + |
| 84 | + // Find bash functions missing from TS |
| 85 | + const missingInTs: string[] = []; |
| 86 | + for (const [camelName, bashName] of bashCamel) { |
| 87 | + if (!tsFns.includes(camelName) && !EXCLUSIONS[bashName]) { |
| 88 | + missingInTs.push(bashName); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Find TS functions missing from bash |
| 93 | + const missingInBash: string[] = []; |
| 94 | + for (const [snakeName, tsName] of tsSnake) { |
| 95 | + if (!bashFns.includes(snakeName) && !EXCLUSIONS[tsName]) { |
| 96 | + missingInBash.push(tsName); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return { bashFns, tsFns, missingInTs, missingInBash }; |
| 101 | +} |
| 102 | + |
| 103 | +describe('bash/TS shared library parity', () => { |
| 104 | + describe('state-utils', () => { |
| 105 | + it('all bash functions have TS equivalents (or are excluded)', () => { |
| 106 | + const { missingInTs } = checkParity('state-utils.sh', 'state-utils.ts'); |
| 107 | + expect( |
| 108 | + missingInTs, |
| 109 | + `Bash functions missing TS equivalent: ${missingInTs.join(', ')}. Either port them or add to EXCLUSIONS with a reason.`, |
| 110 | + ).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('all TS functions have bash equivalents (or are excluded)', () => { |
| 114 | + const { missingInBash } = checkParity('state-utils.sh', 'state-utils.ts'); |
| 115 | + expect( |
| 116 | + missingInBash, |
| 117 | + `TS functions missing bash equivalent: ${missingInBash.join(', ')}. Either port them or add to EXCLUSIONS with a reason.`, |
| 118 | + ).toEqual([]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('extracts functions from both files', () => { |
| 122 | + const { bashFns, tsFns } = checkParity( |
| 123 | + 'state-utils.sh', |
| 124 | + 'state-utils.ts', |
| 125 | + ); |
| 126 | + expect(bashFns.length).toBeGreaterThan(5); |
| 127 | + expect(tsFns.length).toBeGreaterThan(5); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('parse-command', () => { |
| 132 | + it('all bash functions have TS equivalents (or are excluded)', () => { |
| 133 | + const { missingInTs } = checkParity( |
| 134 | + 'parse-command.sh', |
| 135 | + 'parse-command.ts', |
| 136 | + ); |
| 137 | + expect( |
| 138 | + missingInTs, |
| 139 | + `Bash functions missing TS equivalent: ${missingInTs.join(', ')}. Either port them or add to EXCLUSIONS with a reason.`, |
| 140 | + ).toEqual([]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('all TS functions have bash equivalents (or are excluded)', () => { |
| 144 | + const { missingInBash } = checkParity( |
| 145 | + 'parse-command.sh', |
| 146 | + 'parse-command.ts', |
| 147 | + ); |
| 148 | + expect( |
| 149 | + missingInBash, |
| 150 | + `TS functions missing bash equivalent: ${missingInBash.join(', ')}. Either port them or add to EXCLUSIONS with a reason.`, |
| 151 | + ).toEqual([]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('extracts functions from both files', () => { |
| 155 | + const { bashFns, tsFns } = checkParity( |
| 156 | + 'parse-command.sh', |
| 157 | + 'parse-command.ts', |
| 158 | + ); |
| 159 | + expect(bashFns.length).toBeGreaterThan(3); |
| 160 | + expect(tsFns.length).toBeGreaterThan(3); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe('exclusions are valid', () => { |
| 165 | + it('all excluded functions actually exist in their source', () => { |
| 166 | + const stateUtilsBash = extractBashFunctions( |
| 167 | + join(HOOKS_LIB_DIR, 'state-utils.sh'), |
| 168 | + ); |
| 169 | + const parseCommandBash = extractBashFunctions( |
| 170 | + join(HOOKS_LIB_DIR, 'parse-command.sh'), |
| 171 | + ); |
| 172 | + const allBash = [...stateUtilsBash, ...parseCommandBash]; |
| 173 | + |
| 174 | + const stateUtilsTs = extractTsFunctions( |
| 175 | + join(HOOKS_TS_DIR, 'state-utils.ts'), |
| 176 | + ); |
| 177 | + const parseCommandTs = extractTsFunctions( |
| 178 | + join(HOOKS_TS_DIR, 'parse-command.ts'), |
| 179 | + ); |
| 180 | + const allTs = [...stateUtilsTs, ...parseCommandTs]; |
| 181 | + |
| 182 | + for (const excluded of Object.keys(EXCLUSIONS)) { |
| 183 | + const existsInBash = allBash.includes(excluded); |
| 184 | + const existsInTs = allTs.includes(excluded); |
| 185 | + expect( |
| 186 | + existsInBash || existsInTs, |
| 187 | + `Exclusion '${excluded}' doesn't exist in either bash or TS — remove stale exclusion`, |
| 188 | + ).toBe(true); |
| 189 | + } |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments