|
| 1 | +const assert = require('node:assert/strict'); |
| 2 | +const fs = require('node:fs'); |
| 3 | +const path = require('node:path'); |
| 4 | +const test = require('node:test'); |
| 5 | +const vm = require('node:vm'); |
| 6 | + |
| 7 | +const messagePrefix = 'to enable two-factor authentication as an additional security measure. Your activity on GitHub includes you in this requirement. You will need to enable two-factor authentication on your account before'; |
| 8 | +const messageSuffix = ', or be restricted from account actions.'; |
| 9 | + |
| 10 | +function loadPublicRules() { |
| 11 | + const filePath = path.join(__dirname, '..', 'locals.js'); |
| 12 | + const context = vm.createContext({}); |
| 13 | + |
| 14 | + vm.runInContext(fs.readFileSync(filePath, 'utf8'), context, { |
| 15 | + filename: filePath, |
| 16 | + }); |
| 17 | + |
| 18 | + return context.I18N['zh-CN'].public.regexp; |
| 19 | +} |
| 20 | + |
| 21 | +function loadTwoFactorDeadlineRule() { |
| 22 | + const rule = loadPublicRules().find(([pattern]) => ( |
| 23 | + pattern.source.includes('to enable two-factor authentication as an additional security measure') |
| 24 | + )); |
| 25 | + |
| 26 | + assert.ok(rule, 'the public dictionary should include the two-factor deadline rule'); |
| 27 | + return rule; |
| 28 | +} |
| 29 | + |
| 30 | +function translateDeadline(deadline, separator = '') { |
| 31 | + const [pattern, replacement] = loadTwoFactorDeadlineRule(); |
| 32 | + return `${messagePrefix}${separator}${deadline}${messageSuffix}`.replace(pattern, replacement); |
| 33 | +} |
| 34 | + |
| 35 | +test('translates a joined two-factor authentication deadline', () => { |
| 36 | + assert.equal( |
| 37 | + translateDeadline('2026年9月14日'), |
| 38 | + '启用双因素身份验证(2FA)作为额外安全措施。您在 GitHub 上的活动让您接收到此要求。您将需要在 2026年9月14日 前启用双因素身份验证,否则会被限制账户操作。', |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +test('continues to translate a spaced two-factor authentication deadline', () => { |
| 43 | + assert.equal( |
| 44 | + translateDeadline('2026年9月14日', ' '), |
| 45 | + '启用双因素身份验证(2FA)作为额外安全措施。您在 GitHub 上的活动让您接收到此要求。您将需要在 2026年9月14日 前启用双因素身份验证,否则会被限制账户操作。', |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +test('preserves a different two-factor authentication deadline', () => { |
| 50 | + const translated = translateDeadline('October 3, 2027'); |
| 51 | + |
| 52 | + assert.match(translated, /October 3, 2027/); |
| 53 | + assert.doesNotMatch(translated, /or be restricted from account actions/); |
| 54 | +}); |
0 commit comments