app

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

editor-workflow.spec.ts (10550B)


      1 import { expect, test } from '@playwright/test';
      2 import { build } from 'vite';
      3 import { svelte } from '@sveltejs/vite-plugin-svelte';
      4 import { fileURLToPath } from 'node:url';
      5 import { createStaticHarness } from '../integration/harness/static.ts';
      6 import type * as Fixture from './harness/editor-workflow.ts';
      7 import type { PublicDraftForm } from '../../src/lib/contracts/local-records.ts';
      8 declare global {
      9   interface Window {
     10     hcp117: typeof Fixture;
     11     hcp117Fixture: Awaited<ReturnType<typeof Fixture.createFixture>>;
     12   }
     13 }
     14 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string;
     15 const form: PublicDraftForm = {
     16   title: 'Exact 菜 listing',
     17   description:
     18     '  ' + 'Complete public description 菜\r\n'.repeat(40) + ' final line  ',
     19   location: 'Town',
     20   amount: '2.5',
     21   currency: 'GBP',
     22   unit: 'kg',
     23   quantity: '3',
     24   contactType: 'email',
     25   contactValue: 'public@example.org'
     26 };
     27 test.beforeAll(async () => {
     28   server = await createStaticHarness();
     29   const built = await build({
     30     configFile: false,
     31     plugins: [svelte({ configFile: false })],
     32     resolve: { conditions: ['browser'] },
     33     logLevel: 'silent',
     34     build: {
     35       write: false,
     36       minify: false,
     37       lib: {
     38         entry: fileURLToPath(
     39           new URL('./harness/editor-workflow.ts', import.meta.url)
     40         ),
     41         name: 'hcp117',
     42         formats: ['iife']
     43       }
     44     }
     45   });
     46   const result = Array.isArray(built) ? built[0] : built;
     47   if (!('output' in result)) throw Error('actual review bundle missing');
     48   const chunks = result.output.filter((row) => row.type === 'chunk');
     49   expect(chunks).toHaveLength(1);
     50   bundle = chunks[0].code;
     51 });
     52 test.afterAll(async () => {
     53   await server?.close();
     54 });
     55 test.beforeEach(async ({ page }) => {
     56   await page.addInitScript(() => {
     57     window.WebSocket = class extends WebSocket {
     58       constructor() {
     59         super(
     60           (() => {
     61             throw Error('HCP117 no live relay permission');
     62           })()
     63         );
     64       }
     65     };
     66   });
     67   await page.goto(server.url + '/search');
     68   await page.addScriptTag({ content: bundle });
     69 });
     70 test.afterEach(async ({ page }) => {
     71   await page.evaluate(async () => {
     72     await window.hcp117Fixture?.close();
     73   });
     74 });
     75 test('HCP117 exact immutable full outgoing template and destinations review does not publish', async ({
     76   page
     77 }) => {
     78   const result = await page.evaluate(async (form) => {
     79     const f = (window.hcp117Fixture = await window.hcp117.createFixture());
     80     await f.open();
     81     await f.edit(form);
     82     const before = f.counts(),
     83       a = await f.review(form),
     84       b = await f.review(form);
     85     return {
     86       a,
     87       b,
     88       before,
     89       after: f.counts(),
     90       rows: await f.inspect(),
     91       copied: f.snapshot(true)
     92     };
     93   }, form);
     94   expect(result.a.status).toBe('review');
     95   expect(result.b).toEqual(result.a);
     96   if (result.a.status !== 'review' || !result.a.snapshot)
     97     throw Error('genuine exact preview missing');
     98   const wire = JSON.parse(result.a.snapshot.templateWire) as {
     99     content: string;
    100     tags: string[][];
    101     pubkey: string;
    102   };
    103   expect(wire.content).toBe(
    104     form.description.replaceAll('\r\n', '\n').trim() +
    105       '\n\nPublic contact: mailto:public@example.org'
    106   );
    107   expect(result.a.snapshot.content).toBe(wire.content);
    108   expect(result.a.snapshot.summary.length).toBeLessThanOrEqual(240);
    109   expect(result.a.snapshot.content.length).toBeGreaterThan(1000);
    110   expect(result.a.snapshot.owner).toBe(wire.pubkey);
    111   expect(result.a.snapshot.targets).toEqual(['wss://discovery.example.org']);
    112   expect(wire.tags.find((tag) => tag[0] === 'summary')?.[1]).toBe(
    113     result.a.snapshot.summary
    114   );
    115   expect(result.rows.operations).toHaveLength(0);
    116   expect(result.rows.drafts).toHaveLength(1);
    117   expect(result.after.signs).toBe(result.before.signs);
    118   expect(result.copied).toBeUndefined();
    119 });
    120 test('HCP117 copy stale form changed draft and revoked readiness never retain review custody', async ({
    121   page
    122 }) => {
    123   const result = await page.evaluate(async (form) => {
    124     const f = (window.hcp117Fixture = await window.hcp117.createFixture());
    125     await f.open();
    126     await f.edit(form);
    127     const copied = await f.review(form, true),
    128       stale = await f.review({ ...form, title: 'not saved' });
    129     const original = await f.review(form);
    130     await f.edit({ ...form, title: 'new saved values' });
    131     const afterEdit = f.snapshot();
    132     const next = await f.review({ ...form, title: 'new saved values' });
    133     f.stop();
    134     return {
    135       copied,
    136       stale,
    137       original,
    138       afterEdit,
    139       next,
    140       afterStop: f.snapshot(),
    141       rows: await f.inspect()
    142     };
    143   }, form);
    144   expect(result.copied.status).toBe('blocked');
    145   expect(result.stale.status).toBe('blocked');
    146   expect(result.original.status).toBe('review');
    147   expect(result.next.status).toBe('review');
    148   expect(result.afterEdit).toBeUndefined();
    149   expect(result.afterStop).toBeUndefined();
    150   expect(result.rows.operations).toHaveLength(0);
    151 });
    152 test('HCP117 unqualified inbox blocks disclosure while saving the complete draft remains available', async ({
    153   page
    154 }) => {
    155   const result = await page.evaluate(async (form) => {
    156     const f = (window.hcp117Fixture = await window.hcp117.createFixture(false));
    157     await f.open();
    158     const saved = await f.edit(form),
    159       reviewed = await f.review(form);
    160     return {
    161       saved,
    162       reviewed,
    163       rows: await f.inspect(),
    164       signs: f.counts().signs
    165     };
    166   }, form);
    167   expect(result.saved).toBe(true);
    168   expect(result.reviewed.status).toBe('blocked');
    169   expect(result.rows.drafts).toHaveLength(1);
    170   expect(result.rows.operations).toHaveLength(0);
    171   expect(result.signs).toBe(0);
    172 });
    173 for (const width of [320, 1024]) {
    174   test(`HCP117 ${width}px same-route full review and Edit details preserve exact raw values and focus`, async ({
    175     page
    176   }) => {
    177     await page.setViewportSize({ width, height: 900 });
    178     await page.evaluate(async () => {
    179       window.hcp117Fixture = await window.hcp117.createFixture();
    180       window.hcp117Fixture.render();
    181     });
    182     await expect(page.locator('#listing-title')).toBeVisible();
    183     for (const [field, value] of Object.entries(form)) {
    184       const id =
    185         '#listing-' +
    186         (
    187           {
    188             contactType: 'contact-type',
    189             contactValue: 'contact-value'
    190           } as Record<string, string>
    191         )[field];
    192       const selector = field.startsWith('contact') ? id : '#listing-' + field;
    193       if (field === 'contactType') {
    194         await page
    195           .getByText('Add public contact details (optional)', { exact: true })
    196           .click();
    197         await page.locator(selector).selectOption(value);
    198       } else if (field === 'contactValue')
    199         await page.locator(selector).fill(value);
    200       else if (field === 'unit')
    201         await page.locator(selector).selectOption(value);
    202       else await page.locator(selector).fill(value);
    203     }
    204     const originalURL = page.url();
    205     await page
    206       .getByRole('button', { name: 'Review listing', exact: true })
    207       .click();
    208     await expect(
    209       page.getByRole('heading', { name: 'Public review', exact: true })
    210     ).toBeVisible();
    211     await expect(page.locator('#listing-review-content')).toHaveText(
    212       form.description.replaceAll('\r\n', '\n').trim() +
    213         '\n\nPublic contact: mailto:public@example.org',
    214       { useInnerText: false }
    215     );
    216     expect(await page.locator('#listing-review-content').textContent()).toBe(
    217       form.description.replaceAll('\r\n', '\n').trim() +
    218         '\n\nPublic contact: mailto:public@example.org'
    219     );
    220     await expect(page.locator('#listing-title')).toHaveCount(0);
    221     expect(page.url()).toBe(originalURL);
    222     await expect(
    223       page.getByRole('button', { name: 'Publish', exact: true })
    224     ).toHaveCount(0);
    225     await page
    226       .getByRole('button', { name: 'Edit details', exact: true })
    227       .click();
    228     await expect(page.locator('#listing-title')).toBeFocused();
    229     await expect(page.locator('#listing-title')).toHaveValue(form.title);
    230     await expect(page.locator('#listing-description')).toHaveValue(
    231       form.description.replaceAll('\r\n', '\n')
    232     );
    233     await expect(page.locator('#listing-amount')).toHaveValue(form.amount);
    234     await expect(page.locator('#listing-contact-value')).toHaveValue(
    235       form.contactValue
    236     );
    237     for (const [field, value] of Object.entries(form)) {
    238       const id =
    239         { contactType: 'contact-type', contactValue: 'contact-value' }[
    240           field as 'contactType' | 'contactValue'
    241         ] ?? field;
    242       await expect(page.locator('#listing-' + id)).toHaveValue(
    243         value.replaceAll('\r\n', '\n')
    244       );
    245     }
    246     expect(
    247       (await page.evaluate(() => window.hcp117Fixture.inspect())).operations
    248     ).toHaveLength(0);
    249     expect(
    250       await page.evaluate(
    251         () => document.documentElement.scrollWidth <= innerWidth
    252       )
    253     ).toBe(true);
    254   });
    255   test(`HCP117 ${width}px linked inline errors preserve input and refuse stale saved content`, async ({
    256     page
    257   }) => {
    258     await page.setViewportSize({ width, height: 900 });
    259     await page.evaluate(async () => {
    260       window.hcp117Fixture = await window.hcp117.createFixture();
    261       window.hcp117Fixture.render();
    262     });
    263     const title = page.locator('#listing-title');
    264     await expect(title).toBeVisible();
    265     await title.fill('raw retained title');
    266     await page.locator('#listing-description').fill(' raw\n菜 description ');
    267     await page.locator('#listing-location').fill(form.location);
    268     await page.locator('#listing-currency').fill(form.currency);
    269     await page.locator('#listing-unit').selectOption(form.unit);
    270     await page.locator('#listing-amount').fill('2,50');
    271     await page
    272       .getByRole('button', { name: 'Review listing', exact: true })
    273       .click();
    274     await expect(page.locator('#listing-review-errors')).toBeVisible();
    275     await expect(page.locator('#listing-amount')).toHaveAttribute(
    276       'aria-invalid',
    277       'true'
    278     );
    279     await expect(page.locator('#listing-review-errors a')).toHaveAttribute(
    280       'href',
    281       '#listing-amount'
    282     );
    283     await expect(title).toHaveValue('raw retained title');
    284     await expect(page.locator('#listing-description')).toHaveValue(
    285       ' raw\n菜 description '
    286     );
    287     await expect(page.locator('#listing-amount')).toHaveValue('2,50');
    288     await expect(
    289       page.getByRole('heading', { name: 'Public review', exact: true })
    290     ).toHaveCount(0);
    291     expect(
    292       (await page.evaluate(() => window.hcp117Fixture.inspect())).operations
    293     ).toHaveLength(0);
    294   });
    295 }