app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

food-values.test.ts (6367B)


      1 import { describe, expect, it, vi } from 'vitest';
      2 import {
      3   canonicalFoodAmount,
      4   foodCurrency,
      5   foodPrice,
      6   foodQuantity,
      7   foodUnit,
      8   foodUnits
      9 } from '../../src/lib/contracts/food-availability-v1/values';
     10 import {
     11   boundedEnvelopeNumbers,
     12   foodPublishedAt,
     13   safeUnsignedInteger
     14 } from '../../src/lib/nostr/envelope-bounds';
     15 import corpus from '../../../contracts/interop/food_availability/corpus.v1.json';
     16 
     17 describe('exact Food decimal values from pinned public Rust', () => {
     18   for (const vector of corpus.vectors.slice(0, 10))
     19     it(`preserves actual authored ${vector.id} values without numeric conversion`, () => {
     20       const details = vector.input.details!;
     21       expect(foodUnit(details.price.unit)).toBe(details.price.unit);
     22       expect(
     23         foodPrice(
     24           details.price.amount,
     25           details.price.currency,
     26           details.price.unit
     27         )
     28       ).toEqual(details.price);
     29       if (details.quantity)
     30         expect(
     31           foodQuantity(details.quantity.amount, details.quantity.unit)
     32         ).toEqual(details.quantity);
     33       else expect(details.quantity).toBeNull();
     34     });
     35 
     36   it('has exactly the ten pinned public units with no unknown fallback', () => {
     37     expect(foodUnits).toEqual([
     38       'g',
     39       'kg',
     40       'lb',
     41       'oz',
     42       'each',
     43       'dozen',
     44       'bunch',
     45       'punnet',
     46       'bag',
     47       'basket'
     48     ]);
     49     for (const value of ['KG', 'lbs', 'item', '', ' kg', undefined, 1])
     50       expect(foodUnit(value)).toBeUndefined();
     51   });
     52 
     53   for (const amount of [
     54     '0',
     55     '1',
     56     '0.01',
     57     '1234567890123456789012345678',
     58     '12345678901234.56789012345678',
     59     '0.000000000000000000000000001'
     60   ])
     61     it(`retains exact canonical decimal ${amount}`, () => {
     62       expect(canonicalFoodAmount(amount)).toBe(amount);
     63       expect(foodPrice(amount, 'CAD', 'lb')?.amount).toBe(amount);
     64     });
     65 
     66   for (const amount of [
     67     '',
     68     '01',
     69     '00',
     70     '.1',
     71     '1.',
     72     '1.0',
     73     '0.0',
     74     '1.20',
     75     '+1',
     76     '-1',
     77     '-0',
     78     '1e2',
     79     '1E-2',
     80     '1,000',
     81     '1_000',
     82     '1 000',
     83     ' 1',
     84     '1 ',
     85     '1\n',
     86     '1',
     87     '١',
     88     '0x10',
     89     'NaN',
     90     'Infinity',
     91     '12345678901234567890123456789',
     92     '0.0000000000000000000000000001'
     93   ])
     94     it(`rejects noncanonical or oversized decimal ${JSON.stringify(amount)}`, () => {
     95       expect(canonicalFoodAmount(amount)).toBeUndefined();
     96       expect(foodPrice(amount, 'CAD', 'lb')).toBeUndefined();
     97       expect(foodQuantity(amount, 'lb')).toBeUndefined();
     98     });
     99 
    100   it('keeps missing price distinct from a valid zero price and rejects zero quantity', () => {
    101     expect(canonicalFoodAmount(undefined)).toBeUndefined();
    102     expect(canonicalFoodAmount('0')).toBe('0');
    103     expect(foodPrice('0', 'CAD', 'lb')).toEqual({
    104       amount: '0',
    105       currency: 'CAD',
    106       unit: 'lb'
    107     });
    108     expect(foodQuantity('0', 'lb')).toBeUndefined();
    109     expect(foodQuantity(undefined, 'lb')).toBeUndefined();
    110     expect(foodQuantity('0.01', 'lb')).toEqual({ amount: '0.01', unit: 'lb' });
    111   });
    112 
    113   it('uses exactly three uppercase ASCII currency letters without inventing an ISO registry', () => {
    114     for (const value of ['CAD', 'USD', 'EUR', 'AAA'])
    115       expect(foodCurrency(value)).toBe(value);
    116     for (const value of [
    117       '',
    118       'CA',
    119       'CADD',
    120       'cad',
    121       ' Cad',
    122       'CAD\n',
    123       'C1D',
    124       'CAD',
    125       null
    126     ]) {
    127       expect(foodCurrency(value)).toBeUndefined();
    128       expect(foodPrice('1', value, 'lb')).toBeUndefined();
    129     }
    130     expect(foodPrice('1', 'CAD', 'unknown')).toBeUndefined();
    131     expect(foodQuantity('1', 'unknown')).toBeUndefined();
    132   });
    133 
    134   it('rejects huge strings and nonstrings before coercion or normalization', () => {
    135     const toString = vi.fn(() => '1');
    136     for (const value of [
    137       '1'.repeat(1024 * 1024),
    138       { toString },
    139       1,
    140       1n,
    141       null,
    142       Symbol('amount')
    143     ]) {
    144       expect(canonicalFoodAmount(value)).toBeUndefined();
    145       expect(foodCurrency(value)).toBeUndefined();
    146       expect(foodUnit(value)).toBeUndefined();
    147     }
    148     expect(toString).not.toHaveBeenCalled();
    149   });
    150 });
    151 
    152 describe('bounded browser numeric envelopes without an authenticity claim', () => {
    153   for (const value of [0, 1, 4294967296, Number.MAX_SAFE_INTEGER])
    154     it(`accepts exact nonnegative safe integer ${value}`, () => {
    155       expect(safeUnsignedInteger(value)).toBe(value);
    156       expect(boundedEnvelopeNumbers(30402, value)).toEqual({
    157         kind: 30402,
    158         created_at: value
    159       });
    160     });
    161 
    162   for (const value of [
    163     -0,
    164     -1,
    165     0.5,
    166     NaN,
    167     Infinity,
    168     -Infinity,
    169     Number.MAX_SAFE_INTEGER + 1,
    170     '1',
    171     1n,
    172     null,
    173     undefined
    174   ])
    175     it(`rejects unsupported numeric input ${String(value)}`, () => {
    176       expect(safeUnsignedInteger(value)).toBeUndefined();
    177       expect(boundedEnvelopeNumbers(30402, value)).toBeUndefined();
    178       expect(boundedEnvelopeNumbers(value, 1)).toBeUndefined();
    179     });
    180 
    181   it('bounds kind to the frozen NIP-01 unsigned 16-bit range', () => {
    182     expect(boundedEnvelopeNumbers(0, 0)).toEqual({ kind: 0, created_at: 0 });
    183     expect(boundedEnvelopeNumbers(65535, 1)).toEqual({
    184       kind: 65535,
    185       created_at: 1
    186     });
    187     expect(boundedEnvelopeNumbers(65536, 1)).toBeUndefined();
    188   });
    189 
    190   it('rejects a parsed maximum-u64 event timestamp instead of silently rounding', () => {
    191     const raw = JSON.parse(
    192       '{"kind":30402,"created_at":18446744073709551615}'
    193     ) as { kind: number; created_at: number };
    194     expect(Number.isSafeInteger(raw.created_at)).toBe(false);
    195     expect(boundedEnvelopeNumbers(raw.kind, raw.created_at)).toBeUndefined();
    196   });
    197 
    198   for (const value of ['1', '4294967296', '9007199254740991'])
    199     it(`accepts supported exact positive published_at ${value}`, () => {
    200       expect(foodPublishedAt(value)).toBe(Number(value));
    201       expect(String(foodPublishedAt(value))).toBe(value);
    202     });
    203 
    204   for (const value of [
    205     '0',
    206     '01',
    207     '+1',
    208     '-1',
    209     '1.0',
    210     '1e2',
    211     ' 1',
    212     '1 ',
    213     '1\n',
    214     '١',
    215     '9007199254740992',
    216     '9007199254740993',
    217     '18446744073709551615',
    218     '9'.repeat(1024 * 1024),
    219     1,
    220     null
    221   ])
    222     it(`rejects invalid or unsupported published_at ${typeof value === 'string' && value.length > 40 ? 'oversized string' : JSON.stringify(value)}`, () => {
    223       expect(foodPublishedAt(value)).toBeUndefined();
    224     });
    225 });