|
4 | 4 | } from '../../../machines/authenticator/types'; |
5 | 5 |
|
6 | 6 | import { authenticatorTextUtil } from '../textUtil'; |
| 7 | +import { I18n } from 'aws-amplify/utils'; |
| 8 | +import { translations } from '../../../i18n'; |
7 | 9 |
|
8 | 10 | describe('authenticatorTextUtil', () => { |
9 | 11 | describe('getChallengeText', () => { |
@@ -192,3 +194,180 @@ describe('authenticatorTextUtil', () => { |
192 | 194 | }); |
193 | 195 | }); |
194 | 196 | }); |
| 197 | + |
| 198 | +describe('getDeliveryMessageText locale punctuation (#6966)', () => { |
| 199 | + const emailDetails = { |
| 200 | + DeliveryMedium: 'EMAIL' as V6AuthDeliveryMedium, |
| 201 | + Destination: 'user@example.com', |
| 202 | + AttributeName: '', |
| 203 | + }; |
| 204 | + const smsDetails = { |
| 205 | + DeliveryMedium: 'SMS' as V6AuthDeliveryMedium, |
| 206 | + Destination: '+1234567890', |
| 207 | + AttributeName: '', |
| 208 | + }; |
| 209 | + const unknownDetails = { |
| 210 | + DeliveryMedium: 'INVALID_MEDIUM' as V6AuthDeliveryMedium, |
| 211 | + Destination: 'user@example.com', |
| 212 | + AttributeName: '', |
| 213 | + }; |
| 214 | + |
| 215 | + // CJK locales use the ideographic full stop; Thai uses no terminator; every |
| 216 | + // other bundled locale (Latin/Cyrillic/Hangul) keeps the ASCII period. |
| 217 | + const CJK_LOCALES = ['ja', 'zh'] as const; |
| 218 | + const NO_ASCII_PERIOD_LOCALES = ['th'] as const; |
| 219 | + const ASCII_PERIOD_LOCALES = [ |
| 220 | + 'de', |
| 221 | + 'es', |
| 222 | + 'fr', |
| 223 | + 'hu', |
| 224 | + 'id', |
| 225 | + 'it', |
| 226 | + 'kr', |
| 227 | + 'nb', |
| 228 | + 'nl', |
| 229 | + 'pl', |
| 230 | + 'pt', |
| 231 | + 'ru', |
| 232 | + 'sv', |
| 233 | + 'tr', |
| 234 | + 'ua', |
| 235 | + ] as const; |
| 236 | + |
| 237 | + beforeEach(() => { |
| 238 | + // Restore canonical vocabularies so an override from a prior test cannot leak. |
| 239 | + I18n.putVocabularies(translations); |
| 240 | + I18n.setLanguage('en'); |
| 241 | + }); |
| 242 | + |
| 243 | + afterAll(() => { |
| 244 | + I18n.setLanguage('en'); |
| 245 | + }); |
| 246 | + |
| 247 | + it('renders Japanese email with the ideographic full stop after the destination and at the end, never an ASCII period', () => { |
| 248 | + I18n.setLanguage('ja'); |
| 249 | + const result = authenticatorTextUtil.getDeliveryMessageText(emailDetails); |
| 250 | + |
| 251 | + expect(result).toContain('user@example.com。'); |
| 252 | + expect(result).not.toContain('user@example.com. '); |
| 253 | + expect(result.endsWith('。')).toBe(true); |
| 254 | + expect(result.endsWith('.')).toBe(false); |
| 255 | + }); |
| 256 | + |
| 257 | + it('renders Japanese SMS and unknown mediums terminated with the ideographic full stop', () => { |
| 258 | + I18n.setLanguage('ja'); |
| 259 | + |
| 260 | + const sms = authenticatorTextUtil.getDeliveryMessageText(smsDetails); |
| 261 | + expect(sms).toContain('+1234567890。'); |
| 262 | + expect(sms.endsWith('。')).toBe(true); |
| 263 | + |
| 264 | + const unknown = |
| 265 | + authenticatorTextUtil.getDeliveryMessageText(unknownDetails); |
| 266 | + expect(unknown.endsWith('。')).toBe(true); |
| 267 | + expect(unknown.endsWith('.')).toBe(false); |
| 268 | + }); |
| 269 | + |
| 270 | + it.each(CJK_LOCALES)( |
| 271 | + 'locale "%s" terminates every medium with the ideographic full stop, never an ASCII period', |
| 272 | + (locale) => { |
| 273 | + I18n.setLanguage(locale); |
| 274 | + for (const details of [emailDetails, smsDetails, unknownDetails]) { |
| 275 | + const result = authenticatorTextUtil.getDeliveryMessageText(details); |
| 276 | + expect(result.endsWith('。')).toBe(true); |
| 277 | + expect(result.endsWith('.')).toBe(false); |
| 278 | + } |
| 279 | + } |
| 280 | + ); |
| 281 | + |
| 282 | + it.each(NO_ASCII_PERIOD_LOCALES)( |
| 283 | + 'locale "%s" never terminates a delivery message with an ASCII period', |
| 284 | + (locale) => { |
| 285 | + I18n.setLanguage(locale); |
| 286 | + for (const details of [emailDetails, smsDetails, unknownDetails]) { |
| 287 | + const result = authenticatorTextUtil.getDeliveryMessageText(details); |
| 288 | + expect(result.endsWith('.')).toBe(false); |
| 289 | + expect(result.endsWith('。')).toBe(false); |
| 290 | + } |
| 291 | + } |
| 292 | + ); |
| 293 | + |
| 294 | + it.each(ASCII_PERIOD_LOCALES)( |
| 295 | + 'locale "%s" keeps the ASCII period and does not use the ideographic full stop', |
| 296 | + (locale) => { |
| 297 | + I18n.setLanguage(locale); |
| 298 | + for (const details of [emailDetails, smsDetails, unknownDetails]) { |
| 299 | + const result = authenticatorTextUtil.getDeliveryMessageText(details); |
| 300 | + expect(result.endsWith('.')).toBe(true); |
| 301 | + expect(result.endsWith('。')).toBe(false); |
| 302 | + } |
| 303 | + } |
| 304 | + ); |
| 305 | + |
| 306 | + it('keeps English output byte-identical to the pre-fix strings', () => { |
| 307 | + I18n.setLanguage('en'); |
| 308 | + expect(authenticatorTextUtil.getDeliveryMessageText(emailDetails)).toBe( |
| 309 | + 'Your code is on the way. To log in, enter the code we emailed to user@example.com. It may take a minute to arrive.' |
| 310 | + ); |
| 311 | + expect(authenticatorTextUtil.getDeliveryMessageText(smsDetails)).toBe( |
| 312 | + 'Your code is on the way. To log in, enter the code we texted to +1234567890. It may take a minute to arrive.' |
| 313 | + ); |
| 314 | + expect(authenticatorTextUtil.getDeliveryMessageText(unknownDetails)).toBe( |
| 315 | + 'Your code is on the way. To log in, enter the code we sent you. It may take a minute to arrive.' |
| 316 | + ); |
| 317 | + }); |
| 318 | + |
| 319 | + it('honors customer vocabulary overrides on the documented keys (confirm-sign-up pattern)', () => { |
| 320 | + I18n.putVocabulariesForLanguage('en', { |
| 321 | + 'Your code is on the way. To log in, enter the code we emailed to': |
| 322 | + 'Enter this code:', |
| 323 | + 'It may take a minute to arrive': |
| 324 | + 'It will take several minutes to arrive', |
| 325 | + }); |
| 326 | + |
| 327 | + expect(authenticatorTextUtil.getDeliveryMessageText(emailDetails)).toBe( |
| 328 | + 'Enter this code: user@example.com. It will take several minutes to arrive.' |
| 329 | + ); |
| 330 | + }); |
| 331 | + |
| 332 | + it('does not double terminal punctuation when an overridden fragment already ends with it', () => { |
| 333 | + I18n.putVocabulariesForLanguage('en', { |
| 334 | + 'It may take a minute to arrive': 'Arrives soon.', |
| 335 | + }); |
| 336 | + |
| 337 | + const result = authenticatorTextUtil.getDeliveryMessageText(emailDetails); |
| 338 | + expect(result.endsWith('Arrives soon.')).toBe(true); |
| 339 | + expect(result.endsWith('..')).toBe(false); |
| 340 | + }); |
| 341 | + |
| 342 | + it('does not append a terminator when an override already ends with an ellipsis (no.2)', () => { |
| 343 | + I18n.putVocabulariesForLanguage('en', { |
| 344 | + 'It may take a minute to arrive': 'Might take a minute or two…', |
| 345 | + }); |
| 346 | + |
| 347 | + const result = authenticatorTextUtil.getDeliveryMessageText(emailDetails); |
| 348 | + expect(result.endsWith('Might take a minute or two…')).toBe(true); |
| 349 | + expect(result.endsWith('….')).toBe(false); |
| 350 | + }); |
| 351 | + |
| 352 | + it('does not append a terminator when an override already ends with a colon (no.2)', () => { |
| 353 | + I18n.putVocabulariesForLanguage('en', { |
| 354 | + 'It may take a minute to arrive': 'Enter this code:', |
| 355 | + }); |
| 356 | + |
| 357 | + const result = authenticatorTextUtil.getDeliveryMessageText(emailDetails); |
| 358 | + expect(result.endsWith('Enter this code:')).toBe(true); |
| 359 | + expect(result.endsWith(':.')).toBe(false); |
| 360 | + }); |
| 361 | + |
| 362 | + it('keeps the ASCII period when a Latin instruction override contains a CJK proper noun (no.1)', () => { |
| 363 | + I18n.putVocabulariesForLanguage('en', { |
| 364 | + 'Your code is on the way. To log in, enter the code we emailed to': |
| 365 | + 'Enter the code sent by 東京 team to', |
| 366 | + }); |
| 367 | + |
| 368 | + const result = authenticatorTextUtil.getDeliveryMessageText(emailDetails); |
| 369 | + // Copy stays majority-Latin, so the ASCII period is kept, never `。`. |
| 370 | + expect(result).toContain('user@example.com.'); |
| 371 | + expect(result).not.toContain('user@example.com。'); |
| 372 | + }); |
| 373 | +}); |
0 commit comments