app

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

navigation-copy.test.ts (2321B)


      1 import { afterEach, describe, expect, it, vi } from 'vitest';
      2 import { copyProductReference } from '../../src/lib/navigation-copy.ts';
      3 import { encodeProductReference } from '../../src/lib/nostr/references.ts';
      4 afterEach(() => vi.unstubAllGlobals());
      5 describe('explicit product reference copy', () => {
      6   it('copies only canonical public route and reports actual success', async () => {
      7     const writeText = vi.fn().mockResolvedValue(undefined);
      8     vi.stubGlobal('navigator', { clipboard: { writeText } });
      9     const naddr = encodeProductReference({
     10       kind: 30402,
     11       pubkey: 'ab'.repeat(32),
     12       identifier: 'carrots'
     13     })!;
     14     expect(
     15       await copyProductReference(naddr.toUpperCase(), 'https://harvest.example')
     16     ).toBe(true);
     17     expect(writeText).toHaveBeenCalledExactlyOnceWith(
     18       `https://harvest.example/products/${naddr}`
     19     );
     20   });
     21   it('reports denial/unavailable and rejects invalid input before effects', async () => {
     22     const writeText = vi.fn().mockRejectedValue(new Error('denied'));
     23     vi.stubGlobal('navigator', { clipboard: { writeText } });
     24     expect(
     25       await copyProductReference(
     26         'https://bad.example',
     27         'https://harvest.example'
     28       )
     29     ).toBe(false);
     30     expect(writeText).not.toHaveBeenCalled();
     31     const naddr = encodeProductReference({
     32       kind: 30402,
     33       pubkey: 'ab'.repeat(32),
     34       identifier: 'carrots'
     35     })!;
     36     expect(await copyProductReference(naddr, 'https://harvest.example')).toBe(
     37       false
     38     );
     39     vi.stubGlobal('navigator', undefined);
     40     expect(await copyProductReference(naddr, 'https://harvest.example')).toBe(
     41       false
     42     );
     43   });
     44   it('rejects noncanonical, credential, remote HTTP and javascript origins before copying', async () => {
     45     const writeText = vi.fn();
     46     vi.stubGlobal('navigator', { clipboard: { writeText } });
     47     const naddr = encodeProductReference({
     48       kind: 30402,
     49       pubkey: 'ab'.repeat(32),
     50       identifier: 'carrots'
     51     })!;
     52     for (const origin of [
     53       'https://host.example/',
     54       'https://user:pass@host.example',
     55       'http://host.example',
     56       'javascript:alert(1)',
     57       'https://host.example?secret=1',
     58       undefined
     59     ])
     60       expect(await copyProductReference(naddr, origin)).toBe(false);
     61     expect(writeText).not.toHaveBeenCalled();
     62   });
     63 });