import test from 'node:test'; import assert from 'node:assert/strict'; import { parseQuantity } from './quantity.mjs'; test('accepted values and ECMAScript surrounding whitespace', () => { for (const [input, quantity] of [ ['1', 1], ['12', 12], ['1000', 1000], [' 12 ', 12], ['\t12\n', 12], ['\u00a012\u00a0', 12], ]) { assert.deepEqual(parseQuantity(input), { ok: true, quantity }, `accept:${JSON.stringify(input)}`); } }); test('non-string inputs keep the type error', () => { for (const input of [null, undefined, 12, true, {}, ['12']]) { assert.deepEqual(parseQuantity(input), { ok: false, error: 'quantity_must_be_text', }, `type:${JSON.stringify(input)}`); } }); test('malformed strings keep the format error', () => { for (const input of [ '', ' ', '0', '-1', '+1', '01', '1.0', '1.5', '12boxes', '99999', '1 2', '1\n2', '\u200b12', '12', '١٢', ]) { assert.deepEqual(parseQuantity(input), { ok: false, error: 'quantity_must_be_integer', }, `reject:${JSON.stringify(input)}`); } }); test('four-digit values above the product limit keep the range error', () => { for (const input of ['1001', '9999']) { assert.deepEqual(parseQuantity(input), { ok: false, error: 'quantity_out_of_range', }, `range:${JSON.stringify(input)}`); } });