|
| 1 | +/** |
| 2 | + * File structure validation tests |
| 3 | + * |
| 4 | + * These tests ensure there are no duplicate core files that could cause |
| 5 | + * conflicting behavior at runtime. |
| 6 | + */ |
| 7 | + |
| 8 | +import * as assert from 'assert'; |
| 9 | +import * as fs from 'fs'; |
| 10 | +import * as path from 'path'; |
| 11 | + |
| 12 | +suite('File Structure Validation', () => { |
| 13 | + |
| 14 | + test('exactly one import-organizer.ts exists', () => { |
| 15 | + // When tests run from out/test/, we need to go up to project root then into src |
| 16 | + const projectRoot = path.resolve(__dirname, '../..'); |
| 17 | + const srcDir = path.join(projectRoot, 'src/imports'); |
| 18 | + const files = findFilesRecursive(srcDir, 'import-organizer.ts'); |
| 19 | + |
| 20 | + assert.strictEqual( |
| 21 | + files.length, |
| 22 | + 1, |
| 23 | + `Expected exactly 1 import-organizer.ts, found ${files.length}: ${files.join(', ')}` |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + test('exactly one import-types.ts exists', () => { |
| 28 | + // When tests run from out/test/, we need to go up to project root then into src |
| 29 | + const projectRoot = path.resolve(__dirname, '../..'); |
| 30 | + const srcDir = path.join(projectRoot, 'src/imports'); |
| 31 | + const files = findFilesRecursive(srcDir, 'import-types.ts'); |
| 32 | + |
| 33 | + assert.strictEqual( |
| 34 | + files.length, |
| 35 | + 1, |
| 36 | + `Expected exactly 1 import-types.ts, found ${files.length}: ${files.join(', ')}` |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + test('import-types.ts supports attributes', () => { |
| 41 | + const projectRoot = path.resolve(__dirname, '../..'); |
| 42 | + const srcDir = path.join(projectRoot, 'src/imports'); |
| 43 | + const files = findFilesRecursive(srcDir, 'import-types.ts'); |
| 44 | + |
| 45 | + assert.strictEqual(files.length, 1, 'Should have exactly one import-types.ts'); |
| 46 | + |
| 47 | + const content = fs.readFileSync(files[0], 'utf8'); |
| 48 | + |
| 49 | + // Check for attributes field in the Import interface |
| 50 | + assert.ok( |
| 51 | + /attributes\?:\s*string/.test(content), |
| 52 | + 'import-types.ts must have attributes?: string field' |
| 53 | + ); |
| 54 | + |
| 55 | + // Check for isTypeOnly in NamedImport |
| 56 | + assert.ok( |
| 57 | + /isTypeOnly:\s*boolean/.test(content), |
| 58 | + 'import-types.ts must have isTypeOnly: boolean field' |
| 59 | + ); |
| 60 | + |
| 61 | + // Check for specifier comments |
| 62 | + assert.ok( |
| 63 | + /leadingComment\?:\s*string/.test(content), |
| 64 | + 'import-types.ts must have leadingComment?: string field' |
| 65 | + ); |
| 66 | + |
| 67 | + assert.ok( |
| 68 | + /trailingComment\?:\s*string/.test(content), |
| 69 | + 'import-types.ts must have trailingComment?: string field' |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + test('import-organizer.ts does not register old command alias', () => { |
| 74 | + const projectRoot = path.resolve(__dirname, '../..'); |
| 75 | + const srcDir = path.join(projectRoot, 'src/imports'); |
| 76 | + const files = findFilesRecursive(srcDir, 'import-organizer.ts'); |
| 77 | + |
| 78 | + assert.strictEqual(files.length, 1, 'Should have exactly one import-organizer.ts'); |
| 79 | + |
| 80 | + const content = fs.readFileSync(files[0], 'utf8'); |
| 81 | + |
| 82 | + // Check that it does NOT register the old command |
| 83 | + assert.ok( |
| 84 | + !content.includes("'typescriptHero.imports.organize'"), |
| 85 | + 'import-organizer.ts must NOT register the old typescriptHero.imports.organize command' |
| 86 | + ); |
| 87 | + |
| 88 | + // Check that it DOES register the new command |
| 89 | + assert.ok( |
| 90 | + content.includes("'miniTypescriptHero.imports.organize'"), |
| 91 | + 'import-organizer.ts must register miniTypescriptHero.imports.organize command' |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + test('no conflicting source files exist', () => { |
| 96 | + const projectRoot = path.resolve(__dirname, '../..'); |
| 97 | + const srcDir = path.join(projectRoot, 'src'); |
| 98 | + |
| 99 | + // These are the canonical files that must be unique |
| 100 | + const coreFiles = [ |
| 101 | + 'imports/import-organizer.ts', |
| 102 | + 'imports/import-types.ts', |
| 103 | + 'imports/import-manager.ts', |
| 104 | + 'configuration/imports-config.ts', |
| 105 | + ]; |
| 106 | + |
| 107 | + for (const relativePath of coreFiles) { |
| 108 | + const fullPath = path.join(srcDir, relativePath); |
| 109 | + assert.ok( |
| 110 | + fs.existsSync(fullPath), |
| 111 | + `Core file must exist: ${relativePath}` |
| 112 | + ); |
| 113 | + |
| 114 | + // Check that no duplicates exist anywhere else |
| 115 | + const filename = path.basename(relativePath); |
| 116 | + const allMatches = findFilesRecursive(srcDir, filename); |
| 117 | + |
| 118 | + assert.strictEqual( |
| 119 | + allMatches.length, |
| 120 | + 1, |
| 121 | + `Found multiple copies of ${filename}: ${allMatches.join(', ')}` |
| 122 | + ); |
| 123 | + } |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +/** |
| 128 | + * Recursively find all files with a given name in a directory. |
| 129 | + */ |
| 130 | +function findFilesRecursive(dir: string, filename: string): string[] { |
| 131 | + const results: string[] = []; |
| 132 | + |
| 133 | + if (!fs.existsSync(dir)) { |
| 134 | + return results; |
| 135 | + } |
| 136 | + |
| 137 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 138 | + |
| 139 | + for (const entry of entries) { |
| 140 | + const fullPath = path.join(dir, entry.name); |
| 141 | + |
| 142 | + if (entry.isDirectory()) { |
| 143 | + // Skip node_modules and test directories |
| 144 | + if (entry.name !== 'node_modules' && entry.name !== 'test' && entry.name !== 'out') { |
| 145 | + results.push(...findFilesRecursive(fullPath, filename)); |
| 146 | + } |
| 147 | + } else if (entry.isFile() && entry.name === filename) { |
| 148 | + results.push(fullPath); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return results; |
| 153 | +} |
0 commit comments