@algotao/uiscope 0.0.0-stage → 0.1.1

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 (101) hide show
  1. package/.agents/plugins/marketplace.json +21 -0
  2. package/.cursor-plugin/plugin.json +8 -0
  3. package/LICENSE +21 -0
  4. package/README.md +255 -2
  5. package/README.zh-CN.md +256 -0
  6. package/bin/uiscope.js +8 -0
  7. package/package.json +89 -3
  8. package/plugin.json +17 -0
  9. package/presets/README.md +11 -0
  10. package/presets/ocpui/index.ts +107 -0
  11. package/skills/README.md +7 -0
  12. package/skills/uiscope-ux-optimize/SKILL.md +267 -0
  13. package/skills/uiscope-ux-optimize/scripts/uiscope.mjs +43 -0
  14. package/src/__tests__/cli-parity.spec.ts +59 -0
  15. package/src/__tests__/cli-read.spec.ts +141 -0
  16. package/src/__tests__/component-tree.spec.ts +72 -0
  17. package/src/__tests__/enhance-core-ux.spec.ts +72 -0
  18. package/src/__tests__/enhance-feedback-states.spec.ts +104 -0
  19. package/src/__tests__/facet-analyze.spec.ts +55 -0
  20. package/src/__tests__/fixtures/exmarket-integration.config.ts +19 -0
  21. package/src/__tests__/format-scan-stats.spec.ts +32 -0
  22. package/src/__tests__/install-skill.spec.ts +41 -0
  23. package/src/__tests__/merge-cli-config.spec.ts +56 -0
  24. package/src/__tests__/react-tree.spec.ts +29 -0
  25. package/src/__tests__/read-scan-report-boundaries.spec.ts +139 -0
  26. package/src/__tests__/read-scan-report.spec.ts +91 -0
  27. package/src/__tests__/resolve-enhancements.spec.ts +21 -0
  28. package/src/__tests__/resolve-presets.spec.ts +51 -0
  29. package/src/__tests__/resolve-scan-scope.spec.ts +28 -0
  30. package/src/__tests__/run-scan-ux.spec.ts +51 -0
  31. package/src/__tests__/scan-progress.spec.ts +52 -0
  32. package/src/__tests__/skill-forwarder.spec.ts +53 -0
  33. package/src/__tests__/style-path.spec.ts +11 -0
  34. package/src/__tests__/style-rules.spec.ts +25 -0
  35. package/src/__tests__/template-hints.spec.ts +19 -0
  36. package/src/__tests__/wrapper-roles.spec.ts +31 -0
  37. package/src/adapters/react/collect-react-files.ts +17 -0
  38. package/src/adapters/react/component-tree.tsx.ts +79 -0
  39. package/src/adapters/react/index.ts +3 -0
  40. package/src/adapters/react/parse-jsx-imports.ts +38 -0
  41. package/src/adapters/vue/index.ts +4 -0
  42. package/src/cli/brand.ts +35 -0
  43. package/src/cli/install-skill.ts +90 -0
  44. package/src/cli/main.ts +578 -0
  45. package/src/cli/merge-cli-config.ts +86 -0
  46. package/src/config/defaults.ts +65 -0
  47. package/src/config/frameworks.ts +20 -0
  48. package/src/config/load-config.ts +81 -0
  49. package/src/config/normalize-legacy-config.ts +39 -0
  50. package/src/config/preset-types.ts +16 -0
  51. package/src/config/resolve-preset-library-roots.ts +49 -0
  52. package/src/config/resolve-presets.ts +142 -0
  53. package/src/config/resolve-scan-scope.ts +12 -0
  54. package/src/config/resolve-thresholds.ts +12 -0
  55. package/src/enhance/dir-utils.ts +25 -0
  56. package/src/enhance/registry.ts +107 -0
  57. package/src/enhance/resolve-enhancements.ts +56 -0
  58. package/src/enhance/rules/action-semantics.ts +60 -0
  59. package/src/enhance/rules/core-ux.ts +139 -0
  60. package/src/enhance/rules/facet-hygiene.ts +44 -0
  61. package/src/enhance/rules/feedback-states.ts +39 -0
  62. package/src/enhance/rules/form-a11y.ts +62 -0
  63. package/src/enhance/rules/hollow-shell.ts +39 -0
  64. package/src/enhance/rules/i18n-hints.ts +29 -0
  65. package/src/enhance/rules/list-templating.ts +38 -0
  66. package/src/enhance/rules/page-rollup.ts +82 -0
  67. package/src/enhance/tree-walk.ts +29 -0
  68. package/src/enhance/types.ts +48 -0
  69. package/src/enhance/ux-rule-pack.ts +40 -0
  70. package/src/enhance/workbench-defaults.ts +28 -0
  71. package/src/index.ts +80 -0
  72. package/src/manifest/consumer-wrapper-roles.ts +34 -0
  73. package/src/manifest/library-components.ts +46 -0
  74. package/src/manifest/library-style-classes.ts +31 -0
  75. package/src/manifest/library-style-whitelist.ts +82 -0
  76. package/src/manifest/library-ux-taxonomy.ts +103 -0
  77. package/src/paths/monorepo-root.ts +13 -0
  78. package/src/report/facet-output-limits.ts +56 -0
  79. package/src/report/format-scan-stats.ts +18 -0
  80. package/src/report/format-ux-scan-report.ts +59 -0
  81. package/src/report/markdown-report.ts +108 -0
  82. package/src/report/print-facets.ts +129 -0
  83. package/src/report/print-styles.ts +20 -0
  84. package/src/report/print-tree.ts +70 -0
  85. package/src/report/print-ux-findings.ts +53 -0
  86. package/src/report/read-scan-report.ts +201 -0
  87. package/src/report/run-report-scenarios.ts +139 -0
  88. package/src/run-scan.ts +341 -0
  89. package/src/scan/collect-source-files.ts +36 -0
  90. package/src/scan/component-tree.ts +155 -0
  91. package/src/scan/extract-node-attrs.ts +49 -0
  92. package/src/scan/facet-analyze.ts +318 -0
  93. package/src/scan/file-uses-library.ts +37 -0
  94. package/src/scan/library-meta.ts +71 -0
  95. package/src/scan/parse-imports.ts +88 -0
  96. package/src/scan/scan-progress.ts +89 -0
  97. package/src/scan/style-path.ts +26 -0
  98. package/src/scan/style-rules.ts +127 -0
  99. package/src/scan/style-trace.ts +181 -0
  100. package/src/scan/template-hints.ts +70 -0
  101. package/src/types.ts +274 -0
@@ -0,0 +1,72 @@
1
+ import path from 'node:path';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { buildFileComponentTree } from '../scan/component-tree.js';
4
+ import type { UiscopeConfig } from '../types.js';
5
+
6
+ const config: UiscopeConfig = {
7
+ roots: ['.'],
8
+ ignore: [],
9
+ libraryImportSources: ['@acme/ui'],
10
+ libraryRoots: [],
11
+ overrideThreshold: 3,
12
+ };
13
+
14
+ describe('buildFileComponentTree', () => {
15
+ it('marks library imports and nests html inside', () => {
16
+ const source = `
17
+ <script setup lang="ts">
18
+ import { Button, Empty } from '@acme/ui'
19
+ </script>
20
+ <template>
21
+ <Empty title="x">
22
+ <template #footer>
23
+ <Button variant="text">Reset</Button>
24
+ <div><span>hint</span></div>
25
+ </template>
26
+ </Empty>
27
+ </template>
28
+ `;
29
+ const names = new Set(['Button', 'Empty']);
30
+ const report = buildFileComponentTree('Demo.vue', source, config, names, '');
31
+ expect(report?.usesLibrary).toBe(true);
32
+ expect(report?.tree.children[0]?.kind).toBe('library');
33
+ expect(report?.tree.children[0]?.name).toBe('Empty');
34
+ const find = (nodes: typeof report.tree.children, name: string): typeof nodes[0] | undefined => {
35
+ for (const n of nodes) {
36
+ if (n.name === name && n.kind === 'library') return n;
37
+ const hit = find(n.children, name);
38
+ if (hit) return hit;
39
+ }
40
+ return undefined;
41
+ };
42
+ expect(find(report!.tree.children, 'Button')?.kind).toBe('library');
43
+ });
44
+
45
+ it('includes v-if branches in the component tree', () => {
46
+ const source = `
47
+ <script setup lang="ts">
48
+ import { Button, Input } from '@acme/ui'
49
+ const show = true
50
+ </script>
51
+ <template>
52
+ <div>
53
+ <Input placeholder="q" />
54
+ <Button v-if="show" type="button">Clear</Button>
55
+ </div>
56
+ </template>
57
+ `;
58
+ const names = new Set(['Button', 'Input']);
59
+ const report = buildFileComponentTree('Filter.vue', source, config, names, '');
60
+ const hasButton = ((): boolean => {
61
+ const walk = (nodes: typeof report!.tree.children): boolean => {
62
+ for (const n of nodes) {
63
+ if (n.name === 'Button') return true;
64
+ if (walk(n.children)) return true;
65
+ }
66
+ return false;
67
+ };
68
+ return walk(report!.tree.children);
69
+ })();
70
+ expect(hasButton).toBe(true);
71
+ });
72
+ });
@@ -0,0 +1,72 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { analyzeFileFacets } from '../scan/facet-analyze.js';
3
+ import type { ComponentTreeNode } from '../types.js';
4
+ import { runCoreUx } from '../enhance/rules/core-ux.js';
5
+ import { DEFAULT_CONFIG, mergeConfig } from '../config/load-config.js';
6
+ import { DEFAULT_UX_RULE_PACK } from '../enhance/ux-rule-pack.js';
7
+
8
+ function ctx(file: string, tree: ComponentTreeNode) {
9
+ const config = mergeConfig(DEFAULT_CONFIG, {
10
+ consumerWrapperTags: ['SiteButton', 'SiteEmpty'],
11
+ });
12
+ const facets = analyzeFileFacets(file, tree, undefined, config);
13
+ return {
14
+ cwd: '/',
15
+ config,
16
+ trees: [{ file, usesLibrary: true, importSources: [], tree }],
17
+ facetReports: [facets],
18
+ rulePack: DEFAULT_UX_RULE_PACK,
19
+ };
20
+ }
21
+
22
+ describe('runCoreUx', () => {
23
+ it('warns filter-bar without button when only inputs present', () => {
24
+ const tree: ComponentTreeNode = {
25
+ kind: 'root',
26
+ name: 'Filter.vue',
27
+ children: [
28
+ {
29
+ kind: 'html',
30
+ name: 'div',
31
+ attrs: { class: 'flow-wb-filter' },
32
+ children: [
33
+ { kind: 'library', name: 'Input', children: [] },
34
+ { kind: 'library', name: 'Select', children: [] },
35
+ ],
36
+ },
37
+ ],
38
+ };
39
+ const findings = runCoreUx(ctx('Filter.vue', tree));
40
+ expect(findings.some((f) => f.ruleId === 'filter-bar-missing-action')).toBe(true);
41
+ });
42
+
43
+ it('does not warn filter-bar-no-input for empty state with SiteButton only', () => {
44
+ const tree: ComponentTreeNode = {
45
+ kind: 'root',
46
+ name: 'ExMarketEmptyState.vue',
47
+ children: [
48
+ {
49
+ kind: 'unknown',
50
+ name: 'SiteEmpty',
51
+ children: [
52
+ {
53
+ kind: 'html',
54
+ name: 'div',
55
+ attrs: { class: 'exm-empty__actions' },
56
+ children: [
57
+ {
58
+ kind: 'unknown',
59
+ name: 'SiteButton',
60
+ attrs: { class: 'flow-wb-filter__clear' },
61
+ children: [],
62
+ },
63
+ ],
64
+ },
65
+ ],
66
+ },
67
+ ],
68
+ };
69
+ const findings = runCoreUx(ctx('ExMarketEmptyState.vue', tree));
70
+ expect(findings.some((f) => f.ruleId === 'filter-bar-no-input')).toBe(false);
71
+ });
72
+ });
@@ -0,0 +1,104 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { ComponentTreeNode, FileFacetReport } from '../types.js';
3
+ import { runFeedbackStates } from '../enhance/rules/feedback-states.js';
4
+ import { DEFAULT_CONFIG } from '../config/load-config.js';
5
+ import { DEFAULT_UX_RULE_PACK } from '../enhance/ux-rule-pack.js';
6
+ import { dirHasFeedbackCoverage } from '../enhance/dir-utils.js';
7
+
8
+ describe('runFeedbackStates', () => {
9
+ it('suggests feedback when list-region without Empty/Spin', () => {
10
+ const tree: ComponentTreeNode = {
11
+ kind: 'root',
12
+ name: 'List.vue',
13
+ children: [{ kind: 'library', name: 'Table', children: [] }],
14
+ };
15
+ const facetReports: FileFacetReport[] = [
16
+ {
17
+ file: 'List.vue',
18
+ facetCount: 1,
19
+ avgTemplatingRate: 0,
20
+ facets: [
21
+ {
22
+ id: 'root',
23
+ anchorNode: 'root:List.vue',
24
+ kind: 'list-region',
25
+ layoutHints: ['list'],
26
+ depth: 0,
27
+ stats: { nodes: 8, interactiveControls: 0, roles: { list: 1 } },
28
+ templatingRate: 0,
29
+ templateSignals: [],
30
+ },
31
+ ],
32
+ },
33
+ ];
34
+ const findings = runFeedbackStates({
35
+ cwd: '/',
36
+ config: DEFAULT_CONFIG,
37
+ trees: [{ file: 'List.vue', usesLibrary: true, importSources: [], tree }],
38
+ facetReports,
39
+ rulePack: DEFAULT_UX_RULE_PACK,
40
+ });
41
+ expect(findings).toHaveLength(1);
42
+ expect(findings[0].ruleId).toBe('list-missing-feedback');
43
+ });
44
+
45
+ it('passes when SiteEmpty present', () => {
46
+ const tree: ComponentTreeNode = {
47
+ kind: 'root',
48
+ name: 'List.vue',
49
+ children: [
50
+ { kind: 'library', name: 'Table', children: [] },
51
+ { kind: 'unknown', name: 'SiteEmpty', children: [] },
52
+ ],
53
+ };
54
+ const facetReports: FileFacetReport[] = [
55
+ {
56
+ file: 'List.vue',
57
+ facetCount: 1,
58
+ avgTemplatingRate: 0,
59
+ facets: [
60
+ {
61
+ id: 'root',
62
+ anchorNode: 'root:List.vue',
63
+ kind: 'list-region',
64
+ layoutHints: ['list'],
65
+ depth: 0,
66
+ stats: { nodes: 10, interactiveControls: 0, roles: { list: 1 } },
67
+ templatingRate: 0,
68
+ templateSignals: [],
69
+ },
70
+ ],
71
+ },
72
+ ];
73
+ const findings = runFeedbackStates({
74
+ cwd: '/',
75
+ config: DEFAULT_CONFIG,
76
+ trees: [{ file: 'List.vue', usesLibrary: true, importSources: [], tree }],
77
+ facetReports,
78
+ rulePack: DEFAULT_UX_RULE_PACK,
79
+ });
80
+ expect(findings).toHaveLength(0);
81
+ });
82
+
83
+ it('dir sibling EmptyState satisfies coverage', () => {
84
+ const trees = [
85
+ {
86
+ file: '/app/exmarket/ExMarketWorkbench.vue',
87
+ usesLibrary: true,
88
+ importSources: [],
89
+ tree: { kind: 'root' as const, name: 'W.vue', children: [] },
90
+ },
91
+ {
92
+ file: '/app/exmarket/ExMarketEmptyState.vue',
93
+ usesLibrary: true,
94
+ importSources: [],
95
+ tree: {
96
+ kind: 'root' as const,
97
+ name: 'E.vue',
98
+ children: [{ kind: 'unknown' as const, name: 'SiteEmpty', children: [] }],
99
+ },
100
+ },
101
+ ];
102
+ expect(dirHasFeedbackCoverage(trees, trees[0].file, new Set(['SiteEmpty']))).toBe(true);
103
+ });
104
+ });
@@ -0,0 +1,55 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { analyzeFileFacets } from '../scan/facet-analyze.js';
3
+ import type { ComponentTreeNode } from '../types.js';
4
+
5
+ describe('analyzeFileFacets', () => {
6
+ it('detects filter-bar facet with multiple inputs', () => {
7
+ const tree: ComponentTreeNode = {
8
+ kind: 'root',
9
+ name: 'Filter.vue',
10
+ children: [
11
+ {
12
+ kind: 'html',
13
+ name: 'div',
14
+ attrs: { class: 'flow-wb-filter' },
15
+ children: [
16
+ { kind: 'library', name: 'Input', children: [] },
17
+ { kind: 'library', name: 'Select', children: [] },
18
+ { kind: 'library', name: 'Button', children: [] },
19
+ ],
20
+ },
21
+ ],
22
+ };
23
+ const report = analyzeFileFacets('Filter.vue', tree);
24
+ expect(report.facetCount).toBeGreaterThanOrEqual(1);
25
+ expect(report.facets.some((f) => f.kind === 'filter-bar')).toBe(true);
26
+ });
27
+
28
+ it('detects list templating from repeated siblings', () => {
29
+ const item = (i: number): ComponentTreeNode => ({
30
+ kind: 'html',
31
+ name: 'li',
32
+ attrs: { class: 'list-item card-row' },
33
+ children: [
34
+ { kind: 'library', name: 'Icon', children: [] },
35
+ { kind: 'html', name: 'span', children: [] },
36
+ ],
37
+ });
38
+ const tree: ComponentTreeNode = {
39
+ kind: 'root',
40
+ name: 'List.vue',
41
+ children: [
42
+ {
43
+ kind: 'html',
44
+ name: 'ul',
45
+ attrs: { class: 'index-list' },
46
+ children: [item(0), item(1), item(2), item(3)],
47
+ },
48
+ ],
49
+ };
50
+ const report = analyzeFileFacets('List.vue', tree);
51
+ const listFacet = report.facets.find((f) => f.kind === 'list-region');
52
+ expect(listFacet).toBeDefined();
53
+ expect(listFacet?.templateSignals.some((s) => s.kind === 'repeated-sibling')).toBe(true);
54
+ });
55
+ });
@@ -0,0 +1,19 @@
1
+ import type { UiscopeConfig } from '../../types.js';
2
+
3
+ export default {
4
+ roots: ['app'],
5
+ ignore: ['**/node_modules/**', '**/.nuxt/**', '**/.output/**'],
6
+ presets: ['ocpui'],
7
+ libraryImportSources: ['@ocpui/vue', '~/components/ui/ocp'],
8
+ libraryRoots: ['../../packages/ocpui/core/vue'],
9
+ onlyLibraryConsumers: true,
10
+ consumerWrapperTags: ['SiteEmpty', 'SiteButton', 'SiteButtonLink', 'FlowWorkbenchFilter'],
11
+ thresholds: {
12
+ style: {
13
+ overrideRuleCount: 6,
14
+ excludeGlobs: ['**/assets/css/site-layout.css'],
15
+ warnOrphanOverrides: false,
16
+ },
17
+ tree: { maxLibraryNestDepth: 6 },
18
+ },
19
+ } satisfies Partial<UiscopeConfig>;
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { formatScanStatsBanner } from '../report/format-scan-stats.js';
3
+
4
+ describe('formatScanStatsBanner', () => {
5
+ it('includes scan totals and issue file count', () => {
6
+ const line = formatScanStatsBanner({
7
+ stats: {
8
+ vueFilesScanned: 90,
9
+ vueFilesLibraryConsumers: 90,
10
+ reactFilesScanned: 0,
11
+ reactFilesLibraryConsumers: 0,
12
+ cssFilesScanned: 0,
13
+ facetRegions: 98,
14
+ uxFindings: 3,
15
+ },
16
+ trees: [{}, {}, {}] as never,
17
+ facetReports: [
18
+ { file: '/a.vue', facetCount: 2, avgTemplatingRate: 0, facets: [] },
19
+ { file: '/b.vue', facetCount: 0, avgTemplatingRate: 0, facets: [] },
20
+ ],
21
+ uxFindings: [
22
+ { file: '/a.vue' } as never,
23
+ { file: '/a.vue' } as never,
24
+ { file: '/c.vue' } as never,
25
+ ],
26
+ warnings: ['[uiscope:ux] warn'],
27
+ });
28
+ expect(line).toContain('Vue 90');
29
+ expect(line).toContain('UX 发现 3(2 个文件)');
30
+ expect(line).toContain('(1 个文件含切面)');
31
+ });
32
+ });
@@ -0,0 +1,41 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { afterEach, describe, expect, it } from 'vitest';
6
+ import { runInstallSkill } from '../cli/install-skill.js';
7
+
8
+ const pkgRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
9
+
10
+ describe('install skill', () => {
11
+ const tmpRoots: string[] = [];
12
+
13
+ afterEach(() => {
14
+ for (const root of tmpRoots) {
15
+ fs.rmSync(root, { recursive: true, force: true });
16
+ }
17
+ tmpRoots.length = 0;
18
+ });
19
+
20
+ it('copies skill into project .cursor/skills', () => {
21
+ const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'uiscope-install-'));
22
+ tmpRoots.push(cwd);
23
+ const code = runInstallSkill(cwd, { dryRun: false });
24
+ expect(code).toBe(0);
25
+ const skillMd = path.join(cwd, '.cursor/skills/uiscope-ux-optimize/SKILL.md');
26
+ expect(fs.existsSync(skillMd)).toBe(true);
27
+ expect(fs.readFileSync(skillMd, 'utf8')).toContain('uiscope-ux-optimize');
28
+ });
29
+
30
+ it('dry-run does not write files', () => {
31
+ const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'uiscope-install-dry-'));
32
+ tmpRoots.push(cwd);
33
+ const code = runInstallSkill(cwd, { dryRun: true });
34
+ expect(code).toBe(0);
35
+ expect(fs.existsSync(path.join(cwd, '.cursor/skills/uiscope-ux-optimize'))).toBe(false);
36
+ });
37
+
38
+ it('skill source exists in package', () => {
39
+ expect(fs.existsSync(path.join(pkgRoot, 'skills/uiscope-ux-optimize/SKILL.md'))).toBe(true);
40
+ });
41
+ });
@@ -0,0 +1,56 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { describe, expect, it } from 'vitest';
5
+ import { DEFAULT_CONFIG } from '../config/load-config.js';
6
+ import { mergeCliIntoConfig } from '../cli/merge-cli-config.js';
7
+
8
+ const pkgRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
9
+
10
+ describe('mergeCliIntoConfig', () => {
11
+ it('merges repeatable CLI lists and thresholds', () => {
12
+ const out = mergeCliIntoConfig(pkgRoot, { ...DEFAULT_CONFIG }, ['ocpui'], {
13
+ roots: ['app'],
14
+ libraryRoots: ['extra/lib'],
15
+ libraryImports: [],
16
+ consumerWrappers: ['SiteButton'],
17
+ ignoreGlobs: ['**/foo.d.ts'],
18
+ styleExcludes: ['**/skin.css'],
19
+ threshold: 6,
20
+ nestDepth: 6,
21
+ templateWarnRate: 0.7,
22
+ multiFacetWarn: 5,
23
+ noWarnOrphanStyles: true,
24
+ });
25
+
26
+ expect(out.roots).toEqual(['app']);
27
+ expect(out.consumerWrapperTags).toContain('SiteButton');
28
+ expect(out.ignore).toContain('**/foo.d.ts');
29
+ expect(out.overrideThreshold).toBe(6);
30
+ expect(out.thresholds?.tree?.maxLibraryNestDepth).toBe(6);
31
+ expect(out.thresholds?.facet?.templateWarnRate).toBe(0.7);
32
+ expect(out.thresholds?.facet?.multiFacetWarnCount).toBe(5);
33
+ expect(out.thresholds?.style?.warnOrphanOverrides).toBe(false);
34
+ expect(out.thresholds?.style?.excludeGlobs).toContain('**/skin.css');
35
+ expect(out.libraryRoots).toContain('extra/lib');
36
+ });
37
+
38
+ it('auto-infers libraryRoots for ocpui preset in monorepo', () => {
39
+ const monoRoot = path.resolve(pkgRoot, '../..');
40
+ const ocpuiVueRoot = path.join(monoRoot, 'packages/ocpui/core/vue');
41
+ if (!fs.existsSync(ocpuiVueRoot)) return;
42
+
43
+ const out = mergeCliIntoConfig(monoRoot, { ...DEFAULT_CONFIG, presets: ['ocpui'] }, ['ocpui'], {
44
+ roots: ['src'],
45
+ libraryRoots: [],
46
+ libraryImports: [],
47
+ consumerWrappers: [],
48
+ ignoreGlobs: [],
49
+ styleExcludes: [],
50
+ });
51
+ const expected = path.relative(monoRoot, path.join(monoRoot, 'packages/ocpui/core/vue'));
52
+ if (expected && !expected.startsWith('..')) {
53
+ expect(out.libraryRoots).toContain(expected);
54
+ }
55
+ });
56
+ });
@@ -0,0 +1,29 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { buildReactComponentTree, reactFileUsesLibrary } from '../adapters/react/index.js';
3
+ import type { UiscopeConfig } from '../types.js';
4
+
5
+ const config: UiscopeConfig = {
6
+ roots: ['.'],
7
+ ignore: [],
8
+ libraryImportSources: ['@acme/ui'],
9
+ libraryRoots: [],
10
+ };
11
+
12
+ describe('react component tree', () => {
13
+ it('parses JSX component tree', () => {
14
+ const source = `
15
+ import { Button } from '@acme/ui';
16
+ export function Page() {
17
+ return (
18
+ <div className="wrap">
19
+ <Button>Go</Button>
20
+ </div>
21
+ );
22
+ }
23
+ `;
24
+ expect(reactFileUsesLibrary(source, config, new Set(['Button']))).toBe(true);
25
+ const report = buildReactComponentTree('Page.tsx', source, config, new Set(['Button']));
26
+ expect(report.usesLibrary).toBe(true);
27
+ expect(report.tree.children.some((c) => c.kind === 'html' && c.name === 'div')).toBe(true);
28
+ });
29
+ });
@@ -0,0 +1,139 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { describe, expect, it } from 'vitest';
5
+ import type { ScanResult } from '../types.js';
6
+ import {
7
+ countTextReportLines,
8
+ getScanReportCounts,
9
+ isScanResultPayload,
10
+ readScanReportJsonFile,
11
+ readTextReportLines,
12
+ readTextReportLinesSync,
13
+ sliceScanReportSection,
14
+ } from '../report/read-scan-report.js';
15
+
16
+ const SECTIONS = [
17
+ 'trees',
18
+ 'facetReports',
19
+ 'uxFindings',
20
+ 'styleOverrides',
21
+ 'styleChains',
22
+ 'warnings',
23
+ 'enhancementsRun',
24
+ ] as const;
25
+
26
+ function fixture(extra: Partial<ScanResult> = {}): ScanResult {
27
+ return {
28
+ trees: [],
29
+ styleOverrides: [],
30
+ styleChains: [],
31
+ facetReports: [],
32
+ uxFindings: [],
33
+ enhancementsRun: [],
34
+ warnings: [],
35
+ stats: {
36
+ vueFilesScanned: 0,
37
+ vueFilesLibraryConsumers: 0,
38
+ reactFilesScanned: 0,
39
+ reactFilesLibraryConsumers: 0,
40
+ cssFilesScanned: 0,
41
+ facetRegions: 0,
42
+ uxFindings: 0,
43
+ },
44
+ ...extra,
45
+ };
46
+ }
47
+
48
+ describe('read-scan-report boundaries', () => {
49
+ it('isScanResultPayload rejects invalid shapes', () => {
50
+ expect(isScanResultPayload(null)).toBe(false);
51
+ expect(isScanResultPayload({ trees: [] })).toBe(false);
52
+ expect(isScanResultPayload(fixture())).toBe(true);
53
+ });
54
+
55
+ it('readScanReportJsonFile throws on bad json', () => {
56
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocp-bad-'));
57
+ const file = path.join(dir, 'bad.json');
58
+ fs.writeFileSync(file, '{"trees":[]}');
59
+ expect(() => readScanReportJsonFile(file)).toThrow(/非 ScanResult/);
60
+ });
61
+
62
+ it('slice offset beyond total returns empty items', () => {
63
+ const report = fixture({ uxFindings: [{ id: '1', ruleId: 'r', severity: 'info', file: 'a', message: 'm' }] });
64
+ const slice = sliceScanReportSection(report, 'uxFindings', { offset: 99, limit: 10 });
65
+ expect(slice.total).toBe(1);
66
+ expect(slice.offset).toBe(99);
67
+ expect(slice.limit).toBe(0);
68
+ expect(slice.items).toEqual([]);
69
+ });
70
+
71
+ it('slice negative offset treated as 0', () => {
72
+ const report = fixture({ warnings: ['a', 'b'] });
73
+ const slice = sliceScanReportSection(report, 'warnings', { offset: -5, limit: 1 });
74
+ expect(slice.offset).toBe(0);
75
+ expect(slice.items).toEqual(['a']);
76
+ });
77
+
78
+ it('slice omit limit returns rest', () => {
79
+ const report = fixture({ warnings: ['a', 'b', 'c'] });
80
+ const slice = sliceScanReportSection(report, 'warnings', { offset: 1 });
81
+ expect(slice.items).toEqual(['b', 'c']);
82
+ expect(slice.limit).toBe(2);
83
+ });
84
+
85
+ it('slice limit 0 returns empty', () => {
86
+ const report = fixture({ warnings: ['a'] });
87
+ const slice = sliceScanReportSection(report, 'warnings', { offset: 0, limit: 0 });
88
+ expect(slice.items).toEqual([]);
89
+ });
90
+
91
+ it('facetRegions falls back from facetReports when stats missing', () => {
92
+ const report = fixture({
93
+ facetReports: [
94
+ { file: 'a', facetCount: 3, avgTemplatingRate: 0, facets: [] },
95
+ { file: 'b', facetCount: 1, avgTemplatingRate: 0, facets: [] },
96
+ ],
97
+ });
98
+ delete (report as { stats?: ScanResult['stats'] }).stats;
99
+ expect(getScanReportCounts(report).facetRegions).toBe(4);
100
+ });
101
+
102
+ it.each(SECTIONS)('slices section %s on empty report', (section) => {
103
+ const slice = sliceScanReportSection(fixture(), section);
104
+ expect(slice.total).toBe(0);
105
+ expect(slice.items).toEqual([]);
106
+ });
107
+
108
+ it('readTextReportLinesSync clamps range past EOF', () => {
109
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocp-lines-'));
110
+ const file = path.join(dir, 't.md');
111
+ fs.writeFileSync(file, 'only', 'utf8');
112
+ const slice = readTextReportLinesSync(file, { startLine: 5, endLine: 10 });
113
+ expect(slice.totalLines).toBe(1);
114
+ expect(slice.text).toBe('only');
115
+ expect(slice.startLine).toBe(1);
116
+ expect(slice.endLine).toBe(1);
117
+ });
118
+
119
+ it('readTextReportLines async matches sync', async () => {
120
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocp-async-'));
121
+ const file = path.join(dir, 't.md');
122
+ const body = Array.from({ length: 50 }, (_, i) => `L${i + 1}`).join('\n');
123
+ fs.writeFileSync(file, body, 'utf8');
124
+ const asyncSlice = await readTextReportLines(file, { startLine: 10, endLine: 15 });
125
+ const syncSlice = readTextReportLinesSync(file, { startLine: 10, endLine: 15 });
126
+ expect(asyncSlice.text).toBe(syncSlice.text);
127
+ expect(await countTextReportLines(file)).toBe(50);
128
+ expect(asyncSlice.totalLines).toBe(50);
129
+ });
130
+
131
+ it('readTextReportLines when start beyond file yields empty text', async () => {
132
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocp-past-'));
133
+ const file = path.join(dir, 't.md');
134
+ fs.writeFileSync(file, 'a\nb', 'utf8');
135
+ const slice = await readTextReportLines(file, { startLine: 100, endLine: 200 });
136
+ expect(slice.totalLines).toBe(2);
137
+ expect(slice.text).toBe('');
138
+ });
139
+ });