dsh-uitest 2.3.1 → 2.4.0
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.
- package/README.md +72 -11
- package/bin/uitest.mjs +189 -11
- package/client.js +81 -11
- package/host/agent-bridge.mjs +9 -2
- package/host/agent-test.mjs +1 -1
- package/host/context-budget.mjs +1 -1
- package/index.js +14 -2
- package/lib/agent-session.mjs +81 -3
- package/lib/agent-tasks.mjs +212 -0
- package/lib/batch.mjs +204 -13
- package/lib/browser-ab.mjs +21 -6
- package/lib/browser.mjs +5 -5
- package/lib/config.mjs +1 -0
- package/lib/convert.mjs +178 -40
- package/lib/doc-structure.mjs +316 -0
- package/lib/flow.mjs +49 -14
- package/lib/knowledge-central-episodes.mjs +120 -0
- package/lib/knowledge-central-import.mjs +16 -1
- package/lib/knowledge-central.mjs +149 -22
- package/lib/knowledge-clean.mjs +749 -0
- package/lib/knowledge-consolidate.mjs +1271 -0
- package/lib/knowledge-coverage.mjs +150 -0
- package/lib/knowledge-docs.mjs +3 -3
- package/lib/knowledge-effect.mjs +148 -0
- package/lib/knowledge-facts.mjs +1008 -0
- package/lib/knowledge-maintain.mjs +185 -0
- package/lib/knowledge-permission.mjs +447 -0
- package/lib/knowledge-query.mjs +304 -26
- package/lib/knowledge-store.mjs +169 -14
- package/lib/knowledge-summarize.mjs +332 -0
- package/lib/knowledge.mjs +944 -47
- package/lib/learn-run.mjs +157 -51
- package/lib/run-learn.mjs +34 -0
- package/lib/run-observe.mjs +95 -27
- package/lib/run-stats.mjs +71 -0
- package/package.json +12 -2
- package/scripts/knowledge-central-purge.mjs +248 -0
- package/scripts/knowledge-central-sync.mjs +102 -20
- package/scripts/knowledge-consolidate.mjs +158 -0
- package/scripts/knowledge-coverage.mjs +94 -0
- package/scripts/knowledge-effect.mjs +82 -0
- package/scripts/knowledge-permission-probe.mjs +196 -0
- package/scripts/knowledge-permission-report.mjs +140 -0
- package/scripts/knowledge-report.mjs +492 -0
- package/scripts/knowledge-summarize.mjs +200 -0
- package/skills/uitest/SKILL.md +4 -2
- package/skills/uitest/references/convert-knowledge.md +66 -13
- package/studio/host.mjs +120 -3
- package/studio/knowledge.mjs +195 -45
- package/studio/snapshot.mjs +6 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 知识合并:默认只输出计划;--apply 先备份原始知识再落盘,并把执行证据追加到 experiences.jsonl。
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
|
|
7
|
+
import { loadConfig } from '../lib/config.mjs';
|
|
8
|
+
import { getKnowledgeDocument, loadKnowledge, knowledgeDir, readLearnings } from '../lib/knowledge.mjs';
|
|
9
|
+
import { applyConsolidation, planConsolidation } from '../lib/knowledge-consolidate.mjs';
|
|
10
|
+
import { loadFacts } from '../lib/knowledge-facts.mjs';
|
|
11
|
+
|
|
12
|
+
const MAX_DOC_CHARS = 400_000;
|
|
13
|
+
|
|
14
|
+
function parseArgs(argv) {
|
|
15
|
+
const args = { project: process.cwd(), dir: '', library: '', apply: false, json: false, actions: 30, noStructure: false };
|
|
16
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
17
|
+
const token = argv[i];
|
|
18
|
+
if (token === '--project') args.project = path.resolve(argv[++i] || '');
|
|
19
|
+
else if (token === '--dir') args.dir = path.resolve(argv[++i] || '');
|
|
20
|
+
else if (token === '--library') args.library = String(argv[++i] || '');
|
|
21
|
+
else if (token === '--apply') args.apply = true;
|
|
22
|
+
else if (token === '--json') args.json = true;
|
|
23
|
+
else if (token === '--no-structure') args.noStructure = true;
|
|
24
|
+
else if (token === '--actions') args.actions = Math.max(0, Number(argv[++i]) || 0);
|
|
25
|
+
else if (token === '--help' || token === '-h') args.help = true;
|
|
26
|
+
}
|
|
27
|
+
return args;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function usage() {
|
|
31
|
+
return [
|
|
32
|
+
'用法:node uitest/scripts/knowledge-consolidate.mjs --project <dir> [--dir <knowledgeDir> | --library <id>] [--apply] [--json]',
|
|
33
|
+
' --dir <path> 指定知识目录(默认按配置解析:共享库或项目 knowledge/)',
|
|
34
|
+
' --library <id> 指定共享知识库',
|
|
35
|
+
' --apply 落盘合并结果(默认只预览;落盘前备份到 knowledge/.archive/)',
|
|
36
|
+
' --no-structure 不读取文档正文,只用已有文档元数据',
|
|
37
|
+
' --actions N 打印前 N 条变更(默认 30)',
|
|
38
|
+
].join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function withKnowledgeTarget(config, args) {
|
|
42
|
+
const next = { ...config, knowledge: { ...(config.knowledge || {}) } };
|
|
43
|
+
if (args.dir) next.knowledge.dir = args.dir;
|
|
44
|
+
if (args.library) next.knowledge.library = args.library;
|
|
45
|
+
return next;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function loadDocumentTexts(config, knowledge) {
|
|
49
|
+
const texts = {};
|
|
50
|
+
for (const doc of knowledge.documents?.documents || []) {
|
|
51
|
+
if (!doc?.id) continue;
|
|
52
|
+
try {
|
|
53
|
+
const full = getKnowledgeDocument(knowledge.dir, doc.id, { includeContent: true });
|
|
54
|
+
if (full.content) texts[doc.id] = String(full.content).slice(0, MAX_DOC_CHARS);
|
|
55
|
+
} catch {
|
|
56
|
+
/* 文档内容缺失不影响合并 */
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return texts;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function printPlan(plan, { dir, apply, actions }) {
|
|
63
|
+
const stats = plan.stats;
|
|
64
|
+
console.log('知识合并 ' + (apply ? '落盘' : '预览(未写入,使用 --apply 执行)'));
|
|
65
|
+
console.log('知识库 ' + dir);
|
|
66
|
+
console.log('');
|
|
67
|
+
console.log('文档 ' + stats.documents.total + ' 类型 ' + JSON.stringify(stats.documents.kinds));
|
|
68
|
+
console.log(' 有字段/状态结构 ' + stats.documents.withStructure
|
|
69
|
+
+ '(字段 ' + stats.documents.fields + ',状态 ' + stats.documents.statuses
|
|
70
|
+
+ ',角色 ' + stats.documents.roles + ',权限 ' + stats.documents.permissions
|
|
71
|
+
+ ',术语 ' + stats.documents.glossary + ')');
|
|
72
|
+
console.log('页面 ' + stats.pages.before + ' → ' + stats.pages.after
|
|
73
|
+
+ ' 合并 ' + stats.pages.merged + ',归档 ' + stats.pages.archived
|
|
74
|
+
+ ',存根 ' + stats.pages.stubs);
|
|
75
|
+
console.log(' 观察笔记转证据 ' + stats.pages.notesToEvidence
|
|
76
|
+
+ ',页面反馈转证据 ' + stats.pages.feedbacksToEvidence
|
|
77
|
+
+ ',运行观察转证据 ' + stats.pages.observationsToEvidence);
|
|
78
|
+
console.log('流程 ' + stats.processes.before + ' → ' + stats.processes.after
|
|
79
|
+
+ ' 合并 ' + stats.processes.merged + ',归档 ' + stats.processes.archived
|
|
80
|
+
+ ',转业务规则 ' + stats.processes.reclassifiedRules
|
|
81
|
+
+ ',转执行经验 ' + stats.processes.experiences);
|
|
82
|
+
console.log('业务规则 ' + stats.business.rulesBefore + ' → ' + stats.business.rulesAfter
|
|
83
|
+
+ ' 条目 ' + stats.business.before + ' → ' + stats.business.after
|
|
84
|
+
+ ',清洗 ' + stats.business.rulesCleaned
|
|
85
|
+
+ ',丢弃 ' + stats.business.rulesDropped
|
|
86
|
+
+ ',由流程转入 ' + stats.business.reclassifiedRules);
|
|
87
|
+
console.log('系统知识 角色 ' + stats.system.roles + ',权限 ' + stats.system.permissions + ',术语 ' + stats.system.glossary);
|
|
88
|
+
console.log('证据学习 ' + stats.learnings.before + ' → ' + stats.learnings.after
|
|
89
|
+
+ ' 去重 ' + stats.learnings.duplicates + ',补去重键 ' + stats.learnings.keyed);
|
|
90
|
+
console.log('执行证据 新增 ' + stats.experiences + ' 条');
|
|
91
|
+
if (stats.facts) {
|
|
92
|
+
const facts = stats.facts;
|
|
93
|
+
console.log('正典事实 ' + facts.candidates + ' 候选 → ' + facts.groups + ' 键'
|
|
94
|
+
+ ' 新增 ' + facts.created
|
|
95
|
+
+ ',已验证 ' + facts.verified
|
|
96
|
+
+ ',冲突 ' + facts.conflicted
|
|
97
|
+
+ ',废弃 ' + facts.deprecated
|
|
98
|
+
+ (facts.skipped ? ',丢弃 ' + facts.skipped : ''));
|
|
99
|
+
}
|
|
100
|
+
if (actions && plan.actions.length) {
|
|
101
|
+
console.log('');
|
|
102
|
+
console.log('变更样本(前 ' + Math.min(actions, plan.actions.length) + ' 条)');
|
|
103
|
+
for (const action of plan.actions.slice(0, actions)) console.log(' ' + action);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function main() {
|
|
108
|
+
const args = parseArgs(process.argv.slice(2));
|
|
109
|
+
if (args.help) {
|
|
110
|
+
console.log(usage());
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const config = withKnowledgeTarget(loadConfig(args.project), args);
|
|
114
|
+
const knowledge = loadKnowledge(config);
|
|
115
|
+
const documentTexts = args.noStructure ? {} : loadDocumentTexts(config, knowledge);
|
|
116
|
+
const learnings = readLearnings(config, { limit: Number.MAX_SAFE_INTEGER });
|
|
117
|
+
const existingFacts = loadFacts(config).facts;
|
|
118
|
+
const plan = planConsolidation(knowledge, { documentTexts, learnings, existingFacts });
|
|
119
|
+
const dir = knowledgeDir(config);
|
|
120
|
+
|
|
121
|
+
if (args.json) {
|
|
122
|
+
console.log(JSON.stringify({
|
|
123
|
+
dir,
|
|
124
|
+
applied: Boolean(args.apply),
|
|
125
|
+
stats: plan.stats,
|
|
126
|
+
actions: plan.actions,
|
|
127
|
+
archive: {
|
|
128
|
+
pages: plan.archive.pages.map((item) => ({ id: item.id, title: item.title, reason: item.archiveReason })),
|
|
129
|
+
processes: plan.archive.processes.map((item) => ({ id: item.id, name: item.name, reason: item.archiveReason })),
|
|
130
|
+
business: plan.archive.business.map((item) => ({ id: item.id, title: item.title, reason: item.archiveReason })),
|
|
131
|
+
},
|
|
132
|
+
experiences: plan.experiences.map((item) => ({ id: item.id, kind: item.kind })),
|
|
133
|
+
facts: { stats: plan.stats.facts, errors: plan.facts?.errors || [] },
|
|
134
|
+
}, null, 2));
|
|
135
|
+
} else {
|
|
136
|
+
printPlan(plan, { dir, apply: args.apply, actions: args.actions });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (args.apply) {
|
|
140
|
+
const result = applyConsolidation(config, plan);
|
|
141
|
+
if (!args.json) {
|
|
142
|
+
console.log('');
|
|
143
|
+
console.log('已落盘。备份:' + result.archiveDir);
|
|
144
|
+
console.log('写入执行证据 ' + result.experiencesAdded + ' 条,去重证据学习 ' + result.learningsCompacted + ' 条。');
|
|
145
|
+
if (result.factsStats) {
|
|
146
|
+
console.log('正典事实落盘 ' + (result.factsStats.created + result.factsStats.updated + result.factsStats.unchanged + result.factsStats.deprecated)
|
|
147
|
+
+ ' 条,备份:' + result.factsArchiveDir);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
154
|
+
main().catch((error) => {
|
|
155
|
+
console.error('知识合并失败:' + (error.message || error));
|
|
156
|
+
process.exitCode = 1;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 知识覆盖报告:用例 → 页面/单据/角色事实的绑定情况与缺口,页面/权限覆盖与未引用清单。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import { loadConfig } from '../lib/config.mjs';
|
|
9
|
+
import { buildProjectCoverage } from '../lib/knowledge-coverage.mjs';
|
|
10
|
+
|
|
11
|
+
function parseArgs(argv) {
|
|
12
|
+
const args = { project: process.cwd(), json: false, strict: false, out: '', limit: 0, actions: 20 };
|
|
13
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
14
|
+
const token = argv[i];
|
|
15
|
+
if (token === '--project') args.project = path.resolve(argv[++i] || '');
|
|
16
|
+
else if (token === '--json') args.json = true;
|
|
17
|
+
else if (token === '--strict') args.strict = true;
|
|
18
|
+
else if (token === '--out') args.out = path.resolve(argv[++i] || '');
|
|
19
|
+
else if (token === '--limit') args.limit = Math.max(0, Number(argv[++i]) || 0);
|
|
20
|
+
else if (token === '--actions') args.actions = Math.max(0, Number(argv[++i]) || 0);
|
|
21
|
+
else if (token === '--help' || token === '-h') args.help = true;
|
|
22
|
+
}
|
|
23
|
+
return args;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function usage() {
|
|
27
|
+
return [
|
|
28
|
+
'用法:node uitest/scripts/knowledge-coverage.mjs --project <dir> [--limit N] [--json] [--strict] [--out <file>]',
|
|
29
|
+
' --limit N 只分析前 N 个用例(默认全部)',
|
|
30
|
+
' --strict 存在没有页面命中的用例时以退出码 1 结束',
|
|
31
|
+
' --out <file> 同时写入 JSON 报告',
|
|
32
|
+
].join('\n');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function pct(value) {
|
|
36
|
+
return (Number(value || 0) * 100).toFixed(1) + '%';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function printReport(report, { actions }) {
|
|
40
|
+
console.log('知识覆盖 ' + report.generatedAt);
|
|
41
|
+
console.log('用例 ' + report.totals.cases + ' 有页面 ' + report.totals.casesWithPages + ' 有事实 ' + report.totals.casesWithFacts);
|
|
42
|
+
console.log('事实 ' + report.totals.facts + '(有效 ' + report.totals.activeFacts + ')');
|
|
43
|
+
console.log('');
|
|
44
|
+
console.log('页面 正典 ' + report.pages.canonical + ',被用例引用 ' + report.pages.referenced
|
|
45
|
+
+ ' ' + pct(report.pages.coverage) + ',未引用 ' + report.pages.uncovered.length);
|
|
46
|
+
console.log('单据 可学习 ' + report.documents.learnable + ',被用例引用 ' + report.documents.referenced
|
|
47
|
+
+ ',未引用 ' + report.documents.unreferenced.length);
|
|
48
|
+
console.log('权限 事实 ' + report.permissions.facts + ',角色 ' + report.permissions.roles
|
|
49
|
+
+ ',已探测 ' + report.permissions.pagesProbed + ' / ' + report.permissions.pagesProbeable + ' 个可探测页面'
|
|
50
|
+
+ ' ' + pct(report.permissions.pageCoverage)
|
|
51
|
+
+ (report.permissions.pagesWithoutUrl ? '(另有 ' + report.permissions.pagesWithoutUrl + ' 个页面有按钮但无 URL)' : ''));
|
|
52
|
+
const gapTypes = Object.entries(report.gapTypes).sort((a, b) => b[1] - a[1]);
|
|
53
|
+
if (gapTypes.length) {
|
|
54
|
+
console.log('');
|
|
55
|
+
console.log('缺口');
|
|
56
|
+
for (const [type, count] of gapTypes) console.log(' ' + type + ' ' + count);
|
|
57
|
+
}
|
|
58
|
+
const noPages = report.cases.filter((row) => row.pages.length === 0);
|
|
59
|
+
if (noPages.length) {
|
|
60
|
+
console.log('');
|
|
61
|
+
console.log('没有页面命中的用例(前 ' + Math.min(actions, noPages.length) + ' 条)');
|
|
62
|
+
for (const row of noPages.slice(0, actions)) console.log(' ' + row.caseId + ' ' + row.title);
|
|
63
|
+
}
|
|
64
|
+
if (report.pages.uncovered.length) {
|
|
65
|
+
console.log('');
|
|
66
|
+
console.log('未被用例引用的正典页面(前 ' + Math.min(actions, report.pages.uncovered.length) + ' 条)');
|
|
67
|
+
for (const page of report.pages.uncovered.slice(0, actions)) console.log(' ' + page.id + ' ' + page.title);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function main() {
|
|
72
|
+
const args = parseArgs(process.argv.slice(2));
|
|
73
|
+
if (args.help) {
|
|
74
|
+
console.log(usage());
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const config = loadConfig(args.project);
|
|
78
|
+
const report = buildProjectCoverage(config, { limit: args.limit });
|
|
79
|
+
const output = JSON.stringify(report, null, 2);
|
|
80
|
+
if (args.json) console.log(output);
|
|
81
|
+
else printReport(report, { actions: args.actions });
|
|
82
|
+
if (args.out) {
|
|
83
|
+
fs.mkdirSync(path.dirname(args.out), { recursive: true });
|
|
84
|
+
fs.writeFileSync(args.out, output + '\n');
|
|
85
|
+
}
|
|
86
|
+
if (args.strict && report.cases.some((row) => row.pages.length === 0)) process.exitCode = 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
90
|
+
main().catch((error) => {
|
|
91
|
+
console.error('知识覆盖报告失败:' + (error.message || error));
|
|
92
|
+
process.exitCode = 1;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 闭环效果报告:复用(agent/generated flow)与探索执行的成功率、耗时对比,只读 runs/。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import { loadConfig } from '../lib/config.mjs';
|
|
9
|
+
import { buildEffectReport } from '../lib/knowledge-effect.mjs';
|
|
10
|
+
|
|
11
|
+
function parseArgs(argv) {
|
|
12
|
+
const args = { project: process.cwd(), json: false, out: '', limit: 500, strict: false };
|
|
13
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
14
|
+
const token = argv[i];
|
|
15
|
+
if (token === '--project') args.project = path.resolve(argv[++i] || '');
|
|
16
|
+
else if (token === '--json') args.json = true;
|
|
17
|
+
else if (token === '--out') args.out = path.resolve(argv[++i] || '');
|
|
18
|
+
else if (token === '--limit') args.limit = Math.max(1, Number(argv[++i]) || 500);
|
|
19
|
+
else if (token === '--strict') args.strict = true;
|
|
20
|
+
else if (token === '--help' || token === '-h') args.help = true;
|
|
21
|
+
}
|
|
22
|
+
return args;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function usage() {
|
|
26
|
+
return [
|
|
27
|
+
'用法:node uitest/scripts/knowledge-effect.mjs --project <dir> [--limit N] [--json] [--out <file>] [--strict]',
|
|
28
|
+
' --limit N 只分析最近 N 次运行(默认 500)',
|
|
29
|
+
' --out <file> 同时写入 JSON 报告',
|
|
30
|
+
' --strict 复用执行成功率低于探索执行时以退出码 1 结束',
|
|
31
|
+
].join('\n');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function rate(value) {
|
|
35
|
+
return value === null || value === undefined ? '-' : (value * 100).toFixed(1) + '%';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function printReport(report) {
|
|
39
|
+
const { totals, comparison } = report;
|
|
40
|
+
console.log('闭环效果 ' + report.generatedAt);
|
|
41
|
+
console.log(`运行 ${totals.runs} 次(通过 ${totals.passed} / 失败 ${totals.failed}),已沉淀 ${totals.learned} 次`);
|
|
42
|
+
console.log(`复用 ${comparison.reuse.runs} 次 成功率 ${rate(comparison.reuse.successRate)} 中位耗时 ${comparison.reuse.medianDurationMs ?? '-'}ms`);
|
|
43
|
+
console.log(`探索 ${comparison.exploration.runs} 次 成功率 ${rate(comparison.exploration.successRate)} 中位耗时 ${comparison.exploration.medianDurationMs ?? '-'}ms`);
|
|
44
|
+
if (comparison.successRateDelta !== null) {
|
|
45
|
+
console.log(`成功率差 ${(comparison.successRateDelta * 100).toFixed(1)} 个百分点;耗时差 ${comparison.medianDurationDeltaMs}ms`);
|
|
46
|
+
}
|
|
47
|
+
console.log(`说明:${comparison.note}`);
|
|
48
|
+
if (report.flows.length) {
|
|
49
|
+
console.log('');
|
|
50
|
+
console.log('flow');
|
|
51
|
+
for (const flow of report.flows) {
|
|
52
|
+
console.log(` ${flow.flow} 运行 ${flow.runs} / 通过 ${flow.passed} / 失败 ${flow.failed} 成功率 ${rate(flow.successRate)} 最后 ${flow.lastStatus || '-'}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function main() {
|
|
58
|
+
const args = parseArgs(process.argv.slice(2));
|
|
59
|
+
if (args.help) {
|
|
60
|
+
console.log(usage());
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const config = loadConfig(args.project);
|
|
64
|
+
const report = buildEffectReport(config, { limit: args.limit });
|
|
65
|
+
const output = JSON.stringify(report, null, 2);
|
|
66
|
+
if (args.json) console.log(output);
|
|
67
|
+
else printReport(report);
|
|
68
|
+
if (args.out) {
|
|
69
|
+
fs.mkdirSync(path.dirname(args.out), { recursive: true });
|
|
70
|
+
fs.writeFileSync(args.out, output + '\n');
|
|
71
|
+
}
|
|
72
|
+
if (args.strict && report.comparison.successRateDelta !== null && report.comparison.successRateDelta < 0) {
|
|
73
|
+
process.exitCode = 1;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
78
|
+
main().catch((error) => {
|
|
79
|
+
console.error('闭环效果报告失败:' + (error.message || error));
|
|
80
|
+
process.exitCode = 1;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 角色权限探测:默认只打印计划;--run 只读探测并打印结果;--apply 探测并把事实并入 facts.json。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import { loadConfig } from '../lib/config.mjs';
|
|
9
|
+
import { loadKnowledge } from '../lib/knowledge.mjs';
|
|
10
|
+
import {
|
|
11
|
+
applyProbeFacts,
|
|
12
|
+
buildPermissionProbePlan,
|
|
13
|
+
probeRolePermissions,
|
|
14
|
+
probeRolesForAccounts,
|
|
15
|
+
saveProbeRun,
|
|
16
|
+
} from '../lib/knowledge-permission.mjs';
|
|
17
|
+
|
|
18
|
+
function parseArgs(argv) {
|
|
19
|
+
const args = {
|
|
20
|
+
project: process.cwd(),
|
|
21
|
+
dir: '',
|
|
22
|
+
library: '',
|
|
23
|
+
roles: [],
|
|
24
|
+
pages: [],
|
|
25
|
+
account: '',
|
|
26
|
+
limit: 20,
|
|
27
|
+
from: '',
|
|
28
|
+
run: false,
|
|
29
|
+
apply: false,
|
|
30
|
+
json: false,
|
|
31
|
+
headed: false,
|
|
32
|
+
};
|
|
33
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
34
|
+
const token = argv[i];
|
|
35
|
+
if (token === '--project') args.project = path.resolve(argv[++i] || '');
|
|
36
|
+
else if (token === '--dir') args.dir = path.resolve(argv[++i] || '');
|
|
37
|
+
else if (token === '--library') args.library = String(argv[++i] || '');
|
|
38
|
+
else if (token === '--roles') args.roles = String(argv[++i] || '').split(/[、,,/|]+/).map((item) => item.trim()).filter(Boolean);
|
|
39
|
+
else if (token === '--pages') args.pages = String(argv[++i] || '').split(/[、,,/|]+/).map((item) => item.trim()).filter(Boolean);
|
|
40
|
+
else if (token === '--account') args.account = String(argv[++i] || '').trim();
|
|
41
|
+
else if (token === '--from') args.from = path.resolve(argv[++i] || '');
|
|
42
|
+
else if (token === '--limit') args.limit = Math.max(1, Number(argv[++i]) || 20);
|
|
43
|
+
else if (token === '--run') args.run = true;
|
|
44
|
+
else if (token === '--apply') { args.apply = true; args.run = true; }
|
|
45
|
+
else if (token === '--json') args.json = true;
|
|
46
|
+
else if (token === '--headed') args.headed = true;
|
|
47
|
+
else if (token === '--help' || token === '-h') args.help = true;
|
|
48
|
+
}
|
|
49
|
+
return args;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function usage() {
|
|
53
|
+
return [
|
|
54
|
+
'用法:node uitest/scripts/knowledge-permission-probe.mjs --project <dir> [--dir <knowledgeDir> | --library <id>] [选项]',
|
|
55
|
+
' --roles <角色,角色> 要探测的角色(默认:已有会话文件的账号角色)',
|
|
56
|
+
' --account <账号> 强制用指定账号探测(仅用于单角色账号排障)',
|
|
57
|
+
' --pages <页面id,...> 只探测指定页面(默认:前 N 个有 URL 的正典页面)',
|
|
58
|
+
' --from <file> 不重新探测,读取已留档的 permission-probe.json(配合 --apply 落库)',
|
|
59
|
+
' --limit N 页面数上限(默认 20)',
|
|
60
|
+
' --run 只读探测并打印结果,不写事实库',
|
|
61
|
+
' --apply 探测并把 role.permission 事实并入 facts.json(自动备份)',
|
|
62
|
+
' --headed 有头浏览器(默认无头)',
|
|
63
|
+
' --json JSON 输出',
|
|
64
|
+
].join('\n');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function withKnowledgeTarget(config, args) {
|
|
68
|
+
const next = { ...config, knowledge: { ...(config.knowledge || {}) } };
|
|
69
|
+
if (args.dir) next.knowledge.dir = args.dir;
|
|
70
|
+
if (args.library) next.knowledge.library = args.library;
|
|
71
|
+
return next;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function printPlan(plan, roles) {
|
|
75
|
+
console.log('角色权限探测 ' + (plan.pages.length + ' 个页面,' + plan.targets + ' 个期望按钮'));
|
|
76
|
+
console.log('');
|
|
77
|
+
for (const entry of roles) {
|
|
78
|
+
const account = entry.account;
|
|
79
|
+
console.log('角色 ' + entry.role + ' → ' + (account ? account.username : '(无账号)')
|
|
80
|
+
+ (account && account.sessionReady ? '(已有会话)' : account ? '(需登录)' : '')
|
|
81
|
+
+ (account && account.roles && account.roles.length > 1 ? '(多角色账号,结果为角色并集)' : ''));
|
|
82
|
+
}
|
|
83
|
+
console.log('');
|
|
84
|
+
for (const page of plan.pages) {
|
|
85
|
+
console.log(' ' + page.id + ' ' + page.title + ' 期望按钮 ' + page.expected.length);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function printResults(probe) {
|
|
90
|
+
for (const role of probe.roles) {
|
|
91
|
+
console.log('');
|
|
92
|
+
console.log('角色 ' + role.role + (role.error ? ' 错误:' + role.error : ''));
|
|
93
|
+
for (const page of role.pages) {
|
|
94
|
+
if (page.error) {
|
|
95
|
+
console.log(' ' + page.id + ' 错误:' + page.error);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
console.log(' ' + page.id + (page.accessible ? '' : ' (正文为空)')
|
|
99
|
+
+ ' 可见 ' + page.visible.length + ' 按钮'
|
|
100
|
+
+ ',期望未出现 ' + page.missing.length + ':' + (page.missing.slice(0, 8).join('、') || '无'));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function main() {
|
|
106
|
+
const args = parseArgs(process.argv.slice(2));
|
|
107
|
+
if (args.help) {
|
|
108
|
+
console.log(usage());
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const config = withKnowledgeTarget(loadConfig(args.project), args);
|
|
112
|
+
if (args.from) {
|
|
113
|
+
const probe = JSON.parse(fs.readFileSync(args.from, 'utf8'));
|
|
114
|
+
const applied = args.apply ? applyProbeFacts(config, probe, { at: probe.at }) : null;
|
|
115
|
+
if (args.json) {
|
|
116
|
+
console.log(JSON.stringify({
|
|
117
|
+
from: args.from,
|
|
118
|
+
probe,
|
|
119
|
+
applied: applied ? { archiveDir: applied.archiveDir, stats: applied.factStats, errors: applied.factErrors } : null,
|
|
120
|
+
}, null, 2));
|
|
121
|
+
} else {
|
|
122
|
+
printResults(probe);
|
|
123
|
+
console.log('');
|
|
124
|
+
console.log('读取留档:' + args.from);
|
|
125
|
+
if (applied) {
|
|
126
|
+
console.log('事实落盘:新增 ' + applied.factStats.created
|
|
127
|
+
+ ',更新 ' + applied.factStats.updated
|
|
128
|
+
+ ',冲突 ' + applied.factStats.conflicted
|
|
129
|
+
+ ',备份 ' + applied.archiveDir);
|
|
130
|
+
} else {
|
|
131
|
+
console.log('未写入事实库;加 --apply 从留档写入。');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const knowledge = loadKnowledge(config);
|
|
137
|
+
const roles = probeRolesForAccounts(config, args.roles);
|
|
138
|
+
if (!roles.length) {
|
|
139
|
+
throw new Error(args.roles.length
|
|
140
|
+
? '没有匹配角色「' + args.roles.join('、') + '」的测试账号'
|
|
141
|
+
: '没有可用账号;请先导入测试账号,或用 --roles 指定有账号的角色');
|
|
142
|
+
}
|
|
143
|
+
const plan = buildPermissionProbePlan(knowledge, {
|
|
144
|
+
roles: roles.map((entry) => entry.role),
|
|
145
|
+
pages: args.pages,
|
|
146
|
+
limit: args.limit,
|
|
147
|
+
});
|
|
148
|
+
if (!plan.pages.length) throw new Error('没有可探测的正典页面(需要 status=canonical 且有 URL)');
|
|
149
|
+
|
|
150
|
+
if (!args.run) {
|
|
151
|
+
if (args.json) {
|
|
152
|
+
console.log(JSON.stringify({ plan, roles: roles.map((entry) => ({ role: entry.role, account: entry.account })) }, null, 2));
|
|
153
|
+
} else {
|
|
154
|
+
printPlan(plan, roles);
|
|
155
|
+
console.log('');
|
|
156
|
+
console.log('这是预览:加 --run 执行只读探测,加 --apply 探测后写入事实库。');
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const probe = await probeRolePermissions(config, plan, {
|
|
162
|
+
headless: !args.headed,
|
|
163
|
+
roleAccountIds: Object.fromEntries(roles.map((entry) => [entry.role, args.account || entry.account.id])),
|
|
164
|
+
navigationMs: Number(config.timeouts?.navigationMs) || 45000,
|
|
165
|
+
});
|
|
166
|
+
const runFile = saveProbeRun(config, probe);
|
|
167
|
+
const applied = args.apply ? applyProbeFacts(config, probe) : null;
|
|
168
|
+
|
|
169
|
+
if (args.json) {
|
|
170
|
+
console.log(JSON.stringify({
|
|
171
|
+
runFile,
|
|
172
|
+
probe,
|
|
173
|
+
applied: applied ? { archiveDir: applied.archiveDir, stats: applied.factStats, errors: applied.factErrors } : null,
|
|
174
|
+
}, null, 2));
|
|
175
|
+
} else {
|
|
176
|
+
printResults(probe);
|
|
177
|
+
console.log('');
|
|
178
|
+
console.log('探测留档:' + runFile);
|
|
179
|
+
if (applied) {
|
|
180
|
+
console.log('事实落盘:候选 ' + applied.factStats.candidates
|
|
181
|
+
+ ',新增 ' + applied.factStats.created
|
|
182
|
+
+ ',更新 ' + applied.factStats.updated
|
|
183
|
+
+ ',冲突 ' + applied.factStats.conflicted
|
|
184
|
+
+ ',备份 ' + applied.archiveDir);
|
|
185
|
+
} else {
|
|
186
|
+
console.log('未写入事实库;加 --apply 写入。');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
192
|
+
main().catch((error) => {
|
|
193
|
+
console.error('角色权限探测失败:' + (error.message || error));
|
|
194
|
+
process.exitCode = 1;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 权限矩阵报告:只读取 facts.json 里的 role.permission 事实,按角色 × 操作输出可见性。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import { loadConfig } from '../lib/config.mjs';
|
|
9
|
+
import { buildPermissionMatrix, loadFacts } from '../lib/knowledge-facts.mjs';
|
|
10
|
+
|
|
11
|
+
function parseArgs(argv) {
|
|
12
|
+
const args = { project: process.cwd(), dir: '', library: '', json: false, markdown: false, out: '', roles: [], actions: [], all: false };
|
|
13
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
14
|
+
const token = argv[i];
|
|
15
|
+
if (token === '--project') args.project = path.resolve(argv[++i] || '');
|
|
16
|
+
else if (token === '--dir') args.dir = path.resolve(argv[++i] || '');
|
|
17
|
+
else if (token === '--library') args.library = String(argv[++i] || '');
|
|
18
|
+
else if (token === '--json') args.json = true;
|
|
19
|
+
else if (token === '--all') args.all = true;
|
|
20
|
+
else if (token === '--markdown') args.markdown = true;
|
|
21
|
+
else if (token === '--out') args.out = path.resolve(argv[++i] || '');
|
|
22
|
+
else if (token === '--roles') args.roles = String(argv[++i] || '').split(/[、,,/|]+/).map((item) => item.trim()).filter(Boolean);
|
|
23
|
+
else if (token === '--actions') args.actions = String(argv[++i] || '').split(/[、,,/|]+/).map((item) => item.trim()).filter(Boolean);
|
|
24
|
+
else if (token === '--help' || token === '-h') args.help = true;
|
|
25
|
+
}
|
|
26
|
+
return args;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function usage() {
|
|
30
|
+
return [
|
|
31
|
+
'用法:node uitest/scripts/knowledge-permission-report.mjs --project <dir> [--dir <knowledgeDir> | --library <id>] [选项]',
|
|
32
|
+
' --roles <角色,...> 只看指定角色',
|
|
33
|
+
' --actions <操作,...> 只看指定操作',
|
|
34
|
+
' --markdown 输出 Markdown 表格(默认终端对齐表)',
|
|
35
|
+
' --all 同时显示 deprecated/rejected 历史行(默认只显示当前有效事实)',
|
|
36
|
+
' --json JSON 输出',
|
|
37
|
+
' --out <file> 同时写入文件',
|
|
38
|
+
].join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function withKnowledgeTarget(config, args) {
|
|
42
|
+
const next = { ...config, knowledge: { ...(config.knowledge || {}) } };
|
|
43
|
+
if (args.dir) next.knowledge.dir = args.dir;
|
|
44
|
+
if (args.library) next.knowledge.library = args.library;
|
|
45
|
+
return next;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function filterMatrix(matrix, args) {
|
|
49
|
+
const roles = new Set(args.roles);
|
|
50
|
+
const actions = new Set(args.actions);
|
|
51
|
+
const rows = matrix.rows.filter((row) => (!roles.size || roles.has(row.role)) && (!actions.size || actions.has(row.action)));
|
|
52
|
+
return {
|
|
53
|
+
...matrix,
|
|
54
|
+
rows,
|
|
55
|
+
roles: [...new Set(rows.map((row) => row.role))],
|
|
56
|
+
pages: [...new Set(rows.map((row) => row.page).filter(Boolean))],
|
|
57
|
+
actions: [...new Set(rows.map((row) => row.action))],
|
|
58
|
+
rolesCount: new Set(rows.map((row) => row.role)).size,
|
|
59
|
+
pagesCount: new Set(rows.map((row) => row.page).filter(Boolean)).size,
|
|
60
|
+
actionsCount: new Set(rows.map((row) => row.action)).size,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function allowedText(allowed) {
|
|
65
|
+
return allowed === true ? '允许' : allowed === false ? '禁止' : '未知';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function pad(text, width) {
|
|
69
|
+
const value = String(text ?? '');
|
|
70
|
+
const length = [...value].reduce((sum, char) => sum + (char.charCodeAt(0) > 255 ? 2 : 1), 0);
|
|
71
|
+
return value + ' '.repeat(Math.max(0, width - length));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function renderText(matrix) {
|
|
75
|
+
const lines = [];
|
|
76
|
+
const groups = new Map();
|
|
77
|
+
for (const row of matrix.rows) {
|
|
78
|
+
const key = row.role + '\n' + (row.page || '(未指定页面)');
|
|
79
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
80
|
+
groups.get(key).push(row);
|
|
81
|
+
}
|
|
82
|
+
for (const [key, rows] of groups) {
|
|
83
|
+
const [role, page] = key.split('\n');
|
|
84
|
+
lines.push('角色 ' + role + ' 页面 ' + page);
|
|
85
|
+
const actionWidth = Math.min(30, Math.max(8, ...rows.map((row) => [...row.action].reduce((sum, ch) => sum + (ch.charCodeAt(0) > 255 ? 2 : 1), 0))));
|
|
86
|
+
for (const row of rows) {
|
|
87
|
+
lines.push(' ' + pad(row.action, actionWidth) + ' ' + pad(allowedText(row.allowed), 7) + ' ' + row.status
|
|
88
|
+
+ (row.conflicts.length ? ' (冲突 ' + row.conflicts.length + ')' : ''));
|
|
89
|
+
}
|
|
90
|
+
lines.push('');
|
|
91
|
+
}
|
|
92
|
+
if (lines.at(-1) === '') lines.pop();
|
|
93
|
+
return lines.join('\n');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function renderMarkdown(matrix) {
|
|
97
|
+
const lines = [];
|
|
98
|
+
lines.push('| 角色 | 页面 | 操作 | 结论 | 状态 |');
|
|
99
|
+
lines.push('| --- | --- | --- | --- | --- |');
|
|
100
|
+
for (const row of matrix.rows) {
|
|
101
|
+
const mark = row.allowed === true ? '✅ 允许' : row.allowed === false ? '⛔ 禁止' : '❔ 未知';
|
|
102
|
+
lines.push('| ' + [row.role, row.page, row.action, mark, row.status]
|
|
103
|
+
.map((cell) => String(cell ?? '').replace(/\|/g, '\\|'))
|
|
104
|
+
.join(' | ') + ' |');
|
|
105
|
+
}
|
|
106
|
+
return lines.join('\n');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function main() {
|
|
110
|
+
const args = parseArgs(process.argv.slice(2));
|
|
111
|
+
if (args.help) {
|
|
112
|
+
console.log(usage());
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const config = withKnowledgeTarget(loadConfig(args.project), args);
|
|
116
|
+
const facts = loadFacts(config);
|
|
117
|
+
const currentFacts = args.all
|
|
118
|
+
? facts.facts
|
|
119
|
+
: facts.facts.filter((fact) => !['deprecated', 'rejected'].includes(fact.status));
|
|
120
|
+
const matrix = filterMatrix(buildPermissionMatrix(currentFacts), args);
|
|
121
|
+
const output = args.json ? JSON.stringify(matrix, null, 2) : args.markdown ? renderMarkdown(matrix) : renderText(matrix);
|
|
122
|
+
console.log(output);
|
|
123
|
+
if (args.out) {
|
|
124
|
+
fs.mkdirSync(path.dirname(args.out), { recursive: true });
|
|
125
|
+
fs.writeFileSync(args.out, output + '\n');
|
|
126
|
+
}
|
|
127
|
+
if (!args.json) {
|
|
128
|
+
console.log('');
|
|
129
|
+
console.log('角色 ' + matrix.rolesCount + ',页面 ' + matrix.pagesCount + ',操作 ' + matrix.actionsCount
|
|
130
|
+
+ ',事实 ' + matrix.rows.length
|
|
131
|
+
+ '(冲突 ' + matrix.rows.filter((row) => row.conflicts.length).length + ')');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
136
|
+
main().catch((error) => {
|
|
137
|
+
console.error('权限矩阵报告失败:' + (error.message || error));
|
|
138
|
+
process.exitCode = 1;
|
|
139
|
+
});
|
|
140
|
+
}
|