dsh-uitest 2.1.1 → 2.1.3

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.
@@ -1,199 +0,0 @@
1
- import assert from 'node:assert/strict';
2
- import fs from 'node:fs';
3
- import os from 'node:os';
4
- import path from 'node:path';
5
- import test from 'node:test';
6
-
7
- import { loadConfig } from '../../lib/config.mjs';
8
- import { knowledgeContextForCase } from '../../lib/knowledge-query.mjs';
9
- import { appendRunFeedback, latestRunFeedback, readRunFeedbacks } from '../../lib/run-feedback.mjs';
10
- import { createBatch, saveCaseRun } from '../store.mjs';
11
- import { buildStudioPlan, compactPlan } from '../plan.mjs';
12
- import { runBatch } from '../runner.mjs';
13
- import { detectSelfCorrections } from '../experience.mjs';
14
- import { commitBatchKnowledge, knowledgeSnapshot } from '../knowledge.mjs';
15
-
16
- const CASE_MD = [
17
- '## TC-FB-001 履约单查询',
18
- '',
19
- '| 项 | 内容 |',
20
- '|---|---|',
21
- '| **前置条件** | 已登录 |',
22
- '| **操作步骤** | 打开履约单列表<br>输入履约单编号查询 |',
23
- '| **预期结果** | 列表显示匹配的履约单 |',
24
- ].join('\n');
25
-
26
- function makeProject(caseMarkdown = CASE_MD) {
27
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studio-queue-'));
28
- fs.writeFileSync(path.join(dir, 'uitest.config.json'), JSON.stringify({
29
- baseUrl: 'http://example.test',
30
- auth: { username: 'tester', storageState: '.auth/state.json' },
31
- cases: { markdown: 'cases' },
32
- output: { dir: 'reports' },
33
- seed: { allowWrites: false },
34
- }, null, 2));
35
- fs.mkdirSync(path.join(dir, 'cases'), { recursive: true });
36
- fs.writeFileSync(path.join(dir, 'cases', 'cases.md'), caseMarkdown);
37
- fs.mkdirSync(path.join(dir, 'flows'), { recursive: true });
38
- return { dir, config: loadConfig(dir) };
39
- }
40
-
41
- test('run feedback: 保存、筛选与最新一条', () => {
42
- const { config } = makeProject();
43
- const first = appendRunFeedback(config, { caseId: 'TC-FB-001', note: '上一轮卡在列表加载', batchId: 'b-1', runStatus: 'failed' });
44
- const second = appendRunFeedback(config, { caseId: 'TC-FB-001', note: '这次先等表头再查询', batchId: 'b-2', runStatus: 'partial' });
45
- assert.match(first.id, /^fb-/);
46
- assert.equal(readRunFeedbacks(config, { caseId: 'TC-FB-001' }).length, 2);
47
- assert.equal(latestRunFeedback(config, 'TC-FB-001').id, second.id);
48
- assert.equal(readRunFeedbacks(config, { batchId: 'b-1' })[0].note, '上一轮卡在列表加载');
49
- });
50
-
51
- test('experience: 失败步骤后的同类成功动作识别为自我纠错', () => {
52
- const corrections = detectSelfCorrections([
53
- { id: 's1', action: 'comment', status: 'passed', desc: '先点查询' },
54
- { id: 's2', action: 'click', status: 'failed', spec: { action: 'click', text: '查询' }, error: '找不到按钮' },
55
- { id: 's3', action: 'wait', status: 'passed', desc: '等待列表' },
56
- { id: 's4', action: 'comment', status: 'passed', desc: '按钮文案是搜索,改用搜索' },
57
- { id: 's5', action: 'click', status: 'passed', spec: { action: 'click', text: '搜索' } },
58
- ]);
59
- assert.equal(corrections.length, 1);
60
- assert.match(corrections[0].summary, /查询/);
61
- assert.match(corrections[0].summary, /搜索/);
62
- assert.match(corrections[0].thinking, /搜索/);
63
- });
64
-
65
- test('plan: 显式 Agent 重跑可以覆盖人工标记', () => {
66
- const manualCase = [
67
- '## TC-FB-900 人工用例',
68
- '',
69
- '| 项 | 内容 |',
70
- '|---|---|',
71
- '| **前置条件** | 无 |',
72
- '| **操作步骤** | 人工检查 |',
73
- '| **预期结果** | 人工判断 |',
74
- '| **备注** | 人工执行 |',
75
- ].join('\n');
76
- const { config } = makeProject(manualCase);
77
- const plan = buildStudioPlan(config, { caseIds: ['TC-FB-900'], forceAgent: true });
78
- assert.equal(plan.items[0].mode, 'agent');
79
- });
80
-
81
- test('runner: 再次 Agent 运行会带入反馈并记录本轮纠错', async () => {
82
- const { config } = makeProject();
83
- const feedback = appendRunFeedback(config, { caseId: 'TC-FB-001', note: '先等表头出现,再输入单号', runStatus: 'failed' });
84
- const plan = buildStudioPlan(config, { caseIds: ['TC-FB-001'], forceAgent: true });
85
- plan.items[0].feedback = feedback;
86
- const batch = createBatch(config, {
87
- name: 'feedback-rerun',
88
- caseIds: ['TC-FB-001'],
89
- plan: compactPlan(plan),
90
- options: { workers: 1, agentWorkers: 1, writeBack: 'none' },
91
- });
92
- const runDir = path.join(config.paths.runsDir, 'feedback-rerun');
93
- fs.mkdirSync(runDir, { recursive: true });
94
- fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ steps: [] }));
95
- let received = null;
96
- const final = await runBatch({
97
- config,
98
- batchId: batch.id,
99
- panel: { ctx: {}, studioAborts: new Map() },
100
- deps: {
101
- startAgentJobImpl: async (input) => {
102
- received = input;
103
- return { jobId: 'job-fb', sessionId: 'sess-fb', sessionName: 'uitest-agent-sess-fb', runDir };
104
- },
105
- readAgentJobImpl: async () => ({
106
- job: { status: 'done', sessionId: 'sess-fb' },
107
- hasResult: true,
108
- result: { verdict: '通过', note: '已按反馈等待表头后查询' },
109
- session: {
110
- status: 'finished',
111
- verdict: '通过',
112
- runDir,
113
- steps: [
114
- { id: 's1', action: 'click', status: 'failed', spec: { action: 'click', text: '查询' }, error: '按钮未出现' },
115
- { id: 's2', action: 'click', status: 'passed', spec: { action: 'click', text: '搜索' }, desc: '改用搜索' },
116
- ],
117
- },
118
- }),
119
- commitKnowledge: async () => ({ totals: { pages: 0, actions: 0, waits: 0, suggestions: 0, corrections: 1 } }),
120
- },
121
- });
122
- assert.equal(final.status, 'done');
123
- assert.equal(received.feedback.id, feedback.id);
124
- assert.match(received.feedback.note, /先等表头出现/);
125
- const run = fs.readFileSync(path.join(config.paths.outputDir, 'batches', batch.id, 'case-runs', encodeURIComponent('TC-FB-001').replace(/%/g, '_') + '.json'), 'utf8');
126
- const saved = JSON.parse(run);
127
- assert.equal(saved.feedback.id, feedback.id);
128
- assert.equal(saved.corrections.length, 1);
129
- });
130
-
131
- test('knowledge: 失败但自我纠错的运行也沉淀流程经验', async () => {
132
- const { config } = makeProject();
133
- const batch = createBatch(config, { name: 'knowledge-correction', caseIds: ['TC-FB-001'], plan: { items: [] }, options: { writeBack: 'none' } });
134
- const runDir = path.join(config.paths.runsDir, 'knowledge-correction');
135
- fs.mkdirSync(runDir, { recursive: true });
136
- fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({
137
- label: 'knowledge-correction',
138
- status: 'failed',
139
- steps: [
140
- { id: 's1', action: 'click', status: 'failed', spec: { action: 'click', text: '查询' }, error: '按钮未出现', durationMs: 10 },
141
- { id: 's2', action: 'click', status: 'passed', spec: { action: 'click', text: '搜索' }, desc: '改用搜索', durationMs: 8 },
142
- ],
143
- }));
144
- fs.writeFileSync(path.join(runDir, 'pages.jsonl'), JSON.stringify({
145
- at: new Date().toISOString(),
146
- stepId: 's2',
147
- stepAction: 'click',
148
- url: 'http://example.test/list',
149
- title: '履约单列表',
150
- headings: ['履约单列表'],
151
- tableHeaders: ['履约单编号', '状态'],
152
- buttons: ['搜索'],
153
- inputs: [],
154
- text: '履约单列表 搜索 履约单编号',
155
- }) + '\n');
156
- saveCaseRun(config, batch.id, {
157
- caseId: 'TC-FB-001',
158
- batchId: batch.id,
159
- title: '履约单查询',
160
- mode: 'agent',
161
- status: 'failed',
162
- startedAt: new Date().toISOString(),
163
- endedAt: new Date().toISOString(),
164
- durationMs: 20,
165
- note: '查询按钮未出现,改用搜索后通过',
166
- review: {
167
- at: new Date().toISOString(),
168
- summary: '查询按钮定位失败,改用搜索并通过',
169
- lessons: ['列表页优先等待表头稳定后再操作'],
170
- issues: ['详情弹窗加载慢'],
171
- stepCount: 2,
172
- },
173
- steps: [
174
- { id: 's1', action: 'click', status: 'failed', spec: { action: 'click', text: '查询' }, error: '按钮未出现' },
175
- { id: 's2', action: 'click', status: 'passed', spec: { action: 'click', text: '搜索' }, desc: '改用搜索' },
176
- ],
177
- runDir,
178
- });
179
- const summary = await commitBatchKnowledge(config, batch.id);
180
- assert.equal(summary.totals.corrections, 1);
181
- assert.equal(summary.learned.length, 1);
182
- assert.equal(summary.learned[0].process.processId, 'studio-TC-FB-001');
183
- assert.equal(summary.learned[0].process.corrections.length, 1);
184
- assert.equal(summary.learned[0].title, '履约单查询');
185
- assert.equal(summary.learned[0].process.reviews.length, 1);
186
- assert.match(summary.learned[0].process.reviews[0].summary, /改用搜索/);
187
- assert.equal(summary.learned[0].pageItems[0].title, '履约单列表');
188
- const snapshot = knowledgeSnapshot(config);
189
- const process = snapshot.processes.find((item) => item.caseId === 'TC-FB-001');
190
- assert.ok(process);
191
- assert.equal(process.corrections.length, 1);
192
- assert.match(process.corrections[0].summary, /搜索/);
193
- const context = knowledgeContextForCase(config, { caseId: 'TC-FB-001' });
194
- assert.ok(context.processes.some((item) => item.corrections.length > 0));
195
- assert.ok(context.processes.some((item) => item.reviews.length > 0));
196
- const central = knowledgeContextForCase(config, { caseId: 'TC-FB-001', central: [{ fact: '列表页先等表头' }] });
197
- assert.equal(central.summary.central, 1);
198
- assert.equal(central.central[0].fact, '列表页先等表头');
199
- });