Skip to content

Commit 9c622aa

Browse files
committed
Add test to find orphan audit check locales
If any locale is set for an audit check that is no longer there, the test will fail. Makes it possible to keep locales in sync with audit check codes.
1 parent 28a7435 commit 9c622aa

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

packages/evolution-backend/src/services/audits/auditChecks/checks/__tests__/AuditLocales.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { tripAuditChecks } from '../TripAuditChecks';
1515
import { segmentAuditChecks } from '../SegmentAuditChecks';
1616
import i18n, { registerTranslationDir, addTranslationNamespace } from 'chaire-lib-backend/lib/config/i18next';
1717
import path from 'path';
18+
import fs from 'fs';
1819

1920
const languages = ['en', 'fr'];
2021

@@ -70,4 +71,43 @@ describe('Audit Locales', () => {
7071

7172
});
7273

74+
describe('Orphaned audit translations', () => {
75+
// Get all translation keys from the audit translation files
76+
const getTranslationKeysFromFile = (language: string): string[] => {
77+
const translationFilePath = path.join(__dirname, '../../../../../../../../locales/', language, 'audits.json');
78+
if (!fs.existsSync(translationFilePath)) {
79+
throw new Error(`Translation file not found: ${translationFilePath}`);
80+
}
81+
const translationFile = fs.readFileSync(translationFilePath, 'utf-8');
82+
const translations = JSON.parse(translationFile);
83+
return Object.keys(translations);
84+
};
85+
86+
// Define the mapping between prefixes and audit check modules
87+
const auditCheckModules: { [prefix: string]: Record<string, unknown> } = {
88+
'I_': interviewAuditChecks,
89+
'HM_': homeAuditChecks,
90+
'HH_': householdAuditChecks,
91+
'J_': journeyAuditChecks,
92+
'P_': personAuditChecks,
93+
'S_': segmentAuditChecks,
94+
'T_': tripAuditChecks,
95+
'VP_': visitedPlaceAuditChecks
96+
};
97+
98+
describe.each(languages)('Language: %s', (language) => {
99+
describe.each(Object.entries(auditCheckModules))('Prefix: %s', (prefix, auditChecks) => {
100+
it(`should not have orphaned translations starting with ${prefix}`, () => {
101+
const translationKeys = getTranslationKeysFromFile(language);
102+
const keysWithPrefix = translationKeys.filter((key) => key.startsWith(prefix));
103+
const auditCheckCodes = Object.keys(auditChecks);
104+
105+
// Find translations that exist but are not in the audit check module
106+
const orphanedKeys = keysWithPrefix.filter((key) => !auditCheckCodes.includes(key));
107+
expect(orphanedKeys).toEqual([]);
108+
});
109+
});
110+
});
111+
});
112+
73113
});

0 commit comments

Comments
 (0)