|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const assert = require('node:assert/strict'); |
| 4 | +const { execFileSync } = require('node:child_process'); |
| 5 | +const fs = require('node:fs'); |
| 6 | +const os = require('node:os'); |
| 7 | +const path = require('node:path'); |
| 8 | +const ts = require('typescript'); |
| 9 | + |
| 10 | +const SCRIPT = path.resolve(__dirname, 'generate-messages.js'); |
| 11 | + |
| 12 | +const FILE1_EXPECTED = [ |
| 13 | + { name: 'HELLO', value: 'Hello, %s!' }, |
| 14 | + { name: 'GOODBYE', value: 'Goodbye, %s!' }, |
| 15 | + { name: 'WARNING', value: 'Warning: %s' }, |
| 16 | +]; |
| 17 | + |
| 18 | +const FILE2_EXPECTED = [ |
| 19 | + { name: 'ERR_NOT_FOUND', value: 'Resource %s not found' }, |
| 20 | + { name: 'ERR_TIMEOUT', value: 'Request timed out after %s ms' }, |
| 21 | + { name: 'ERR_INVALID', value: 'Invalid input: %s' }, |
| 22 | + { name: 'ERR_PERMISSION', value: 'Permission denied for %s' }, |
| 23 | +]; |
| 24 | + |
| 25 | +function buildFileContent(expected, { includeMessages = true, includePlatforms = false } = {}) { |
| 26 | + let content = expected.map((e) => `export const ${e.name} = '${e.value}';`).join('\n'); |
| 27 | + if (includeMessages) content += `\n\nexport const messages: string[] = [];`; |
| 28 | + if (includePlatforms) content += `\nexport const __platforms: string[] = ['__universal__'];`; |
| 29 | + return content; |
| 30 | +} |
| 31 | + |
| 32 | +function compileAndLoad(tsPath) { |
| 33 | + const source = fs.readFileSync(tsPath, 'utf-8'); |
| 34 | + const { outputText } = ts.transpileModule(source, { |
| 35 | + compilerOptions: { module: ts.ModuleKind.CommonJS }, |
| 36 | + }); |
| 37 | + const jsPath = tsPath.replace(/\.ts$/, '.js'); |
| 38 | + fs.writeFileSync(jsPath, outputText, 'utf-8'); |
| 39 | + return require(jsPath); |
| 40 | +} |
| 41 | + |
| 42 | +function runTest() { |
| 43 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gen-msg-test-')); |
| 44 | + |
| 45 | + try { |
| 46 | + const file1 = path.join(tmpDir, 'file1.ts'); |
| 47 | + const file2 = path.join(tmpDir, 'file2.ts'); |
| 48 | + fs.writeFileSync(file1, buildFileContent(FILE1_EXPECTED, { includePlatforms: true }), 'utf-8'); |
| 49 | + fs.writeFileSync(file2, buildFileContent(FILE2_EXPECTED), 'utf-8'); |
| 50 | + |
| 51 | + execFileSync(process.execPath, [SCRIPT, file1, file2], { stdio: 'pipe' }); |
| 52 | + |
| 53 | + const genFile1 = path.join(tmpDir, 'file1.gen.ts'); |
| 54 | + const genFile2 = path.join(tmpDir, 'file2.gen.ts'); |
| 55 | + assert.ok(fs.existsSync(genFile1), 'file1.gen.ts should be created'); |
| 56 | + assert.ok(fs.existsSync(genFile2), 'file2.gen.ts should be created'); |
| 57 | + |
| 58 | + for (const [expected, genPath, label] of [ |
| 59 | + [FILE1_EXPECTED, genFile1, 'file1'], |
| 60 | + [FILE2_EXPECTED, genFile2, 'file2'], |
| 61 | + ]) { |
| 62 | + const mod = compileAndLoad(genPath); |
| 63 | + |
| 64 | + assert.ok(Array.isArray(mod.messages), `${label}: messages should be an array`); |
| 65 | + assert.equal(mod.messages.length, expected.length, |
| 66 | + `${label}: messages array length should be ${expected.length}, got ${mod.messages.length}`); |
| 67 | + |
| 68 | + assert.equal(mod.__platforms, undefined, `${label}: __platforms should not be exported`); |
| 69 | + |
| 70 | + const constantCount = Object.keys(mod).filter((k) => k !== 'messages').length; |
| 71 | + assert.equal(constantCount, expected.length, |
| 72 | + `${label}: should have ${expected.length} constants, got ${constantCount}`); |
| 73 | + |
| 74 | + for (const exp of expected) { |
| 75 | + assert.ok(exp.name in mod, |
| 76 | + `${label}: constant "${exp.name}" should be present`); |
| 77 | + |
| 78 | + const index = Number(mod[exp.name]); |
| 79 | + assert.equal(mod.messages[index], exp.value, |
| 80 | + `${label}: messages[${index}] should be "${exp.value}", got "${mod.messages[index]}"`); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + console.log('All tests passed.'); |
| 85 | + } finally { |
| 86 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +runTest(); |
0 commit comments