|
6 | 6 | generateJson, |
7 | 7 | generateSvg, |
8 | 8 | generateTailwind, |
| 9 | + reservedSemanticTokens, |
9 | 10 | sanitizeFilename, |
10 | 11 | } from './exporters' |
11 | 12 |
|
@@ -105,6 +106,107 @@ describe('browser palette exporters', () => { |
105 | 106 | ) |
106 | 107 | }) |
107 | 108 |
|
| 109 | + it('reserves every semantic role token even when roles are unassigned', () => { |
| 110 | + const colors = reservedSemanticTokens.map((token, index) => ({ |
| 111 | + ...(index % 2 === 0 ? red : blue), |
| 112 | + id: `reserved-${index}`, |
| 113 | + name: token |
| 114 | + .split('-') |
| 115 | + .map((part) => `${part[0].toUpperCase()}${part.slice(1)}`) |
| 116 | + .join(' '), |
| 117 | + })) |
| 118 | + expect(exportTokens(colors)).toEqual( |
| 119 | + reservedSemanticTokens.map((token) => `${token}-2`), |
| 120 | + ) |
| 121 | + const withoutRoles = generateTailwind({ |
| 122 | + ...palette, |
| 123 | + colors, |
| 124 | + roles: {}, |
| 125 | + }) |
| 126 | + const withRoles = generateTailwind({ |
| 127 | + ...palette, |
| 128 | + colors, |
| 129 | + roles: { primaryAction: colors[0].id }, |
| 130 | + }) |
| 131 | + const keys = (output: string) => |
| 132 | + [...output.matchAll(/^\s+'([^']+)':/gm)].map((match) => match[1]) |
| 133 | + expect(keys(withRoles).slice(0, colors.length)).toEqual( |
| 134 | + keys(withoutRoles).slice(0, colors.length), |
| 135 | + ) |
| 136 | + }) |
| 137 | + |
| 138 | + it('allocates deterministic suffixes across reserved and existing suffixes', () => { |
| 139 | + const colors = [ |
| 140 | + { ...red, id: 'one', name: 'Role primary action' }, |
| 141 | + { ...blue, id: 'two', name: 'Role primary action 2' }, |
| 142 | + { ...red, id: 'three', name: 'Role primary action' }, |
| 143 | + { ...blue, id: 'four', name: 'Ordinary' }, |
| 144 | + { ...red, id: 'five', name: 'Ordinary' }, |
| 145 | + { ...blue, id: 'six', name: 'Ordinary 2' }, |
| 146 | + ] |
| 147 | + expect(exportTokens(colors)).toEqual([ |
| 148 | + 'role-primary-action-2', |
| 149 | + 'role-primary-action-2-2', |
| 150 | + 'role-primary-action-3', |
| 151 | + 'ordinary', |
| 152 | + 'ordinary-2', |
| 153 | + 'ordinary-2-2', |
| 154 | + ]) |
| 155 | + }) |
| 156 | + |
| 157 | + it.each([ |
| 158 | + 'Role primary action', |
| 159 | + 'ROLE PRIMARY ACTION', |
| 160 | + 'role---primary___action!!!', |
| 161 | + ])('normalizes the reserved-name variant %s safely', (name) => { |
| 162 | + expect(exportTokens([{ ...red, name }])).toEqual(['role-primary-action-2']) |
| 163 | + }) |
| 164 | + |
| 165 | + it('keeps unnamed fallbacks and ordinary names unchanged', () => { |
| 166 | + expect(exportTokens([red, blue])).toEqual(['palette-1', 'palette-2']) |
| 167 | + expect( |
| 168 | + exportTokens([ |
| 169 | + { ...red, name: 'Primary action' }, |
| 170 | + { ...blue, name: 'Surface neutral' }, |
| 171 | + ]), |
| 172 | + ).toEqual(['primary-action', 'surface-neutral']) |
| 173 | + }) |
| 174 | + |
| 175 | + it('uses collision-safe base tokens consistently in CSS and Tailwind', () => { |
| 176 | + const collisionPalette = { |
| 177 | + ...palette, |
| 178 | + colors: [ |
| 179 | + { ...red, name: 'Role primary action' }, |
| 180 | + { ...blue, name: 'Role primary action' }, |
| 181 | + ], |
| 182 | + roles: { |
| 183 | + primaryAction: red.id, |
| 184 | + pageBackground: blue.id, |
| 185 | + }, |
| 186 | + } |
| 187 | + const css = generateCss(collisionPalette) |
| 188 | + expect(css).toContain('--color-role-primary-action-2: #ff0000;') |
| 189 | + expect(css).toContain('--color-role-primary-action-3: #0000ff;') |
| 190 | + expect(css).toContain( |
| 191 | + '--role-primary-action: var(--color-role-primary-action-2);', |
| 192 | + ) |
| 193 | + expect(css).toContain( |
| 194 | + '--role-page-background: var(--color-role-primary-action-3);', |
| 195 | + ) |
| 196 | + |
| 197 | + const tailwind = generateTailwind(collisionPalette) |
| 198 | + const keys = [...tailwind.matchAll(/^\s+'([^']+)':/gm)].map( |
| 199 | + (match) => match[1], |
| 200 | + ) |
| 201 | + expect(new Set(keys).size).toBe(keys.length) |
| 202 | + expect(keys).toEqual([ |
| 203 | + 'role-primary-action-2', |
| 204 | + 'role-primary-action-3', |
| 205 | + 'role-page-background', |
| 206 | + 'role-primary-action', |
| 207 | + ]) |
| 208 | + }) |
| 209 | + |
108 | 210 | it('keeps duplicate-color semantic ownership distinct', () => { |
109 | 211 | const duplicate = { ...red, id: 'second-red', name: 'Second red' } |
110 | 212 | const duplicatePalette = { |
|
0 commit comments