Files
NODEDC_DESIGN_GUIDELINE/scripts/operational-typography.test.mjs
T

42 lines
3.4 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import postcss from 'postcss';
import {readFileSync,readdirSync} from 'node:fs';
import {fileURLToPath} from 'node:url';
import path from 'node:path';
import {auditStyles,typographyViolation,operationalFontSizes,titleFontSize} from '../packages/ui-core/typography.mjs';
const root=fileURLToPath(new URL('../',import.meta.url));
const policy=JSON.parse(readFileSync(new URL('./typography-policy.json',import.meta.url),'utf8'));
function walk(dir){return readdirSync(dir,{withFileTypes:true}).flatMap(e=>['dist','node_modules','tokens'].includes(e.name)?[]:e.isDirectory()?walk(path.join(dir,e.name)):[path.join(dir,e.name)]);}
test('all shared CSS uses compact operational roles and canonical title exceptions',()=>{
const violations=[];for(const file of walk(path.join(root,'packages')).filter(f=>f.endsWith('.css')))violations.push(...auditStyles(postcss,readFileSync(file,'utf8'),path.relative(root,file),policy));assert.deepEqual(violations,[]);
});
test('UI roots and portaled windows establish their own 13px typography, independent of host body',()=>{
const css=postcss.parse(readFileSync(path.join(root,'packages/ui-core/styles.css'),'utf8'));
for(const scope of ['.nodedc-ui-root','.nodedc-window','.nodedc-workspace-window','.nodedc-application-panel','.nodedc-admin-panel']){
let base=false,headings=false;css.walkRules(rule=>{if(rule.selector.includes(scope)){if(!rule.selector.includes(' :where(')&&rule.nodes.some(d=>d.prop==='font-size'&&d.value==='var(--nodedc-font-size-md)'))base=true;if(rule.selector.includes(':where(h1, h2, h3, h4, h5, h6)')&&rule.nodes.some(d=>d.prop==='font-size'&&d.value==='var(--nodedc-font-size-md)'))headings=true;}});assert.ok(base,scope+' independent base');assert.ok(headings,scope+' semantic heading reset');
}
});
test('token values cannot silently make all operational consumers larger',()=>{
const css=postcss.parse(readFileSync(path.join(root,'packages/tokens/tokens.css'),'utf8'));
for(const [role,px] of Object.entries({...operationalFontSizes,title:titleFontSize})){
const values=[];css.walkDecls('--nodedc-font-size-'+role,d=>values.push(d.value));assert.ok(values.length,role);for(const value of values)assert.equal(parseFloat(value)*16,px,role);
}
});
test('title role never grants large typography to a data field or nested card',()=>{
assert.equal(typographyViolation({file:'example.css',selector:'.nodedc-window__title',property:'font-size',value:'var(--nodedc-font-size-title)'},policy),null);
for(const selector of ['.value','.settings-card h2','.nodedc-window__body'])assert.ok(typographyViolation({file:'example.css',selector,property:'font-size',value:'var(--nodedc-font-size-title)'},policy));
assert.ok(typographyViolation({file:'example.css',selector:'.value',property:'font-size',value:'16px'},{compactLegacy:['example.css | .value | font-size | 16px']}));
});
test('theme attributes on html cannot shrink the rem coordinate system',()=>{
const css=postcss.parse(readFileSync(path.join(root,'packages/ui-core/styles.css'),'utf8'));
css.walkRules(rule=>{
if(rule.nodes.some(d=>d.prop==='font-size')&&rule.selector.startsWith(':where([data-nodedc-ui]')&&!rule.selector.includes(' :where(')){
assert.ok(rule.selector.includes('[data-nodedc-ui]:not(html)'));
assert.ok(rule.selector.includes('.nodedc-ui-root:not(html)'));
assert.ok(rule.selector.includes('html[data-nodedc-ui] > body'));
}
});
});