app

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

route-inventory.test.ts (9787B)


      1 import SearchForm from '../../src/lib/components/SearchForm.svelte';
      2 import { prerender as editPrerender } from '../../src/routes/products/[naddr=naddr]/edit/+page';
      3 import { params } from '../../src/params';
      4 import { readFileSync, readdirSync } from 'node:fs';
      5 import { fileURLToPath } from 'node:url';
      6 import { render } from 'svelte/server';
      7 import { describe, expect, it, vi } from 'vitest';
      8 
      9 import Sell from '../../src/routes/sell/+page.svelte';
     10 import Selling from '../../src/routes/selling/+page.svelte';
     11 import Draft from '../../src/routes/selling/drafts/[draftId=local_id]/+page.svelte';
     12 import Edit from '../../src/routes/products/[naddr=naddr]/edit/+page.svelte';
     13 import Messages from '../../src/routes/messages/+page.svelte';
     14 import Conversation from '../../src/routes/messages/[conversationId=local_id]/+page.svelte';
     15 import Product from '../../src/routes/products/[naddr=naddr]/+page.svelte';
     16 import About from '../../src/routes/about/+page.svelte';
     17 import Privacy from '../../src/routes/privacy/+page.svelte';
     18 import ErrorPage from '../../src/routes/+error.svelte';
     19 import { prerender as productPrerender } from '../../src/routes/products/[naddr=naddr]/+page';
     20 import { prerender as draftPrerender } from '../../src/routes/selling/drafts/[draftId=local_id]/+page';
     21 import { prerender as conversationPrerender } from '../../src/routes/messages/[conversationId=local_id]/+page';
     22 
     23 const errorState = vi.hoisted(() => ({
     24   status: 404,
     25   error: { message: 'PRIVATE_TEST_ONLY' },
     26   params: { naddr: 'invalid-reference' }
     27 }));
     28 vi.mock('$app/state', () => ({ page: errorState }));
     29 vi.mock('$app/navigation', () => ({
     30   beforeNavigate: vi.fn(),
     31   goto: () => {
     32     throw new Error('SSR must not navigate');
     33   }
     34 }));
     35 
     36 const routeRoot = fileURLToPath(new URL('../../src/routes/', import.meta.url));
     37 const expectedPages = [
     38   '+page.svelte',
     39   'search/+page.svelte',
     40   'products/[naddr=naddr]/+page.svelte',
     41   'sell/+page.svelte',
     42   'selling/+page.svelte',
     43   'selling/drafts/[draftId=local_id]/+page.svelte',
     44   'products/[naddr=naddr]/edit/+page.svelte',
     45   'messages/+page.svelte',
     46   'messages/[conversationId=local_id]/+page.svelte',
     47   'about/+page.svelte',
     48   'privacy/+page.svelte'
     49 ].sort();
     50 
     51 describe('approved route inventory', () => {
     52   it('owns exactly eleven page files with the existing bounded parameter matchers', () => {
     53     const pages = readdirSync(routeRoot, { recursive: true })
     54       .filter((name): name is string => typeof name === 'string')
     55       .filter((name) => name.endsWith('+page.svelte'))
     56       .sort();
     57     expect(pages).toEqual(expectedPages);
     58   });
     59 });
     60 
     61 describe('thin protected route SSR', () => {
     62   const editorTitles: Readonly<Record<string, string>> = {
     63     Sell: 'New listing',
     64     Draft: 'Continue draft',
     65     Edit: 'Edit listing'
     66   };
     67   for (const [name, renderComponent] of Object.entries({
     68     Sell: () => render(Sell),
     69     Selling: () => render(Selling),
     70     Draft: () => render(Draft),
     71     Edit: () => render(Edit),
     72     Messages: () => render(Messages),
     73     Conversation: () => render(Conversation)
     74   })) {
     75     it(
     76       name +
     77         (editorTitles[name]
     78           ? ' renders the shared editor connection gate before protected values or browser effects'
     79           : ' renders only a generic unavailable connection/unlock gate without browser effects'),
     80       () => {
     81         const output = renderComponent();
     82         const editorTitle = editorTitles[name];
     83         if (editorTitle) {
     84           expect(output.head).toContain(
     85             '<title>' + editorTitle + ' ยท HarvestCircle</title>'
     86           );
     87           expect(output.head).toMatch(/name="robots" content="noindex"/);
     88           expect(output.body).toContain('Connect to continue');
     89           expect(output.body).toMatch(
     90             /<button[^>]*disabled[^>]*>Connect extension/
     91           );
     92           expect(output.body).not.toMatch(
     93             /<form|<textarea|<input|<select|PRIVATE_TEST_ONLY|naddr1|Inbox setup|Check current preference/
     94           );
     95           // Original SSR effect tripwires still reject IDB/extension/socket/fetch.
     96           return;
     97         }
     98 
     99         expect(output.head).toContain('<title>HarvestCircle</title>');
    100         expect(output.head).toMatch(/name="robots" content="noindex"/);
    101         if (name === 'Selling') {
    102           expect(output.body).toContain('Connect to continue');
    103           expect(output.body).toContain('href="/sell"');
    104           expect(output.body).toContain('List food');
    105           expect(output.body).toContain('Inbox settings');
    106           expect(output.body).toContain('Inbox setup is unavailable');
    107           expect(output.body).toMatch(
    108             /<button[^>]*disabled[^>]*>Connect extension/
    109           );
    110           expect(output.body).toMatch(
    111             /<button[^>]*disabled[^>]*>Check current preference/
    112           );
    113           expect(output.body).not.toMatch(
    114             /<form|<textarea|<input|<select|PRIVATE_TEST_ONLY|naddr1|No listings yet\.|<h2>Needs attention|<h2>Drafts|<h2>Listings/
    115           );
    116           // Original SSR effect tripwires still reject IDB/extension/socket/fetch.
    117           return;
    118         }
    119         expect(output.body).toContain('Connect or unlock');
    120         if (name === 'Conversation') {
    121           expect(output.body).toMatch(
    122             /This conversation is unavailable until it is unlocked in the matching\s+browser and account\./
    123           );
    124           expect(output.body).toContain('href="/messages"');
    125           expect(output.body).not.toContain('data-message-id');
    126         } else
    127           expect(output.body).toContain(
    128             'Messages stay hidden until you connect and explicitly unlock your inbox.'
    129           );
    130         expect(output.body).toMatch(
    131           /<button[^>]*disabled[^>]*>Connect extension/
    132         );
    133         expect(output.body).toContain('Inbox setup');
    134         expect(output.body).toMatch(
    135           /<button[^>]*disabled[^>]*>Check current preference/
    136         );
    137         expect(output.body).not.toMatch(
    138           /<form|<textarea|<input|PRIVATE_TEST_ONLY|naddr1/
    139         );
    140         // The shared SSR setup throws on IDB, extension, socket or fetch access.
    141       }
    142     );
    143   }
    144   it('dynamic routes inherit SSR and use navigation fallback rather than fake prerendered identities', () => {
    145     expect([
    146       productPrerender,
    147       draftPrerender,
    148       conversationPrerender,
    149       editPrerender
    150     ]).toEqual([false, false, false, false]);
    151   });
    152   it('public shells honestly disclose unavailable content without invented listings or support', () => {
    153     for (const output of [render(About), render(Privacy)]) {
    154       expect(output.body).not.toMatch(/<form|<textarea|<input|mailto:|tel:/);
    155       expect(output.head).not.toContain('noindex');
    156     }
    157     expect(render(About).body).toContain('About HarvestCircle');
    158     expect(render(About).body).toContain('Search without connecting.');
    159     expect(render(About).body).toContain(
    160       'Messages do not reserve food or confirm an order.'
    161     );
    162     expect(render(About).body).toContain('Support has not been configured.');
    163     expect(render(About).body).toContain('id="report"');
    164     expect(render(About).body).not.toMatch(
    165       /Contact support|Selected report preview|Share message text|[0-9a-f]{64}/
    166     );
    167     expect(render(Privacy).body).toContain('Hiding a publisher is local');
    168     expect(render(Privacy).body).toContain('Blocking a person is local');
    169     expect(render(Privacy).body.replace(/\s+/g, ' ')).toContain(
    170       'Private messages have no forward secrecy or post-compromise security.'
    171     );
    172     expect(render(Privacy).body.replace(/\s+/g, ' ')).toContain(
    173       'Guest browsing does not expose an account inventory'
    174     );
    175     expect(render(Privacy).body).not.toMatch(
    176       /Undo hide|Undo block|[0-9a-f]{64}/
    177     );
    178     expect(render(About).body).toContain('id="help"');
    179     const details = render(Product);
    180     expect(details.body).toContain('This food reference is invalid.');
    181     expect(details.body).not.toMatch(/<form|<textarea|<input|mailto:|tel:/);
    182   });
    183   for (const status of [404, 500]) {
    184     it(
    185       'error ' +
    186         status +
    187         ' never renders arbitrary error details or route state',
    188       () => {
    189         errorState.status = status;
    190         const output = render(ErrorPage);
    191         expect(output.head).toContain('<title>HarvestCircle</title>');
    192         expect(output.head).toContain('noindex');
    193         expect(output.body).not.toContain('PRIVATE_TEST_ONLY');
    194         expect(output.body).toContain(
    195           status === 404 ? 'Not Found' : 'This page is unavailable.'
    196         );
    197       }
    198     );
    199   }
    200   it('robots excludes private indexes and opaque edit paths without inventing a sitemap or access control', () => {
    201     expect(
    202       readFileSync(new URL('../../static/robots.txt', import.meta.url), 'utf8')
    203     ).toBe(
    204       'User-agent: *\nDisallow: /sell\nDisallow: /selling\nDisallow: /messages\nDisallow: /products/*/edit\n'
    205     );
    206   });
    207 });
    208 
    209 it('the pinned Kit3 consolidated matcher adapter preserves existing bounded syntax admission', async () => {
    210   const valid = '12345678-1234-4234-8234-123456789abc';
    211   expect(await params.local_id['~standard'].validate(valid)).toEqual({
    212     value: valid
    213   });
    214   expect(
    215     await params.local_id['~standard'].validate('unknown-draft')
    216   ).toHaveProperty('issues');
    217   expect(
    218     await params.naddr['~standard'].validate('unsupported-coordinate')
    219   ).toHaveProperty('issues');
    220 });
    221 
    222 it('public search SSR cannot accept input or submit before client initialization', () => {
    223   const output = render(SearchForm, {
    224     props: {
    225       onsubmit: () => {
    226         throw new Error('SSR must not submit');
    227       }
    228     }
    229   });
    230   expect(output.body).toMatch(/<textarea[^>]*disabled/);
    231   expect(output.body).toMatch(/<button[^>]*disabled/);
    232   expect(output.body).not.toContain(
    233     'Search is unavailable during development.'
    234   );
    235 });