|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the unified check-locale-changes script |
| 5 | + * Usage: node check-locale-changes.test.js |
| 6 | + */ |
| 7 | + |
| 8 | +const { test } = require('node:test'); |
| 9 | +const assert = require('node:assert'); |
| 10 | +const { |
| 11 | + generateFilesConfig, |
| 12 | + processManualTrigger, |
| 13 | + processAutoTrigger, |
| 14 | + isLocaleEnabled, |
| 15 | + getLocaleConfig, |
| 16 | +} = require('./check-locale-changes.js'); |
| 17 | + |
| 18 | +// Mock locale config for testing |
| 19 | +const mockLocaleConfig = { |
| 20 | + en: { |
| 21 | + secret_project_id: 'VERCEL_PROJECT_EN_ID', |
| 22 | + orama_private_api_key: 'ORAMA_PRIVATE_API_KEY_EN', |
| 23 | + enabled: true, |
| 24 | + }, |
| 25 | + 'zh-hans': { |
| 26 | + secret_project_id: 'VERCEL_PROJECT_ZH_HANS_ID', |
| 27 | + orama_private_api_key: 'ORAMA_PRIVATE_API_KEY_ZH_HANS', |
| 28 | + enabled: true, |
| 29 | + }, |
| 30 | + fr: { |
| 31 | + secret_project_id: 'VERCEL_PROJECT_FR_ID', |
| 32 | + orama_private_api_key: 'ORAMA_PRIVATE_API_KEY_FR', |
| 33 | + enabled: false, |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +test('isLocaleEnabled should return correct boolean values', () => { |
| 38 | + assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'en'), true); |
| 39 | + assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'zh-hans'), true); |
| 40 | + assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'fr'), false); |
| 41 | + assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'nonexistent'), false); |
| 42 | +}); |
| 43 | + |
| 44 | +test('getLocaleConfig should return correct configuration values', () => { |
| 45 | + assert.strictEqual( |
| 46 | + getLocaleConfig(mockLocaleConfig, 'en', 'secret_project_id'), |
| 47 | + 'VERCEL_PROJECT_EN_ID', |
| 48 | + ); |
| 49 | + assert.strictEqual( |
| 50 | + getLocaleConfig(mockLocaleConfig, 'en', 'orama_private_api_key'), |
| 51 | + 'ORAMA_PRIVATE_API_KEY_EN', |
| 52 | + ); |
| 53 | + assert.strictEqual( |
| 54 | + getLocaleConfig(mockLocaleConfig, 'nonexistent', 'secret_project_id'), |
| 55 | + '', |
| 56 | + ); |
| 57 | + assert.strictEqual( |
| 58 | + getLocaleConfig(mockLocaleConfig, 'en', 'nonexistent_field'), |
| 59 | + '', |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +test('generateFilesConfig should create proper YAML configuration', () => { |
| 64 | + const filesYaml = generateFilesConfig(mockLocaleConfig); |
| 65 | + |
| 66 | + // Should contain core files |
| 67 | + assert.ok(filesYaml.includes("core:\n - 'apps/docs/**'")); |
| 68 | + assert.ok(filesYaml.includes(" - 'packages/**'")); |
| 69 | + assert.ok(filesYaml.includes(" - '!apps/docs/content/**'")); |
| 70 | + |
| 71 | + // Should contain locale-specific files |
| 72 | + assert.ok(filesYaml.includes("en:\n - 'apps/docs/content/en/**'")); |
| 73 | + assert.ok(filesYaml.includes(" - 'apps/docs/messages/en.json'")); |
| 74 | + assert.ok(filesYaml.includes("zh-hans:\n - 'apps/docs/content/zh-hans/**'")); |
| 75 | + assert.ok(filesYaml.includes("fr:\n - 'apps/docs/content/fr/**'")); |
| 76 | +}); |
| 77 | + |
| 78 | +test('processManualTrigger should handle empty manual locales', () => { |
| 79 | + const result = processManualTrigger(mockLocaleConfig, ''); |
| 80 | + |
| 81 | + // Should include all enabled locales |
| 82 | + assert.strictEqual(result.matrixInclude.length, 2); |
| 83 | + assert.strictEqual(result.hasChanges, true); |
| 84 | + |
| 85 | + const locales = result.matrixInclude.map((item) => item.locale); |
| 86 | + assert.ok(locales.includes('en')); |
| 87 | + assert.ok(locales.includes('zh-hans')); |
| 88 | + assert.ok(!locales.includes('fr')); // fr is disabled |
| 89 | +}); |
| 90 | + |
| 91 | +test('processManualTrigger should handle specific manual locales', () => { |
| 92 | + const result = processManualTrigger(mockLocaleConfig, 'en'); |
| 93 | + |
| 94 | + // Should include only the specified locale |
| 95 | + assert.strictEqual(result.matrixInclude.length, 1); |
| 96 | + assert.strictEqual(result.hasChanges, true); |
| 97 | + assert.strictEqual(result.matrixInclude[0].locale, 'en'); |
| 98 | +}); |
| 99 | + |
| 100 | +test('processManualTrigger should skip disabled locales', () => { |
| 101 | + const result = processManualTrigger(mockLocaleConfig, 'fr'); |
| 102 | + |
| 103 | + // Should not include disabled locale |
| 104 | + assert.strictEqual(result.matrixInclude.length, 0); |
| 105 | + assert.strictEqual(result.hasChanges, false); |
| 106 | +}); |
| 107 | + |
| 108 | +test('processAutoTrigger should handle core changes', () => { |
| 109 | + const mockChanges = { |
| 110 | + core_any_changed: true, |
| 111 | + en_any_changed: false, |
| 112 | + zh_hans_any_changed: false, |
| 113 | + fr_any_changed: false, |
| 114 | + }; |
| 115 | + |
| 116 | + const result = processAutoTrigger(mockLocaleConfig, mockChanges); |
| 117 | + |
| 118 | + // Should include all enabled locales when core changes |
| 119 | + assert.strictEqual(result.matrixInclude.length, 2); |
| 120 | + assert.strictEqual(result.hasChanges, true); |
| 121 | + |
| 122 | + const locales = result.matrixInclude.map((item) => item.locale); |
| 123 | + assert.ok(locales.includes('en')); |
| 124 | + assert.ok(locales.includes('zh-hans')); |
| 125 | +}); |
| 126 | + |
| 127 | +test('processAutoTrigger should handle specific locale changes', () => { |
| 128 | + const mockChanges = { |
| 129 | + core_any_changed: false, |
| 130 | + en_any_changed: true, |
| 131 | + zh_hans_any_changed: false, |
| 132 | + fr_any_changed: false, |
| 133 | + }; |
| 134 | + |
| 135 | + const result = processAutoTrigger(mockLocaleConfig, mockChanges); |
| 136 | + |
| 137 | + // Should include only the changed locale |
| 138 | + assert.strictEqual(result.matrixInclude.length, 1); |
| 139 | + assert.strictEqual(result.hasChanges, true); |
| 140 | + assert.strictEqual(result.matrixInclude[0].locale, 'en'); |
| 141 | +}); |
| 142 | + |
| 143 | +test('processAutoTrigger should handle no changes', () => { |
| 144 | + const mockChanges = { |
| 145 | + core_any_changed: false, |
| 146 | + en_any_changed: false, |
| 147 | + zh_hans_any_changed: false, |
| 148 | + fr_any_changed: false, |
| 149 | + }; |
| 150 | + |
| 151 | + const result = processAutoTrigger(mockLocaleConfig, mockChanges); |
| 152 | + |
| 153 | + // Should include no locales when no changes |
| 154 | + assert.strictEqual(result.matrixInclude.length, 0); |
| 155 | + assert.strictEqual(result.hasChanges, false); |
| 156 | +}); |
| 157 | + |
| 158 | +test('Matrix items should have correct structure', () => { |
| 159 | + const result = processManualTrigger(mockLocaleConfig, 'en'); |
| 160 | + const item = result.matrixInclude[0]; |
| 161 | + |
| 162 | + assert.ok(Object.prototype.hasOwnProperty.call(item, 'locale')); |
| 163 | + assert.ok(Object.prototype.hasOwnProperty.call(item, 'secret_project_id')); |
| 164 | + assert.ok( |
| 165 | + Object.prototype.hasOwnProperty.call(item, 'orama_private_api_key'), |
| 166 | + ); |
| 167 | + assert.strictEqual(typeof item.locale, 'string'); |
| 168 | + assert.strictEqual(typeof item.secret_project_id, 'string'); |
| 169 | + assert.strictEqual(typeof item.orama_private_api_key, 'string'); |
| 170 | +}); |
| 171 | + |
| 172 | +console.log('All tests passed! ✅'); |
0 commit comments