diff --git a/src/dlr.ts b/src/dlr.ts index 6931594..6b90bef 100644 --- a/src/dlr.ts +++ b/src/dlr.ts @@ -66,12 +66,13 @@ const patterns = { stat: field('stat'), sub: field('sub'), submitDate: field('submit date'), - // The last field, and the only one that may hold a space: it carries the message's own start. + // The one field that may hold a space, carrying the message's own start, so it ends at its line. text: /\btext:([^\r\n]*)/i, }; function toNumber(value: string | undefined): number | undefined { - if (value === undefined) return undefined; + // Number('') is 0, which would report a receipt that stated no count as one that stated none sent. + if (value === undefined || value === '') return undefined; const parsed = Number(value); diff --git a/test/dlr.test.ts b/test/dlr.test.ts index 73e0b2a..8aef21e 100644 --- a/test/dlr.test.ts +++ b/test/dlr.test.ts @@ -61,7 +61,7 @@ describe('parseReceipt()', () => { }); test('leaves absent fields undefined rather than guessing', () => { - const receipt = parseReceipt('id:abc stat:UNDELIV'); + const receipt = parseReceipt('id:abc sub: stat:UNDELIV'); assert.equal(receipt.id, 'abc'); assert.equal(receipt.stat, 'UNDELIV'); @@ -94,6 +94,7 @@ describe('dlrFromPdu()', () => { assert.equal(dlr.statusId, 2); assert.equal(dlr.errorCode, '000'); assert.equal(dlr.doneDate?.toISOString(), '2025-08-25T14:31:00.000Z'); + assert.equal(dlrFromPdu(deliverSm('id:beef-1\r\nstat:DELIVRD'))?.smsId, 'beef-1'); }); test('maps every spec status code back to its message state and id', () => {