@astryxdesign/cli 0.7.3-canary.641d08e → 0.7.3-canary.84c7b4f

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 (48) hide show
  1. package/README.md +63 -63
  2. package/api/component/component-replacement.test.mjs +1 -0
  3. package/api/component/component.mjs +1 -1
  4. package/api/hook/hook.mjs +1 -1
  5. package/api/integration/validate-integration-fixes.test.mjs +6 -5
  6. package/api/integration/validate-integration-gaps.test.mjs +9 -1
  7. package/api/integration/validate-integration.d.mts +4 -0
  8. package/api/integration/validate-integration.mjs +15 -0
  9. package/api/integration/validate-integration.type.d.mts +33 -4
  10. package/api/integration/validate-integration.type.mjs +24 -5
  11. package/api/integration/validateIntegration.doc.mjs +1 -1
  12. package/api/search/_adapter.d.mts +16 -0
  13. package/api/search/_adapter.mjs +39 -0
  14. package/api/search/_adapter.test.mjs +173 -0
  15. package/api/search/search.mjs +6 -2
  16. package/assets/docs/tree/checks.doc.mjs +1 -1
  17. package/assets/docs/use-integrations.doc.mjs +120 -0
  18. package/clients/cli/command-result-coverage.test.mjs +6 -5
  19. package/clients/cli/commands/build-theme.color-scheme.test.mjs +1 -1
  20. package/clients/cli/commands/{component/index.mjs → component.mjs} +37 -37
  21. package/clients/cli/commands/component.test.mjs +6 -6
  22. package/clients/cli/commands/doctor-integration.test.mjs +49 -1
  23. package/clients/cli/commands/doctor.mjs +10 -0
  24. package/clients/cli/commands/{hook/index.mjs → hook.mjs} +22 -22
  25. package/clients/cli/commands/import-hint-correctness.test.mjs +1 -1
  26. package/clients/cli/commands/manifest.doc.mjs +1 -1
  27. package/clients/cli/commands/manifest.mjs +67 -0
  28. package/clients/cli/commands/postinstall.doc.mjs +23 -0
  29. package/clients/cli/commands/postinstall.mjs +31 -0
  30. package/clients/cli/commands/{build-theme.mjs → theme.mjs} +1 -1
  31. package/clients/cli/index.mjs +10 -85
  32. package/clients/cli/lib/component-format.mjs +1 -1
  33. package/clients/cli/lib/define-command.mjs +12 -7
  34. package/clients/cli/lib/exit-codes.test.mjs +5 -0
  35. package/clients/cli/lib/manifest.test.mjs +12 -0
  36. package/clients/cli/lib/welcome-box.mjs +50 -0
  37. package/foundation/discovery/runner-commands.mjs +1 -0
  38. package/foundation/doc-compiler/doc-loads.test.mjs +21 -9
  39. package/foundation/fs/read-record.d.mts +18 -0
  40. package/foundation/fs/read-record.mjs +131 -0
  41. package/foundation/fs/read-record.test.mjs +77 -0
  42. package/foundation/integrations/exports-subpaths.d.mts +57 -0
  43. package/foundation/integrations/exports-subpaths.mjs +113 -0
  44. package/foundation/integrations/public-imports.d.mts +81 -0
  45. package/foundation/integrations/public-imports.mjs +976 -0
  46. package/foundation/integrations/public-imports.test.mjs +868 -0
  47. package/foundation/response/response-types.doc.mjs +1 -1
  48. package/package.json +9 -9
@@ -0,0 +1,868 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Source-level public-import checks run by
5
+ * `astryx doctor integration validate` (and so `integration check --strict`).
6
+ *
7
+ * Each test stands up a small integration package under the repo root (so the
8
+ * repo's own Core is the installed Core) and validates it.
9
+ */
10
+
11
+ import * as fs from 'node:fs';
12
+ import * as os from 'node:os';
13
+ import * as path from 'node:path';
14
+ import {spawnSync} from 'node:child_process';
15
+ import {afterEach, beforeEach, describe, expect, it} from 'vitest';
16
+ import {
17
+ validateIntegration,
18
+ validateLocalIntegration,
19
+ } from '../../api/integration/validate-integration.mjs';
20
+ import {readImports} from './public-imports.mjs';
21
+ import {subpathDeclaration} from './exports-subpaths.mjs';
22
+ import {
23
+ integrationAddComponent,
24
+ integrationAddTemplate,
25
+ } from '../../api/integration/add-contribution.mjs';
26
+ import {
27
+ integrationCheck,
28
+ strictIssues,
29
+ } from '../../api/integration/check/check.mjs';
30
+ import {CLI_ROOT} from '../fs/paths.mjs';
31
+
32
+ const CODES = new Set([
33
+ 'component_import_not_exported',
34
+ 'component_import_other_package',
35
+ 'example_import_not_exported',
36
+ 'template_import_not_exported',
37
+ 'template_import_outside_root',
38
+ 'template_private_import',
39
+ ]);
40
+
41
+ let dir;
42
+
43
+ /** @param {string} file @param {string} text */
44
+ function write(file, text) {
45
+ fs.mkdirSync(path.dirname(path.join(dir, file)), {recursive: true});
46
+ fs.writeFileSync(path.join(dir, file), text);
47
+ }
48
+
49
+ /** @param {(pkg: any) => void} edit */
50
+ function pkg(edit) {
51
+ const file = path.join(dir, 'package.json');
52
+ const value = JSON.parse(fs.readFileSync(file, 'utf-8'));
53
+ edit(value);
54
+ fs.writeFileSync(file, JSON.stringify(value, null, 2));
55
+ }
56
+
57
+ const DOC = (
58
+ importPath = '@acme/widgets/components/AcmeWidget',
59
+ example = 'import {AcmeWidget} from \'@acme/widgets/components/AcmeWidget\';\\n\\n<AcmeWidget label=\\"Save\\" />',
60
+ ) =>
61
+ `export default {
62
+ type: 'component',
63
+ name: 'AcmeWidget',
64
+ displayName: 'Acme Widget',
65
+ import: '${importPath}',
66
+ usage: {description: 'A pressable widget.'},
67
+ props: [{name: 'label', type: 'string', description: 'Text.', required: true}],
68
+ examples: [{label: 'Basic', code: "${example}"}],
69
+ };
70
+ `;
71
+
72
+ const BLOCK = () =>
73
+ `export default {
74
+ type: 'block',
75
+ name: 'account-card',
76
+ displayName: 'Account Card',
77
+ description: 'Account Card template.',
78
+ aspectRatio: 1,
79
+ };
80
+ `;
81
+
82
+ beforeEach(() => {
83
+ dir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-public-imports-'));
84
+ write(
85
+ 'package.json',
86
+ JSON.stringify({
87
+ name: '@acme/widgets',
88
+ version: '1.0.0',
89
+ type: 'module',
90
+ peerDependencies: {
91
+ '@astryxdesign/core': '>=0.1.0',
92
+ '@astryxdesign/cli': '>=0.1.0',
93
+ },
94
+ exports: {
95
+ './components/AcmeWidget': './components/AcmeWidget.tsx',
96
+ './templates/account-card': './templates/account-card.tsx',
97
+ },
98
+ }),
99
+ );
100
+ write(
101
+ 'astryx.integration.mjs',
102
+ "export default {components: './components', templates: './templates'};\n",
103
+ );
104
+ write('components/AcmeWidget.doc.mjs', DOC());
105
+ write(
106
+ 'components/AcmeWidget.tsx',
107
+ 'export type AcmeWidgetProps = {label: string};\nexport function AcmeWidget({label}: AcmeWidgetProps) {\n return <button>{label}</button>;\n}\n',
108
+ );
109
+ write('templates/account-card.doc.mjs', BLOCK());
110
+ write(
111
+ 'templates/account-card.tsx',
112
+ 'import {AcmeWidget} from \'@acme/widgets/components/AcmeWidget\';\nexport default function AccountCard() {\n return <AcmeWidget label="Account" />;\n}\n',
113
+ );
114
+ });
115
+
116
+ afterEach(() => {
117
+ fs.rmSync(dir, {recursive: true, force: true});
118
+ });
119
+
120
+ /** The public-import findings, listed as notes on validate's result. */
121
+ async function found() {
122
+ const result = await validateLocalIntegration(dir);
123
+ return (result.notes ?? []).filter(note => CODES.has(note.code));
124
+ }
125
+
126
+ async function codes() {
127
+ return (await found()).map(issue => issue.code).sort();
128
+ }
129
+
130
+ describe('public imports: clean packages', () => {
131
+ it('passes the known-good package', async () => {
132
+ const result = await validateLocalIntegration(dir);
133
+ expect(result.issues).toEqual([]);
134
+ });
135
+
136
+ it('passes a condition object with import (types first) or default', async () => {
137
+ pkg(p => {
138
+ p.exports['./components/AcmeWidget'] = {
139
+ types: './components/AcmeWidget.tsx',
140
+ import: './components/AcmeWidget.tsx',
141
+ };
142
+ });
143
+ expect(await codes()).toEqual([]);
144
+ pkg(p => {
145
+ p.exports['./components/AcmeWidget'] = {
146
+ default: './components/AcmeWidget.tsx',
147
+ import: './missing.js',
148
+ };
149
+ });
150
+ expect(await codes()).toEqual([]);
151
+ });
152
+
153
+ it('passes a wildcard that covers the component', async () => {
154
+ pkg(p => {
155
+ delete p.exports['./components/AcmeWidget'];
156
+ p.exports['./components/*'] = './components/*.tsx';
157
+ });
158
+ expect(await codes()).toEqual([]);
159
+ });
160
+
161
+ it('checks a package whose name is not a bare package name as nameless', async () => {
162
+ pkg(p => {
163
+ p.name = 'acme/kit';
164
+ });
165
+ const result = await validateIntegration(undefined, {cwd: dir});
166
+ expect(await codes()).toEqual([]);
167
+ expect(result.data.publicImports?.unchecked).toContainEqual(
168
+ expect.objectContaining({
169
+ file: 'package.json',
170
+ reason: expect.stringContaining(
171
+ '"acme/kit" is not a bare package name',
172
+ ),
173
+ }),
174
+ );
175
+ });
176
+
177
+ it('reads "exports": null as no exports field', async () => {
178
+ pkg(p => {
179
+ p.exports = null;
180
+ });
181
+ // A doc module loads once per run, so the doc is written before checking.
182
+ write(
183
+ 'components/AcmeWidget.doc.mjs',
184
+ DOC('@acme/widgets/components/Missing'),
185
+ );
186
+ const issue = (await found()).find(
187
+ i => i.code === 'component_import_not_exported',
188
+ );
189
+ expect(issue?.message).toContain(
190
+ 'no file exists at "./components/Missing"',
191
+ );
192
+ // The template's deep path to its own file still resolves.
193
+ expect((await found()).filter(i => i.code.startsWith('template_'))).toEqual(
194
+ [],
195
+ );
196
+ });
197
+
198
+ it('keeps legacy deep paths when package.json has no exports', async () => {
199
+ pkg(p => delete p.exports);
200
+ expect(await codes()).toEqual([]);
201
+ });
202
+
203
+ it('flags a deep path with no file when package.json has no exports', async () => {
204
+ pkg(p => delete p.exports);
205
+ write(
206
+ 'components/AcmeWidget.doc.mjs',
207
+ DOC('@acme/widgets/components/Missing'),
208
+ );
209
+ const issue = (await found()).find(
210
+ i => i.code === 'component_import_not_exported',
211
+ );
212
+ expect(issue?.message).toContain(
213
+ 'no file exists at "./components/Missing"',
214
+ );
215
+ });
216
+
217
+ it('lists a deep path into build output not built yet as not checked', async () => {
218
+ pkg(p => {
219
+ delete p.exports;
220
+ p.files = ['dist', 'components', 'templates'];
221
+ });
222
+ write(
223
+ 'components/AcmeWidget.doc.mjs',
224
+ DOC(
225
+ '@acme/widgets/dist/AcmeWidget.js',
226
+ "import {AcmeWidget} from '@acme/widgets/dist/AcmeWidget.js';\\n<AcmeWidget />",
227
+ ),
228
+ );
229
+ const result = await validateIntegration(undefined, {cwd: dir});
230
+ expect(
231
+ (result.data.notes ?? []).filter(i => i.code.includes('_not_exported')),
232
+ ).toEqual([]);
233
+ expect(result.data.publicImports?.unchecked).toEqual([
234
+ expect.objectContaining({
235
+ file: 'components/AcmeWidget.doc.mjs',
236
+ specifier: '@acme/widgets/dist/AcmeWidget.js',
237
+ reason: expect.stringContaining('build output'),
238
+ }),
239
+ expect.objectContaining({
240
+ specifier: '@acme/widgets/dist/AcmeWidget.js',
241
+ }),
242
+ ]);
243
+ });
244
+
245
+ it('never maps a built .js path to the .tsx source in a fix', async () => {
246
+ pkg(p => delete p.exports);
247
+ write(
248
+ 'components/AcmeWidget.doc.mjs',
249
+ DOC('@acme/widgets/components/AcmeWidget.js'),
250
+ );
251
+ const issue = (await found()).find(
252
+ i => i.code === 'component_import_not_exported',
253
+ );
254
+ expect(issue?.message).not.toContain(
255
+ '"./components/AcmeWidget.js": "./components/AcmeWidget.tsx"',
256
+ );
257
+ });
258
+
259
+ it('never requires an exports target to exist (build output)', async () => {
260
+ pkg(p => {
261
+ p.exports['./components/AcmeWidget'] = {
262
+ types: './dist/AcmeWidget.d.ts',
263
+ import: './dist/AcmeWidget.js',
264
+ };
265
+ });
266
+ expect(await codes()).toEqual([]);
267
+ });
268
+ });
269
+
270
+ describe('public imports: component doc import', () => {
271
+ it('flags a missing exports key with the entry to add', async () => {
272
+ pkg(p => delete p.exports['./components/AcmeWidget']);
273
+ const issues = await found();
274
+ const issue = issues.find(i => i.code === 'component_import_not_exported');
275
+ expect(issue).not.toHaveProperty('severity');
276
+ expect(issue?.message).toContain(
277
+ '"./components/AcmeWidget": "./components/AcmeWidget.tsx"',
278
+ );
279
+ });
280
+
281
+ it('lists its findings only: neither validate nor --strict counts or fails on them', async () => {
282
+ pkg(p => {
283
+ delete p.exports['./components/AcmeWidget'];
284
+ });
285
+ write(
286
+ 'templates/account-card.tsx',
287
+ "import {X} from '../components/AcmeWidget';\nexport function A() {\n return <X />;\n}\n",
288
+ );
289
+ const issues = await found();
290
+ expect(issues.length).toBeGreaterThan(2);
291
+ expect(issues.every(i => !('severity' in i))).toBe(true);
292
+ const validated = await validateLocalIntegration(dir);
293
+ expect(validated.issues.filter(issue => CODES.has(issue.code))).toEqual([]);
294
+ const plain = await integrationCheck({cwd: dir});
295
+ expect(plain.data.errors).toBe(0);
296
+ expect(plain.data.warnings).toBe(0);
297
+ const strict = await integrationCheck({cwd: dir, strict: true});
298
+ expect(strict.data.failed).toBe(false);
299
+ expect(strict.data.failures).toEqual([]);
300
+ });
301
+
302
+ it('flags a require-only condition', async () => {
303
+ pkg(p => {
304
+ p.exports['./components/AcmeWidget'] = {
305
+ require: './components/AcmeWidget.tsx',
306
+ };
307
+ });
308
+ const issue = (await found()).find(
309
+ i => i.code === 'component_import_not_exported',
310
+ );
311
+ expect(issue?.message).toContain('only the "require" condition');
312
+ });
313
+
314
+ it('names the conditions a types-only entry has', async () => {
315
+ pkg(p => {
316
+ p.exports['./components/AcmeWidget'] = {types: './a.d.ts'};
317
+ });
318
+ const issue = (await found()).find(
319
+ i => i.code === 'component_import_not_exported',
320
+ );
321
+ expect(issue?.message).toContain('only the "types" condition');
322
+ expect(issue?.message).not.toContain('require');
323
+ });
324
+
325
+ it('flags a bare-string exports field', async () => {
326
+ pkg(p => {
327
+ p.exports = './components/AcmeWidget.tsx';
328
+ });
329
+ const all = await codes();
330
+ expect(all).toContain('component_import_not_exported');
331
+ expect(all).toContain('template_import_not_exported');
332
+ });
333
+
334
+ it('flags a wildcard that does not match', async () => {
335
+ pkg(p => {
336
+ delete p.exports['./components/AcmeWidget'];
337
+ p.exports['./widgets/*'] = './components/*.tsx';
338
+ });
339
+ expect(await codes()).toContain('component_import_not_exported');
340
+ });
341
+
342
+ it('points an undeclared import at the subpath already exported', async () => {
343
+ write('components/AcmeWidget.doc.mjs', DOC('@acme/widgets/AcmeWidget'));
344
+ const issue = (await found()).find(
345
+ i => i.code === 'component_import_not_exported',
346
+ );
347
+ expect(issue?.message).toContain(
348
+ "set import: '@acme/widgets/components/AcmeWidget' (already exported)",
349
+ );
350
+ });
351
+
352
+ it('flags an import that names another package', async () => {
353
+ write(
354
+ 'components/AcmeWidget.doc.mjs',
355
+ DOC('@acme/other/components/AcmeWidget'),
356
+ );
357
+ const issue = (await found()).find(
358
+ i => i.code === 'component_import_other_package',
359
+ );
360
+ expect(issue?.message).toContain(
361
+ "set import: '@acme/widgets/components/AcmeWidget'",
362
+ );
363
+ });
364
+ });
365
+
366
+ describe('public imports: component examples', () => {
367
+ it('flags a subpath exports does not declare', async () => {
368
+ write(
369
+ 'components/AcmeWidget.doc.mjs',
370
+ DOC(
371
+ undefined,
372
+ "import {AcmeWidget} from '@acme/widgets/src/AcmeWidget';\\n<AcmeWidget />",
373
+ ),
374
+ );
375
+ expect(await codes()).toEqual(['example_import_not_exported']);
376
+ });
377
+
378
+ it('never points an example back at the same undeclared doc import', async () => {
379
+ pkg(p => delete p.exports['./components/AcmeWidget']);
380
+ const issue = (await found()).find(
381
+ i => i.code === 'example_import_not_exported',
382
+ );
383
+ expect(issue?.message).not.toContain('in the example, as the doc');
384
+ expect(issue?.message).toContain(
385
+ 'add "./components/AcmeWidget": "./components/AcmeWidget.tsx" to package.json "exports"',
386
+ );
387
+ });
388
+
389
+ it('gives the exports entry for a hooks path when the doc import lacks the hook', async () => {
390
+ write(
391
+ 'components/useAcmeTheme.ts',
392
+ 'export function useAcmeTheme() {\n return null;\n}\n',
393
+ );
394
+ write(
395
+ 'components/AcmeWidget.doc.mjs',
396
+ DOC(
397
+ undefined,
398
+ "import {useAcmeTheme} from '@acme/widgets/components/useAcmeTheme';\\nuseAcmeTheme();",
399
+ ),
400
+ );
401
+ const issue = (await found()).find(
402
+ i => i.code === 'example_import_not_exported',
403
+ );
404
+ expect(issue?.message).not.toContain(
405
+ "import from '@acme/widgets/components/AcmeWidget'",
406
+ );
407
+ expect(issue?.message).toContain(
408
+ 'add "./components/useAcmeTheme": "./components/useAcmeTheme.ts" to package.json "exports"',
409
+ );
410
+ });
411
+
412
+ it('accepts aliased, type-only and other-package imports', async () => {
413
+ write(
414
+ 'components/AcmeWidget.doc.mjs',
415
+ DOC(
416
+ undefined,
417
+ "import {AcmeWidget as W, type AcmeWidgetProps} from '@acme/widgets/components/AcmeWidget';\\nimport {Button} from '@astryxdesign/core/Button';\\nimport x from 'left-pad';\\n<W label=\\\"a\\\" />",
418
+ ),
419
+ );
420
+ expect(await codes()).toEqual([]);
421
+ });
422
+
423
+ it('reads named, default, aliased, namespace and type-only imports', () => {
424
+ const statements = readImports(
425
+ "import D, {A as B, type T} from 'p/a';\nimport type {U} from 'p/b';\nimport * as N from 'p/c';\n...\n<D />",
426
+ );
427
+ expect(statements.map(s => s.source)).toEqual(['p/a', 'p/b', 'p/c']);
428
+ expect(statements[0].names).toEqual([
429
+ {imported: 'default', typeOnly: false},
430
+ {imported: 'A', typeOnly: false},
431
+ {imported: 'T', typeOnly: true},
432
+ ]);
433
+ expect(statements[1].names).toEqual([{imported: 'U', typeOnly: true}]);
434
+ expect(statements[2].namespace).toBe(true);
435
+ });
436
+ });
437
+
438
+ describe('public imports: template sources', () => {
439
+ it('flags a relative import that leaves the templates root', async () => {
440
+ write(
441
+ 'templates/account-card.tsx',
442
+ 'import {AcmeWidget} from \'../components/AcmeWidget\';\nexport default function A() {\n return <AcmeWidget label="x" />;\n}\n',
443
+ );
444
+ expect(await codes()).toEqual(['template_import_outside_root']);
445
+ });
446
+
447
+ it('flags a Core path Core does not export, and accepts a public one', async () => {
448
+ write(
449
+ 'templates/account-card.tsx',
450
+ 'import {Button} from \'@astryxdesign/core/src/Button/Button\';\nexport default function A() {\n return <Button label="x" />;\n}\n',
451
+ );
452
+ const issue = (await found()).find(
453
+ i => i.code === 'template_private_import',
454
+ );
455
+ expect(issue?.message).toContain("'@astryxdesign/core/Button'");
456
+ write(
457
+ 'templates/account-card.tsx',
458
+ 'import {Button} from \'@astryxdesign/core/Button\';\nexport default function A() {\n return <Button label="x" />;\n}\n',
459
+ );
460
+ expect(await codes()).toEqual([]);
461
+ });
462
+
463
+ it('flags a template whose public import exports does not declare', async () => {
464
+ pkg(p => delete p.exports['./templates/account-card']);
465
+ const issue = (await found()).find(
466
+ i => i.code === 'template_import_not_exported',
467
+ );
468
+ expect(issue?.message).toContain(
469
+ '"./templates/account-card": "./templates/account-card.tsx"',
470
+ );
471
+ });
472
+ });
473
+
474
+ describe('subpathDeclaration', () => {
475
+ it('reads each way exports can declare a subpath', () => {
476
+ expect(subpathDeclaration(undefined, './a').state).toBe('no_exports');
477
+ expect(subpathDeclaration('./i.js', './a').state).toBe('root_only');
478
+ expect(subpathDeclaration({import: './i.js'}, './a').state).toBe(
479
+ 'root_only',
480
+ );
481
+ expect(subpathDeclaration({'./a': null}, './a').state).toBe('blocked');
482
+ expect(subpathDeclaration({'./a': {require: './a.cjs'}}, './a').state).toBe(
483
+ 'not_importable',
484
+ );
485
+ expect(subpathDeclaration({'./*': './src/*.ts'}, './a').state).toBe(
486
+ 'declared',
487
+ );
488
+ expect(
489
+ subpathDeclaration(
490
+ {'./*': './src/*.ts', './private/*': null},
491
+ './private/a',
492
+ ).state,
493
+ ).toBe('blocked');
494
+ });
495
+ });
496
+
497
+ describe('derived component imports', () => {
498
+ const NO_IMPORT = `export default {
499
+ type: 'component',
500
+ name: 'AcmeWidget',
501
+ displayName: 'Acme Widget',
502
+ usage: {description: 'A pressable widget.'},
503
+ props: [{name: 'label', type: 'string', description: 'Text.', required: true}],
504
+ };
505
+ `;
506
+
507
+ it('warns when the derived root import is not declared', async () => {
508
+ write('components/AcmeWidget.doc.mjs', NO_IMPORT);
509
+ const issue = (await found()).find(
510
+ i => i.code === 'component_import_not_exported',
511
+ );
512
+ expect(issue?.message).toContain(
513
+ 'has no import, so apps are told to import from "@acme/widgets"',
514
+ );
515
+ });
516
+ });
517
+
518
+ describe('fixes for two components in one folder', () => {
519
+ const docFor = (/** @type {string} */ name) => `export default {
520
+ type: 'component',
521
+ name: '${name}',
522
+ displayName: '${name}',
523
+ usage: {description: 'A widget.'},
524
+ props: [],
525
+ };
526
+ `;
527
+
528
+ beforeEach(() => {
529
+ for (const name of ['AcmeAlpha', 'AcmeBeta']) {
530
+ write(`components/${name}.doc.mjs`, docFor(name));
531
+ write(
532
+ `components/${name}.tsx`,
533
+ `export function ${name}() {\n return null;\n}\n`,
534
+ );
535
+ }
536
+ });
537
+
538
+ it('gives each import-less component its own entry, never the root', async () => {
539
+ const fixes = (await found())
540
+ .filter(i => i.code === 'component_import_not_exported')
541
+ .map(i => i.message.split('Fix: ')[1]);
542
+ expect(fixes).toEqual(
543
+ expect.arrayContaining([
544
+ `add "./components/AcmeAlpha": "./components/AcmeAlpha.tsx" to package.json "exports" and set import: '@acme/widgets/components/AcmeAlpha' in the doc.`,
545
+ `add "./components/AcmeBeta": "./components/AcmeBeta.tsx" to package.json "exports" and set import: '@acme/widgets/components/AcmeBeta' in the doc.`,
546
+ ]),
547
+ );
548
+ for (const fix of fixes) expect(fix).not.toContain('".":');
549
+ });
550
+ });
551
+
552
+ describe('type-only imports', () => {
553
+ it.each([
554
+ [
555
+ 'a statement-level import type',
556
+ {'./types': {types: './types.d.ts'}},
557
+ {'types.d.ts': 'export type AcmeWidgetSize = "sm" | "md";\n'},
558
+ "import type {AcmeWidgetSize} from '@acme/widgets/types';",
559
+ ],
560
+ [
561
+ 'an import whose every binding is an inline type',
562
+ {'./types': {types: './types.d.ts'}},
563
+ {'types.d.ts': 'export type AcmeWidgetSize = "sm" | "md";\n'},
564
+ "import {type AcmeWidgetSize} from '@acme/widgets/types';",
565
+ ],
566
+ [
567
+ 'a plain import of names the types file declares as types',
568
+ {'./types': {types: './types.d.ts'}},
569
+ {
570
+ 'types.d.ts':
571
+ 'export type AcmeWidgetSize = "sm" | "md";\ninterface AcmeTone {}\nexport {type AcmeTone as Tone};\n',
572
+ },
573
+ "import {AcmeWidgetSize, Tone} from '@acme/widgets/types';",
574
+ ],
575
+ [
576
+ 'a plain import of a declared type (wildcard entry)',
577
+ {'./types/*': {types: './types/*.d.ts'}},
578
+ {'types/size.d.ts': 'export type AcmeWidgetSize = "sm" | "md";\n'},
579
+ "import {AcmeWidgetSize} from '@acme/widgets/types/size';",
580
+ ],
581
+ [
582
+ 'a statement-level import type (types beside only require)',
583
+ {'./legacy': {types: './legacy.d.ts', require: './legacy.cjs'}},
584
+ {'legacy.d.ts': 'export type AcmeLegacy = string;\n'},
585
+ "import type {AcmeLegacy} from '@acme/widgets/legacy';",
586
+ ],
587
+ [
588
+ 'a plain import of a declared type (types beside only require)',
589
+ {'./legacy': {types: './legacy.d.ts', require: './legacy.cjs'}},
590
+ {'legacy.d.ts': 'export type AcmeLegacy = string;\n'},
591
+ "import {AcmeLegacy} from '@acme/widgets/legacy';",
592
+ ],
593
+ ])(
594
+ 'accepts %s from a subpath with a types target',
595
+ async (_label, entries, files, typeImport) => {
596
+ pkg(p => {
597
+ Object.assign(p.exports, entries);
598
+ });
599
+ for (const [file, text] of Object.entries(files)) write(file, text);
600
+ write(
601
+ 'components/AcmeWidget.doc.mjs',
602
+ DOC(
603
+ undefined,
604
+ `${typeImport}\\nimport {AcmeWidget} from '@acme/widgets/components/AcmeWidget';\\n<AcmeWidget />`,
605
+ ),
606
+ );
607
+ expect(await codes()).toEqual([]);
608
+ const report = (await validateLocalIntegration(dir)).publicImports;
609
+ expect(report?.unchecked).toEqual([]);
610
+ },
611
+ );
612
+
613
+ it('notes an import type whose only types target sits under require', async () => {
614
+ pkg(p => {
615
+ p.exports['./legacy'] = {
616
+ require: {types: './legacy.d.cts', default: './legacy.cjs'},
617
+ };
618
+ });
619
+ write('legacy.d.cts', 'export type AcmeLegacy = string;\n');
620
+ write(
621
+ 'components/AcmeWidget.doc.mjs',
622
+ DOC(
623
+ undefined,
624
+ "import type {AcmeLegacy} from '@acme/widgets/legacy';\\nimport {AcmeWidget} from '@acme/widgets/components/AcmeWidget';\\n<AcmeWidget />",
625
+ ),
626
+ );
627
+ expect(await codes()).not.toEqual([]);
628
+ });
629
+
630
+ it('lists a value import from a subpath with types beside only require as not checked', async () => {
631
+ pkg(p => {
632
+ p.exports['./legacy'] = {types: './legacy.d.ts', require: './legacy.cjs'};
633
+ });
634
+ write('legacy.d.ts', 'export declare const acmeLegacy: string;\n');
635
+ write(
636
+ 'components/AcmeWidget.doc.mjs',
637
+ DOC(
638
+ undefined,
639
+ "import {acmeLegacy} from '@acme/widgets/legacy';\\nimport {AcmeWidget} from '@acme/widgets/components/AcmeWidget';\\n<AcmeWidget />",
640
+ ),
641
+ );
642
+ expect(await codes()).toEqual([]);
643
+ const report = (await validateLocalIntegration(dir)).publicImports;
644
+ expect(report?.unchecked.map(u => u.specifier)).toEqual([
645
+ '@acme/widgets/legacy',
646
+ ]);
647
+ });
648
+
649
+ it('lists a value or unreadable import from a types-only subpath as not checked, never a note', async () => {
650
+ pkg(p => {
651
+ p.exports['./types'] = {types: './types.d.ts'};
652
+ p.exports['./gone'] = {types: './gone.d.ts'};
653
+ });
654
+ write(
655
+ 'types.d.ts',
656
+ 'export type AcmeWidgetSize = "sm";\nexport declare const ACME_SIZES: string[];\n',
657
+ );
658
+ write(
659
+ 'components/AcmeWidget.doc.mjs',
660
+ DOC(
661
+ undefined,
662
+ "import {type AcmeWidgetSize, ACME_SIZES} from '@acme/widgets/types';\\nimport {Gone} from '@acme/widgets/gone';\\nimport type {X} from '@acme/widgets/nope';\\n<AcmeWidget />",
663
+ ),
664
+ );
665
+ const notes = await found();
666
+ expect(notes.map(n => n.code)).toEqual(['example_import_not_exported']);
667
+ expect(notes[0].message).toContain('@acme/widgets/nope');
668
+ expect(notes[0].message).not.toContain('"default"');
669
+ const report = (await validateLocalIntegration(dir)).publicImports;
670
+ expect(report?.unchecked.map(u => u.specifier).sort()).toEqual([
671
+ '@acme/widgets/gone',
672
+ '@acme/widgets/types',
673
+ ]);
674
+ expect(
675
+ report?.unchecked.find(u => u.specifier === '@acme/widgets/types')
676
+ ?.reason,
677
+ ).toContain('ACME_SIZES');
678
+ });
679
+ });
680
+
681
+ describe('an empty exports map', () => {
682
+ it('names the missing subpath and adds only that entry', async () => {
683
+ pkg(p => {
684
+ p.exports = {};
685
+ });
686
+ const issue = (await found()).find(
687
+ i => i.code === 'component_import_not_exported',
688
+ );
689
+ expect(issue?.message).toContain(
690
+ 'package.json "exports" does not declare "./components/AcmeWidget"',
691
+ );
692
+ expect(issue?.message).toContain(
693
+ 'add "./components/AcmeWidget": "./components/AcmeWidget.tsx" to package.json "exports"',
694
+ );
695
+ expect(issue?.message).not.toContain('subpath map');
696
+ expect(subpathDeclaration({}, '.').state).toBe('missing');
697
+ expect(subpathDeclaration({}, './a').state).toBe('missing');
698
+ });
699
+ });
700
+
701
+ describe('a fresh scaffold passes the checks', () => {
702
+ for (const exportsMap of [undefined, {'.': './index.js'}]) {
703
+ it(`with ${exportsMap ? 'an' : 'no'} exports map`, async () => {
704
+ fs.rmSync(dir, {recursive: true, force: true});
705
+ fs.mkdirSync(dir);
706
+ write(
707
+ 'package.json',
708
+ JSON.stringify({
709
+ name: '@acme/fresh',
710
+ version: '1.0.0',
711
+ type: 'module',
712
+ ...(exportsMap ? {exports: exportsMap} : {}),
713
+ }),
714
+ );
715
+ write('index.js', 'export {};\n');
716
+ await integrationAddComponent('AcmeThing', {cwd: dir});
717
+ await integrationAddTemplate('acme-page', {cwd: dir});
718
+ const check = await integrationCheck({cwd: dir, strict: true});
719
+ expect(check.data.failures).toEqual([]);
720
+ });
721
+ }
722
+ });
723
+
724
+ describe('imports listed as not checked', () => {
725
+ it('lists a template import of Core when Core is not installed', () => {
726
+ const out = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-no-core-'));
727
+ try {
728
+ const put = (/** @type {string} */ rel, /** @type {string} */ text) => {
729
+ fs.mkdirSync(path.dirname(path.join(out, rel)), {recursive: true});
730
+ fs.writeFileSync(path.join(out, rel), text);
731
+ };
732
+ put(
733
+ 'package.json',
734
+ JSON.stringify({
735
+ name: '@acme/widgets',
736
+ version: '1.0.0',
737
+ type: 'module',
738
+ exports: {'./templates/account-card': './templates/account-card.tsx'},
739
+ }),
740
+ );
741
+ put(
742
+ 'astryx.integration.mjs',
743
+ "export default {templates: './templates'};\n",
744
+ );
745
+ put('templates/account-card.doc.mjs', BLOCK());
746
+ put(
747
+ 'templates/account-card.tsx',
748
+ "import {Button} from '@astryxdesign/core/src/Button';\nexport default function A() {\n return <Button />;\n}\n",
749
+ );
750
+ const run = spawnSync(
751
+ process.execPath,
752
+ [
753
+ path.join(CLI_ROOT, 'clients/cli/bin/astryx.mjs'),
754
+ '--json',
755
+ 'doctor',
756
+ 'integration',
757
+ 'validate',
758
+ ],
759
+ {cwd: out, encoding: 'utf-8', timeout: 60_000},
760
+ );
761
+ const {data} = JSON.parse(run.stdout);
762
+ expect(
763
+ data.issues.filter(
764
+ (/** @type {{code: string}} */ i) =>
765
+ i.code === 'template_private_import',
766
+ ),
767
+ ).toEqual([]);
768
+ expect(data.publicImports.unchecked).toEqual([
769
+ expect.objectContaining({
770
+ file: 'templates/account-card.tsx',
771
+ specifier: '@astryxdesign/core/src/Button',
772
+ }),
773
+ ]);
774
+ } finally {
775
+ fs.rmSync(out, {recursive: true, force: true});
776
+ }
777
+ }, 60_000);
778
+ });
779
+
780
+ describe('which Core and which package', () => {
781
+ it("lists Core imports as not checked when the found core is a workspace's own package", async () => {
782
+ // An npm-workspaces monorepo: its own packages/core is @acme/core, and
783
+ // the integration lives in packages/widgets.
784
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-workspace-'));
785
+ try {
786
+ const put = (/** @type {string} */ rel, /** @type {string} */ text) => {
787
+ fs.mkdirSync(path.dirname(path.join(root, rel)), {recursive: true});
788
+ fs.writeFileSync(path.join(root, rel), text);
789
+ };
790
+ put(
791
+ 'package.json',
792
+ JSON.stringify({
793
+ name: 'acme-monorepo',
794
+ private: true,
795
+ workspaces: ['packages/*'],
796
+ }),
797
+ );
798
+ put(
799
+ 'packages/core/package.json',
800
+ JSON.stringify({
801
+ name: '@acme/core',
802
+ version: '1.0.0',
803
+ exports: {'.': './index.js'},
804
+ }),
805
+ );
806
+ put(
807
+ 'packages/widgets/package.json',
808
+ JSON.stringify({
809
+ name: '@acme/widgets',
810
+ version: '1.0.0',
811
+ type: 'module',
812
+ exports: {'./templates/account-card': './templates/account-card.tsx'},
813
+ }),
814
+ );
815
+ put(
816
+ 'packages/widgets/astryx.integration.mjs',
817
+ "export default {templates: './templates'};\n",
818
+ );
819
+ put('packages/widgets/templates/account-card.doc.mjs', BLOCK());
820
+ put(
821
+ 'packages/widgets/templates/account-card.tsx',
822
+ "import {Button} from '@astryxdesign/core/Button';\nexport default function A() {\n return <Button />;\n}\n",
823
+ );
824
+ const result = await validateIntegration(undefined, {
825
+ cwd: path.join(root, 'packages/widgets'),
826
+ });
827
+ expect(
828
+ (result.data.notes ?? []).filter(
829
+ i => i.code === 'template_private_import',
830
+ ),
831
+ ).toEqual([]);
832
+ expect(result.data.publicImports?.unchecked).toEqual([
833
+ expect.objectContaining({
834
+ file: 'templates/account-card.tsx',
835
+ specifier: '@astryxdesign/core/Button',
836
+ }),
837
+ ]);
838
+ } finally {
839
+ fs.rmSync(root, {recursive: true, force: true});
840
+ }
841
+ });
842
+
843
+ it('with no package name, runs only the checks that need none and lists the rest as not checked', async () => {
844
+ pkg(p => delete p.name);
845
+ write(
846
+ 'templates/account-card.tsx',
847
+ "import {AcmeWidget} from '../components/AcmeWidget';\nimport {X} from '@acme/widgets/nope';\nexport default function C() {\n return <AcmeWidget label={X} />;\n}\n",
848
+ );
849
+ const result = await validateIntegration(undefined, {cwd: dir});
850
+ expect(await codes()).toEqual(['template_import_outside_root']);
851
+ expect(result.data.publicImports?.status).toBe('checked');
852
+ expect(result.data.publicImports?.unchecked).toContainEqual(
853
+ expect.objectContaining({
854
+ file: 'package.json',
855
+ reason: expect.stringContaining('no "name"'),
856
+ }),
857
+ );
858
+ // Nothing from the public-import check fails `integration check
859
+ // --strict`: no skip, no warning (other checks may report the missing
860
+ // name themselves).
861
+ const {warnings, skipped} = strictIssues('validate', result);
862
+ expect(
863
+ [...warnings, ...skipped].filter(
864
+ each => CODES.has(each.code) || each.code === 'public_imports_skipped',
865
+ ),
866
+ ).toEqual([]);
867
+ });
868
+ });