@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/assets/pano-bootstrap.css +17903 -0
  2. package/bin/__tests__/new/e2e.test.js +111 -0
  3. package/bin/__tests__/new/helpers.js +76 -0
  4. package/bin/__tests__/new/new.test.js +472 -0
  5. package/bin/new.js +530 -0
  6. package/bin/pano-plugin.js +162 -0
  7. package/bin/templates/plugin/README.md +30 -0
  8. package/bin/templates/plugin/_gitignore +15 -0
  9. package/bin/templates/plugin/build.gradle.kts +389 -0
  10. package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
  11. package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
  12. package/bin/templates/plugin/gradle.properties +55 -0
  13. package/bin/templates/plugin/gradlew +249 -0
  14. package/bin/templates/plugin/gradlew.bat +92 -0
  15. package/bin/templates/plugin/package.json.tpl +14 -0
  16. package/bin/templates/plugin/rollup.config.js +2 -0
  17. package/bin/templates/plugin/settings.gradle.kts +5 -0
  18. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
  19. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
  20. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
  21. package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
  22. package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
  23. package/bin/templates/plugin/src/main/resources/config.conf +1 -0
  24. package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
  25. package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
  26. package/bin/templates/plugin/src/main/resources/logo.png +0 -0
  27. package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
  28. package/bin/ui.js +96 -0
  29. package/package.json +48 -4
  30. package/src/config.js +191 -0
  31. package/src/controller/__tests__/controller.test.js +647 -0
  32. package/src/controller/index.js +604 -0
  33. package/src/dev.js +208 -0
  34. package/src/index.js +22 -0
  35. package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
  36. package/src/rollup/__tests__/controllers/fixture.js +143 -0
  37. package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
  38. package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
  39. package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
  40. package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
  41. package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
  42. package/src/rollup/__tests__/preset/preset.test.js +418 -0
  43. package/src/rollup/__tests__/rules/_build.js +39 -0
  44. package/src/rollup/__tests__/rules/_fixture.js +83 -0
  45. package/src/rollup/__tests__/rules/helpers.test.js +148 -0
  46. package/src/rollup/__tests__/rules/rules.test.js +259 -0
  47. package/src/rollup/__tests__/rules/stamp.test.js +113 -0
  48. package/src/rollup/__tests__/samples/samples.test.js +481 -0
  49. package/src/rollup/__tests__/seams/config.test.js +78 -0
  50. package/src/rollup/__tests__/seams/lock.test.js +124 -0
  51. package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
  52. package/src/rollup/__tests__/seams/stubs.test.js +52 -0
  53. package/src/rollup/__tests__/views/helpers.js +82 -0
  54. package/src/rollup/__tests__/views/views.test.js +522 -0
  55. package/src/rollup/__tests__/widgets/helpers.js +117 -0
  56. package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
  57. package/src/rollup/cli-build.js +90 -0
  58. package/src/rollup/cli-check.js +280 -0
  59. package/src/rollup/cli-classes.js +75 -0
  60. package/src/rollup/client-gen.js +67 -0
  61. package/src/rollup/controllers.js +434 -0
  62. package/src/rollup/entry.js +250 -0
  63. package/src/rollup/helpers.js +193 -0
  64. package/src/rollup/index.js +601 -0
  65. package/src/rollup/integrate.js +205 -0
  66. package/src/rollup/lock.js +230 -0
  67. package/src/rollup/meta.js +479 -0
  68. package/src/rollup/rules.js +950 -0
  69. package/src/rollup/samples.js +667 -0
  70. package/src/rollup/stamp.js +410 -0
  71. package/src/rollup/types.js +248 -0
  72. package/src/rollup/views.js +1205 -0
  73. package/src/rollup/widgets.js +1000 -0
  74. package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
  75. package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
  76. package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
  77. package/src/styles/__tests__/fallback/plugins.test.js +301 -0
  78. package/src/styles/__tests__/lint/check-styles.test.js +423 -0
  79. package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
  80. package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
  81. package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
  82. package/src/styles/bootstrap-classes.js +155 -0
  83. package/src/styles/check-styles.js +626 -0
  84. package/src/styles/fallback-css.js +1182 -0
  85. package/src/styles/plugin-css.js +237 -0
  86. package/src/styles/scan.js +206 -0
  87. package/src/styles/semantic-classes.js +436 -0
  88. package/src/styles/tokens.map.js +53 -0
  89. package/README.md +0 -3
@@ -0,0 +1,526 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import postcss from 'postcss';
6
+ import {
7
+ BASE_CSS_FILE,
8
+ JS_TOGGLED,
9
+ buildFallbackCss,
10
+ rewriteRgb,
11
+ scanSelector,
12
+ stringLiterals,
13
+ } from '../../fallback-css.js';
14
+
15
+ const here = path.dirname(fileURLToPath(import.meta.url));
16
+ const base = fs.readFileSync(path.join(here, 'fixtures/base.css'), 'utf8');
17
+
18
+ /** @param {string} used space separated @param {string} [css] */
19
+ function build(used, css = base) {
20
+ return buildFallbackCss({ baseCss: css, usedClasses: used.split(' ').filter(Boolean), ns: 'market' });
21
+ }
22
+
23
+ /** Text of the rule with exactly this selector list (first match), or null. */
24
+ function ruleText(css, selector) {
25
+ let found = null;
26
+
27
+ postcss.parse(css).walkRules((rule) => {
28
+ if (found === null && rule.selectors.map((s) => s.trim()).join(',') === selector) found = rule.toString();
29
+ });
30
+
31
+ return found;
32
+ }
33
+
34
+ /** The at-rule chain ("layer pano-fallback.components > scope ...") around every rule with this selector. */
35
+ function contextsOf(css, selector) {
36
+ const out = [];
37
+
38
+ postcss.parse(css).walkRules((rule) => {
39
+ if (!rule.selectors.some((s) => s.trim() === selector)) return;
40
+
41
+ const chain = [];
42
+
43
+ for (let p = rule.parent; p && p.type !== 'root'; p = p.parent) chain.unshift(`${p.name} ${p.params}`.trim());
44
+
45
+ out.push(chain);
46
+ });
47
+
48
+ return out;
49
+ }
50
+
51
+ const hasSelector = (css, selector) => contextsOf(css, selector).length > 0;
52
+
53
+ describe('output shape (doc 03 section 4.3)', () => {
54
+ test('layer statement, one scope per sublayer, scoped to the namespace', () => {
55
+ const { css } = build('btn');
56
+ const lines = css.split('\n');
57
+
58
+ expect(lines[1]).toBe('@layer pano-defaults, pano-fallback.components, pano-fallback.utilities, pano-plugin;');
59
+ expect(css).toContain('@layer pano-fallback.components {');
60
+ expect(css).toContain(
61
+ '@scope (.pano-fb[data-pano-fb="market"]) to (.pano-fb-stop, .pano-fb:not([data-pano-fb="market"])) {',
62
+ );
63
+ });
64
+
65
+ test('a namespace that is not a plain token is refused', () => {
66
+ expect(() => buildFallbackCss({ baseCss: base, usedClasses: [], ns: 'Bad Ns' })).toThrow(/namespace/);
67
+ expect(() => buildFallbackCss({ baseCss: base, usedClasses: [], ns: 'a"]' })).toThrow(/namespace/);
68
+ });
69
+
70
+ test('an empty base gives an empty sheet', () => {
71
+ expect(buildFallbackCss({ baseCss: '', usedClasses: ['btn'], ns: 'x' })).toEqual({ css: '', classes: [] });
72
+ });
73
+
74
+ test('the output parses and holds no !important and no comment but the banner', () => {
75
+ const { css } = build('btn btn-primary mb-0 text-bg-success modal tooltip');
76
+
77
+ expect(() => postcss.parse(css)).not.toThrow();
78
+ expect(css).not.toContain('!important');
79
+ expect(css.match(/\/\*/g)).toHaveLength(1);
80
+ });
81
+
82
+ test('is deterministic and independent of the order of the used classes', () => {
83
+ const a = build('btn card mb-0 fade');
84
+ const b = build('fade mb-0 card btn');
85
+
86
+ expect(a).toEqual(b);
87
+ expect(build('btn card mb-0 fade')).toEqual(a);
88
+ });
89
+
90
+ test('snapshot for a five-class input', () => {
91
+ expect(build('btn btn-primary card mb-0 text-bg-success')).toMatchSnapshot();
92
+ });
93
+ });
94
+
95
+ describe('STEP 1 / 4: purge', () => {
96
+ test('keeps only rules whose classes are all in use', () => {
97
+ const { css, classes } = build('btn card mb-0');
98
+
99
+ expect(hasSelector(css, '.btn')).toBe(true);
100
+ expect(hasSelector(css, '.card')).toBe(true);
101
+ expect(hasSelector(css, '.mb-0')).toBe(true);
102
+ expect(hasSelector(css, '.card-body')).toBe(false);
103
+ expect(hasSelector(css, '.mb-1')).toBe(false);
104
+ expect(hasSelector(css, '.btn-primary')).toBe(false);
105
+ expect(classes).toContain('mb-0');
106
+ expect(classes).not.toContain('mb-1');
107
+ });
108
+
109
+ test('a comma list is cut selector by selector', () => {
110
+ const { css } = build('btn');
111
+
112
+ expect(ruleText(css, '.btn:disabled,.btn.disabled')).not.toBeNull();
113
+ // `.btn-check:checked + .btn` needs `btn-check`
114
+ expect(css).not.toContain('btn-check');
115
+ });
116
+
117
+ test('every selector of a compound rule needs all of its classes', () => {
118
+ expect(hasSelector(build('nav-pills').css, '.nav-pills .nav-link.active')).toBe(false);
119
+ expect(hasSelector(build('nav-pills nav-link').css, '.nav-pills .nav-link.active')).toBe(true);
120
+ });
121
+
122
+ test("Bootstrap's JS-toggled classes are always in use", () => {
123
+ const { css } = build('');
124
+
125
+ for (const cls of JS_TOGGLED) expect(JS_TOGGLED).toContain(cls);
126
+ expect(hasSelector(css, '.fade:not(.show)')).toBe(true);
127
+ expect(hasSelector(css, '.collapse:not(.show)')).toBe(true);
128
+ expect(hasSelector(css, '.fade')).toBe(true);
129
+ });
130
+
131
+ test('classless selectors survive only from the Reboot list', () => {
132
+ const { css } = build('');
133
+
134
+ expect(hasSelector(css, '*')).toBe(true);
135
+ expect(hasSelector(css, 'a')).toBe(true);
136
+ expect(hasSelector(css, 'body')).toBe(false);
137
+ expect(hasSelector(css, '[type=button]:not(:disabled)')).toBe(false);
138
+ expect(hasSelector(css, 'optgroup')).toBe(false);
139
+ expect(css).not.toContain('scroll-behavior');
140
+ });
141
+
142
+ test('a media block that ends up empty is dropped, a kept one stays', () => {
143
+ const { css } = build('');
144
+ const media = [];
145
+
146
+ postcss.parse(css).walkAtRules('media', (m) => media.push(m.params));
147
+ expect(media).toEqual(['(prefers-reduced-motion: reduce)']);
148
+ });
149
+
150
+ test('@keyframes stays only when a kept rule names it', () => {
151
+ expect(build('spinner-border').css).toContain('@keyframes spinner-border');
152
+ expect(build('spinner-border').css).not.toContain('unused-frames');
153
+ expect(build('btn').css).not.toContain('@keyframes');
154
+ });
155
+
156
+ test('@font-face and @charset are dropped', () => {
157
+ const { css } = build('btn');
158
+
159
+ expect(css).not.toContain('@font-face');
160
+ expect(css).not.toContain('@charset');
161
+ });
162
+
163
+ test('unknown classes are ignored, a leading dot is tolerated', () => {
164
+ expect(build('market-product-card btn').css).toEqual(build('btn').css);
165
+ expect(build('.btn').css).toEqual(build('btn').css);
166
+ });
167
+ });
168
+
169
+ describe('STEP 5: !important and sublayers', () => {
170
+ test('a rule that is !important all through is a utility, the rest a component', () => {
171
+ const { css } = build('btn mb-0 text-bg-success border');
172
+
173
+ expect(contextsOf(css, '.mb-0')[0][0]).toBe('layer pano-fallback.utilities');
174
+ expect(contextsOf(css, '.btn')[0][0]).toBe('layer pano-fallback.components');
175
+ // `.border` mixes an !important and a plain declaration -> component
176
+ expect(contextsOf(css, '.border')[0][0]).toBe('layer pano-fallback.components');
177
+ });
178
+
179
+ test('the declarations keep their values without !important', () => {
180
+ expect(ruleText(build('mb-0').css, '.mb-0')).toContain('margin-bottom: 0;');
181
+ expect(ruleText(build('border').css, '.border')).toContain('border: 1px solid #dee2e6;');
182
+ });
183
+
184
+ test('a repeated block of the base is emitted once', () => {
185
+ const matches = build('mb-0').css.match(/\.mb-0 \{/g);
186
+
187
+ expect(matches).toHaveLength(1);
188
+ });
189
+
190
+ test('cascade: an unlayered theme rule beats a kept utility, a utility beats a component', () => {
191
+ const { css } = build('mb-0 btn');
192
+ const rank = new Map();
193
+
194
+ // layer order as the statement declares it
195
+ ['pano-defaults', 'pano-fallback.components', 'pano-fallback.utilities', 'pano-plugin'].forEach((l, i) => rank.set(l, i));
196
+
197
+ /** Winner among candidates {layer|null, decl}: unlayered beats every layer, later layer beats earlier. */
198
+ const winner = (list) => list.reduce((a, b) => ((b.layer === null ? 99 : rank.get(b.layer)) >= (a.layer === null ? 99 : rank.get(a.layer)) ? b : a));
199
+ const layerOf = (selector) => contextsOf(css, selector)[0][0].replace(/^layer /, '');
200
+
201
+ expect(css).not.toContain('!important');
202
+ expect(layerOf('.mb-0')).toBe('pano-fallback.utilities');
203
+ expect(winner([{ layer: layerOf('.mb-0'), v: 'mb-0' }, { layer: null, v: 'theme' }]).v).toBe('theme');
204
+ expect(winner([{ layer: layerOf('.btn'), v: 'component' }, { layer: layerOf('.mb-0'), v: 'utility' }]).v).toBe('utility');
205
+ // nothing the sheet emits sits outside a layer
206
+ postcss.parse(css).each((node) => {
207
+ expect(node.type === 'comment' || (node.type === 'atrule' && node.name === 'layer')).toBe(true);
208
+ });
209
+ });
210
+ });
211
+
212
+ describe('STEP 6: rgba(var(--bs-X-rgb)) rewrite', () => {
213
+ test('a token colour becomes a color-mix of the variable (case insensitive)', () => {
214
+ const { css } = build('text-bg-success card');
215
+
216
+ expect(css).toContain(
217
+ 'background-color: color-mix(in srgb, var(--bs-success) calc(var(--bs-bg-opacity, 1) * 100%), transparent);',
218
+ );
219
+ expect(css).toContain('color-mix(in srgb, var(--bs-body-color) calc(0.125 * 100%), transparent)');
220
+ });
221
+
222
+ test('a channel that is not a token colour stays', () => {
223
+ const { css } = build('btn btn-primary card');
224
+
225
+ expect(css).toContain('rgba(var(--bs-btn-focus-shadow-rgb), .5)');
226
+ expect(css).toContain('rgba(var(--bs-light-rgb), 0.2)');
227
+ expect(css).toContain('--bs-btn-focus-shadow-rgb: 42, 95, 155;');
228
+ });
229
+
230
+ test('rewriteRgb handles slash alpha, no alpha and leaves other functions alone', () => {
231
+ expect(rewriteRgb('rgb(var(--bs-primary-rgb) / 50%)')).toBe(
232
+ 'color-mix(in srgb, var(--bs-primary) calc(50% * 100%), transparent)',
233
+ );
234
+ expect(rewriteRgb('rgb(var(--bs-primary-rgb))')).toBe('var(--bs-primary)');
235
+ expect(rewriteRgb('rgba(255, 255, 255, 0.1)')).toBe('rgba(255, 255, 255, 0.1)');
236
+ expect(rewriteRgb('rgba(var(--bs-dark-rgb), 0.5)')).toBe('rgba(var(--bs-dark-rgb), 0.5)');
237
+ expect(rewriteRgb('0 0 0 1px rgba(var(--bs-info-rgb), .1), 0 1px rgba(var(--bs-danger-rgb), .2)')).toBe(
238
+ '0 0 0 1px color-mix(in srgb, var(--bs-info) calc(.1 * 100%), transparent), 0 1px color-mix(in srgb, var(--bs-danger) calc(.2 * 100%), transparent)',
239
+ );
240
+ });
241
+
242
+ test('a swap of two rewritten channels becomes a swap of the colours (a:hover)', () => {
243
+ const { css } = build('');
244
+
245
+ expect(ruleText(css, 'a:hover')).toContain('--bs-link-color: var(--bs-link-hover-color);');
246
+ expect(css).not.toContain('--bs-link-color-rgb');
247
+ });
248
+
249
+ test('the -rgb variables of rewritten colours are not declared, the others are', () => {
250
+ const { css } = build('btn card');
251
+ const scope = ruleText(css, ':scope');
252
+
253
+ expect(scope).not.toContain('--bs-primary-rgb');
254
+ expect(scope).not.toContain('--bs-body-color-rgb');
255
+ expect(scope).toContain('--bs-light-rgb: 245, 247, 250;');
256
+ });
257
+ });
258
+
259
+ describe('STEP 2: literal defaults and palette extras', () => {
260
+ const scope = () => ruleText(build('btn').css, ':scope');
261
+
262
+ test('every base :root declaration that is not a token is defined (--bs-border-style)', () => {
263
+ expect(scope()).toContain('--bs-border-style: solid;');
264
+ expect(scope()).toContain('--bs-white: #fff;');
265
+ expect(scope()).toContain('--bs-breakpoint-md: 768px;');
266
+ expect(scope()).toContain('--bs-btn-close-filter: ;');
267
+ });
268
+
269
+ test('a variable a token mirrors is bound to the token, not given its literal', () => {
270
+ expect(scope()).toContain('--bs-body-bg: var(--pano-color-bg);');
271
+ expect(scope()).toContain('--bs-primary: var(--pano-color-primary);');
272
+ expect(scope()).not.toContain('--bs-body-bg: #fff');
273
+ expect(scope()).not.toContain('--bs-primary: #044389');
274
+ });
275
+
276
+ test('palette extras are one block per palette, without token variables', () => {
277
+ const { css } = build('btn');
278
+ const dark = ruleText(css, ":scope:where([data-bs-theme='dark'] *)");
279
+ const copper = ruleText(css, ":scope:where([data-bs-theme='copper'] *)");
280
+
281
+ expect(dark).toContain('color-scheme: dark;');
282
+ expect(dark).toContain('--bs-code-color: #e685b5;');
283
+ expect(dark).not.toContain('--bs-body-bg');
284
+ expect(dark).not.toContain('--bs-copper-only');
285
+ expect(copper).toContain('--bs-code-color: #e685b5;');
286
+ expect(copper).toContain('--bs-copper-only: 1px;');
287
+ expect(copper).not.toContain('--bs-primary:');
288
+ expect(hasSelector(css, ":scope:where([data-bs-theme='light'] *)")).toBe(false);
289
+ });
290
+
291
+ test('values the Sass left unevaluated and variables the scope derives are not copied', () => {
292
+ const { css } = build('btn');
293
+
294
+ expect(css).not.toContain('$primary');
295
+ expect(ruleText(css, ":scope:where([data-bs-theme='dark'] *)")).not.toContain('bg-subtle');
296
+ expect(ruleText(css, ":scope:where([data-bs-theme='dark'] *)")).not.toContain('--bs-heading-color');
297
+ });
298
+
299
+ test('token bindings, then derived colours, then the body text', () => {
300
+ const text = scope();
301
+
302
+ expect(text).toContain('--bs-primary-bg-subtle: color-mix(in srgb, var(--pano-color-primary) 12%, transparent);');
303
+ expect(text).toContain('--bs-primary-text-emphasis: color-mix(in srgb, var(--pano-color-primary) 60%, #fff);');
304
+ expect(text).toContain('--bs-primary-border-subtle: color-mix(in srgb, var(--pano-color-primary) 75%, #000);');
305
+ expect(text).toContain('--bs-heading-color: var(--pano-color-heading);');
306
+ expect(text).toContain(
307
+ '--bs-border-color-translucent: color-mix(in srgb, var(--pano-color-text) 17.5%, transparent);',
308
+ );
309
+ expect(text).toContain('--bs-focus-ring-color: color-mix(in srgb, var(--pano-color-primary) 25%, transparent);');
310
+ expect(text).toContain('font-family: var(--bs-body-font-family);');
311
+ expect(text.indexOf('--bs-border-style')).toBeLessThan(text.indexOf('--bs-body-bg'));
312
+ expect(text.indexOf('--bs-body-bg')).toBeLessThan(text.indexOf('--bs-primary-bg-subtle'));
313
+ expect(text.indexOf('--bs-primary-bg-subtle')).toBeLessThan(text.indexOf('--bs-heading-color'));
314
+ });
315
+ });
316
+
317
+ describe('STEP 3: [data-bs-theme=X] descendant rules', () => {
318
+ test('the prefix becomes a :scope:where() ancestor test', () => {
319
+ const { css } = build('form-select');
320
+
321
+ expect(hasSelector(css, ":scope:where([data-bs-theme='dark'] *) .form-select")).toBe(true);
322
+ expect(hasSelector(css, ":scope:where([data-bs-theme='copper'] *) .form-select")).toBe(true);
323
+ expect(css).not.toContain('[data-bs-theme=dark] .form-select');
324
+ });
325
+
326
+ test('a rule whose class is not in use is dropped with its palette prefixes', () => {
327
+ expect(build('card').css).not.toContain('.form-select');
328
+ expect(build('card').css).not.toContain('.blocks');
329
+ });
330
+
331
+ test('an attribute on the element itself is left alone', () => {
332
+ const { css } = build('navbar');
333
+
334
+ expect(hasSelector(css, '.navbar[data-bs-theme=dark]')).toBe(true);
335
+ });
336
+ });
337
+
338
+ describe('binding templates (doc 03 section 4.3)', () => {
339
+ test('only for classes in use, after the Bootstrap rule of the same class', () => {
340
+ const { css } = build('btn btn-primary');
341
+ const rules = contextsOf(css, '.btn-primary');
342
+
343
+ expect(rules).toHaveLength(2);
344
+ expect(hasSelector(css, '.btn-secondary')).toBe(false);
345
+
346
+ const text = css.slice(css.lastIndexOf('.btn-primary {'));
347
+
348
+ expect(text).toContain('--bs-btn-color: var(--pano-color-on-primary);');
349
+ expect(text).toContain('--bs-btn-bg: var(--pano-color-primary);');
350
+ expect(text).toContain('--bs-btn-border-color: var(--pano-color-primary);');
351
+ expect(text).toContain('--bs-btn-hover-bg: color-mix(in srgb, var(--pano-color-primary) 85%, #000);');
352
+ expect(text).toContain('--bs-btn-hover-border-color: color-mix(in srgb, var(--pano-color-primary) 85%, #000);');
353
+ expect(text).toContain('--bs-btn-active-bg: var(--pano-color-primary);');
354
+ expect(css.lastIndexOf('.btn-primary {')).toBeGreaterThan(css.indexOf('--bs-btn-padding-x'));
355
+ });
356
+
357
+ test('no template for a class that is not in use', () => {
358
+ expect(build('btn').css).not.toContain('--pano-color-on-primary');
359
+ expect(build('card').css).not.toContain('.btn:not(.btn-link)');
360
+ });
361
+
362
+ test('btn border except .btn-link; nav-pills; alert; outline', () => {
363
+ const { css } = build('btn nav-pills alert alert-danger btn-outline-success');
364
+
365
+ expect(ruleText(css, '.btn:not(.btn-link)')).toContain(
366
+ 'border: 1.5px solid color-mix(in srgb, var(--pano-color-text) 10%, transparent);',
367
+ );
368
+ expect(ruleText(css, '.nav-pills')).toContain('--bs-nav-pills-link-active-bg: var(--pano-color-primary);');
369
+ expect(ruleText(css, '.nav-pills')).toContain('--bs-nav-pills-link-active-color: var(--pano-color-on-primary);');
370
+
371
+ const alert = css.slice(css.lastIndexOf('.alert-danger {'));
372
+
373
+ expect(alert).toContain('--bs-alert-color: color-mix(in srgb, var(--pano-color-danger) 85%, #000);');
374
+ expect(alert).toContain('--bs-alert-border-color: color-mix(in srgb, var(--pano-color-danger) 25%, transparent);');
375
+ expect(alert).toContain('border-radius: var(--pano-radius);');
376
+
377
+ const outline = css.slice(css.lastIndexOf('.btn-outline-success {'));
378
+
379
+ expect(outline).toContain('--bs-btn-color: var(--pano-color-success);');
380
+ expect(outline).toContain('--bs-btn-hover-bg: var(--pano-color-success);');
381
+ expect(outline).toContain('--bs-btn-hover-color: #fff;');
382
+ });
383
+
384
+ test('utility templates sit in the utilities sublayer, after the Bootstrap utility', () => {
385
+ const { css } = build('text-bg-success bg-warning-subtle text-info-emphasis');
386
+ const contexts = contextsOf(css, '.text-bg-success');
387
+
388
+ expect(contexts).toHaveLength(2);
389
+ expect(contexts.every((chain) => chain[0] === 'layer pano-fallback.utilities')).toBe(true);
390
+
391
+ const last = css.slice(css.lastIndexOf('.text-bg-success {'));
392
+
393
+ expect(last).toContain('background-color: var(--pano-color-success);');
394
+ expect(last).toContain('color: #fff;');
395
+ expect(css).toContain('background-color: color-mix(in srgb, var(--pano-color-warning) 12%, transparent);');
396
+ expect(css).toContain('color: var(--pano-color-warning);');
397
+ expect(css).toContain('border-color: color-mix(in srgb, var(--pano-color-warning) 20%, transparent);');
398
+ expect(css).toContain('color: color-mix(in srgb, var(--pano-color-info) 80%, #000);');
399
+ });
400
+
401
+ test('contrast colours: primary / secondary have tokens, the rest are Bootstrap results', () => {
402
+ const { css } = build('text-bg-warning text-bg-danger text-bg-secondary');
403
+
404
+ expect(css).toContain('background-color: var(--pano-color-warning);\n color: #000;');
405
+ expect(css).toContain('background-color: var(--pano-color-danger);\n color: #fff;');
406
+ expect(css).toContain('background-color: var(--pano-color-secondary);\n color: var(--pano-color-on-secondary);');
407
+ });
408
+ });
409
+
410
+ describe('body-level block', () => {
411
+ test('modal: the backdrop rules are emitted outside the scope', () => {
412
+ const { css } = build('modal');
413
+
414
+ expect(contextsOf(css, '.modal-backdrop')).toEqual([['layer pano-fallback.components']]);
415
+ expect(contextsOf(css, '.modal-backdrop.fade')).toEqual([['layer pano-fallback.components']]);
416
+ expect(contextsOf(css, '.modal-backdrop.show')).toEqual([['layer pano-fallback.components']]);
417
+ expect(contextsOf(css, '.modal-open .modal')).toEqual([['layer pano-fallback.components']]);
418
+ // the modal itself stays inside the scope
419
+ expect(contextsOf(css, '.modal')[0][1]).toMatch(/^scope /);
420
+ });
421
+
422
+ test('no modal in use, no backdrop', () => {
423
+ expect(build('card').css).not.toContain('modal');
424
+ });
425
+
426
+ test('tooltip: its rules, the :scope declarations on .tooltip and the generic .fade', () => {
427
+ const { css } = build('tooltip');
428
+
429
+ expect(contextsOf(css, '.tooltip')).toHaveLength(2); // the repeated declarations + the Bootstrap rule
430
+ expect(contextsOf(css, '.tooltip .tooltip-arrow')).toEqual([['layer pano-fallback.components']]);
431
+
432
+ const repeated = ruleText(css, '.tooltip');
433
+
434
+ expect(repeated).toContain('--bs-body-bg: var(--pano-color-bg);');
435
+ expect(repeated).toContain('font-family: var(--bs-body-font-family);');
436
+ expect(hasSelector(css, ".tooltip:where([data-bs-theme='dark'] *)")).toBe(true);
437
+ // .fade also outside the scope, because the tooltip element sits in <body>
438
+ expect(contextsOf(css, '.fade').some((chain) => chain.length === 1)).toBe(true);
439
+ });
440
+
441
+ test('popover and tooltip share one repeated block', () => {
442
+ const { css } = build('tooltip popover');
443
+
444
+ expect(ruleText(css, '.tooltip,.popover')).not.toBeNull();
445
+ });
446
+
447
+ test('without tooltip or popover the generic .fade stays inside the scope', () => {
448
+ const { css } = build('modal');
449
+
450
+ expect(contextsOf(css, '.fade').every((chain) => chain.length > 1)).toBe(true);
451
+ });
452
+ });
453
+
454
+ describe('classes', () => {
455
+ test('lists every base class the sheet covers, sorted', () => {
456
+ const { classes } = build('btn btn-primary mb-0 market-x');
457
+
458
+ expect(classes).toEqual([...classes].sort());
459
+ expect(classes).toEqual(expect.arrayContaining(['btn', 'btn-primary', 'mb-0', 'fade', 'show']));
460
+ expect(classes).not.toContain('market-x');
461
+ });
462
+ });
463
+
464
+ describe('helpers', () => {
465
+ test('scanSelector finds classes in pseudo-classes and types at compound starts only', () => {
466
+ expect(scanSelector('.btn-check:checked + .btn')).toEqual({ classes: ['btn-check', 'btn'], types: [] });
467
+ expect(scanSelector(':not(.btn-check) + .btn:active')).toEqual({ classes: ['btn-check', 'btn'], types: [] });
468
+ expect(scanSelector('a:not([href]):not([class])')).toEqual({ classes: [], types: ['a'] });
469
+ expect(scanSelector('.table > :not(caption) > * > *')).toEqual({ classes: ['table'], types: ['caption', '*', '*'] });
470
+ expect(scanSelector('.bs-tooltip-auto[data-popper-placement^=top] .tooltip-arrow::before').classes).toEqual([
471
+ 'bs-tooltip-auto',
472
+ 'tooltip-arrow',
473
+ ]);
474
+ expect(scanSelector('li:nth-child(2n+1)')).toEqual({ classes: [], types: ['li'] });
475
+ expect(scanSelector(':root')).toEqual({ classes: [], types: [] });
476
+ expect(scanSelector('.col-md-1\\.5').classes).toEqual(['col-md-1.5']);
477
+ });
478
+
479
+ test('stringLiterals reads quotes, templates (quasis and ${} literals) and skips comments', () => {
480
+ const code = [
481
+ "const a = 'badge bg-success'; // 'ignored comment'",
482
+ 'const b = "btn btn-primary";',
483
+ '/* "block comment" */',
484
+ 'const c = `text-bg-${kind} ${ok ? "is-valid" : \'is-invalid\'} px-2`;',
485
+ ].join('\n');
486
+ const found = stringLiterals(code);
487
+
488
+ expect(found).toContain('badge bg-success');
489
+ expect(found).toContain('btn btn-primary');
490
+ expect(found).toContain('is-valid');
491
+ expect(found).toContain('is-invalid');
492
+ expect(found.some((s) => s.includes('text-bg-'))).toBe(true);
493
+ expect(found.some((s) => s.includes('px-2'))).toBe(true);
494
+ expect(found).not.toContain('ignored comment');
495
+ expect(found).not.toContain('block comment');
496
+ });
497
+ });
498
+
499
+ describe('the real base sheet', () => {
500
+ const real = fs.readFileSync(BASE_CSS_FILE, 'utf8');
501
+
502
+ test('a small view gets a small, valid, layered sheet', () => {
503
+ const { css, classes } = buildFallbackCss({
504
+ baseCss: real,
505
+ usedClasses: ['card', 'card-body', 'btn', 'btn-primary', 'text-bg-success', 'mb-0', 'form-control'],
506
+ ns: 'market',
507
+ });
508
+
509
+ expect(() => postcss.parse(css)).not.toThrow();
510
+ expect(css).not.toContain('!important');
511
+ expect(css).not.toContain('@font-face');
512
+ expect(css.length).toBeLessThan(real.length / 4);
513
+ expect(classes).toEqual(expect.arrayContaining(['card', 'card-body', 'btn-primary', 'text-bg-success', 'mb-0']));
514
+ expect(css).toContain('--bs-border-style: solid;');
515
+ expect(css).toContain('--bs-body-bg: var(--pano-color-bg);');
516
+ expect(contextsOf(css, '.text-bg-success').length).toBeGreaterThanOrEqual(2);
517
+ expect(css).not.toMatch(/\.col-md-6/);
518
+ });
519
+
520
+ test('the modal backdrop and a spinner survive in the real sheet', () => {
521
+ const { css } = buildFallbackCss({ baseCss: real, usedClasses: ['modal', 'modal-dialog', 'spinner-border'], ns: 'market' });
522
+
523
+ expect(contextsOf(css, '.modal-backdrop')[0]).toEqual(['layer pano-fallback.components']);
524
+ expect(css).toContain('@keyframes spinner-border');
525
+ });
526
+ });