|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +const css = readFileSync( |
| 6 | + resolve(import.meta.dirname, '../src/components/settings/settings.css'), |
| 7 | + 'utf8', |
| 8 | +); |
| 9 | + |
| 10 | +function rule(selector: string): string { |
| 11 | + const escaped = selector.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 12 | + const match = css.match(new RegExp(`(?:^|\\n)${escaped}\\s*\\{([^{}]*)\\}`, 'u')); |
| 13 | + if (!match?.[1]) throw new Error(`Missing CSS rule: ${selector}`); |
| 14 | + return match[1]; |
| 15 | +} |
| 16 | + |
| 17 | +function declaration(selector: string, property: string): string { |
| 18 | + const escaped = property.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 19 | + const match = rule(selector).match(new RegExp(`${escaped}\\s*:\\s*([^;]+);`, 'u')); |
| 20 | + if (!match?.[1]) throw new Error(`Missing ${property} in ${selector}`); |
| 21 | + return match[1].trim(); |
| 22 | +} |
| 23 | + |
| 24 | +function resolvePx(value: string, fontSize: number, variables: Record<string, string> = {}): number { |
| 25 | + let expression = value.trim().replace(/^calc\((.*)\)$/u, '$1'); |
| 26 | + expression = expression.replaceAll(/var\((--[\w-]+)\)/gu, (_match: string, name: string) => { |
| 27 | + if (name === '--ui-font-size') return String(fontSize); |
| 28 | + const variable = variables[name]; |
| 29 | + if (variable === undefined) throw new Error(`Missing test variable: ${name}`); |
| 30 | + return String(resolvePx(variable, fontSize, variables)); |
| 31 | + }); |
| 32 | + expression = expression.replaceAll(/px\b/gu, ''); |
| 33 | + if (!/^[\d\s.+*()-]+$/u.test(expression)) throw new Error(`Unsupported CSS arithmetic: ${value}`); |
| 34 | + |
| 35 | + // `calc()` requires whitespace around + and -, so splitting on the spaced |
| 36 | + // operator is both safe and a check that the expression is valid CSS. |
| 37 | + const parts = expression.split(/\s+([+-])\s+/u); |
| 38 | + let sum = 0; |
| 39 | + let sign = 1; |
| 40 | + for (const part of parts) { |
| 41 | + if (part === '+' || part === '-') { |
| 42 | + sign = part === '+' ? 1 : -1; |
| 43 | + continue; |
| 44 | + } |
| 45 | + const product = part.split(/\s*\*\s*/u).reduce((acc, factor) => acc * Number(factor), 1); |
| 46 | + if (Number.isNaN(product)) throw new Error(`Unsupported CSS arithmetic: ${value}`); |
| 47 | + sum += sign * product; |
| 48 | + } |
| 49 | + return sum; |
| 50 | +} |
| 51 | + |
| 52 | +function transformDistance(selector: string, variables: Record<string, string>, fontSize: number): number { |
| 53 | + const transform = declaration(selector, 'transform'); |
| 54 | + const match = transform.match(/^translateX\((.*)\)$/u); |
| 55 | + if (!match?.[1]) throw new Error(`Missing translateX in ${selector}`); |
| 56 | + return resolvePx(match[1], fontSize, variables); |
| 57 | +} |
| 58 | + |
| 59 | +function splitCssValues(value: string): string[] { |
| 60 | + const values: string[] = []; |
| 61 | + let depth = 0; |
| 62 | + let start = 0; |
| 63 | + for (let index = 0; index < value.length; index += 1) { |
| 64 | + if (value[index] === '(') depth += 1; |
| 65 | + if (value[index] === ')') depth -= 1; |
| 66 | + if (/\s/u.test(value[index]!) && depth === 0) { |
| 67 | + if (start < index) values.push(value.slice(start, index)); |
| 68 | + start = index + 1; |
| 69 | + } |
| 70 | + } |
| 71 | + if (start < value.length) values.push(value.slice(start)); |
| 72 | + return values; |
| 73 | +} |
| 74 | + |
| 75 | +type SwitchSize = '' | 'base' | 'small'; |
| 76 | + |
| 77 | +function switchVariables(size: Exclude<SwitchSize, ''>): Record<string, string> { |
| 78 | + return { |
| 79 | + '--switch-knob-size': declaration(size === 'base' ? '.switch' : '.switch.sm', '--switch-knob-size'), |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +const derivedSizes = [ |
| 84 | + ['.row', 'min-height', 34, {}], |
| 85 | + ['.act', 'padding-top', 6, {}], |
| 86 | + ['.act', 'padding-right', 12, {}], |
| 87 | + ['.icon-btn', 'width', 24, {}], |
| 88 | + ['.icon-btn', 'height', 24, {}], |
| 89 | + ['.icon-btn svg', 'width', 15, {}], |
| 90 | + ['.icon-btn svg', 'height', 15, {}], |
| 91 | + ['.switch', 'width', 40, 'base'], |
| 92 | + ['.switch', 'height', 22, 'base'], |
| 93 | + ['.switch.sm', 'width', 30, 'small'], |
| 94 | + ['.switch.sm', 'height', 17, 'small'], |
| 95 | + ['.knob', 'width', 18, 'base'], |
| 96 | + ['.knob', 'height', 18, 'base'], |
| 97 | + ['.switch.sm .knob', 'width', 13, 'small'], |
| 98 | + ['.switch.sm .knob', 'height', 13, 'small'], |
| 99 | +] as const; |
| 100 | + |
| 101 | +function sizeValue(selector: string, property: string): string { |
| 102 | + if (selector === '.act' && property.startsWith('padding-')) { |
| 103 | + const padding = splitCssValues(declaration('.act', 'padding')); |
| 104 | + return property === 'padding-top' || property === 'padding-bottom' ? padding[0]! : padding[1]!; |
| 105 | + } |
| 106 | + return declaration(selector, property); |
| 107 | +} |
| 108 | + |
| 109 | +describe('settings design tokens', () => { |
| 110 | + it('rejects raw colours and dark-mode utilities', () => { |
| 111 | + expect(css.match(/#[\da-f]{3,8}\b/giu) ?? []).toHaveLength(0); |
| 112 | + expect(css.match(/\brgba?\s*\(/gu) ?? []).toHaveLength(0); |
| 113 | + expect(css.match(/\bdark:/gu) ?? []).toHaveLength(0); |
| 114 | + }); |
| 115 | + |
| 116 | + it('allows only token, pill, and circle radii and maps the five named radii', () => { |
| 117 | + const radii = [...css.matchAll(/border-radius\s*:\s*([^;]+);/gu)].map((match) => match[1]!.trim()); |
| 118 | + |
| 119 | + expect(radii.length).toBeGreaterThan(0); |
| 120 | + expect(radii.filter((radius) => !/^(?:var\(--r-[\w-]+\)|999px|50%)$/u.test(radius))).toEqual([]); |
| 121 | + |
| 122 | + const mappings = { |
| 123 | + '.tag': '--r-xs', |
| 124 | + '.act': '--r-sm', |
| 125 | + '.icon-btn': '--r-sm', |
| 126 | + '.page-search': '--r-sm', |
| 127 | + '.stat-card': '--r-md', |
| 128 | + } as const; |
| 129 | + for (const [selector, token] of Object.entries(mappings)) { |
| 130 | + expect(declaration(selector, 'border-radius')).toBe(`var(${token})`); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it('uses the ink token for the knob shadow', () => { |
| 135 | + expect(declaration('.knob', 'box-shadow')).toBe( |
| 136 | + '0 1px 2px color-mix(in srgb, var(--ink) 20%, transparent)', |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('keeps every listed control size at its current 14px value', () => { |
| 141 | + for (const [selector, property, expected, size] of derivedSizes) { |
| 142 | + const variables = size === '' ? {} : switchVariables(size); |
| 143 | + const value = sizeValue(selector, property); |
| 144 | + expect(value).toMatch(/calc\(|var\(--switch-knob-size\)/u); |
| 145 | + expect(resolvePx(value, 14, variables)).toBeCloseTo(expected, 5); |
| 146 | + } |
| 147 | + |
| 148 | + expect(resolvePx(declaration('.switch.on .knob', 'transform').match(/^translateX\((.*)\)$/u)![1]!, 14, switchVariables('base'))).toBeCloseTo(18, 5); |
| 149 | + expect(transformDistance('.switch.sm.on .knob', switchVariables('small'), 14)).toBeCloseTo(13, 5); |
| 150 | + }); |
| 151 | + |
| 152 | + it('grows every listed control size when the UI font grows to 20px', () => { |
| 153 | + for (const [selector, property, _expected, size] of derivedSizes) { |
| 154 | + const variables = size === '' ? {} : switchVariables(size); |
| 155 | + expect(resolvePx(sizeValue(selector, property), 20, variables)).toBeGreaterThan( |
| 156 | + resolvePx(sizeValue(selector, property), 14, variables), |
| 157 | + ); |
| 158 | + } |
| 159 | + expect(transformDistance('.switch.on .knob', switchVariables('base'), 20)).toBeGreaterThan( |
| 160 | + transformDistance('.switch.on .knob', switchVariables('base'), 14), |
| 161 | + ); |
| 162 | + expect(transformDistance('.switch.sm.on .knob', switchVariables('small'), 20)).toBeGreaterThan( |
| 163 | + transformDistance('.switch.sm.on .knob', switchVariables('small'), 14), |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it('derives switch tracks and travel from the same knob size', () => { |
| 168 | + for (const [fontSize, size, trackSelector, travelSelector] of [ |
| 169 | + [14, 'base', '.switch', '.switch.on .knob'], |
| 170 | + [14, 'small', '.switch.sm', '.switch.sm.on .knob'], |
| 171 | + [20, 'base', '.switch', '.switch.on .knob'], |
| 172 | + [20, 'small', '.switch.sm', '.switch.sm.on .knob'], |
| 173 | + ] as const) { |
| 174 | + const variables = switchVariables(size); |
| 175 | + const knob = resolvePx(variables['--switch-knob-size'], fontSize, variables); |
| 176 | + const trackWidth = resolvePx(declaration(trackSelector, 'width'), fontSize, variables); |
| 177 | + const trackHeight = resolvePx(declaration(trackSelector, 'height'), fontSize, variables); |
| 178 | + const travel = transformDistance(travelSelector, variables, fontSize); |
| 179 | + |
| 180 | + expect(trackWidth).toBeCloseTo(knob + travel + 4, 5); |
| 181 | + expect(trackHeight).toBeCloseTo(knob + 4, 5); |
| 182 | + expect(travel).toBeCloseTo(knob, 5); |
| 183 | + } |
| 184 | + }); |
| 185 | +}); |
0 commit comments