asma-core-helpers 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,462 @@
1
+ import assert from 'node:assert/strict'
2
+ import { describe, it } from 'node:test'
3
+
4
+ import {
5
+ formatNationalAsYouType,
6
+ formatPhoneForDisplay,
7
+ isValidPhone,
8
+ isValidPhoneValue,
9
+ parsePhoneNr,
10
+ parsePhoneValue,
11
+ phoneTelHref,
12
+ toE164,
13
+ } from './phoneNumber.js'
14
+ import type { PhoneCountry } from './phoneCountries.js'
15
+
16
+ describe('parsePhoneNr', () => {
17
+ // The canonicalisation contract every writer and the backfill share. Each row is a
18
+ // shape found in, or reachable into, `customer_user.phone_nr` today.
19
+ const contract: readonly [string, PhoneCountry, string | 'INVALID'][] = [
20
+ ['45456565', 'NO', '+4745456565'],
21
+ ['4745456565', 'NO', '+4745456565'],
22
+ ['004745456565', 'NO', '+4745456565'],
23
+ ['+47 454 56 565', 'NO', '+4745456565'],
24
+ ['47123456', 'NO', '+4747123456'],
25
+ ['+37379094538', 'NO', '+37379094538'],
26
+ ['0701234567', 'NO', 'INVALID'],
27
+ ['0701234567', 'SE', '+46701234567'],
28
+ ['12345', 'NO', 'INVALID'],
29
+ ]
30
+
31
+ for (const [input, region, expected] of contract) {
32
+ it(`canonicalises ${input} under ${region} to ${expected}`, () => {
33
+ const result = parsePhoneNr(input, region)
34
+
35
+ if (expected === 'INVALID') {
36
+ assert.deepEqual(result, { ok: false, reason: 'INVALID', input: input.trim() })
37
+ return
38
+ }
39
+
40
+ assert.equal(result.ok, true)
41
+ assert.equal(result.ok && result.e164, expected)
42
+ })
43
+ }
44
+
45
+ it('reports the country and calling code the value resolved to', () => {
46
+ const result = parsePhoneNr('45456565', 'NO')
47
+
48
+ assert.deepEqual(result, { ok: true, e164: '+4745456565', country: 'NO', callingCode: '47' })
49
+ })
50
+
51
+ it('separates an empty value from an invalid one so a report can tell them apart', () => {
52
+ for (const value of ['', ' ', null, undefined]) {
53
+ assert.deepEqual(parsePhoneNr(value, 'NO'), { ok: false, reason: 'EMPTY', input: '' })
54
+ }
55
+ })
56
+
57
+ it('reports rather than rewrites a number that is not real for the region', () => {
58
+ // `12345678` and `00000000` passed the pre-ticket digit-count rules and are in the
59
+ // data; no algorithm can recover an intended number from them.
60
+ for (const junk of ['12345678', '00000000', '11111111']) {
61
+ assert.deepEqual(parsePhoneNr(junk, 'NO'), { ok: false, reason: 'INVALID', input: junk })
62
+ }
63
+ })
64
+ })
65
+
66
+ describe('parsePhoneValue', () => {
67
+ it('splits a stored E.164 value into country and national number', () => {
68
+ assert.deepEqual(parsePhoneValue('+4748012345'), { iso2: 'NO', country: 'NO', national: '48012345' })
69
+ assert.deepEqual(parsePhoneValue('+12015550123', 'NO'), { iso2: 'US', country: 'US', national: '2015550123' })
70
+ })
71
+
72
+ it('reads a pre-ASMA-7485 bare national number as the fallback country', () => {
73
+ // Every record written before this ticket is 8 bare digits with no country. The fallback is
74
+ // what the field edits with; `country` says whether the numbering plan actually confirms it.
75
+ assert.deepEqual(parsePhoneValue('48012345', 'NO'), { iso2: 'NO', country: 'NO', national: '48012345' })
76
+ assert.deepEqual(parsePhoneValue('48012345', 'SE'), { iso2: 'SE', country: 'SE', national: '48012345' })
77
+ })
78
+
79
+ it('keeps the caller country while a matching number is still being typed', () => {
80
+ assert.deepEqual(parsePhoneValue('+47', 'NO'), { iso2: 'NO', country: 'NO', national: '' })
81
+ assert.deepEqual(parsePhoneValue('+4748', 'NO'), { iso2: 'NO', country: 'NO', national: '48' })
82
+ })
83
+
84
+ it('switches country when the typed prefix belongs to another one', () => {
85
+ assert.deepEqual(parsePhoneValue('+46701', 'NO'), { iso2: 'SE', country: 'SE', national: '701' })
86
+ })
87
+
88
+ it('treats empty, whitespace and nullish values as an empty number', () => {
89
+ for (const value of ['', ' ', null, undefined]) {
90
+ assert.deepEqual(parsePhoneValue(value, 'NO'), { iso2: 'NO', country: undefined, national: '' })
91
+ }
92
+ })
93
+
94
+ it('falls back to the given country for an unknown calling code', () => {
95
+ assert.deepEqual(parsePhoneValue('+9991234', 'NO'), { iso2: 'NO', country: undefined, national: '9991234' })
96
+ })
97
+ })
98
+
99
+ describe('toE164', () => {
100
+ it('composes the stored value from the country and the typed digits', () => {
101
+ assert.equal(toE164('48012345', 'NO'), '+4748012345')
102
+ assert.equal(toE164('2015550123', 'US'), '+12015550123')
103
+ })
104
+
105
+ it('drops mask separators so the stored value is digits and a plus only', () => {
106
+ assert.equal(toE164('48 01 23 45', 'NO'), '+4748012345')
107
+ assert.equal(toE164('(201) 555-0123', 'US'), '+12015550123')
108
+ })
109
+
110
+ it('stores nothing rather than a bare dial code for an empty number', () => {
111
+ assert.equal(toE164('', 'NO'), '')
112
+ assert.equal(toE164(' ', 'NO'), '')
113
+ })
114
+
115
+ it('does not validate — an in-progress number must still round-trip', () => {
116
+ assert.equal(toE164('4', 'NO'), '+474')
117
+ })
118
+
119
+ it('does not double a country code the input already carries', () => {
120
+ // Concatenation stored `+474748012345` and `+47004748012345`, both unusable.
121
+ assert.equal(toE164('4748012345', 'NO'), '+4748012345')
122
+ assert.equal(toE164('004748012345', 'NO'), '+4748012345')
123
+ assert.equal(toE164('4781234567', 'NO'), '+4781234567')
124
+ })
125
+
126
+ it('drops the trunk prefix countries write their national numbers with', () => {
127
+ // SE/GB/DE national form carries a leading 0 that is not part of the E.164 number,
128
+ // so concatenation stored `+460701234567` for a number people write as 070-123 45 67.
129
+ assert.equal(toE164('0701234567', 'SE'), '+46701234567')
130
+ assert.equal(toE164('07911123456', 'GB'), '+447911123456')
131
+ assert.equal(toE164('015112345678', 'DE'), '+4915112345678')
132
+ })
133
+
134
+ it('keeps a genuine national number that merely starts with the dial code', () => {
135
+ assert.equal(toE164('47123456', 'NO'), '+4747123456')
136
+ })
137
+
138
+ it('round-trips through parsePhoneValue', () => {
139
+ const parsed = parsePhoneValue(toE164('48012345', 'NO'))
140
+ assert.deepEqual(parsed, { iso2: 'NO', country: 'NO', national: '48012345' })
141
+ })
142
+ })
143
+
144
+ describe('formatNationalAsYouType', () => {
145
+ it('groups by the country format, progressively', () => {
146
+ assert.equal(formatNationalAsYouType('4', 'NO'), '4')
147
+ assert.equal(formatNationalAsYouType('480', 'NO'), '48 0')
148
+ assert.equal(formatNationalAsYouType('48012345', 'NO'), '48 01 23 45')
149
+ })
150
+
151
+ it('uses each country own grouping, not a Norwegian one', () => {
152
+ assert.equal(formatNationalAsYouType('2133734253', 'US'), '213 373 4253')
153
+ assert.equal(formatNationalAsYouType('512345678', 'PL'), '512 345 678')
154
+ })
155
+
156
+ it('groups countries that carry a trunk prefix, which the national format cannot', () => {
157
+ // AsYouType('SE') returns '701234567' unformatted: Swedish national formats are
158
+ // defined with the leading 0, which a national significant number does not carry.
159
+ assert.equal(formatNationalAsYouType('701234567', 'SE'), '70 123 45 67')
160
+ assert.equal(formatNationalAsYouType('7400123456', 'GB'), '7400 123456')
161
+ assert.equal(formatNationalAsYouType('612345678', 'FR'), '6 12 34 56 78')
162
+ })
163
+
164
+ it('returns an empty string when there is nothing to format', () => {
165
+ assert.equal(formatNationalAsYouType('', 'NO'), '')
166
+ assert.equal(formatNationalAsYouType('abc', 'NO'), '')
167
+ })
168
+ })
169
+
170
+ describe('isValidPhone', () => {
171
+ it('accepts a real number for the country', () => {
172
+ assert.equal(isValidPhone('48012345', 'NO'), true)
173
+ assert.equal(isValidPhone('2015550123', 'US'), true)
174
+ })
175
+
176
+ it('rejects by country pattern, not merely by digit count', () => {
177
+ // Both are eight digits — the length the field used to accept unconditionally.
178
+ assert.equal(isValidPhone('12345678', 'NO'), false)
179
+ assert.equal(isValidPhone('00000000', 'NO'), false)
180
+ })
181
+
182
+ it('accepts a short number where the plan is genuinely short', () => {
183
+ // The same rule in the accepting direction, which the case above does not cover: six digits
184
+ // is not a number in Norway and is one in the Faroes and Greenland — both reachable from
185
+ // the picker, and both plausible for a Nordic customer. Raising PHONE_MIN_DIGITS to the
186
+ // Norwegian length would read as a tidy-up and would silently lock those subscribers out.
187
+ assert.equal(isValidPhone('211234', 'FO'), true)
188
+ assert.equal(isValidPhone('221234', 'GL'), true)
189
+ assert.equal(isValidPhone('123456', 'NO'), false)
190
+ })
191
+
192
+ it('rejects numbers outside the E.164 length bounds', () => {
193
+ assert.equal(isValidPhone('123', 'NO'), false)
194
+ assert.equal(isValidPhone('1234567890123456', 'NO'), false)
195
+ })
196
+
197
+ it('accepts a number that is valid elsewhere but not for the selected country', () => {
198
+ assert.equal(isValidPhone('2015550123', 'NO'), false)
199
+ })
200
+
201
+ it('ignores mask separators', () => {
202
+ assert.equal(isValidPhone('48 01 23 45', 'NO'), true)
203
+ })
204
+ })
205
+
206
+ describe('isValidPhoneValue', () => {
207
+ it('resolves the country from the stored value', () => {
208
+ assert.equal(isValidPhoneValue('+12015550123'), true)
209
+ assert.equal(isValidPhoneValue('+4712345678'), false)
210
+ })
211
+
212
+ it('validates a legacy bare number against the fallback country', () => {
213
+ assert.equal(isValidPhoneValue('48012345', 'NO'), true)
214
+ assert.equal(isValidPhoneValue('12345678', 'NO'), false)
215
+ })
216
+
217
+ it('treats an empty value as invalid — emptiness is the required rule job', () => {
218
+ assert.equal(isValidPhoneValue(''), false)
219
+ assert.equal(isValidPhoneValue(null), false)
220
+ })
221
+ })
222
+
223
+ describe('formatPhoneForDisplay', () => {
224
+ it('renders the international form for read-only views', () => {
225
+ assert.equal(formatPhoneForDisplay('+4748012345'), '+47 48 01 23 45')
226
+ assert.equal(formatPhoneForDisplay('+12015550123'), '+1 201 555 0123')
227
+ })
228
+
229
+ it('upgrades a legacy bare number to the international form', () => {
230
+ assert.equal(formatPhoneForDisplay('48012345', 'NO'), '+47 48 01 23 45')
231
+ })
232
+
233
+ it('returns an empty string for an empty value', () => {
234
+ assert.equal(formatPhoneForDisplay(''), '')
235
+ assert.equal(formatPhoneForDisplay(null), '')
236
+ })
237
+
238
+ it('shows a value it cannot resolve exactly as stored, inventing no country', () => {
239
+ // The 13 rows measured 2026-09-14 whose country is not derivable, plus the junk classes.
240
+ // `'+47 0701234567'` is what this used to render for the first of them: the fallback region
241
+ // presented as fact, on digits that are not Norwegian (invariant 7).
242
+ assert.equal(formatPhoneForDisplay('0701234567', 'NO'), '0701234567')
243
+ assert.equal(formatPhoneForDisplay('37376002949', 'NO'), '37376002949')
244
+ assert.equal(formatPhoneForDisplay('12345678', 'NO'), '12345678')
245
+ assert.equal(formatPhoneForDisplay('ana@example.com', 'NO'), 'ana@example.com')
246
+ })
247
+
248
+ it('resolves the same digits once the country is known', () => {
249
+ // The same number, stored after a therapist picked Sweden. Nothing was guessed here.
250
+ assert.equal(formatPhoneForDisplay('+46701234567'), '+46 70 123 45 67')
251
+ })
252
+ })
253
+
254
+ describe('phoneTelHref', () => {
255
+ it('is the only place the tel: scheme is added (ASMA-7485)', () => {
256
+ assert.equal(phoneTelHref('+4748012345'), 'tel:+4748012345')
257
+ assert.equal(phoneTelHref('48012345', 'NO'), 'tel:+4748012345')
258
+ })
259
+
260
+ it('carries no spaces or dashes', () => {
261
+ assert.equal(phoneTelHref('48 01 23 45', 'NO'), 'tel:+4748012345')
262
+ })
263
+
264
+ it('returns an empty string when there is nothing to dial', () => {
265
+ assert.equal(phoneTelHref(''), '')
266
+ assert.equal(phoneTelHref(null), '')
267
+ })
268
+
269
+ it('still dials a value it cannot resolve, using the digits as stored', () => {
270
+ // These stay reachable on purpose — a therapist has to be able to ring them — but the link
271
+ // carries what the column holds, not a country composed for it. `tel:+470701234567` was
272
+ // offered here: a Swedish mobile with a Norwegian code welded on, a different subscriber.
273
+ assert.equal(phoneTelHref('0701234567', 'NO'), 'tel:0701234567')
274
+ assert.equal(phoneTelHref('37376002949', 'NO'), 'tel:37376002949')
275
+ assert.equal(phoneTelHref('12345678', 'NO'), 'tel:12345678')
276
+ })
277
+
278
+ it('keeps a stored `+` and never invents one', () => {
279
+ // Invalid under NO, but the value names its own code, so the link repeats it verbatim.
280
+ assert.equal(phoneTelHref('+4712345678', 'NO'), 'tel:+4712345678')
281
+ assert.equal(phoneTelHref('47 01 23 45 67', 'NO'), 'tel:4701234567')
282
+ })
283
+
284
+ it('offers no link when the value holds no number at all', () => {
285
+ assert.equal(phoneTelHref('ana@example.com', 'NO'), '')
286
+ assert.equal(phoneTelHref('ukjent', 'NO'), '')
287
+ assert.equal(phoneTelHref('123', 'NO'), '')
288
+ })
289
+
290
+ it('still dials a legacy bare number that is valid for the fallback country', () => {
291
+ assert.equal(phoneTelHref('48012345', 'NO'), 'tel:+4748012345')
292
+ })
293
+ })
294
+
295
+ /**
296
+ * Phase 7 invariants (plan §2, TASK-030). These are the properties the production corpus is run
297
+ * against; the cases here are the shape classes measured on 2026-09-14, so a regression shows up
298
+ * in CI rather than only in a corpus run someone has to remember to do.
299
+ *
300
+ * No real number appears here (CON-008). Fixtures are the ticket's example and libphonenumber's.
301
+ */
302
+ describe('REQ-007 — the two parsers agree (TEST-009)', () => {
303
+ const REGION: PhoneCountry = 'NO'
304
+
305
+ // One value per measured shape class, plus the shapes that only exist in the ambiguous tail.
306
+ const corpus = [
307
+ '45456565',
308
+ '48012345',
309
+ '+4745456565',
310
+ '4745456565',
311
+ '004745456565',
312
+ '+47 454 56 565',
313
+ '+46701234567',
314
+ '+37379094538',
315
+ '37376002949',
316
+ '0701234567',
317
+ '12345678',
318
+ '00000000',
319
+ '123',
320
+ 'ana@example.com',
321
+ '',
322
+ ]
323
+
324
+ for (const value of corpus) {
325
+ it(`agrees on ${value === '' ? '(empty)' : value}`, () => {
326
+ const loose = parsePhoneValue(value, REGION)
327
+ const strict = parsePhoneNr(value, REGION)
328
+
329
+ if (strict.ok) {
330
+ assert.equal(loose.country, strict.country)
331
+ return
332
+ }
333
+
334
+ // The failure this guards: reporting the fallback as though the value named it. A
335
+ // therapist would see a Norwegian flag on a foreign number and "correct" it into
336
+ // somebody else's valid Norwegian one.
337
+ assert.notEqual(
338
+ loose.country,
339
+ REGION,
340
+ `parsePhoneValue claimed ${REGION} for a value parsePhoneNr rejects: ${value}`,
341
+ )
342
+ })
343
+ }
344
+
345
+ it('still offers a country to edit with even when it does not know one', () => {
346
+ // `iso2` and `country` answer different questions, and the field needs both.
347
+ const parsed = parsePhoneValue('37376002949', 'NO')
348
+
349
+ assert.equal(parsed.iso2, 'NO')
350
+ assert.equal(parsed.country, undefined)
351
+ })
352
+ })
353
+
354
+ describe('REQ-007 — idempotence (TEST-010)', () => {
355
+ const shapes: [string, PhoneCountry][] = [
356
+ ['45456565', 'NO'],
357
+ ['+4745456565', 'NO'],
358
+ ['4745456565', 'NO'],
359
+ ['004745456565', 'NO'],
360
+ ['+47 454 56 565', 'NO'],
361
+ ['0701234567', 'SE'],
362
+ ['+46701234567', 'SE'],
363
+ ['2015550123', 'US'],
364
+ ]
365
+
366
+ for (const [value, region] of shapes) {
367
+ it(`normalising ${value} twice changes nothing the second time`, () => {
368
+ const once = toE164(parsePhoneValue(value, region).national, region)
369
+ const twice = toE164(parsePhoneValue(once, region).national, region)
370
+
371
+ // A failure here means the Adopus sync rewrites the same row forever (RISK-001).
372
+ assert.equal(twice, once)
373
+ })
374
+ }
375
+
376
+ it('is idempotent through parsePhoneNr as well', () => {
377
+ const first = parsePhoneNr('004745456565', 'NO')
378
+ assert.equal(first.ok, true)
379
+
380
+ const second = parsePhoneNr(first.ok ? first.e164 : '', 'NO')
381
+ assert.equal(second.ok && second.e164, first.ok && first.e164)
382
+ })
383
+ })
384
+
385
+ describe('REQ-007 — no value gains validity (TEST-011)', () => {
386
+ it('does not turn a Swedish national number into a Norwegian one', () => {
387
+ // `0701234567` is a real Swedish mobile written nationally. Under NO it is nothing, and it
388
+ // must stay nothing: the plus-less branch has no way to learn the country, and guessing
389
+ // would silently hand the SMS to whoever owns the Norwegian number it invents.
390
+ assert.equal(parsePhoneNr('0701234567', 'NO').ok, false)
391
+ assert.equal(isValidPhone('0701234567', 'NO'), false)
392
+ })
393
+
394
+ it('does not resolve a plus-less Moldovan number to Norway', () => {
395
+ // Measured 2026-09-14: `parsePhoneNumberWithError('37376002949', 'NO')` yields
396
+ // `+4737376002949` with `isValid() === false` — not Moldova. The INVALID verdict is what
397
+ // stops that string being stored as a Norwegian number.
398
+ const parsed = parsePhoneNr('37376002949', 'NO')
399
+
400
+ assert.equal(parsed.ok, false)
401
+ assert.equal(parsed.ok === false && parsed.reason, 'INVALID')
402
+ })
403
+
404
+ it('keeps junk invalid rather than prefixing it', () => {
405
+ for (const junk of ['12345678', '00000000', '123', 'ana@example.com']) {
406
+ assert.equal(parsePhoneNr(junk, 'NO').ok, false, junk)
407
+ }
408
+ })
409
+ })
410
+
411
+ describe('REQ-007 — one case per measured shape class (TEST-012)', () => {
412
+ // The classes and their production counts, measured 2026-09-14 (plan §0.2).
413
+ const classes: [string, string, string | 'INVALID'][] = [
414
+ ['bare 8 digits · 90,190 rows', '45456565', '+4745456565'],
415
+ ['already E.164 +47 · 1,730 rows', '+4745456565', '+4745456565'],
416
+ ['47 + 8, no plus · 268 rows', '4745456565', '+4745456565'],
417
+ ['contains separators · 127 rows', '+47 454 56 565', '+4745456565'],
418
+ ['E.164, not Norway · 25 rows', '+37379094538', '+37379094538'],
419
+ ['1–7 digits · 16 rows', '123456', 'INVALID'],
420
+ ['0047 + 8 · 9 rows', '004745456565', '+4745456565'],
421
+ ['9+ digits, no plus · 9 rows', '37376002949', 'INVALID'],
422
+ ['leading zero national · 4 rows', '0745456565', 'INVALID'],
423
+ ['contains @ · 26 rows', 'ana@example.com', 'INVALID'],
424
+ ]
425
+
426
+ for (const [label, value, expected] of classes) {
427
+ it(label, () => {
428
+ const parsed = parsePhoneNr(value, 'NO')
429
+
430
+ if (expected === 'INVALID') {
431
+ assert.equal(parsed.ok, false, `${value} should not parse`)
432
+ return
433
+ }
434
+
435
+ assert.equal(parsed.ok && parsed.e164, expected)
436
+ })
437
+ }
438
+ })
439
+
440
+ describe('REQ-007 — the shared-plan collision is known, not solved (TEST-013)', () => {
441
+ it('reads the same digits as two different valid numbers depending on the region', () => {
442
+ // `20123456` is a valid Norwegian landline and a valid Danish mobile. Nothing in the digits
443
+ // distinguishes them, so the region must come from an explicit selection wherever a foreign
444
+ // number is possible — never from a default. This test documents the limit; it does not
445
+ // pretend the limit is gone.
446
+ const asNorwegian = parsePhoneNr('20123456', 'NO')
447
+ const asDanish = parsePhoneNr('20123456', 'DK')
448
+
449
+ assert.equal(asNorwegian.ok, true)
450
+ assert.equal(asNorwegian.ok && asNorwegian.e164, '+4720123456')
451
+
452
+ assert.equal(asDanish.ok, true)
453
+ assert.equal(asDanish.ok && asDanish.e164, '+4520123456')
454
+ })
455
+
456
+ it('is why a default region may never stand in for a user choice', () => {
457
+ // The same string, stored bare, is unrecoverable: both readings are valid numbers owned by
458
+ // different people. `parsePhoneValue` must therefore not claim a country for it.
459
+ assert.equal(parsePhoneValue('20123456', 'NO').country, 'NO')
460
+ assert.equal(parsePhoneValue('20123456', 'DK').country, 'DK')
461
+ })
462
+ })