app

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

style-contract.test.ts (5763B)


      1 import { readFileSync } from 'node:fs';
      2 import {
      3   copyFile,
      4   mkdir,
      5   mkdtemp,
      6   rm,
      7   symlink,
      8   writeFile
      9 } from 'node:fs/promises';
     10 import { tmpdir } from 'node:os';
     11 import path from 'node:path';
     12 import { fileURLToPath } from 'node:url';
     13 import { spawnSync } from 'node:child_process';
     14 import { describe, expect, it } from 'vitest';
     15 
     16 const root = fileURLToPath(new URL('../../', import.meta.url));
     17 const layout = readFileSync(
     18   path.join(root, 'src/routes/+layout.svelte'),
     19   'utf8'
     20 );
     21 const theme = readFileSync(path.join(root, 'src/theme.css'), 'utf8');
     22 const app = () => readFileSync(path.join(root, 'src/app.css'), 'utf8');
     23 
     24 describe('finite two-file style contract', () => {
     25   it('imports theme then compositions once at the root layout', () => {
     26     expect(
     27       [...layout.matchAll(/import\s+['"]([^'"]+\.css)['"]/g)].map((m) => m[1])
     28     ).toEqual(['../theme.css', '../app.css']);
     29   });
     30   it('provides the declared finite composition vocabulary and layer order', () => {
     31     const css = app();
     32     expect(css).toContain('@layer reset, base, components, utilities;');
     33     for (const name of [
     34       'page',
     35       'page--reading',
     36       'stack',
     37       'stack--tight',
     38       'cluster',
     39       'navbar',
     40       'field',
     41       'label',
     42       'input',
     43       'textarea',
     44       'button',
     45       'button--primary',
     46       'button--secondary',
     47       'border-list',
     48       'list-row',
     49       'facts',
     50       'notice',
     51       'message',
     52       'disclosure',
     53       'text-muted',
     54       'visually-hidden'
     55     ]) {
     56       expect(css).toMatch(new RegExp('\\.' + name + '(?=[\\s:{,.])'));
     57     }
     58     expect(css).not.toMatch(
     59       /@media|@import|url\(|#[0-9a-f]{3,8}\b|\b(?:rgb|hsl)a?\(|(?:^|[;{])\s*--[\w-]+\s*:/im
     60     );
     61     const tokens = new Set(
     62       [...theme.matchAll(/(--[\w-]+)\s*:/g)].map((m) => m[1])
     63     );
     64     for (const match of css.matchAll(/var\((--[\w-]+)\)/g))
     65       expect(tokens.has(match[1])).toBe(true);
     66     expect(css).not.toMatch(/\b\d*\.?\d+(?:px|rem|em|ch|vh|vw)\b/);
     67   });
     68 });
     69 
     70 async function audit(extra: { name: string; content: string }) {
     71   const directory = await mkdtemp(path.join(tmpdir(), 'hcp012 styles '));
     72   try {
     73     await mkdir(path.join(directory, 'tools'));
     74     await mkdir(path.join(directory, 'src/routes'), { recursive: true });
     75     for (const name of [
     76       'package.json',
     77       'tsconfig.json',
     78       'vite.config.ts',
     79       'tools/check-source.mjs',
     80       'tools/source-boundaries.mjs'
     81     ])
     82       await copyFile(path.join(root, name), path.join(directory, name));
     83     await symlink(
     84       path.join(root, 'node_modules'),
     85       path.join(directory, 'node_modules')
     86     );
     87     await writeFile(path.join(directory, 'src/theme.css'), theme);
     88     await writeFile(
     89       path.join(directory, 'src/app.css'),
     90       '@layer reset, base, components, utilities;'
     91     );
     92     await writeFile(
     93       path.join(directory, 'src/routes/+layout.svelte'),
     94       '<script>import "../theme.css"; import "../app.css";</script>'
     95     );
     96     await mkdir(path.dirname(path.join(directory, extra.name)), {
     97       recursive: true
     98     });
     99     await writeFile(path.join(directory, extra.name), extra.content);
    100     const result = spawnSync(process.execPath, ['tools/check-source.mjs'], {
    101       cwd: directory,
    102       encoding: 'utf8',
    103       timeout: 86_400_000
    104     });
    105     expect(result.error).toBeUndefined();
    106     return result;
    107   } finally {
    108     await rm(directory, { recursive: true, force: true });
    109   }
    110 }
    111 
    112 describe('actual source guard rejects forbidden styles', () => {
    113   it('admits the isolated paired stylesheet positive control', async () => {
    114     expect(
    115       (
    116         await audit({
    117           name: 'src/routes/+page.svelte',
    118           content: '<p class="notice">Test</p>'
    119         })
    120       ).status
    121     ).toBe(0);
    122   });
    123   it('admits ordinary layered compositions and ignores comments', async () => {
    124     expect(
    125       (
    126         await audit({
    127           name: 'src/app.css',
    128           content:
    129             '/* @tailwind utilities; */ @layer components { .notice { padding: var(--space-2); } }'
    130         })
    131       ).status
    132     ).toBe(0);
    133   });
    134   it.each([
    135     'tailwind',
    136     'apply',
    137     'theme',
    138     'utility',
    139     'variant',
    140     'custom-variant',
    141     'config',
    142     'plugin',
    143     'source',
    144     'reference'
    145   ])('rejects Tailwind @%s', async (directive) => {
    146     const result = await audit({
    147       name: 'src/app.css',
    148       content: `@${directive} fixture;`
    149     });
    150     expect(result.status).not.toBe(0);
    151     expect(result.stderr).toContain('Tailwind CSS directives are forbidden');
    152   });
    153   it.each([
    154     '@\\74 ailwind utilities;',
    155     '@tai/**/lwind utilities;',
    156     '.notice { @\\61 pply bg-white; }'
    157   ])('rejects decoded Tailwind syntax %s', async (content) => {
    158     const result = await audit({ name: 'src/app.css', content });
    159     expect(result.status).not.toBe(0);
    160     expect(result.stderr).toContain('Tailwind CSS directives are forbidden');
    161   });
    162   it.each([
    163     [
    164       'component CSS',
    165       'src/routes/+page.svelte',
    166       '<p>Test</p><style>p { color: red; }</style>'
    167     ],
    168     [
    169       'inline attribute',
    170       'src/routes/+page.svelte',
    171       '<p style="color: red">Test</p>'
    172     ],
    173     [
    174       'inline directive',
    175       'src/routes/+page.svelte',
    176       '<p style:color="red">Test</p>'
    177     ],
    178     [
    179       'CSS in JavaScript',
    180       'src/routes/+page.ts',
    181       'export function decorate(element: HTMLElement) { element.style.color = "red"; }'
    182     ],
    183     ['Tailwind import', 'src/routes/+page.ts', 'import "tailwindcss";'],
    184     [
    185       'Tailwind directives',
    186       'src/app.css',
    187       '@tailwind utilities; .notice { @apply bg-white; }'
    188     ]
    189   ])('rejects %s', async (_name, name, content) => {
    190     const result = await audit({ name, content });
    191     expect(result.status, result.stdout + result.stderr).not.toBe(0);
    192   });
    193 });