@astryxdesign/cli 0.7.5-canary.2af4d30 → 0.7.5-canary.2b0f9a1

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 (30) hide show
  1. package/api/template/tokenizer-showcase.test.mjs +47 -0
  2. package/api/theme/build/build.adaptation-enrollment.test.mjs +4 -0
  3. package/api/theme/build/build.icon-entry-variants.test.mjs +134 -0
  4. package/api/theme/build/build.icon-function-entries.test.mjs +499 -0
  5. package/api/theme/build/build.icon-variants.test.mjs +105 -0
  6. package/api/theme/build/build.mjs +362 -109
  7. package/api/theme/build/build.project-core.test.mjs +63 -8
  8. package/api/theme/build/build.test.mjs +77 -0
  9. package/api/theme/build/icon-imports.d.mts +12 -2
  10. package/api/theme/build/icon-imports.mjs +36 -11
  11. package/assets/codemods/transforms/next/__tests__/migrate-codeblock-tokenizer-to-language.test.mjs +130 -0
  12. package/assets/codemods/transforms/next/index.mjs +11 -1
  13. package/assets/codemods/transforms/next/migrate-codeblock-tokenizer-to-language.mjs +315 -0
  14. package/assets/docs/author-a-theme.doc.mjs +5 -1
  15. package/assets/docs/icons.doc.mjs +82 -1
  16. package/assets/docs/theme.doc.dense.mjs +8 -0
  17. package/assets/docs/theme.doc.mjs +16 -0
  18. package/assets/docs/theme.doc.zh.mjs +11 -0
  19. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.doc.mjs +6 -4
  20. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.tsx +61 -55
  21. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.doc.mjs +6 -4
  22. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.tsx +68 -37
  23. package/assets/templates/blocks/components/Thumbnail/ThumbnailDisabled.doc.mjs +1 -1
  24. package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +13 -6
  25. package/assets/theme.template.ts +18 -5
  26. package/clients/cli/commands/build-theme.outside-project.test.mjs +140 -0
  27. package/foundation/agent-docs/agent-docs.mjs +1 -0
  28. package/foundation/agent-docs/agent-docs.test.mjs +5 -0
  29. package/foundation/doc-compiler/doc-loads.test.mjs +2 -6
  30. package/package.json +9 -9
@@ -0,0 +1,47 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Contract test for the Tokenizer showcase block.
5
+ * @input The shipped TokenizerShowcase block source.
6
+ * @output Guards the block's search source: typing in the showcase must list
7
+ * matching tags rather than an empty menu.
8
+ * @position CLI template regression coverage; runs in the node test project.
9
+ * The block is the hero example of the Tokenizer docs page. Like the other
10
+ * template contract tests here it reads the block's source instead of
11
+ * importing it, so it needs no built `@astryxdesign/core`.
12
+ */
13
+
14
+ import fs from 'node:fs';
15
+ import path from 'node:path';
16
+ import {fileURLToPath} from 'node:url';
17
+ import {describe, expect, it} from 'vitest';
18
+
19
+ const sourcePath = path.resolve(
20
+ path.dirname(fileURLToPath(import.meta.url)),
21
+ '../../assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx',
22
+ );
23
+ const source = fs.readFileSync(sourcePath, 'utf8');
24
+
25
+ // Evaluate the block's tag list and search source on their own, with the
26
+ // type annotations removed, so the test exercises the shipped logic.
27
+ function loadSearchSource() {
28
+ const tags = source.match(/const tags[^=]*=\s*(\[[\s\S]*?\n\]);/)?.[1];
29
+ const searchSource = source.match(
30
+ /const source[^=]*=\s*(\{[\s\S]*?\n\});/,
31
+ )?.[1];
32
+ if (tags == null || searchSource == null) {
33
+ throw new Error('TokenizerShowcase no longer declares `tags` and `source`');
34
+ }
35
+ const untyped = searchSource.replace(/\((\w+):\s*string\)/g, '($1)');
36
+ return new Function(`const tags = ${tags}; return ${untyped};`)();
37
+ }
38
+
39
+ describe('Tokenizer showcase block', () => {
40
+ it('lists the tags that match a typed query, ignoring case', async () => {
41
+ const {search, bootstrap} = loadSearchSource();
42
+ const labels = async query => (await search(query)).map(tag => tag.label);
43
+ expect(await labels('mar')).toEqual(['Marketing']);
44
+ expect(await labels('A')).toEqual(['Marketing', 'Research', 'Sales']);
45
+ expect((await bootstrap()).length).toBeGreaterThan(0);
46
+ });
47
+ });
@@ -86,6 +86,10 @@ describe('values the root accepts without enrollment are accepted rule-only', ()
86
86
  // case. It must NOT be an adaptation-specific exception; it is simply
87
87
  // whatever root does.
88
88
  ['an unresolved domain', 'avatar-group', 'size:not-a-real-avatar-size'],
89
+ // `IconVariantMap` is public and augmentable, but `Icon.variant` selects
90
+ // artwork and has no component styling map (component-theming-surface
91
+ // INV14), so a styling key on it never enrolls a value.
92
+ ['an artwork-request axis', 'icon', 'variant:filled'],
89
93
  ])('accepts %s on both surfaces', async (_label, component, key) => {
90
94
  const rootReceipt = await buildWithRootValue('root-ok', component, key);
91
95
  expect(rootReceipt?.type).toBe('theme.build');
@@ -0,0 +1,134 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Built and runtime themes pass an inherited icon entry the same variants.
5
+ * @input Real themeBuild calls on a base theme with a function icon entry and a
6
+ * child that extends it with a wider `iconVariants` list.
7
+ * @output A native ESM load renders `<Icon variant>` under the built child and
8
+ * under the runtime child and compares the variant each entry received.
9
+ * @position `architecture:theme-compilation` INV15 for `spec:AST-087` FR4: an
10
+ * inherited entry keeps its defining theme's list, so a variant only the
11
+ * child lists never reaches it, built or runtime. The node project's global
12
+ * setup supplies the compiled core package.
13
+ */
14
+
15
+ import {expect, it} from 'vitest';
16
+ import {execFileSync} from 'node:child_process';
17
+ import * as fs from 'node:fs';
18
+ import * as path from 'node:path';
19
+ import {themeBuild} from './build.mjs';
20
+
21
+ it('passes an inherited entry only its defining theme variants in a built child, as at runtime', async () => {
22
+ const tmpDir = fs.mkdtempSync(
23
+ path.resolve(import.meta.dirname, '../../..', '.tmp-icon-entry-variants-'),
24
+ );
25
+ try {
26
+ for (const directory of ['base', 'child', 'unlisted']) {
27
+ fs.mkdirSync(path.join(tmpDir, directory));
28
+ }
29
+ const write = (file, source) =>
30
+ fs.writeFileSync(path.join(tmpDir, file), source);
31
+
32
+ write(
33
+ 'base/icons.mjs',
34
+ `import {createElement} from 'react';
35
+ export const icons = {
36
+ info: ({variant}) => createElement('svg', {'data-variant': variant ?? 'normal'}),
37
+ };
38
+ `,
39
+ );
40
+ write(
41
+ 'base/source.mjs',
42
+ `import {defineTheme} from '@astryxdesign/core/theme';
43
+ import {icons} from './icons.mjs';
44
+ export const baseTheme = defineTheme({
45
+ name: 'entry-variants-base', tokens: {}, iconVariants: ['outline'], icons,
46
+ });
47
+ `,
48
+ );
49
+ write(
50
+ 'child/source.mjs',
51
+ `import {defineTheme} from '@astryxdesign/core/theme';
52
+ import {baseTheme} from '../base/source.mjs';
53
+ export default defineTheme({
54
+ name: 'entry-variants-child', extends: baseTheme, tokens: {},
55
+ iconVariants: ['outline', 'filled'],
56
+ });
57
+ `,
58
+ );
59
+ // A defining theme with no list: the built module writes it as `[]`.
60
+ write(
61
+ 'unlisted/base.mjs',
62
+ `import {defineTheme} from '@astryxdesign/core/theme';
63
+ import {icons} from '../base/icons.mjs';
64
+ export const unlistedBase = defineTheme({
65
+ name: 'entry-variants-unlisted-base', tokens: {}, icons,
66
+ });
67
+ `,
68
+ );
69
+ write(
70
+ 'unlisted/source.mjs',
71
+ `import {defineTheme} from '@astryxdesign/core/theme';
72
+ import {unlistedBase} from './base.mjs';
73
+ export default defineTheme({
74
+ name: 'entry-variants-unlisted-child', extends: unlistedBase, tokens: {},
75
+ iconVariants: ['filled'],
76
+ });
77
+ `,
78
+ );
79
+ for (const source of ['child/source.mjs', 'unlisted/source.mjs']) {
80
+ const receipt = await themeBuild(source, {}, {cwd: tmpDir});
81
+ expect(receipt?.type).toBe('theme.build');
82
+ }
83
+
84
+ const output = execFileSync(
85
+ process.execPath,
86
+ [
87
+ '--input-type=module',
88
+ '--eval',
89
+ `import {createElement} from 'react';
90
+ import {renderToStaticMarkup} from 'react-dom/server';
91
+ import {Icon} from '@astryxdesign/core/Icon';
92
+ import {Theme} from '@astryxdesign/core/theme';
93
+ import {entryVariantsChildTheme as built} from './child/entry-variants-child.js';
94
+ import runtime from './child/source.mjs';
95
+ import {entryVariantsUnlistedChildTheme as unlistedBuilt} from './unlisted/entry-variants-unlisted-child.js';
96
+ import unlistedRuntime from './unlisted/source.mjs';
97
+ const received = (theme, variant) => {
98
+ const html = renderToStaticMarkup(
99
+ createElement(Theme, {theme}, createElement(Icon, {icon: 'info', variant})),
100
+ );
101
+ return /data-variant="([^"]+)"/.exec(html)?.[1] ?? null;
102
+ };
103
+ const result = {};
104
+ for (const [label, theme] of [
105
+ ['built', built],
106
+ ['runtime', runtime],
107
+ ['unlistedBuilt', unlistedBuilt],
108
+ ['unlistedRuntime', unlistedRuntime],
109
+ ]) {
110
+ result[label] = {
111
+ filled: received(theme, 'filled'),
112
+ outline: received(theme, 'outline'),
113
+ };
114
+ }
115
+ process.stdout.write(JSON.stringify(result));`,
116
+ ],
117
+ {cwd: tmpDir, stdio: ['ignore', 'pipe', 'pipe'], timeout: 20000},
118
+ );
119
+ const result = JSON.parse(String(output));
120
+
121
+ // The base never listed 'filled', so its entry draws normal artwork for
122
+ // it in the child; 'outline' is listed by both and reaches the entry.
123
+ expect(result.runtime).toEqual({filled: 'normal', outline: 'outline'});
124
+ expect(result.built).toEqual(result.runtime);
125
+ // A base with no list passes its entry no variant, even one its child lists.
126
+ expect(result.unlistedRuntime).toEqual({
127
+ filled: 'normal',
128
+ outline: 'normal',
129
+ });
130
+ expect(result.unlistedBuilt).toEqual(result.unlistedRuntime);
131
+ } finally {
132
+ fs.rmSync(tmpDir, {recursive: true, force: true});
133
+ }
134
+ });
@@ -0,0 +1,499 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Built themes carry function icon entries and resolve them as runtime.
5
+ * @input Real themeBuild and themeBuildFamily calls on source themes whose icon
6
+ * registries hold functions of `{size, variant}` and that list icon variants:
7
+ * a child under a replaced list, a theme extending a built module, a theme
8
+ * extending a package theme, and a family.
9
+ * @output Native ESM loads that render `<Icon>` for every request a theme's
10
+ * entries can receive under the built and the runtime theme, read the public
11
+ * icon views, and a TypeScript program that extends a built theme.
12
+ * @position `architecture:theme-compilation` INV15 and `spec:AST-087` FR3,
13
+ * FR18, and FR19 for theme build. The node project's global setup supplies
14
+ * the compiled core package.
15
+ */
16
+
17
+ import {afterEach, beforeEach, describe, expect, it} from 'vitest';
18
+ import {execFileSync} from 'node:child_process';
19
+ import * as fs from 'node:fs';
20
+ import * as path from 'node:path';
21
+ import ts from 'typescript';
22
+ import {themeBuild, themeBuildFamily} from './build.mjs';
23
+
24
+ const SIZES = ['xsm', 'sm', 'md', 'lg'];
25
+
26
+ let tmpDir;
27
+ beforeEach(() => {
28
+ // Under the CLI package, so core, React, and TypeScript resolve both in the
29
+ // theme loader and in the native artifact checks.
30
+ tmpDir = fs.mkdtempSync(
31
+ path.resolve(import.meta.dirname, '../../..', '.tmp-icon-functions-'),
32
+ );
33
+ });
34
+ afterEach(() => {
35
+ fs.rmSync(tmpDir, {recursive: true, force: true});
36
+ });
37
+
38
+ /** @param {string} file @param {string} source */
39
+ function write(file, source) {
40
+ fs.mkdirSync(path.dirname(path.join(tmpDir, file)), {recursive: true});
41
+ fs.writeFileSync(path.join(tmpDir, file), source);
42
+ }
43
+
44
+ /** A registry module whose function entries label what they were asked for. */
45
+ function writeRegistry(file, exportName, entries) {
46
+ write(
47
+ file,
48
+ `import {createElement} from 'react';
49
+ const draw = label => ({size, variant}) =>
50
+ createElement('svg', {'data-icon': label + ':' + size + ':' + (variant ?? 'normal')});
51
+ export const ${exportName} = {
52
+ ${Object.entries(entries)
53
+ .map(([key, label]) =>
54
+ label.startsWith('node:')
55
+ ? ` ${JSON.stringify(key)}: createElement('svg', {'data-icon': ${JSON.stringify(label.slice(5))}}),`
56
+ : ` ${JSON.stringify(key)}: draw(${JSON.stringify(label)}),`,
57
+ )
58
+ .join('\n')}
59
+ };
60
+ `,
61
+ );
62
+ }
63
+
64
+ /** The base, its child under a replaced list, and their registries. */
65
+ function writeLineage() {
66
+ writeRegistry('base/icons.mjs', 'baseIcons', {
67
+ info: 'base-info',
68
+ close: 'base-close',
69
+ check: 'node:base-check',
70
+ 'parity:mark': 'base-mark',
71
+ });
72
+ write(
73
+ 'base/source.mjs',
74
+ `import {defineTheme} from '@astryxdesign/core/theme';
75
+ import {baseIcons} from './icons.mjs';
76
+ export const baseTheme = defineTheme({
77
+ name: 'parity-base', tokens: {}, iconVariants: ['outline', 'filled'], icons: baseIcons,
78
+ });
79
+ export default baseTheme;
80
+ `,
81
+ );
82
+ writeRegistry('child/icons.mjs', 'childIcons', {
83
+ close: 'child-close',
84
+ menu: 'child-menu',
85
+ });
86
+ write(
87
+ 'child/source.mjs',
88
+ `import {defineTheme} from '@astryxdesign/core/theme';
89
+ import {baseTheme} from '../base/source.mjs';
90
+ import {childIcons} from './icons.mjs';
91
+ export const childTheme = defineTheme({
92
+ name: 'parity-child', extends: baseTheme, tokens: {},
93
+ iconVariants: ['filled', 'bold'], icons: childIcons,
94
+ });
95
+ export default childTheme;
96
+ `,
97
+ );
98
+ }
99
+
100
+ /** @param {string} file */
101
+ async function buildAndCheck(file) {
102
+ const built = await themeBuild(file, {}, {cwd: tmpDir});
103
+ expect(built?.type).toBe('theme.build');
104
+ const checked = await themeBuild(file, {check: true}, {cwd: tmpDir});
105
+ expect(checked?.data.upToDate).toBe(true);
106
+ return built;
107
+ }
108
+
109
+ /**
110
+ * Load built and runtime themes with native Node and report, for each pair,
111
+ * what `<Icon>` renders for every request and what the public reads return.
112
+ * The requests are every key a theme draws × the four sizes × no variant and
113
+ * each name any theme here lists, so unlisted names are covered too.
114
+ *
115
+ * @param {Array<{label: string, built: string, runtime: string}>} pairs -
116
+ * Module specifiers with `#export` suffixes, relative to the fixture root.
117
+ */
118
+ function inspect(pairs) {
119
+ const load = specifier => {
120
+ const [file, name] = specifier.split('#');
121
+ return `(await import(${JSON.stringify(file)}))[${JSON.stringify(name)}]`;
122
+ };
123
+ const output = execFileSync(
124
+ process.execPath,
125
+ [
126
+ '--input-type=module',
127
+ '--eval',
128
+ `import {createElement, isValidElement} from 'react';
129
+ import {renderToStaticMarkup} from 'react-dom/server';
130
+ import {Icon, getIcon, getIconRegistry} from '@astryxdesign/core/Icon';
131
+ import {Theme} from '@astryxdesign/core/theme';
132
+ const SIZES = ${JSON.stringify(SIZES)};
133
+ const VARIANTS = [undefined, 'outline', 'filled', 'bold'];
134
+ const label = node => {
135
+ if (node == null) return null;
136
+ if (!isValidElement(node)) return 'not-a-node:' + typeof node;
137
+ return node.props['data-icon'];
138
+ };
139
+ const rendered = (theme, icon, size, variant) => {
140
+ const html = renderToStaticMarkup(
141
+ createElement(Theme, {theme}, createElement(Icon, {icon, size, variant})),
142
+ );
143
+ return /data-icon="([^"]+)"/.exec(html)?.[1] ?? null;
144
+ };
145
+ const describe = theme => {
146
+ const keys = Object.keys(theme.icons ?? {}).sort();
147
+ const requests = {};
148
+ for (const key of keys) {
149
+ for (const size of SIZES) {
150
+ for (const variant of VARIANTS) {
151
+ requests[key + '|' + size + '|' + (variant ?? '-')] = rendered(theme, key, size, variant);
152
+ }
153
+ }
154
+ }
155
+ return {
156
+ keys,
157
+ requests,
158
+ icons: Object.fromEntries(keys.map(key => [key, label(theme.icons[key])])),
159
+ getIcon: Object.fromEntries(keys.map(key => [key, label(getIcon(key, theme))])),
160
+ registry: Object.fromEntries(
161
+ Object.entries(getIconRegistry(theme))
162
+ .filter(([key]) => keys.includes(key))
163
+ .map(([key, node]) => [key, label(node)]),
164
+ ),
165
+ iconVariants: theme.iconVariants ?? null,
166
+ };
167
+ };
168
+ const result = {};
169
+ ${pairs
170
+ .map(
171
+ ({label, built, runtime}) => `{
172
+ const built = ${load(built)};
173
+ const runtime = ${load(runtime)};
174
+ const mod = await import(${JSON.stringify(built.split('#')[0])});
175
+ const reexports = Object.entries(mod).filter(([name]) => name !== ${JSON.stringify(built.split('#')[1])});
176
+ result[${JSON.stringify(label)}] = {
177
+ built: describe(built),
178
+ runtime: describe(runtime),
179
+ reexportIsNodeView: reexports.length === 0 ? null : reexports.every(([, value]) => value === built.icons),
180
+ };
181
+ }`,
182
+ )
183
+ .join('\n')}
184
+ process.stdout.write(JSON.stringify(result));`,
185
+ ],
186
+ {cwd: tmpDir, encoding: 'utf8', timeout: 60000},
187
+ );
188
+ return JSON.parse(output);
189
+ }
190
+
191
+ describe('built themes keep function icon entries', () => {
192
+ it('resolve every request as the runtime theme does, including an inherited entry under a replaced list', async () => {
193
+ writeLineage();
194
+ await buildAndCheck('base/source.mjs');
195
+ await buildAndCheck('child/source.mjs');
196
+ // A grandchild extends the BUILT child module.
197
+ write(
198
+ 'grandchild/source.mjs',
199
+ `import {defineTheme} from '@astryxdesign/core/theme';
200
+ import {parityChildTheme} from '../child/parity-child.js';
201
+ export default defineTheme({name: 'parity-grandchild', extends: parityChildTheme, tokens: {}});
202
+ `,
203
+ );
204
+ await buildAndCheck('grandchild/source.mjs');
205
+
206
+ const result = inspect([
207
+ {
208
+ label: 'base',
209
+ built: './base/parity-base.js#parityBaseTheme',
210
+ runtime: './base/source.mjs#default',
211
+ },
212
+ {
213
+ label: 'child',
214
+ built: './child/parity-child.js#parityChildTheme',
215
+ runtime: './child/source.mjs#default',
216
+ },
217
+ {
218
+ label: 'grandchild',
219
+ built: './grandchild/parity-grandchild.js#parityGrandchildTheme',
220
+ runtime: './grandchild/source.mjs#default',
221
+ },
222
+ ]);
223
+
224
+ for (const {built, runtime, reexportIsNodeView} of Object.values(result)) {
225
+ // INV15: built resolution and public views match runtime.
226
+ expect(built).toEqual(runtime);
227
+ expect(reexportIsNodeView).not.toBe(false);
228
+ }
229
+
230
+ const child = result.child.built;
231
+ expect(child.keys).toEqual([
232
+ 'check',
233
+ 'close',
234
+ 'info',
235
+ 'menu',
236
+ 'parity:mark',
237
+ ]);
238
+ for (const size of SIZES) {
239
+ // The child's own entry receives the names the child lists.
240
+ expect(child.requests[`close|${size}|bold`]).toBe(
241
+ `child-close:${size}:bold`,
242
+ );
243
+ expect(child.requests[`close|${size}|filled`]).toBe(
244
+ `child-close:${size}:filled`,
245
+ );
246
+ expect(child.requests[`close|${size}|outline`]).toBe(
247
+ `child-close:${size}:normal`,
248
+ );
249
+ // The inherited entry keeps its defining theme's list: 'outline' is no
250
+ // longer listed by the child, and the base never listed 'bold'.
251
+ expect(child.requests[`info|${size}|filled`]).toBe(
252
+ `base-info:${size}:filled`,
253
+ );
254
+ expect(child.requests[`info|${size}|outline`]).toBe(
255
+ `base-info:${size}:normal`,
256
+ );
257
+ expect(child.requests[`info|${size}|bold`]).toBe(
258
+ `base-info:${size}:normal`,
259
+ );
260
+ expect(child.requests[`info|${size}|-`]).toBe(`base-info:${size}:normal`);
261
+ // A node entry renders the same node for every request.
262
+ expect(child.requests[`check|${size}|filled`]).toBe('base-check');
263
+ }
264
+ expect(result.grandchild.built.requests).toEqual(child.requests);
265
+
266
+ // FR18: public reads are React nodes, each function read at md.
267
+ expect(child.icons).toEqual({
268
+ check: 'base-check',
269
+ close: 'child-close:md:normal',
270
+ info: 'base-info:md:normal',
271
+ menu: 'child-menu:md:normal',
272
+ 'parity:mark': 'base-mark:md:normal',
273
+ });
274
+ expect(child.getIcon).toEqual(child.icons);
275
+ expect(child.registry).toEqual({
276
+ check: 'base-check',
277
+ close: 'child-close:md:normal',
278
+ info: 'base-info:md:normal',
279
+ menu: 'child-menu:md:normal',
280
+ });
281
+ expect(result.base.reexportIsNodeView).toBe(true);
282
+ expect(result.child.reexportIsNodeView).toBe(true);
283
+ });
284
+
285
+ it('keeps the function entries of a package theme it extends', async () => {
286
+ const pkg = 'node_modules/@fixture/function-icons';
287
+ write(
288
+ `${pkg}/package.json`,
289
+ JSON.stringify({
290
+ name: '@fixture/function-icons',
291
+ type: 'module',
292
+ exports: './index.mjs',
293
+ }),
294
+ );
295
+ writeRegistry(`${pkg}/icons.mjs`, 'packageIcons', {info: 'package-info'});
296
+ write(
297
+ `${pkg}/index.mjs`,
298
+ `import {defineTheme} from '@astryxdesign/core/theme';
299
+ import {packageIcons} from './icons.mjs';
300
+ export const packageTheme = defineTheme({
301
+ name: 'parity-package', tokens: {}, iconVariants: ['filled'], icons: packageIcons,
302
+ });
303
+ `,
304
+ );
305
+ write(
306
+ 'package-child.mjs',
307
+ `import {defineTheme} from '@astryxdesign/core/theme';
308
+ import {packageTheme} from '@fixture/function-icons';
309
+ export default defineTheme({name: 'parity-package-child', extends: packageTheme, tokens: {}});
310
+ `,
311
+ );
312
+ await buildAndCheck('package-child.mjs');
313
+
314
+ const {packageChild} = inspect([
315
+ {
316
+ label: 'packageChild',
317
+ built: './parity-package-child.js#parityPackageChildTheme',
318
+ runtime: './package-child.mjs#default',
319
+ },
320
+ ]);
321
+
322
+ expect(packageChild.built).toEqual(packageChild.runtime);
323
+ for (const size of SIZES) {
324
+ expect(packageChild.built.requests[`info|${size}|filled`]).toBe(
325
+ `package-info:${size}:filled`,
326
+ );
327
+ }
328
+ expect(packageChild.built.icons).toEqual({info: 'package-info:md:normal'});
329
+ });
330
+
331
+ it('keeps function entries in a family build', async () => {
332
+ writeLineage();
333
+ // A member that sets no icons of its own inherits the child's entries.
334
+ write(
335
+ 'inherit/source.mjs',
336
+ `import {defineTheme} from '@astryxdesign/core/theme';
337
+ import {childTheme} from '../child/source.mjs';
338
+ export default defineTheme({name: 'parity-inherit', extends: childTheme, tokens: {}});
339
+ `,
340
+ );
341
+ const receipt = await themeBuildFamily(
342
+ ['base/source.mjs', 'child/source.mjs', 'inherit/source.mjs'],
343
+ {familyKey: 'parity-family'},
344
+ {cwd: tmpDir},
345
+ );
346
+ expect(receipt?.type).toBe('theme.build');
347
+ const familyModule = `./${receipt.data.outputs.js}`;
348
+
349
+ const result = inspect([
350
+ {
351
+ label: 'base',
352
+ built: `${familyModule}#parityBaseTheme`,
353
+ runtime: './base/source.mjs#default',
354
+ },
355
+ {
356
+ label: 'child',
357
+ built: `${familyModule}#parityChildTheme`,
358
+ runtime: './child/source.mjs#default',
359
+ },
360
+ {
361
+ label: 'inherit',
362
+ built: `${familyModule}#parityInheritTheme`,
363
+ runtime: './inherit/source.mjs#default',
364
+ },
365
+ ]);
366
+
367
+ expect(result.base.built).toEqual(result.base.runtime);
368
+ expect(result.child.built).toEqual(result.child.runtime);
369
+ expect(result.inherit.built).toEqual(result.inherit.runtime);
370
+ expect(result.inherit.built.requests).toEqual(result.child.built.requests);
371
+ expect(result.child.built.requests['info|sm|filled']).toBe(
372
+ 'base-info:sm:filled',
373
+ );
374
+ expect(result.child.built.requests['info|sm|outline']).toBe(
375
+ 'base-info:sm:normal',
376
+ );
377
+ });
378
+ });
379
+
380
+ describe('the node view never captures a registry binding', () => {
381
+ it.each(['entries', 'nodes', 'name', 'entry', 'Object'])(
382
+ 'loads a built theme whose registry is imported as `%s`',
383
+ async binding => {
384
+ writeRegistry('icons.mjs', 'registry', {info: 'bound-info'});
385
+ write(
386
+ 'bound.mjs',
387
+ `import {defineTheme} from '@astryxdesign/core/theme';
388
+ import {registry as ${binding}} from './icons.mjs';
389
+ export default defineTheme({
390
+ name: 'bound', tokens: {}, iconVariants: ['filled'], icons: ${binding},
391
+ });
392
+ `,
393
+ );
394
+ await buildAndCheck('bound.mjs');
395
+
396
+ const {bound} = inspect([
397
+ {
398
+ label: 'bound',
399
+ built: './bound.js#boundTheme',
400
+ runtime: './bound.mjs#default',
401
+ },
402
+ ]);
403
+ expect(bound.built).toEqual(bound.runtime);
404
+ expect(bound.built.icons).toEqual({info: 'bound-info:md:normal'});
405
+ expect(bound.built.requests['info|lg|filled']).toBe(
406
+ 'bound-info:lg:filled',
407
+ );
408
+ expect(bound.reexportIsNodeView).toBe(true);
409
+ },
410
+ );
411
+ });
412
+
413
+ describe('a built theme types its icon variants', () => {
414
+ it('lets a theme extending the built module type `variant` by its list', async () => {
415
+ writeLineage();
416
+ await buildAndCheck('base/source.mjs');
417
+
418
+ const variants = fs.readFileSync(
419
+ path.join(tmpDir, 'base/parity-base.variants.d.ts'),
420
+ 'utf8',
421
+ );
422
+ expect(variants).toContain("declare module '@astryxdesign/core/Icon'");
423
+ expect(variants).toContain("'outline': true;");
424
+ expect(variants).toContain("'filled': true;");
425
+
426
+ write(
427
+ 'consumer.ts',
428
+ `import {defineTheme} from '@astryxdesign/core/theme';
429
+ import type {IconVariant} from '@astryxdesign/core/Icon';
430
+ import {parityBaseTheme} from './base/parity-base.js';
431
+
432
+ type Equal<A, B> =
433
+ (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
434
+
435
+ defineTheme({
436
+ name: 'consumer',
437
+ extends: parityBaseTheme,
438
+ icons: {
439
+ info: ({variant}) => {
440
+ const exact: Equal<typeof variant, 'outline' | 'filled' | undefined> = true;
441
+ return exact ? null : null;
442
+ },
443
+ // @ts-expect-error -- 'bold' is not a name the built theme lists.
444
+ close: ({variant}) => (variant === 'bold' ? null : null),
445
+ },
446
+ });
447
+
448
+ // The built theme's augmentation adds its names to IconVariant.
449
+ export const listed: IconVariant = 'filled';
450
+ // @ts-expect-error -- no theme here lists 'bold'.
451
+ export const unlisted: IconVariant = 'bold';
452
+ `,
453
+ );
454
+ const program = ts.createProgram([path.join(tmpDir, 'consumer.ts')], {
455
+ strict: true,
456
+ noEmit: true,
457
+ skipLibCheck: true,
458
+ target: ts.ScriptTarget.ES2022,
459
+ module: ts.ModuleKind.ESNext,
460
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
461
+ types: [],
462
+ });
463
+ const diagnostics = ts
464
+ .getPreEmitDiagnostics(program)
465
+ .filter(diagnostic => diagnostic.file?.fileName.endsWith('consumer.ts'))
466
+ .map(diagnostic =>
467
+ ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
468
+ );
469
+ expect(diagnostics).toEqual([]);
470
+ });
471
+ });
472
+
473
+ describe('a function the build cannot carry fails the build', () => {
474
+ it.each([
475
+ [
476
+ 'an inline function entry',
477
+ `icons: {info: ({size}) => createElement('svg', {'data-size': size})}`,
478
+ ],
479
+ [
480
+ 'a function beside a spread registry',
481
+ `icons: {...baseIcons, info: () => null}`,
482
+ ],
483
+ ])('rejects %s before writing', async (_label, iconsField) => {
484
+ writeRegistry('icons.mjs', 'baseIcons', {close: 'base-close'});
485
+ write(
486
+ 'inline.mjs',
487
+ `import {createElement} from 'react';
488
+ import {defineTheme} from '@astryxdesign/core/theme';
489
+ import {baseIcons} from './icons.mjs';
490
+ export default defineTheme({name: 'inline-functions', tokens: {}, ${iconsField}});
491
+ `,
492
+ );
493
+
494
+ await expect(
495
+ themeBuild('inline.mjs', {}, {cwd: tmpDir}),
496
+ ).rejects.toMatchObject({code: 'ERR_THEME_INVALID'});
497
+ expect(fs.readdirSync(tmpDir).sort()).toEqual(['icons.mjs', 'inline.mjs']);
498
+ });
499
+ });