app

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

primitives.spec.ts (9904B)


      1 import { expect, test } from '@playwright/test';
      2 import { createHash } from 'node:crypto';
      3 import { execFileSync } from 'node:child_process';
      4 import {
      5   cp,
      6   mkdtemp,
      7   readFile,
      8   readdir,
      9   rm,
     10   writeFile
     11 } from 'node:fs/promises';
     12 import { tmpdir } from 'node:os';
     13 import path from 'node:path';
     14 import { fileURLToPath } from 'node:url';
     15 import { createStaticHarness } from '../integration/harness/static.ts';
     16 
     17 // Isolated qualification clone: actual components compile into disposable output.
     18 // The primary static payload and product route source are never overwritten.
     19 const capsule = fileURLToPath(new URL('../../../', import.meta.url));
     20 let directory: string;
     21 let server: Awaited<ReturnType<typeof createStaticHarness>>;
     22 test.beforeAll(async () => {
     23   test.setTimeout(86_400_000);
     24   directory = await mkdtemp(path.join(tmpdir(), 'hcp013-components-'));
     25   try {
     26     const checkout = path.join(directory, 'checkout');
     27     execFileSync('git', [
     28       'clone',
     29       '--quiet',
     30       '--no-hardlinks',
     31       capsule,
     32       checkout
     33     ]);
     34     for (const relative of [
     35       'web/src/lib/components/primitives',
     36       'web/tests/unit/components/primitives.test.ts',
     37       'web/tests/e2e/primitives.spec.ts',
     38       'web/tests/e2e/harness/primitives.svelte',
     39       'web/tests/integration/harness/static.ts'
     40     ])
     41       await cp(path.join(capsule, relative), path.join(checkout, relative), {
     42         recursive: true
     43       });
     44     const fixture = await readFile(
     45       path.join(capsule, 'web/tests/e2e/harness/primitives.svelte'),
     46       'utf8'
     47     );
     48     await writeFile(
     49       path.join(checkout, 'web/src/routes/+page.svelte'),
     50       fixture.replaceAll('../../../src/lib/', '../lib/')
     51     );
     52     // Runtime store metadata is volume-specific; install the exact locked graph
     53     // into this disposable clone without a network or lock-repair fallback.
     54     const sourceStore = execFileSync('corepack', ['pnpm', 'store', 'path'], {
     55       cwd: path.join(capsule, 'web')
     56     })
     57       .toString()
     58       .trim();
     59     const installed = execFileSync(
     60       'corepack',
     61       [
     62         'pnpm',
     63         'install',
     64         '--offline',
     65         '--frozen-lockfile',
     66         '--store-dir',
     67         path.dirname(sourceStore)
     68       ],
     69       { cwd: path.join(checkout, 'web'), stdio: 'pipe', timeout: 86400000 }
     70     );
     71     const sourceAudit = execFileSync(
     72       process.execPath,
     73       ['tools/check-source.mjs'],
     74       { cwd: path.join(checkout, 'web') }
     75     ).toString();
     76     const identities = [];
     77     for (const name of [
     78       'FormField',
     79       'Button',
     80       'Disclosure',
     81       'PageHeading',
     82       'ActionGroup',
     83       'EmptyState',
     84       'ConfirmPanel'
     85     ]) {
     86       const source = await readFile(
     87         path.join(
     88           checkout,
     89           'web/src/lib/components/primitives',
     90           name + '.svelte'
     91         )
     92       );
     93       identities.push({
     94         name,
     95         sha256: createHash('sha256').update(source).digest('hex')
     96       });
     97     }
     98     execFileSync(process.execPath, ['tools/build-info.mjs'], {
     99       cwd: path.join(checkout, 'web')
    100     });
    101     const compiled = execFileSync(
    102       process.execPath,
    103       ['node_modules/vite/bin/vite.js', 'build'],
    104       {
    105         cwd: path.join(checkout, 'web'),
    106         stdio: 'pipe',
    107         timeout: 86400000
    108       }
    109     );
    110     async function payload(
    111       directory: string,
    112       prefix = ''
    113     ): Promise<{ path: string; sha256: string }[]> {
    114       const files = [];
    115       for (const entry of await readdir(directory, { withFileTypes: true })) {
    116         const relative = prefix + entry.name;
    117         if (entry.isDirectory())
    118           files.push(
    119             ...(await payload(path.join(directory, entry.name), relative + '/'))
    120           );
    121         else
    122           files.push({
    123             path: relative,
    124             sha256: createHash('sha256')
    125               .update(await readFile(path.join(directory, entry.name)))
    126               .digest('hex')
    127           });
    128       }
    129       return files.sort((left, right) => left.path.localeCompare(right.path));
    130     }
    131     console.log(
    132       JSON.stringify({
    133         fixture: 'HCP013_TEST_ONLY_COMPILE',
    134         identities,
    135         sourceAudit,
    136         fixtureSourceSha256: createHash('sha256').update(fixture).digest('hex'),
    137         compiledRouteSha256: createHash('sha256')
    138           .update(
    139             await readFile(path.join(checkout, 'web/src/routes/+page.svelte'))
    140           )
    141           .digest('hex'),
    142         payload: await payload(path.join(checkout, 'web/build')),
    143         sourceStore,
    144         installed: installed.toString(),
    145         compiled: compiled.toString(),
    146         metadata: JSON.parse(
    147           await readFile(
    148             path.join(checkout, 'web/build/build-info.json'),
    149             'utf8'
    150           )
    151         ) as unknown
    152       })
    153     );
    154     server = await createStaticHarness({
    155       buildRoot: path.join(checkout, 'web/build')
    156     });
    157   } catch (error) {
    158     await rm(directory, { recursive: true, force: true });
    159     throw error;
    160   }
    161 });
    162 test.afterAll(async () => {
    163   await server?.close();
    164   if (directory) await rm(directory, { recursive: true, force: true });
    165   if (server)
    166     expect(server.state()).toEqual({ listening: false, childProcesses: 0 });
    167 });
    168 
    169 for (const width of [320, 1024]) {
    170   test(`actual primitives preserve labels, keyboard and safe return at ${width}px`, async ({
    171     browser
    172   }) => {
    173     const context = await browser.newContext({
    174       viewport: { width, height: 900 }
    175     });
    176     const external: string[] = [];
    177     try {
    178       await context.route('**/*', (route) => {
    179         if (new URL(route.request().url()).origin === server.url)
    180           return route.continue();
    181         external.push(route.request().url());
    182         return route.abort();
    183       });
    184       const page = await context.newPage();
    185       await page.goto(server.url);
    186       const input = page.getByLabel('Public terms', { exact: true });
    187       await expect(input).toHaveAttribute(
    188         'aria-describedby',
    189         'terms-hint terms-error'
    190       );
    191       await expect(input).toHaveAttribute('aria-invalid', 'true');
    192       await expect(page.locator('#terms-error')).toHaveText(
    193         'Enter public terms'
    194       );
    195       const errorBox = await page.locator('#terms-error').boundingBox();
    196       const inputBox = await input.boundingBox();
    197       expect(errorBox!.y + errorBox!.height).toBeLessThanOrEqual(inputBox!.y);
    198       await input.focus();
    199       await page.keyboard.press('Tab');
    200       await expect(
    201         page.getByRole('button', { name: 'Fixture command', exact: true })
    202       ).toBeFocused();
    203       await page.keyboard.press('Enter');
    204       await expect(page.getByRole('main').getByRole('status')).toHaveText(
    205         'Commands: 1'
    206       );
    207       await expect(
    208         page.getByRole('button', { name: 'Disabled command' })
    209       ).toBeDisabled();
    210       await page
    211         .getByRole('button', { name: 'Disabled command' })
    212         .evaluate((element) => (element as HTMLButtonElement).click());
    213       await expect(page.getByRole('main').getByRole('status')).toHaveText(
    214         'Commands: 1'
    215       );
    216       await page
    217         .getByRole('button', { name: 'Disabled command' })
    218         .evaluate((element) =>
    219           element.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    220         );
    221       await expect(page.getByRole('main').getByRole('status')).toHaveText(
    222         'Commands: 1'
    223       );
    224       await page.keyboard.press('Tab');
    225       await expect(
    226         page.getByRole('link', { name: 'Fixture navigation' })
    227       ).toBeFocused();
    228       await page.keyboard.press('Tab');
    229       const details = page.locator('main details').filter({
    230         has: page.locator('summary').filter({
    231           hasText: /^Public fixture details$/
    232         })
    233       });
    234       await expect(details).toHaveCount(1);
    235       await expect(details.locator('summary')).toBeFocused();
    236       await page.keyboard.press('Enter');
    237       await expect(details).toHaveAttribute('open', '');
    238       await page.keyboard.press('Space');
    239       await expect(details).not.toHaveAttribute('open', '');
    240       await page.getByRole('button', { name: 'Review discard' }).click();
    241       await page.keyboard.press('Tab');
    242       await expect(
    243         page.getByRole('button', { name: 'Keep editing' })
    244       ).toBeFocused();
    245       await expect(
    246         page.getByRole('region', { name: 'Discard this draft?' })
    247       ).toContainText('Remote copies remain.');
    248       await page.keyboard.press('Enter');
    249       await expect(
    250         page.getByRole('button', { name: 'Review discard' })
    251       ).toBeFocused();
    252       await expect(
    253         page.getByRole('button', { name: 'Keep editing' })
    254       ).toHaveCount(0);
    255       await page.keyboard.press('Enter');
    256       await page
    257         .getByRole('button', { name: 'Discard draft', exact: true })
    258         .click();
    259       await expect(page.getByRole('main').getByRole('status')).toHaveText(
    260         'Commands: 2'
    261       );
    262       await expect(
    263         page.getByRole('button', { name: 'Discard draft', exact: true })
    264       ).toBeDisabled();
    265       await page.getByRole('button', { name: 'Keep editing' }).click();
    266       await expect(
    267         page.getByRole('button', { name: 'Review discard' })
    268       ).toBeFocused();
    269       const measured = await page.evaluate(() => ({
    270         width: innerWidth,
    271         scroll: document.documentElement.scrollWidth,
    272         heights: Array.from(
    273           document.querySelectorAll('input,button,a.button,summary')
    274         ).map((element) => element.getBoundingClientRect().height)
    275       }));
    276       expect(measured.scroll).toBeLessThanOrEqual(measured.width);
    277       for (const height of measured.heights)
    278         expect(height).toBeGreaterThanOrEqual(44);
    279       expect(external).toEqual([]);
    280       console.log(
    281         JSON.stringify({
    282           fixture: 'HCP013_ACTUAL_PRESENTATION_COMPONENTS_ONLY',
    283           measured
    284         })
    285       );
    286     } finally {
    287       await context.close();
    288     }
    289     expect(context.pages()).toHaveLength(0);
    290   });
    291 }