app

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

message-template.test.ts (4662B)


      1 import { expect, it } from 'vitest';
      2 import {
      3   messageFromWireParts,
      4   messageToWireParts
      5 } from '../../src/lib/contracts/message-v1/index.ts';
      6 import { buildUnsignedMessageTemplate } from '../../src/lib/nostr/message-template.ts';
      7 import { inspectLocalTemplate } from '../../src/lib/nostr/local-template.ts';
      8 const peer = '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798';
      9 const parent = '1'.repeat(64);
     10 const input = (extra: Record<string, unknown> = {}) =>
     11   JSON.stringify({
     12     recipients: [{ public_key: peer, relay_url: null }],
     13     content: 'Could I buy carrots?\nPlease reply here.',
     14     reply_to: null,
     15     subject: null,
     16     ...extra
     17   });
     18 it('first enquiry is readable unsigned14 with no reply parent or new order schema', () => {
     19   const value = buildUnsignedMessageTemplate(input());
     20   expect(value).toEqual({
     21     kind: 14,
     22     tags: [['p', peer]],
     23     content: 'Could I buy carrots?\nPlease reply here.'
     24   });
     25   expect(value).not.toHaveProperty('sig');
     26   expect(value).not.toHaveProperty('pubkey');
     27   expect(value).not.toHaveProperty('id');
     28   expect(
     29     inspectLocalTemplate(
     30       JSON.stringify({ ...value, pubkey: peer, created_at: 100 }),
     31       peer,
     32       14
     33     )
     34   ).toBeUndefined();
     35   expect(
     36     inspectLocalTemplate(
     37       JSON.stringify({ ...value, kind: 13, pubkey: peer, created_at: 100 }),
     38       peer,
     39       13
     40     )
     41   ).toBeUndefined();
     42 });
     43 it('reply parent and optional subject use the exact shared p/e/subject fields', () => {
     44   const raw = input({
     45     reply_to: { id: parent, relays: 'wss://hint.example.org' },
     46     subject: 'Carrots'
     47   });
     48   const parts = messageToWireParts(raw);
     49   expect(parts?.tags).toEqual([
     50     ['p', peer],
     51     ['e', parent, 'wss://hint.example.org'],
     52     ['subject', 'Carrots']
     53   ]);
     54   expect(messageFromWireParts(JSON.stringify(parts))).toEqual(JSON.parse(raw));
     55 });
     56 it('canonical public keys and parent hashes go beyond generic nonempty codec checks', () => {
     57   for (const public_key of ['alice', peer.toUpperCase(), 'f'.repeat(64)])
     58     expect(
     59       messageToWireParts(input({ recipients: [{ public_key }] }))
     60     ).toBeUndefined();
     61   for (const id of [
     62     'product',
     63     parent.toUpperCase().replace('1', 'A'),
     64     '0'.repeat(63)
     65   ])
     66     expect(messageToWireParts(input({ reply_to: { id } }))).toBeUndefined();
     67 });
     68 it('body limits count UTF8 without trimming or truncating the shared text', () => {
     69   for (const content of [
     70     'a'.repeat(4096),
     71     'é'.repeat(2048),
     72     '\ufeff',
     73     '\u001c'
     74   ])
     75     expect(messageToWireParts(input({ content }))?.content).toBe(content);
     76   for (const content of [
     77     'a'.repeat(4097),
     78     'é'.repeat(2049),
     79     '',
     80     ' \n\u0085',
     81     '\ud800'
     82   ])
     83     expect(messageToWireParts(input({ content }))).toBeUndefined();
     84 });
     85 it('encoded parts limits apply in addition to the four KiB body limit', () => {
     86   expect(
     87     messageToWireParts(input({ content: '"'.repeat(4096) }))
     88   ).toBeUndefined();
     89   expect(
     90     messageToWireParts(
     91       input({ content: 'a'.repeat(4096), subject: 'b'.repeat(4096) })
     92     )
     93   ).toBeUndefined();
     94   expect(
     95     messageFromWireParts(
     96       JSON.stringify({
     97         kind: 14,
     98         tags: [['p', peer]],
     99         content: 'a'.repeat(8193)
    100       })
    101     )
    102   ).toBeUndefined();
    103 });
    104 it('authoring cannot add an order tag or replace plain text with a JSON body object', () => {
    105   expect(messageToWireParts(input({ order: 'new-protocol' }))).toBeUndefined();
    106   expect(messageToWireParts(input({ tags: [['order', 'x']] }))).toBeUndefined();
    107   expect(
    108     messageToWireParts(input({ content: { order: 'x' } }))
    109   ).toBeUndefined();
    110   const value = messageToWireParts(
    111     JSON.stringify({
    112       recipients: [{ public_key: peer }],
    113       content: 'ordinary text'
    114     })
    115   );
    116   expect(value?.tags).toEqual([['p', peer]]);
    117   expect(messageFromWireParts(JSON.stringify(value))?.reply_to).toBeNull();
    118 });
    119 it('reader matches first optional-tag semantics and ignores unknown tags without granting context', () => {
    120   const value = messageFromWireParts(
    121     JSON.stringify({
    122       kind: 14,
    123       content: 'hello',
    124       tags: [
    125         ['order', 'x'],
    126         ['p', peer],
    127         ['e', parent],
    128         ['e'],
    129         ['subject', 'first'],
    130         ['subject', 'second']
    131       ]
    132     })
    133   );
    134   expect(value?.reply_to).toEqual({ id: parent, relays: null });
    135   expect(value?.subject).toBe('first');
    136   expect(value).not.toHaveProperty('order');
    137   expect(
    138     messageFromWireParts(
    139       JSON.stringify({ kind: 14, content: 'hello', tags: [['p', peer], ['p']] })
    140     )
    141   ).toBeUndefined();
    142   expect(
    143     messageFromWireParts(
    144       JSON.stringify({ kind: 15, content: 'hello', tags: [['p', peer]] })
    145     )
    146   ).toBeUndefined();
    147 });