app

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

product-details.test.ts (3346B)


      1 import { readFileSync } from 'node:fs';
      2 import { render } from 'svelte/server';
      3 import { describe, expect, it, vi } from 'vitest';
      4 import { incomingAmount } from '../../../src/lib/contracts/food-availability-v1/text.ts';
      5 import ListingFacts from '../../../src/lib/components/ListingFacts.svelte';
      6 import PublisherIdentity from '../../../src/lib/components/PublisherIdentity.svelte';
      7 import Product from '../../../src/routes/products/[naddr=naddr]/+page.svelte';
      8 import { encodeProductReference } from '../../../src/lib/nostr/references.ts';
      9 import {
     10   projectFoodParts,
     11   type FoodParts
     12 } from '../../../src/lib/contracts/food-availability-v1/read.ts';
     13 const host = vi.hoisted(() => ({
     14   params: { naddr: 'invalid' },
     15   url: new URL('https://harvest.example/products/invalid')
     16 }));
     17 vi.mock('$app/state', () => ({ page: host }));
     18 vi.mock('$app/navigation', () => ({
     19   beforeNavigate: vi.fn(),
     20   goto: vi.fn()
     21 }));
     22 const corpus = JSON.parse(
     23   readFileSync(
     24     new URL(
     25       '../../../../contracts/interop/food_availability/corpus.v1.json',
     26       import.meta.url
     27     ),
     28     'utf8'
     29   )
     30 ) as { vectors: { id: string; signed_wires: Record<string, string> }[] };
     31 const event = JSON.parse(
     32   corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous
     33 ) as FoodParts;
     34 const projected = projectFoodParts(event);
     35 if (projected.outcome !== 'focused') throw Error('fixture food');
     36 describe('truthful public detail presentation', () => {
     37   it('renders escaped exact advertised values without remote media or link discovery', () => {
     38     const food = {
     39       ...projected.projection,
     40       content: '<img src=https://track.example> mailto:private@example.org',
     41       price: {
     42         ...projected.projection.price,
     43         amount: incomingAmount('4.0000000000000000000001')!
     44       },
     45       quantity: null
     46     };
     47     const result = render(ListingFacts, {
     48       props: { food, createdAt: 999999999999999 }
     49     }).body;
     50     expect(result).toContain('4.0000000000000000000001');
     51     expect(result).toContain('Advertised quantity not specified');
     52     expect(result).toContain('&lt;img');
     53     expect(result).toContain('seconds since the Unix epoch');
     54     expect(result).not.toMatch(/<img|<a|<iframe|<script/);
     55   });
     56   it('discloses asserted name and key without managed identity or farm certification', () => {
     57     const publisher = {
     58       pubkey: 'ab'.repeat(32),
     59       label: '<img src=https://track.example>',
     60       assertedName: true
     61     };
     62     const result = render(PublisherIdentity, { props: { publisher } }).body;
     63     expect(result).toContain(publisher.pubkey);
     64     expect(result).toContain('asserted by the publisher');
     65     expect(result).toContain('&lt;img');
     66     expect(result).not.toMatch(/<img|<a|verified farm|Manage listing/);
     67   });
     68   it('distinguishes invalid from valid checking SSR without reads or fabricated facts', () => {
     69     host.params.naddr = 'invalid';
     70     expect(render(Product).body).toContain('This food reference is invalid.');
     71     host.params.naddr = encodeProductReference({
     72       kind: 30402,
     73       pubkey: 'ab'.repeat(32),
     74       identifier: 'carrots'
     75     })!;
     76     const result = render(Product).body;
     77     expect(result).toContain('Checking food sources');
     78     expect(result).toContain('Copy link');
     79     expect(result).not.toMatch(
     80       /<img|mailto:|tel:|Message seller|Manage listing|Cedar Farm/
     81     );
     82   });
     83 });