Refuse what we cannot send: an alphabet that cannot carry the message, and a time nobody can read (#97)
* Regression tests for a named alphabet and a time that cannot carry the send * Refuse a message the named alphabet cannot carry, and a time nobody can read * Close the architecture review: a builder that cannot drop a time, and docs that hold * Refuse a time of no known kind, and clamp a relative period to what the format holds * Reflow the decision record and name the clamp in its test
This commit is contained in:
+25
-5
@@ -234,25 +234,45 @@ describe('smppDate()', () => {
|
||||
|
||||
describe('smppTime', () => {
|
||||
test('encodes an absolute time', () => {
|
||||
assert.equal(smppTime.encode(new Date(Date.UTC(2026, 7, 25, 14, 30, 0))), '260825143000000+');
|
||||
assert.equal(smppTime.encode(new Date(Date.UTC(2026, 7, 25, 14, 30, 0))).text, '260825143000000+');
|
||||
});
|
||||
|
||||
test('encodes a relative time given in seconds', () => {
|
||||
assert.equal(smppTime.encode(3600), '000000010000000R');
|
||||
test('encodes a relative time given in seconds, clamped to what the format holds', () => {
|
||||
assert.equal(smppTime.encode(3600).text, '000000010000000R');
|
||||
assert.equal(smppTime.encode(99 * 86400).text, '000099000000000R');
|
||||
assert.equal(smppTime.encode(100 * 86400).text, '000099235959000R');
|
||||
});
|
||||
|
||||
test('passes an already-formatted string through', () => {
|
||||
assert.equal(smppTime.encode('260825143000000+'), '260825143000000+');
|
||||
assert.equal(smppTime.encode('260825143000000+').text, '260825143000000+');
|
||||
});
|
||||
|
||||
test('decodes an absolute time back to the same instant', () => {
|
||||
const when = new Date(Date.UTC(2026, 7, 25, 14, 30, 0));
|
||||
const { err, date } = smppTime.decode(smppTime.encode(when));
|
||||
const encoded = smppTime.encode(when);
|
||||
|
||||
assert.ok(encoded.text);
|
||||
|
||||
const { err, date } = smppTime.decode(encoded.text);
|
||||
|
||||
assert.equal(err, undefined);
|
||||
assert.equal(date.toISOString(), when.toISOString());
|
||||
});
|
||||
|
||||
test('reports a time it cannot express rather than encoding it as a string of NaNs', () => {
|
||||
for (const value of [new Date('nope'), NaN, Infinity, -Infinity]) {
|
||||
const encoded = smppTime.encode(value);
|
||||
|
||||
assert.ok(encoded.err instanceof Error, String(value));
|
||||
assert.equal(encoded.text, undefined);
|
||||
}
|
||||
|
||||
const invalidDate = smppTime.encode(new Date('nope'));
|
||||
|
||||
assert.ok(invalidDate.err);
|
||||
assert.match(invalidDate.err.message, /invalid Date/);
|
||||
});
|
||||
|
||||
test('reports malformed input rather than returning an invalid date', () => {
|
||||
assert.ok(smppTime.decode('nonsense').err instanceof Error);
|
||||
assert.ok(smppTime.decode('').err instanceof Error);
|
||||
|
||||
Reference in New Issue
Block a user