|
| 1 | +const {execSync} = require('child_process'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const os = require('os'); |
| 5 | + |
| 6 | +const SCRIPT_PATH = path.join(__dirname, '..', 'validate-l10n.js'); |
| 7 | +const LOCALES_DIR = path.join(__dirname, '..', '..', 'src', 'locales'); |
| 8 | + |
| 9 | +/** |
| 10 | + * Run the validate-l10n.js script against a temporary locale directory. |
| 11 | + * This avoids modifying the real locale files (which would cause race conditions |
| 12 | + * when Jest runs tests in parallel). |
| 13 | + * |
| 14 | + * Creates a modified copy of the script that points to the temp directory, |
| 15 | + * copies the locale files there, applies any overrides, and runs. |
| 16 | + */ |
| 17 | +function runWithLocales(overrides = {}) { |
| 18 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'l10n-test-')); |
| 19 | + const tmpLocalesDir = path.join(tmpDir, 'locales'); |
| 20 | + fs.mkdirSync(tmpLocalesDir); |
| 21 | + |
| 22 | + try { |
| 23 | + // Copy original locale files to temp dir |
| 24 | + for (const filename of ['en.json', 'ja.json', 'zh.json']) { |
| 25 | + const src = path.join(LOCALES_DIR, filename); |
| 26 | + const dest = path.join(tmpLocalesDir, filename); |
| 27 | + fs.copyFileSync(src, dest); |
| 28 | + } |
| 29 | + |
| 30 | + // Apply overrides |
| 31 | + for (const [filename, content] of Object.entries(overrides)) { |
| 32 | + const dest = path.join(tmpLocalesDir, filename); |
| 33 | + fs.writeFileSync(dest, content, 'utf-8'); |
| 34 | + } |
| 35 | + |
| 36 | + // Create a modified copy of the script that points to the temp dir |
| 37 | + let scriptContent = fs.readFileSync(SCRIPT_PATH, 'utf-8'); |
| 38 | + scriptContent = scriptContent.replace( |
| 39 | + /const LOCALES_DIR = .+;/, |
| 40 | + `const LOCALES_DIR = ${JSON.stringify(tmpLocalesDir)};`, |
| 41 | + ); |
| 42 | + scriptContent = scriptContent.replace( |
| 43 | + /const EN_PATH = .+;/, |
| 44 | + `const EN_PATH = ${JSON.stringify(path.join(tmpLocalesDir, 'en.json'))};`, |
| 45 | + ); |
| 46 | + const tmpScriptPath = path.join(tmpDir, 'validate-l10n.js'); |
| 47 | + fs.writeFileSync(tmpScriptPath, scriptContent, 'utf-8'); |
| 48 | + |
| 49 | + // Run the modified script |
| 50 | + try { |
| 51 | + const output = execSync(`node "${tmpScriptPath}" 2>&1`, { |
| 52 | + encoding: 'utf-8', |
| 53 | + timeout: 10000, |
| 54 | + }); |
| 55 | + return {exitCode: 0, output}; |
| 56 | + } catch (e) { |
| 57 | + return { |
| 58 | + exitCode: e.status, |
| 59 | + output: (e.stdout || '') + (e.stderr || ''), |
| 60 | + }; |
| 61 | + } |
| 62 | + } finally { |
| 63 | + // Clean up temp directory |
| 64 | + fs.rmSync(tmpDir, {recursive: true, force: true}); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +describe('validate-l10n.js', () => { |
| 69 | + it('passes with valid locale files', () => { |
| 70 | + const result = runWithLocales(); |
| 71 | + expect(result.exitCode).toBe(0); |
| 72 | + expect(result.output).toContain('en.json: valid JSON'); |
| 73 | + expect(result.output).toContain('ja.json: valid JSON'); |
| 74 | + expect(result.output).toContain('zh.json: valid JSON'); |
| 75 | + expect(result.output).toContain('All l10n files valid'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('reports the number of keys in en.json', () => { |
| 79 | + const result = runWithLocales(); |
| 80 | + expect(result.output).toMatch(/en\.json: \d+ keys/); |
| 81 | + }); |
| 82 | + |
| 83 | + it('fails on invalid JSON in ja.json', () => { |
| 84 | + const result = runWithLocales({ |
| 85 | + 'ja.json': '{ invalid json content', |
| 86 | + }); |
| 87 | + expect(result.exitCode).not.toBe(0); |
| 88 | + expect(result.output).toContain('INVALID JSON'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('fails on invalid JSON in zh.json', () => { |
| 92 | + const result = runWithLocales({ |
| 93 | + 'zh.json': '{ "unclosed": ', |
| 94 | + }); |
| 95 | + expect(result.exitCode).not.toBe(0); |
| 96 | + expect(result.output).toContain('INVALID JSON'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('warns about missing keys in translation files', () => { |
| 100 | + const result = runWithLocales({ |
| 101 | + 'ja.json': JSON.stringify({common: {cancel: 'test'}}), |
| 102 | + }); |
| 103 | + // Missing keys are warnings, not errors -- script should still pass |
| 104 | + expect(result.exitCode).toBe(0); |
| 105 | + expect(result.output).toContain('missing keys'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('fails on placeholder mismatch', () => { |
| 109 | + // en.json has storage.lowStorage with {{modelSize}} and {{freeSpace}} |
| 110 | + // Create ja.json with a wrong placeholder at that path |
| 111 | + const enData = JSON.parse( |
| 112 | + fs.readFileSync(path.join(LOCALES_DIR, 'en.json'), 'utf-8'), |
| 113 | + ); |
| 114 | + const jaModified = JSON.parse(JSON.stringify(enData)); |
| 115 | + jaModified.storage.lowStorage = 'wrong {{wrong}} > {{freeSpace}}'; |
| 116 | + |
| 117 | + const result = runWithLocales({ |
| 118 | + 'ja.json': JSON.stringify(jaModified), |
| 119 | + }); |
| 120 | + expect(result.exitCode).not.toBe(0); |
| 121 | + expect(result.output).toContain('placeholder mismatch'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('passes when translation file has identical placeholders to en', () => { |
| 125 | + const enContent = fs.readFileSync( |
| 126 | + path.join(LOCALES_DIR, 'en.json'), |
| 127 | + 'utf-8', |
| 128 | + ); |
| 129 | + const result = runWithLocales({ |
| 130 | + 'ja.json': enContent, |
| 131 | + }); |
| 132 | + expect(result.exitCode).toBe(0); |
| 133 | + expect(result.output).toContain('All l10n files valid'); |
| 134 | + }); |
| 135 | +}); |
0 commit comments