app

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

route-inventory.spec.ts (11343B)


      1 import { test, expect } from '@playwright/test';
      2 import { encodeProductReference } from '../../src/lib/nostr/references';
      3 import { createStaticHarness } from '../integration/harness/static';
      4 
      5 // Canonical coordinate uses the already qualified public generator point.
      6 const naddr = encodeProductReference({
      7   kind: 30402,
      8   pubkey: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
      9   identifier: 'HCP020_TEST_ONLY'
     10 });
     11 if (!naddr) throw new Error('Qualified coordinate could not be encoded');
     12 const handle = '12345678-1234-4234-8234-123456789abc';
     13 // Current-source applicability only: public shipment history remains unknown.
     14 test('current source has no revision-one aliases or query-driven redirects', async ({
     15   browser
     16 }) => {
     17   const server = await createStaticHarness();
     18   const context = await browser.newContext();
     19   const external: string[] = [];
     20   const errors: string[] = [];
     21   try {
     22     await context.route('**/*', (route) => {
     23       if (new URL(route.request().url()).origin === server.url)
     24         return route.continue();
     25       external.push(route.request().url());
     26       return route.abort();
     27     });
     28     await context.addInitScript(() => {
     29       for (const name of ['indexedDB', 'nostr', 'WebSocket'])
     30         Object.defineProperty(window, name, {
     31           configurable: true,
     32           get() {
     33             throw new Error('Guest attempted ' + name);
     34           }
     35         });
     36     });
     37     for (const pathname of [
     38       '/products',
     39       '/products?q=carrots',
     40       '/my-listings'
     41     ]) {
     42       const response = await context.request.get(server.url + pathname, {
     43         headers: { accept: 'text/html' },
     44         maxRedirects: 0
     45       });
     46       expect(response.status()).toBe(404);
     47       expect(response.headers().location).toBeUndefined();
     48       expect(await response.text()).toBe('');
     49     }
     50     const page = await context.newPage();
     51     page.on('pageerror', (error) => errors.push(error.message));
     52     for (const query of [
     53       new URLSearchParams({ draft: handle }),
     54       new URLSearchParams({ edit: naddr }),
     55       new URLSearchParams({
     56         draft: handle,
     57         edit: naddr,
     58         returnTo: 'https://outside.invalid/'
     59       })
     60     ]) {
     61       const target = server.url + '/sell?' + query.toString();
     62       expect((await page.goto(target))?.status()).toBe(200);
     63       await expect(
     64         page.getByRole('heading', { name: 'Connect to continue', exact: true })
     65       ).toBeVisible();
     66       expect(page.url()).toBe(target);
     67       // Exercise the real initialized client and Back, rather than SSR alone.
     68       await page.getByRole('link', { name: 'Search', exact: true }).click();
     69       await expect(page.getByLabel('What are you looking for?')).toBeEnabled();
     70       await page.goBack();
     71       await expect(
     72         page.getByRole('heading', { name: 'Connect to continue', exact: true })
     73       ).toBeVisible();
     74       expect(page.url()).toBe(target);
     75       await expect(
     76         page.locator('main input, main textarea, main form')
     77       ).toHaveCount(0);
     78       expect(await page.locator('main').textContent()).not.toContain(handle);
     79       expect(await page.locator('main').textContent()).not.toContain(naddr);
     80     }
     81     expect(external).toEqual([]);
     82     expect(errors).toEqual([]);
     83   } finally {
     84     await context.close();
     85     await server.close();
     86   }
     87   expect(context.pages()).toHaveLength(0);
     88   expect(server.state()).toEqual({ listening: false, childProcesses: 0 });
     89 });
     90 const cases = [
     91   { path: '/', title: 'HarvestCircle', private: false },
     92   { path: '/search', title: 'Search food — HarvestCircle', private: false },
     93   { path: '/products/' + naddr, title: 'Food — HarvestCircle', private: false },
     94   {
     95     path: '/sell',
     96     title: 'New listing · HarvestCircle',
     97     private: true,
     98     editor: true
     99   },
    100   { path: '/selling', title: 'HarvestCircle', private: true },
    101   {
    102     path: '/selling/drafts/' + handle,
    103     title: 'Continue draft · HarvestCircle',
    104     private: true,
    105     editor: true
    106   },
    107   {
    108     path: '/products/' + naddr + '/edit',
    109     title: 'Edit listing · HarvestCircle',
    110     private: true,
    111     editor: true
    112   },
    113   { path: '/messages', title: 'HarvestCircle', private: true },
    114   { path: '/messages/' + handle, title: 'HarvestCircle', private: true },
    115   { path: '/about', title: 'About — HarvestCircle', private: false },
    116   { path: '/privacy', title: 'Privacy — HarvestCircle', private: false }
    117 ];
    118 for (const routeCase of cases) {
    119   test(
    120     'direct navigation and reload preserve actual route shell ' +
    121       routeCase.path,
    122     async ({ browser }) => {
    123       const server = await createStaticHarness();
    124       const context = await browser.newContext();
    125       try {
    126         await context.route('**/*', (route) =>
    127           new URL(route.request().url()).origin === server.url
    128             ? route.continue()
    129             : route.abort()
    130         );
    131         // Tripwires enforce no guest account/storage/signing/relay acquisition.
    132         await context.addInitScript(() => {
    133           for (const name of ['indexedDB', 'nostr', 'WebSocket'])
    134             Object.defineProperty(window, name, {
    135               configurable: true,
    136               get() {
    137                 throw new Error('Guest attempted ' + name);
    138               }
    139             });
    140         });
    141         const page = await context.newPage();
    142         const errors: string[] = [];
    143         page.on('pageerror', (error) => errors.push(error.message));
    144         for (const navigate of [
    145           () => page.goto(server.url + routeCase.path),
    146           () => page.reload()
    147         ]) {
    148           expect((await navigate())?.status()).toBe(200);
    149           await expect(page).toHaveTitle(routeCase.title);
    150           await expect(page.getByRole('main')).toHaveCount(1);
    151           expect(new URL(page.url()).pathname).toBe(routeCase.path);
    152           await expect(
    153             page.getByRole('link', { name: 'Search', exact: true })
    154           ).toHaveCount(1);
    155           if (routeCase.private) {
    156             await expect(
    157               page.getByRole('heading', {
    158                 name:
    159                   routeCase.editor || routeCase.path === '/selling'
    160                     ? 'Connect to continue'
    161                     : 'Connect or unlock',
    162                 exact: true
    163               })
    164             ).toBeVisible();
    165             await expect(page.locator('meta[name="robots"]')).toHaveAttribute(
    166               'content',
    167               'noindex'
    168             );
    169             if (routeCase.editor) {
    170               await expect(page.locator('main button')).toHaveCount(1);
    171               await expect(
    172                 page.getByRole('button', {
    173                   name: 'Check current preference',
    174                   exact: true
    175                 })
    176               ).toHaveCount(0);
    177               await expect(
    178                 page.getByRole('button', { name: 'Not now', exact: true })
    179               ).toHaveCount(0);
    180             } else {
    181               if (routeCase.path === '/selling') {
    182                 await expect(
    183                   page.locator('main').getByRole('link', {
    184                     name: 'List food',
    185                     exact: true
    186                   })
    187                 ).toHaveAttribute('href', '/sell');
    188                 await expect(
    189                   page.locator('main').getByRole('button')
    190                 ).toHaveCount(1);
    191                 await expect(
    192                   page.getByRole('button', {
    193                     name: 'Check current preference',
    194                     exact: true
    195                   })
    196                 ).toHaveCount(0);
    197                 const settings = page.locator('main details').filter({
    198                   has: page.locator('summary').filter({
    199                     hasText: /^Inbox settings$/
    200                   })
    201                 });
    202                 await expect(settings).toHaveCount(1);
    203                 await expect(settings).not.toHaveAttribute('open', '');
    204                 await settings
    205                   .getByText('Inbox settings', { exact: true })
    206                   .click();
    207                 await expect(settings).toHaveAttribute('open', '');
    208               }
    209               await expect(
    210                 page.getByRole('button', {
    211                   name: 'Check current preference',
    212                   exact: true
    213                 })
    214               ).toBeDisabled();
    215               await expect(page.locator('main button')).toHaveCount(3);
    216               await expect(
    217                 page.getByRole('button', { name: 'Not now', exact: true })
    218               ).toBeEnabled();
    219             }
    220             await expect(
    221               page.getByRole('button', { name: 'Unlock inbox', exact: true })
    222             ).toHaveCount(0);
    223             await expect(
    224               page.getByRole('button', { name: 'Enable inbox', exact: true })
    225             ).toHaveCount(0);
    226             await expect(
    227               page
    228                 .locator('main')
    229                 .getByRole('button', { name: 'Connect extension', exact: true })
    230             ).toBeEnabled();
    231             await expect(
    232               page
    233                 .locator('main')
    234                 .getByRole('link', { name: 'Connection help' })
    235             ).toHaveAttribute('href', '/about#help');
    236             await expect(
    237               page.locator('main input, main textarea, main form')
    238             ).toHaveCount(0);
    239             expect(await page.locator('main').textContent()).not.toContain(
    240               handle
    241             );
    242             expect(await page.locator('main').textContent()).not.toContain(
    243               naddr
    244             );
    245           }
    246         }
    247         expect(errors).toEqual([]);
    248       } finally {
    249         await context.close();
    250         await server.close();
    251       }
    252       expect(context.pages()).toHaveLength(0);
    253       expect(server.state()).toEqual({ listening: false, childProcesses: 0 });
    254     }
    255   );
    256 }
    257 
    258 test('unknown routes and invalid opaque syntax never substitute data or return missing assets as HTML', async ({
    259   browser
    260 }) => {
    261   const server = await createStaticHarness();
    262   const context = await browser.newContext();
    263   try {
    264     const page = await context.newPage();
    265     for (const pathname of [
    266       '/orders',
    267       '/checkout',
    268       '/login',
    269       '/api/login',
    270       '/profiles'
    271     ]) {
    272       const response = await context.request.get(server.url + pathname, {
    273         headers: { accept: 'text/html' }
    274       });
    275       expect(response.status()).toBe(404);
    276       expect(await response.text()).toBe('');
    277     }
    278     for (const pathname of [
    279       '/products/unsupported-coordinate',
    280       '/selling/drafts/unknown-draft',
    281       '/messages/unknown-conversation'
    282     ]) {
    283       await page.goto(server.url + pathname);
    284       await expect(
    285         page.getByRole('heading', { name: '404', exact: true })
    286       ).toBeVisible();
    287       await expect(page).toHaveTitle('HarvestCircle');
    288       await expect(
    289         page.locator('main input, main textarea, main form')
    290       ).toHaveCount(0);
    291     }
    292     for (const pathname of [
    293       '/missing.js',
    294       '/missing.css',
    295       '/_app/missing.js'
    296     ]) {
    297       const response = await context.request.get(server.url + pathname, {
    298         headers: { accept: 'text/html' }
    299       });
    300       expect(response.status()).toBe(404);
    301       expect(await response.text()).toBe('');
    302     }
    303   } finally {
    304     await context.close();
    305     await server.close();
    306   }
    307   expect(context.pages()).toHaveLength(0);
    308   expect(server.state()).toEqual({ listening: false, childProcesses: 0 });
    309 });