dsh-uitest 2.1.2 → 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.
- package/README.md +23 -23
- package/lib/{vendor/ab.mjs → ab.mjs} +0 -1
- package/lib/browser-ab.mjs +1 -1
- package/package.json +7 -2
- package/skills/uitest/SKILL.md +11 -11
- package/skills/uitest/references/cases-xlsx.md +11 -11
- package/skills/uitest/references/convert-knowledge.md +32 -32
- package/skills/uitest/references/flows.md +1 -1
- package/skills/uitest/references/results-report.md +3 -3
- package/skills/uitest/references/troubleshooting.md +9 -9
- package/studio/accounts.mjs +1 -0
- package/scripts/sync-ab.mjs +0 -20
- package/studio/preview/index.html +0 -88
- package/studio/preview/sample.json +0 -2215
- package/studio/preview/server.mjs +0 -498
- package/studio/test/accounts.test.mjs +0 -90
- package/studio/test/agent-live.test.mjs +0 -53
- package/studio/test/browser-profile.test.mjs +0 -85
- package/studio/test/documents.test.mjs +0 -305
- package/studio/test/export.test.mjs +0 -196
- package/studio/test/feedback.test.mjs +0 -199
- package/studio/test/host.test.mjs +0 -723
- package/studio/test/knowledge.test.mjs +0 -450
- package/studio/test/live-proxy.test.mjs +0 -161
- package/studio/test/live.test.mjs +0 -43
- package/studio/test/queue.test.mjs +0 -157
- package/studio/test/studio.test.mjs +0 -1462
|
@@ -1,1462 +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 { deleteCases, loadProjectCases } from '../../lib/cases-import.mjs';
|
|
9
|
-
import { splitNatural, renderNaturalCase, normalizeStudioCase, summarizeCaseRuns } from '../model.mjs';
|
|
10
|
-
import { importDocument, loadStudioCases, parseNaturalDocument } from '../import.mjs';
|
|
11
|
-
import { buildStudioPlan, compactPlan } from '../plan.mjs';
|
|
12
|
-
import { acquireLock, createBatch, batchDir, deleteBatch, lockFile, mergeBatchRuns, patchBatch, readBatch, readCaseRun, readCaseRuns, listBatches, saveBatch, saveCaseRun } from '../store.mjs';
|
|
13
|
-
import { agentTimeoutMsFor, requestBatchStop, requestCasePrioritize, requestCaseStopInBatch, requeueCase, runBatch, summarizeRunError } from '../runner.mjs';
|
|
14
|
-
import { createStudioPanel, registerStudioRoutes, stopInactiveBatch } from '../host.mjs';
|
|
15
|
-
import { summaryOf } from '../export.mjs';
|
|
16
|
-
import { renderBatchDoc } from '../writeback.mjs';
|
|
17
|
-
import { agentLiveFile } from '../agent-live.mjs';
|
|
18
|
-
|
|
19
|
-
const CASE_MD = [
|
|
20
|
-
'# 登录模块',
|
|
21
|
-
'',
|
|
22
|
-
'## TC-LOGIN-001 登录成功',
|
|
23
|
-
'',
|
|
24
|
-
'| 项 | 内容 |',
|
|
25
|
-
'|---|---|',
|
|
26
|
-
'| **前置条件** | 系统可访问<br>账号有效 |',
|
|
27
|
-
'| **操作步骤** | 打开登录页<br>输入账号密码<br>点击登录 |',
|
|
28
|
-
'| **预期结果** | 进入首页<br>显示待办 |',
|
|
29
|
-
'',
|
|
30
|
-
'## TC-LOGIN-002 错误密码',
|
|
31
|
-
'',
|
|
32
|
-
'**前置条件**:系统可访问',
|
|
33
|
-
'',
|
|
34
|
-
'**操作步骤**',
|
|
35
|
-
'1. 打开登录页',
|
|
36
|
-
'2. 输入错误密码',
|
|
37
|
-
'3. 点击登录',
|
|
38
|
-
'',
|
|
39
|
-
'**预期结果**',
|
|
40
|
-
'1. 提示账号或密码错误',
|
|
41
|
-
'',
|
|
42
|
-
].join('\n');
|
|
43
|
-
|
|
44
|
-
function makeProject(caseMarkdown = CASE_MD) {
|
|
45
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studio-test-'));
|
|
46
|
-
fs.writeFileSync(path.join(dir, 'uitest.config.json'), JSON.stringify({
|
|
47
|
-
baseUrl: 'http://example.test',
|
|
48
|
-
auth: { username: 'tester', storageState: '.auth/state.json' },
|
|
49
|
-
cases: { markdown: 'cases' },
|
|
50
|
-
output: { dir: 'reports' },
|
|
51
|
-
seed: { allowWrites: false },
|
|
52
|
-
}, null, 2));
|
|
53
|
-
fs.mkdirSync(path.join(dir, 'cases'), { recursive: true });
|
|
54
|
-
fs.writeFileSync(path.join(dir, 'cases', 'cases.md'), caseMarkdown);
|
|
55
|
-
fs.mkdirSync(path.join(dir, 'flows'), { recursive: true });
|
|
56
|
-
fs.writeFileSync(path.join(dir, 'flows', 'flow-case.json'), JSON.stringify({
|
|
57
|
-
name: 'flow-case',
|
|
58
|
-
title: 'flow case',
|
|
59
|
-
cases: ['TC-LOGIN-001'],
|
|
60
|
-
steps: [{ id: 's1', action: 'goto', url: '/' }],
|
|
61
|
-
}, null, 2));
|
|
62
|
-
return { dir, config: loadConfig(dir) };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
test('model: splitNatural 拆分编号与 <br>', () => {
|
|
66
|
-
assert.deepEqual(splitNatural('1. 打开页面<br>2. 点击登录'), ['打开页面', '点击登录']);
|
|
67
|
-
assert.deepEqual(splitNatural('1. 打开页面 2. 点击登录'), ['打开页面', '点击登录']);
|
|
68
|
-
assert.deepEqual(splitNatural('- a\n- b'), ['a', 'b']);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('model: 自然语言用例包含前置/过程/预期', () => {
|
|
72
|
-
const item = normalizeStudioCase({
|
|
73
|
-
tcId: 'TC-1',
|
|
74
|
-
functionPoint: '登录',
|
|
75
|
-
precondition: '系统可访问',
|
|
76
|
-
steps: '打开页面\n点击登录',
|
|
77
|
-
expected: '进入首页',
|
|
78
|
-
});
|
|
79
|
-
assert.equal(item.mode, 'agent');
|
|
80
|
-
assert.match(renderNaturalCase(item), /前置条件/);
|
|
81
|
-
assert.match(renderNaturalCase(item), /测试过程/);
|
|
82
|
-
assert.match(renderNaturalCase(item), /进入首页/);
|
|
83
|
-
assert.deepEqual(item.nl.steps, ['打开页面', '点击登录']);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('import: 从 Markdown 生成自然语言用例且重复导入不漂移 id', async () => {
|
|
87
|
-
const { dir, config } = makeProject('');
|
|
88
|
-
const source = path.join(dir, 'login-cases.md');
|
|
89
|
-
fs.writeFileSync(source, CASE_MD);
|
|
90
|
-
const first = await importDocument(config, { file: source, name: '登录模块' });
|
|
91
|
-
assert.deepEqual(first.cases.map((item) => item.tcId), ['TC-LOGIN-001', 'TC-LOGIN-002']);
|
|
92
|
-
assert.ok(fs.existsSync(first.doc.generatedPath));
|
|
93
|
-
const second = await importDocument(config, { file: source, name: '登录模块' });
|
|
94
|
-
assert.deepEqual(second.cases.map((item) => item.tcId), ['TC-LOGIN-001', 'TC-LOGIN-002']);
|
|
95
|
-
const loaded = loadStudioCases(config);
|
|
96
|
-
assert.equal(loaded.cases.length, 2);
|
|
97
|
-
assert.match(loaded.cases[0].nl.script, /测试过程/);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('import: parseNaturalDocument 识别编号标题和字段表', () => {
|
|
101
|
-
const parsed = parseNaturalDocument(CASE_MD, { name: '登录模块' });
|
|
102
|
-
assert.equal(parsed.cases.length, 2);
|
|
103
|
-
assert.equal(parsed.cases[0].tcId, 'TC-LOGIN-001');
|
|
104
|
-
assert.match(parsed.cases[0].steps, /输入账号密码/);
|
|
105
|
-
assert.match(parsed.cases[1].expected, /账号或密码错误/);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test('plan: flow / agent / manual 分泳道', async () => {
|
|
109
|
-
const { config } = makeProject();
|
|
110
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
111
|
-
assert.equal(plan.summary.total, 2);
|
|
112
|
-
assert.equal(plan.items.find((item) => item.caseId === 'TC-LOGIN-001').mode, 'flow');
|
|
113
|
-
assert.equal(plan.items.find((item) => item.caseId === 'TC-LOGIN-002').mode, 'agent');
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test('cases: 前置条件 flow 不混入用例 flow,单独列出', async () => {
|
|
117
|
-
const { config } = makeProject();
|
|
118
|
-
const flows = [
|
|
119
|
-
{ name: 'case-flow', title: '用例流程', kind: 'case', cases: ['TC-LOGIN-001'], tags: [], steps: [] },
|
|
120
|
-
{ name: 'pre-flow', title: '前置流程', kind: 'precondition', cases: ['TC-LOGIN-001'], covers: ['TC-LOGIN-001'], provides: ['orderNo'], tags: ['precondition'], steps: [] },
|
|
121
|
-
];
|
|
122
|
-
const loaded = loadStudioCases(config, { flows });
|
|
123
|
-
const item = loaded.cases.find((entry) => entry.tcId === 'TC-LOGIN-001');
|
|
124
|
-
assert.equal(item.flow.name, 'case-flow');
|
|
125
|
-
assert.equal(item.preconditionFlows.length, 1);
|
|
126
|
-
assert.equal(item.preconditionFlows[0].name, 'pre-flow');
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test('runner: Agent 缺少造数配方仍继续执行,不返回前置阻塞', async () => {
|
|
130
|
-
const md = [
|
|
131
|
-
'## TC-AGENT-098 订单造数',
|
|
132
|
-
'',
|
|
133
|
-
'| 项 | 内容 |',
|
|
134
|
-
'|---|---|',
|
|
135
|
-
'| **前置条件** | 订单已受理,客户信息完整 |',
|
|
136
|
-
'| **操作步骤** | 打开订单详情 |',
|
|
137
|
-
'| **预期结果** | 详情可打开 |',
|
|
138
|
-
].join('\n');
|
|
139
|
-
const { config } = makeProject(md);
|
|
140
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-AGENT-098'] });
|
|
141
|
-
const batch = createBatch(config, { name: 'agent-advisory', caseIds: ['TC-AGENT-098'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
142
|
-
const runDir = path.join(config.paths.runsDir, 'agent-advisory-run');
|
|
143
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
144
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ steps: [] }));
|
|
145
|
-
const final = await runBatch({
|
|
146
|
-
config,
|
|
147
|
-
batchId: batch.id,
|
|
148
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
149
|
-
deps: {
|
|
150
|
-
startAgentJobImpl: async () => ({ jobId: 'job-a', sessionId: 'sess-a', sessionName: 'uitest-agent-sess-a', runDir }),
|
|
151
|
-
readAgentJobImpl: async () => ({ job: { status: 'done', sessionId: 'sess-a' }, hasResult: true, result: { verdict: '通过', note: 'Agent 自行准备数据后通过' }, session: { status: 'finished', verdict: '通过', runDir, steps: [] } }),
|
|
152
|
-
commitKnowledge: async () => ({ totals: { pages: 0, actions: 0, waits: 0, suggestions: 0 } }),
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
assert.equal(final.status, 'done');
|
|
156
|
-
assert.equal(final.summary.passed, 1);
|
|
157
|
-
const run = readCaseRuns(config, batch.id)[0];
|
|
158
|
-
assert.equal(run.status, 'passed');
|
|
159
|
-
assert.doesNotMatch(run.note, /前置数据准备失败/);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
test('plan: Agent 用例缺少造数配方时只提示,不阻塞', async () => {
|
|
163
|
-
const md = [
|
|
164
|
-
'## TC-AGENT-099 订单造数',
|
|
165
|
-
'',
|
|
166
|
-
'| 项 | 内容 |',
|
|
167
|
-
'|---|---|',
|
|
168
|
-
'| **前置条件** | 订单已受理,客户信息完整 |',
|
|
169
|
-
'| **操作步骤** | 打开订单详情 |',
|
|
170
|
-
'| **预期结果** | 详情可打开 |',
|
|
171
|
-
].join('\n');
|
|
172
|
-
const { config } = makeProject(md);
|
|
173
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-AGENT-099'] });
|
|
174
|
-
const item = plan.items[0];
|
|
175
|
-
assert.equal(item.mode, 'agent');
|
|
176
|
-
assert.equal(item.seed.advisory, true);
|
|
177
|
-
assert.equal(item.seed.state, 'missing');
|
|
178
|
-
assert.match(item.seedWarning, /Agent/);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
test('plan: forceAgent 忽略已关联 flow,改用 Agent 执行', async () => {
|
|
182
|
-
const { config } = makeProject();
|
|
183
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'], forceAgent: true });
|
|
184
|
-
assert.equal(plan.items[0].mode, 'agent');
|
|
185
|
-
assert.equal(plan.items[0].flow, null);
|
|
186
|
-
assert.equal(plan.summary.agent, 1);
|
|
187
|
-
assert.equal(plan.summary.flow, 0);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
test('store: 创建批次、读取与列表', async () => {
|
|
191
|
-
const { config } = makeProject();
|
|
192
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
193
|
-
const batch = createBatch(config, { name: 'test', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 4, agentWorkers: 2, writeBack: 'none' } });
|
|
194
|
-
assert.match(batch.id, /^b-/);
|
|
195
|
-
assert.equal(batch.options.concurrency, 4, '并发数取 flow 与 Agent 的较大值');
|
|
196
|
-
assert.equal(readBatch(config, batch.id).name, 'test');
|
|
197
|
-
assert.ok(listBatches(config).some((item) => item.id === batch.id));
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
test('runner: 强制造数批次给 Agent 45 分钟,普通批次 20 分钟', () => {
|
|
201
|
-
assert.equal(agentTimeoutMsFor({ options: { forceCreateData: true } }), 45 * 60 * 1000);
|
|
202
|
-
assert.equal(agentTimeoutMsFor({ options: {} }), 20 * 60 * 1000);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
test('store: 强制造数自动开启写操作并记录选项', () => {
|
|
206
|
-
const { config } = makeProject();
|
|
207
|
-
const batch = createBatch(config, { name: 'fresh', caseIds: ['TC-LOGIN-001'], options: { forceCreateData: true, writeBack: 'none' } });
|
|
208
|
-
assert.equal(batch.options.forceCreateData, true);
|
|
209
|
-
assert.equal(batch.options.allowWrites, true);
|
|
210
|
-
const plain = createBatch(config, { name: 'plain', caseIds: ['TC-LOGIN-001'], options: { writeBack: 'none' } });
|
|
211
|
-
assert.equal(plain.options.forceCreateData, false);
|
|
212
|
-
assert.equal(plain.options.allowWrites, false);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
test('store: 删除批次会删除目录并清理该批次的结果记录', async () => {
|
|
216
|
-
const { config } = makeProject();
|
|
217
|
-
const batch = createBatch(config, { name: '待删除', caseIds: ['TC-LOGIN-001'] });
|
|
218
|
-
await patchBatch(config, batch.id, { status: 'done' });
|
|
219
|
-
const dir = batchDir(config, batch.id);
|
|
220
|
-
fs.mkdirSync(path.join(dir, 'case-runs'), { recursive: true });
|
|
221
|
-
fs.writeFileSync(path.join(dir, 'case-runs', 'TC-LOGIN-001.json'), '{"caseId":"TC-LOGIN-001"}');
|
|
222
|
-
fs.mkdirSync(path.dirname(config.paths.resultsFile), { recursive: true });
|
|
223
|
-
fs.writeFileSync(config.paths.resultsFile, [
|
|
224
|
-
JSON.stringify({ caseId: 'TC-LOGIN-001', status: '通过', batchId: batch.id }),
|
|
225
|
-
JSON.stringify({ caseId: 'TC-LOGIN-002', status: '失败', batchId: 'b-other' }),
|
|
226
|
-
].join('\n') + '\n');
|
|
227
|
-
const deleted = await deleteBatch(config, batch.id);
|
|
228
|
-
assert.equal(deleted.batchId, batch.id);
|
|
229
|
-
assert.equal(deleted.prunedResults, 1);
|
|
230
|
-
assert.equal(fs.existsSync(dir), false);
|
|
231
|
-
assert.equal(listBatches(config).some((item) => item.id === batch.id), false);
|
|
232
|
-
const remaining = fs.readFileSync(config.paths.resultsFile, 'utf8').trim().split('\n').map((line) => JSON.parse(line));
|
|
233
|
-
assert.equal(remaining.length, 1);
|
|
234
|
-
assert.equal(remaining[0].caseId, 'TC-LOGIN-002');
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test('store: 执行中的批次不允许删除', async () => {
|
|
238
|
-
const { config } = makeProject();
|
|
239
|
-
const batch = createBatch(config, { name: '运行中', caseIds: ['TC-LOGIN-001'] });
|
|
240
|
-
assert.equal(readBatch(config, batch.id).status, 'queued');
|
|
241
|
-
await assert.rejects(() => deleteBatch(config, batch.id), /先停止批次/);
|
|
242
|
-
assert.equal(fs.existsSync(batchDir(config, batch.id)), true);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
test('store: 持有进程已退出的锁可立即接管', () => {
|
|
246
|
-
const { config } = makeProject();
|
|
247
|
-
const file = lockFile(config, 'knowledge');
|
|
248
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
249
|
-
fs.writeFileSync(file, JSON.stringify({ pid: 2147483647, at: new Date().toISOString(), name: 'knowledge' }) + '\n');
|
|
250
|
-
const lock = acquireLock(config, 'knowledge');
|
|
251
|
-
assert.equal(fs.existsSync(file), true);
|
|
252
|
-
lock.release();
|
|
253
|
-
assert.equal(fs.existsSync(file), false);
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
test('store: 活进程持有的新锁仍拒绝并发', () => {
|
|
257
|
-
const { config } = makeProject();
|
|
258
|
-
const file = lockFile(config, 'knowledge');
|
|
259
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
260
|
-
fs.writeFileSync(file, JSON.stringify({ pid: process.pid, at: new Date().toISOString(), name: 'knowledge' }) + '\n');
|
|
261
|
-
assert.throws(() => acquireLock(config, 'knowledge'), /正在进行中/);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
test('runner: flow 与 Agent 并发、按批次回写一次、知识合并一次', async () => {
|
|
265
|
-
const { dir, config } = makeProject();
|
|
266
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
267
|
-
const batch = createBatch(config, { name: 'integration', caseIds: plan.items.map((item) => item.caseId), plan: compactPlan(plan), options: { workers: 2, agentWorkers: 1, writeBack: 'source' } });
|
|
268
|
-
let flowCalls = 0;
|
|
269
|
-
let agentCalls = 0;
|
|
270
|
-
let knowledgeCalls = 0;
|
|
271
|
-
const flowRunDir = path.join(config.paths.runsDir, 'flow-run');
|
|
272
|
-
fs.mkdirSync(path.join(flowRunDir, 'screenshots'), { recursive: true });
|
|
273
|
-
fs.writeFileSync(path.join(flowRunDir, 'run.json'), JSON.stringify({ label: 'flow-run', status: 'passed', steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png' }] }));
|
|
274
|
-
fs.writeFileSync(path.join(flowRunDir, 'screenshots', 's1.png'), 'x');
|
|
275
|
-
const agentRunDir = path.join(config.paths.runsDir, 'agent-run');
|
|
276
|
-
fs.mkdirSync(path.join(agentRunDir, 'screenshots'), { recursive: true });
|
|
277
|
-
fs.writeFileSync(path.join(agentRunDir, 'run.json'), JSON.stringify({ label: 'agent-run', status: 'passed', steps: [{ id: 'a1', action: 'click', status: 'passed', screenshot: 'screenshots/a1.png' }] }));
|
|
278
|
-
fs.writeFileSync(path.join(agentRunDir, 'screenshots', 'a1.png'), 'x');
|
|
279
|
-
|
|
280
|
-
const final = await runBatch({
|
|
281
|
-
config,
|
|
282
|
-
batchId: batch.id,
|
|
283
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
284
|
-
deps: {
|
|
285
|
-
launchSessionImpl: async () => ({ close: async () => {} }),
|
|
286
|
-
runFlowImpl: async () => {
|
|
287
|
-
flowCalls += 1;
|
|
288
|
-
return {
|
|
289
|
-
status: 'passed', passed: 1, failed: 0, total: 1,
|
|
290
|
-
runDir: flowRunDir,
|
|
291
|
-
runJsonPath: path.join(flowRunDir, 'run.json'),
|
|
292
|
-
steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png', durationMs: 10 }],
|
|
293
|
-
label: 'flow-run',
|
|
294
|
-
flow: { name: 'flow-case' },
|
|
295
|
-
startedAt: new Date().toISOString(),
|
|
296
|
-
endedAt: new Date().toISOString(),
|
|
297
|
-
};
|
|
298
|
-
},
|
|
299
|
-
startAgentJobImpl: async () => {
|
|
300
|
-
agentCalls += 1;
|
|
301
|
-
return { jobId: 'job-1', sessionId: 'sess-1', childId: 'child-1', runDir: agentRunDir };
|
|
302
|
-
},
|
|
303
|
-
readAgentJobImpl: async () => ({
|
|
304
|
-
job: { id: 'job-1', status: 'done', sessionId: 'sess-1' },
|
|
305
|
-
hasResult: true,
|
|
306
|
-
result: { verdict: '通过', note: 'Agent 核对通过', evidence: ['screenshots/a1.png'] },
|
|
307
|
-
session: {
|
|
308
|
-
status: 'finished',
|
|
309
|
-
verdict: '通过',
|
|
310
|
-
note: 'Agent 核对通过',
|
|
311
|
-
runDir: agentRunDir,
|
|
312
|
-
steps: [{ id: 'a1', action: 'click', status: 'passed', screenshot: 'screenshots/a1.png', durationMs: 20 }],
|
|
313
|
-
},
|
|
314
|
-
}),
|
|
315
|
-
commitKnowledge: async () => {
|
|
316
|
-
knowledgeCalls += 1;
|
|
317
|
-
return { totals: { pages: 1, actions: 2, waits: 1, suggestions: 0 } };
|
|
318
|
-
},
|
|
319
|
-
},
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
assert.equal(flowCalls, 1);
|
|
323
|
-
assert.equal(agentCalls, 1);
|
|
324
|
-
assert.equal(knowledgeCalls, 1);
|
|
325
|
-
assert.equal(final.status, 'done');
|
|
326
|
-
assert.equal(final.summary.passed, 2);
|
|
327
|
-
const runs = readCaseRuns(config, batch.id);
|
|
328
|
-
assert.equal(runs.length, 2);
|
|
329
|
-
assert.equal(runs.find((item) => item.caseId === 'TC-LOGIN-001').mode, 'flow');
|
|
330
|
-
assert.equal(runs.find((item) => item.caseId === 'TC-LOGIN-002').mode, 'agent');
|
|
331
|
-
assert.ok(final.resultDoc && fs.existsSync(final.resultDoc));
|
|
332
|
-
assert.match(fs.readFileSync(final.resultDoc, 'utf8'), /TC-LOGIN-001/);
|
|
333
|
-
assert.match(fs.readFileSync(path.join(dir, 'cases', 'cases.md'), 'utf8'), /uitest:result:start/);
|
|
334
|
-
assert.ok(final.writeback);
|
|
335
|
-
assert.equal(final.writeback.markdownFiles, 1);
|
|
336
|
-
assert.equal(final.knowledge.totals.pages, 1);
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
test('runner: 并发数限制同时执行的用例数,flow 与 Agent 共享', async () => {
|
|
340
|
-
const { config } = makeProject();
|
|
341
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
342
|
-
const batch = createBatch(config, {
|
|
343
|
-
name: 'concurrency',
|
|
344
|
-
caseIds: plan.items.map((item) => item.caseId),
|
|
345
|
-
plan: compactPlan(plan),
|
|
346
|
-
options: { concurrency: 1, writeBack: 'none' },
|
|
347
|
-
});
|
|
348
|
-
const flowRunDir = path.join(config.paths.runsDir, 'limit-flow');
|
|
349
|
-
fs.mkdirSync(path.join(flowRunDir, 'screenshots'), { recursive: true });
|
|
350
|
-
fs.writeFileSync(path.join(flowRunDir, 'run.json'), JSON.stringify({ label: 'limit-flow', status: 'passed', steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png' }] }));
|
|
351
|
-
fs.writeFileSync(path.join(flowRunDir, 'screenshots', 's1.png'), 'x');
|
|
352
|
-
const agentRunDir = path.join(config.paths.runsDir, 'limit-agent');
|
|
353
|
-
fs.mkdirSync(path.join(agentRunDir, 'screenshots'), { recursive: true });
|
|
354
|
-
fs.writeFileSync(path.join(agentRunDir, 'run.json'), JSON.stringify({ label: 'limit-agent', status: 'passed', steps: [{ id: 'a1', action: 'click', status: 'passed', screenshot: 'screenshots/a1.png' }] }));
|
|
355
|
-
fs.writeFileSync(path.join(agentRunDir, 'screenshots', 'a1.png'), 'x');
|
|
356
|
-
|
|
357
|
-
let active = 0;
|
|
358
|
-
let maxActive = 0;
|
|
359
|
-
let flowCalls = 0;
|
|
360
|
-
let agentCalls = 0;
|
|
361
|
-
const within = async (fn) => {
|
|
362
|
-
active += 1;
|
|
363
|
-
maxActive = Math.max(maxActive, active);
|
|
364
|
-
try {
|
|
365
|
-
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
366
|
-
return await fn();
|
|
367
|
-
} finally {
|
|
368
|
-
active -= 1;
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
const final = await runBatch({
|
|
373
|
-
config,
|
|
374
|
-
batchId: batch.id,
|
|
375
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
376
|
-
deps: {
|
|
377
|
-
launchSessionImpl: async () => ({ close: async () => {} }),
|
|
378
|
-
runFlowImpl: () => within(async () => {
|
|
379
|
-
flowCalls += 1;
|
|
380
|
-
return {
|
|
381
|
-
status: 'passed', passed: 1, failed: 0, total: 1,
|
|
382
|
-
runDir: flowRunDir,
|
|
383
|
-
steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png', durationMs: 10 }],
|
|
384
|
-
label: 'limit-flow',
|
|
385
|
-
flow: { name: 'flow-case' },
|
|
386
|
-
startedAt: new Date().toISOString(),
|
|
387
|
-
endedAt: new Date().toISOString(),
|
|
388
|
-
};
|
|
389
|
-
}),
|
|
390
|
-
startAgentJobImpl: () => within(async () => {
|
|
391
|
-
agentCalls += 1;
|
|
392
|
-
return { jobId: 'job-1', sessionId: 'sess-1', childId: 'child-1', runDir: agentRunDir };
|
|
393
|
-
}),
|
|
394
|
-
readAgentJobImpl: async () => ({
|
|
395
|
-
job: { id: 'job-1', status: 'done', sessionId: 'sess-1' },
|
|
396
|
-
hasResult: true,
|
|
397
|
-
result: { verdict: '通过', note: 'Agent 通过', evidence: [] },
|
|
398
|
-
session: {
|
|
399
|
-
status: 'finished', verdict: '通过', note: 'Agent 通过', runDir: agentRunDir,
|
|
400
|
-
steps: [{ id: 'a1', action: 'click', status: 'passed', screenshot: 'screenshots/a1.png', durationMs: 20 }],
|
|
401
|
-
},
|
|
402
|
-
}),
|
|
403
|
-
commitKnowledge: async () => ({ totals: { pages: 0, actions: 0, waits: 0, suggestions: 0 } }),
|
|
404
|
-
},
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
assert.equal(flowCalls, 1);
|
|
408
|
-
assert.equal(agentCalls, 1);
|
|
409
|
-
assert.equal(final.status, 'done');
|
|
410
|
-
assert.equal(maxActive, 1, '并发数为 1 时 flow 与 Agent 用例不会同时执行');
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
test('writeback: 回写锁阻止并发写,重复回写不重复插入结果块', async () => {
|
|
414
|
-
const { dir, config } = makeProject();
|
|
415
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
416
|
-
const batch = createBatch(config, { name: 'wb', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, writeBack: 'source' } });
|
|
417
|
-
const flowRunDir = path.join(config.paths.runsDir, 'wb-run');
|
|
418
|
-
fs.mkdirSync(path.join(flowRunDir, 'screenshots'), { recursive: true });
|
|
419
|
-
fs.writeFileSync(path.join(flowRunDir, 'run.json'), JSON.stringify({ label: 'wb-run', status: 'passed', steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png' }] }));
|
|
420
|
-
fs.writeFileSync(path.join(flowRunDir, 'screenshots', 's1.png'), 'x');
|
|
421
|
-
const final = await runBatch({
|
|
422
|
-
config,
|
|
423
|
-
batchId: batch.id,
|
|
424
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
425
|
-
deps: {
|
|
426
|
-
launchSessionImpl: async () => ({ close: async () => {} }),
|
|
427
|
-
runFlowImpl: async () => ({ status: 'passed', passed: 1, failed: 0, total: 1, runDir: flowRunDir, steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png' }], label: 'wb-run', flow: { name: 'flow-case' }, startedAt: new Date().toISOString(), endedAt: new Date().toISOString() }),
|
|
428
|
-
commitKnowledge: async () => ({ totals: { pages: 0, actions: 0, waits: 0, suggestions: 0 } }),
|
|
429
|
-
},
|
|
430
|
-
});
|
|
431
|
-
assert.equal(final.status, 'done');
|
|
432
|
-
const source = path.join(dir, 'cases', 'cases.md');
|
|
433
|
-
assert.equal((fs.readFileSync(source, 'utf8').match(/uitest:result:start/g) || []).length, 1);
|
|
434
|
-
const { applyBatchWriteback } = await import('../writeback.mjs');
|
|
435
|
-
await applyBatchWriteback(config, batch.id);
|
|
436
|
-
assert.equal((fs.readFileSync(source, 'utf8').match(/uitest:result:start/g) || []).length, 1);
|
|
437
|
-
});
|
|
438
|
-
|
|
439
|
-
test('import: 重新导入会清除软删除 tombstone,用例重新可见', async () => {
|
|
440
|
-
const { config } = makeProject('');
|
|
441
|
-
const fixture = path.resolve('uitest/test/fixtures/M006履约管理模块-测试用例.xlsx');
|
|
442
|
-
const first = await importDocument(config, { file: fixture, name: 'M006' });
|
|
443
|
-
assert.ok(first.cases.length >= 200);
|
|
444
|
-
const before = loadProjectCases(config).cases;
|
|
445
|
-
const hiddenIds = before.slice(1).map((item) => item.tcId);
|
|
446
|
-
deleteCases(config.projectDir, hiddenIds, { cases: before });
|
|
447
|
-
assert.ok(loadProjectCases(config).cases.length < 5);
|
|
448
|
-
const second = await importDocument(config, { file: fixture, name: 'M006' });
|
|
449
|
-
assert.ok(second.cases.length >= 200);
|
|
450
|
-
assert.ok(loadProjectCases(config).cases.length >= 200);
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
test('import: M006 xlsx 导入 242 条而不是 1 条', async () => {
|
|
454
|
-
const { config } = makeProject('');
|
|
455
|
-
const fixture = path.resolve('uitest/test/fixtures/M006履约管理模块-测试用例.xlsx');
|
|
456
|
-
const result = await importDocument(config, { file: fixture, name: 'M006' });
|
|
457
|
-
assert.ok(result.cases.length >= 200, 'expected hundreds of cases, got ' + result.cases.length);
|
|
458
|
-
assert.equal(result.doc.count, result.cases.length);
|
|
459
|
-
assert.equal(result.cases[0].tcId, 'TC-FP001-01');
|
|
460
|
-
assert.ok(result.warnings.length >= 1);
|
|
461
|
-
});
|
|
462
|
-
|
|
463
|
-
test('runner: 按步骤角色自动切换测试账号', async () => {
|
|
464
|
-
const { config } = makeProject();
|
|
465
|
-
const accounts = await import('../accounts.mjs');
|
|
466
|
-
accounts.saveAccount(config, { id: '', patch: { username: 'finance', name: '财务', roles: ['OMS总部财务'], department: '财务部', area: '所有' } });
|
|
467
|
-
accounts.saveAccount(config, { id: '', patch: { username: 'sales', name: '销售', roles: ['OMS销售'], department: '营口办', area: '东北片区' } });
|
|
468
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
469
|
-
const batch = createBatch(config, { name: 'role-switch', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
470
|
-
const runDir = path.join(config.paths.runsDir, 'role-switch-run');
|
|
471
|
-
fs.mkdirSync(path.join(runDir, 'screenshots'), { recursive: true });
|
|
472
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'role-switch-run', status: 'passed', steps: [] }));
|
|
473
|
-
const logins = [];
|
|
474
|
-
let switched = [];
|
|
475
|
-
const final = await runBatch({
|
|
476
|
-
config,
|
|
477
|
-
batchId: batch.id,
|
|
478
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
479
|
-
deps: {
|
|
480
|
-
launchSessionImpl: async (sessionConfig) => ({ close: async () => {}, config: sessionConfig }),
|
|
481
|
-
loginImpl: async (session) => { logins.push(session.config.auth.username); },
|
|
482
|
-
runFlowImpl: async (args) => {
|
|
483
|
-
const sales = await args.switchAccount({ role: 'OMS销售' }, null);
|
|
484
|
-
const finance = await args.switchAccount({ role: 'OMS总部财务' }, sales);
|
|
485
|
-
switched = [sales.config.auth.username, finance.config.auth.username];
|
|
486
|
-
return {
|
|
487
|
-
status: 'passed', passed: 1, failed: 0, total: 1, runDir,
|
|
488
|
-
runJsonPath: path.join(runDir, 'run.json'),
|
|
489
|
-
steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png', durationMs: 10 }],
|
|
490
|
-
label: 'role-switch-run', flow: { name: 'flow-case' },
|
|
491
|
-
startedAt: new Date().toISOString(), endedAt: new Date().toISOString(),
|
|
492
|
-
};
|
|
493
|
-
},
|
|
494
|
-
commitKnowledge: async () => ({ totals: { pages: 0, actions: 0, waits: 0, suggestions: 0 } }),
|
|
495
|
-
},
|
|
496
|
-
});
|
|
497
|
-
assert.equal(final.status, 'done');
|
|
498
|
-
assert.deepEqual(switched, ['sales', 'finance']);
|
|
499
|
-
assert.ok(logins.includes('sales'));
|
|
500
|
-
assert.ok(logins.includes('finance'));
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
/* ------------------------------------------------------------------ */
|
|
504
|
-
/* 批次队列:状态同步、重排、停止、优先 */
|
|
505
|
-
/* ------------------------------------------------------------------ */
|
|
506
|
-
|
|
507
|
-
function passedFlowResult(runDir, label = 'queue-flow') {
|
|
508
|
-
return {
|
|
509
|
-
status: 'passed', passed: 1, failed: 0, total: 1,
|
|
510
|
-
runDir,
|
|
511
|
-
steps: [{ id: 's1', action: 'goto', status: 'passed', screenshot: 'screenshots/s1.png', durationMs: 10 }],
|
|
512
|
-
label,
|
|
513
|
-
flow: { name: 'flow-case' },
|
|
514
|
-
startedAt: new Date().toISOString(),
|
|
515
|
-
endedAt: new Date().toISOString(),
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
function quietDeps(extra = {}) {
|
|
520
|
-
return {
|
|
521
|
-
launchSessionImpl: async () => ({ close: async () => {} }),
|
|
522
|
-
commitKnowledge: async () => ({ totals: { pages: 0, actions: 0, waits: 0, suggestions: 0 } }),
|
|
523
|
-
...extra,
|
|
524
|
-
};
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
test('model: stopped 单独计数,不计入 done', () => {
|
|
528
|
-
const summary = summarizeCaseRuns([
|
|
529
|
-
{ status: 'passed' },
|
|
530
|
-
{ status: 'stopped' },
|
|
531
|
-
{ status: 'queued' },
|
|
532
|
-
]);
|
|
533
|
-
assert.equal(summary.stopped, 1);
|
|
534
|
-
assert.equal(summary.done, 1);
|
|
535
|
-
assert.equal(summary.percent, 100);
|
|
536
|
-
});
|
|
537
|
-
|
|
538
|
-
test('runner: summarizeRunError 归并 Chrome profile 锁与启动失败', () => {
|
|
539
|
-
const locked = '测试浏览器 profile 被占用,浏览器没有启动。请等上一次执行结束后重试。';
|
|
540
|
-
const launch = 'Chrome 启动失败,没有写入 DevToolsActivePort。请检查浏览器环境后重试。';
|
|
541
|
-
assert.equal(summarizeRunError('{"error":"ProcessSingleton: 另一个程序正在使用此文件。"}'), locked);
|
|
542
|
-
assert.equal(summarizeRunError('Lock file can not be created: /tmp/profile/SingletonLock'), locked);
|
|
543
|
-
assert.equal(summarizeRunError('DevToolsActivePort file missing'), launch);
|
|
544
|
-
assert.equal(summarizeRunError(' 浏览器 Auto-launch failed\t'), launch);
|
|
545
|
-
const generic = summarizeRunError('x'.repeat(400));
|
|
546
|
-
assert.ok(generic.length <= 301, '其它错误去空白后截断到 300 字符');
|
|
547
|
-
assert.match(generic, /^x{300}/);
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
test('store: mergeBatchRuns 用队列覆盖状态且保留 run 明细', () => {
|
|
551
|
-
const item = (caseId, mode = 'flow') => ({ caseId, title: caseId, mode });
|
|
552
|
-
const baseEntry = (caseId, patch) => ({
|
|
553
|
-
caseId,
|
|
554
|
-
state: 'queued',
|
|
555
|
-
seq: 1,
|
|
556
|
-
priority: 0,
|
|
557
|
-
enqueuedAt: '2026-11-19T00:00:00.000Z',
|
|
558
|
-
startedAt: null,
|
|
559
|
-
endedAt: null,
|
|
560
|
-
attempt: 1,
|
|
561
|
-
stopRequested: false,
|
|
562
|
-
...patch,
|
|
563
|
-
});
|
|
564
|
-
const batch = {
|
|
565
|
-
id: 'batch-synthetic',
|
|
566
|
-
plan: { items: [item('RUN'), item('RETRY'), item('STOPPED'), item('NEVER'), item('STALE')] },
|
|
567
|
-
queue: {
|
|
568
|
-
nextSeq: 6,
|
|
569
|
-
entries: {
|
|
570
|
-
RUN: baseEntry('RUN', { state: 'running', startedAt: '2026-11-19T00:01:00.000Z' }),
|
|
571
|
-
RETRY: baseEntry('RETRY', { attempt: 2, seq: 2, enqueuedAt: '2026-11-19T01:00:00.000Z' }),
|
|
572
|
-
STOPPED: baseEntry('STOPPED', { state: 'stopped', seq: 3 }),
|
|
573
|
-
NEVER: baseEntry('NEVER', { state: 'stopped', seq: 4 }),
|
|
574
|
-
STALE: baseEntry('STALE', { state: 'stopped', seq: 5 }),
|
|
575
|
-
},
|
|
576
|
-
},
|
|
577
|
-
};
|
|
578
|
-
const runs = [
|
|
579
|
-
{ caseId: 'RUN', status: 'running', note: '正在执行', steps: [{ id: 's1' }], evidence: [{ file: 'a.png' }], runDir: '/runs/a', agent: { sessionId: 'sess-a' } },
|
|
580
|
-
{ caseId: 'RETRY', status: 'failed', note: '上次失败', endedAt: '2026-11-19T00:30:00.000Z' },
|
|
581
|
-
{ caseId: 'STOPPED', status: 'passed', steps: [{ id: 's2' }], evidence: [{ file: 'b.png' }], runDir: '/runs/b' },
|
|
582
|
-
{ caseId: 'STALE', status: 'running', note: '正在执行', startedAt: '2026-11-19T00:00:00.000Z' },
|
|
583
|
-
];
|
|
584
|
-
const merged = mergeBatchRuns(batch, runs);
|
|
585
|
-
const byId = new Map(merged.map((run) => [run.caseId, run]));
|
|
586
|
-
|
|
587
|
-
assert.equal(byId.get('RUN').status, 'running');
|
|
588
|
-
assert.deepEqual(byId.get('RUN').steps, [{ id: 's1' }]);
|
|
589
|
-
assert.equal(byId.get('RUN').runDir, '/runs/a');
|
|
590
|
-
assert.equal(byId.get('RUN').queue.state, 'running');
|
|
591
|
-
|
|
592
|
-
assert.equal(byId.get('RETRY').status, 'queued');
|
|
593
|
-
assert.equal(byId.get('RETRY').previousStatus, 'failed');
|
|
594
|
-
assert.equal(byId.get('RETRY').note, '已重新排队,等待执行');
|
|
595
|
-
assert.equal(byId.get('RETRY').queue.ahead, 0, 'RETRY 是唯一排队项');
|
|
596
|
-
|
|
597
|
-
assert.equal(byId.get('STOPPED').status, 'passed', '停止排队不覆盖上次结果');
|
|
598
|
-
assert.deepEqual(byId.get('STOPPED').steps, [{ id: 's2' }]);
|
|
599
|
-
assert.equal(byId.get('STOPPED').queue.state, 'stopped');
|
|
600
|
-
|
|
601
|
-
assert.equal(byId.get('NEVER').status, 'stopped');
|
|
602
|
-
assert.equal(byId.get('NEVER').note, '已手动停止,未执行');
|
|
603
|
-
assert.equal(byId.get('NEVER').batchId, batch.id, '合成结果也带批次 id,用例库能回链批次');
|
|
604
|
-
assert.equal(byId.get('RUN').batchId, batch.id);
|
|
605
|
-
assert.equal(byId.get('STALE').status, 'stopped', '队列已停止时残留的 running 结果收敛为 stopped');
|
|
606
|
-
assert.equal(byId.get('STALE').note, '已手动停止');
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
test('store: manual 与 blocked 计划项不显示为排队', () => {
|
|
610
|
-
const batch = {
|
|
611
|
-
id: 'batch-plan-only',
|
|
612
|
-
plan: {
|
|
613
|
-
items: [
|
|
614
|
-
{ caseId: 'FLOW', title: '流程用例', mode: 'flow' },
|
|
615
|
-
{ caseId: 'MANUAL', title: '人工用例', mode: 'manual' },
|
|
616
|
-
{ caseId: 'BLOCKED', title: '阻塞用例', mode: 'blocked' },
|
|
617
|
-
],
|
|
618
|
-
},
|
|
619
|
-
queue: {
|
|
620
|
-
nextSeq: 2,
|
|
621
|
-
entries: {
|
|
622
|
-
FLOW: {
|
|
623
|
-
caseId: 'FLOW',
|
|
624
|
-
state: 'queued',
|
|
625
|
-
seq: 1,
|
|
626
|
-
priority: 0,
|
|
627
|
-
enqueuedAt: '2026-11-19T00:00:00.000Z',
|
|
628
|
-
startedAt: null,
|
|
629
|
-
endedAt: null,
|
|
630
|
-
attempt: 1,
|
|
631
|
-
stopRequested: false,
|
|
632
|
-
},
|
|
633
|
-
},
|
|
634
|
-
},
|
|
635
|
-
};
|
|
636
|
-
const byId = new Map(mergeBatchRuns(batch, []).map((run) => [run.caseId, run]));
|
|
637
|
-
|
|
638
|
-
assert.equal(byId.get('FLOW').status, 'queued');
|
|
639
|
-
assert.ok(byId.get('FLOW').queue, '入队用例仍有排队信息');
|
|
640
|
-
|
|
641
|
-
const manual = byId.get('MANUAL');
|
|
642
|
-
assert.equal(manual.status, 'untested');
|
|
643
|
-
assert.equal(manual.queue, null);
|
|
644
|
-
assert.equal(manual.note, '需人工执行');
|
|
645
|
-
|
|
646
|
-
const blocked = byId.get('BLOCKED');
|
|
647
|
-
assert.equal(blocked.status, 'blocked');
|
|
648
|
-
assert.equal(blocked.queue, null);
|
|
649
|
-
assert.equal(blocked.note, '未进入本轮执行队列');
|
|
650
|
-
|
|
651
|
-
for (const id of ['MANUAL', 'BLOCKED']) {
|
|
652
|
-
assert.ok(!['running', 'queued'].includes(byId.get(id).status), id + ' 不应显示执行中或排队中');
|
|
653
|
-
}
|
|
654
|
-
});
|
|
655
|
-
|
|
656
|
-
test('export: 已停止单独计数,不计入完成率', () => {
|
|
657
|
-
const summary = summaryOf([{}, {}, {}], ['通过', '已停止', '未执行']);
|
|
658
|
-
assert.equal(summary.stopped, 1);
|
|
659
|
-
assert.equal(summary.done, 1, '已停止不计入 done');
|
|
660
|
-
assert.equal(summary.percent, 100, '通过率只按已执行用例计算');
|
|
661
|
-
assert.equal(summary.untested, 1);
|
|
662
|
-
});
|
|
663
|
-
|
|
664
|
-
test('writeback: 结果文档汇总包含已停止', () => {
|
|
665
|
-
const text = renderBatchDoc({
|
|
666
|
-
config: { projectDir: '/tmp', paths: { outputDir: '/tmp' } },
|
|
667
|
-
batch: {
|
|
668
|
-
id: 'b-20260101-000000-abcd',
|
|
669
|
-
name: '停止批次',
|
|
670
|
-
status: 'stopped',
|
|
671
|
-
stage: 'done',
|
|
672
|
-
createdAt: '2026-11-19T00:00:00.000Z',
|
|
673
|
-
summary: { total: 3, passed: 1, stopped: 1, untested: 1, percent: 100 },
|
|
674
|
-
options: {},
|
|
675
|
-
},
|
|
676
|
-
caseRuns: [],
|
|
677
|
-
});
|
|
678
|
-
assert.match(text, /\| 已停止 \| 1 \|/);
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
test('runner: 开始执行立即写 running case-run,结束后队列 done', async () => {
|
|
682
|
-
const { config } = makeProject();
|
|
683
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
684
|
-
const batch = createBatch(config, { name: 'running-placeholder', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
685
|
-
const runDir = path.join(config.paths.runsDir, 'placeholder-run');
|
|
686
|
-
fs.mkdirSync(path.join(runDir, 'screenshots'), { recursive: true });
|
|
687
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'placeholder-run', status: 'passed', steps: [] }));
|
|
688
|
-
fs.writeFileSync(path.join(runDir, 'screenshots', 's1.png'), 'x');
|
|
689
|
-
|
|
690
|
-
let duringRun = null;
|
|
691
|
-
const final = await runBatch({
|
|
692
|
-
config,
|
|
693
|
-
batchId: batch.id,
|
|
694
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
695
|
-
deps: quietDeps({
|
|
696
|
-
runFlowImpl: async () => {
|
|
697
|
-
duringRun = readCaseRun(config, batch.id, 'TC-LOGIN-001');
|
|
698
|
-
return passedFlowResult(runDir, 'placeholder-run');
|
|
699
|
-
},
|
|
700
|
-
}),
|
|
701
|
-
});
|
|
702
|
-
|
|
703
|
-
assert.equal(duringRun.status, 'running', '用例一开始就写 running,列表与详情同源');
|
|
704
|
-
assert.equal(duringRun.note, '正在执行');
|
|
705
|
-
assert.equal(final.status, 'done');
|
|
706
|
-
const savedQueue = readBatch(config, batch.id).queue;
|
|
707
|
-
assert.equal(savedQueue.entries['TC-LOGIN-001'].state, 'done');
|
|
708
|
-
assert.equal(savedQueue.entries['TC-LOGIN-001'].attempt, 1);
|
|
709
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'passed');
|
|
710
|
-
});
|
|
711
|
-
|
|
712
|
-
test('runner: 同批次重排不新建批次,第二次执行显示重新执行中', async () => {
|
|
713
|
-
const { config } = makeProject();
|
|
714
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
715
|
-
const batch = createBatch(config, { name: 'requeue', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
716
|
-
const runDir = path.join(config.paths.runsDir, 'requeue-flow');
|
|
717
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
718
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'requeue-flow', status: 'passed', steps: [] }));
|
|
719
|
-
|
|
720
|
-
await runBatch({
|
|
721
|
-
config,
|
|
722
|
-
batchId: batch.id,
|
|
723
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
724
|
-
deps: quietDeps({ runFlowImpl: async () => passedFlowResult(runDir, 'requeue-flow') }),
|
|
725
|
-
});
|
|
726
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'passed');
|
|
727
|
-
assert.equal(listBatches(config).length, 1);
|
|
728
|
-
|
|
729
|
-
const fresh = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'], forceAgent: true }).items[0];
|
|
730
|
-
const requeued = await requeueCase({
|
|
731
|
-
config,
|
|
732
|
-
batchId: batch.id,
|
|
733
|
-
caseId: 'TC-LOGIN-001',
|
|
734
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
735
|
-
planItem: fresh,
|
|
736
|
-
});
|
|
737
|
-
assert.equal(requeued.started, true);
|
|
738
|
-
const afterRequeue = readBatch(config, batch.id);
|
|
739
|
-
assert.equal(afterRequeue.id, batch.id, '重排使用原批次');
|
|
740
|
-
assert.equal(listBatches(config).length, 1);
|
|
741
|
-
assert.equal(afterRequeue.status, 'queued');
|
|
742
|
-
assert.equal(afterRequeue.queue.entries['TC-LOGIN-001'].state, 'queued');
|
|
743
|
-
assert.equal(afterRequeue.queue.entries['TC-LOGIN-001'].attempt, 2);
|
|
744
|
-
assert.equal(afterRequeue.plan.items[0].mode, 'agent', 'fresh plan item 覆盖原 flow 项');
|
|
745
|
-
assert.equal(afterRequeue.plan.summary.agent, 1);
|
|
746
|
-
|
|
747
|
-
const merged = mergeBatchRuns(afterRequeue, readCaseRuns(config, batch.id));
|
|
748
|
-
assert.equal(merged[0].status, 'queued');
|
|
749
|
-
assert.equal(merged[0].previousStatus, 'passed');
|
|
750
|
-
|
|
751
|
-
const again = await requeueCase({
|
|
752
|
-
config,
|
|
753
|
-
batchId: batch.id,
|
|
754
|
-
caseId: 'TC-LOGIN-001',
|
|
755
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
756
|
-
planItem: fresh,
|
|
757
|
-
});
|
|
758
|
-
assert.equal(again.alreadyActive, true);
|
|
759
|
-
|
|
760
|
-
let rerunPlaceholder = null;
|
|
761
|
-
const final = await runBatch({
|
|
762
|
-
config,
|
|
763
|
-
batchId: batch.id,
|
|
764
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
765
|
-
deps: quietDeps({
|
|
766
|
-
startAgentJobImpl: async () => {
|
|
767
|
-
rerunPlaceholder = readCaseRun(config, batch.id, 'TC-LOGIN-001');
|
|
768
|
-
return { jobId: 'job-requeue', sessionId: 'sess-requeue', runDir };
|
|
769
|
-
},
|
|
770
|
-
readAgentJobImpl: async () => ({
|
|
771
|
-
job: { status: 'done', sessionId: 'sess-requeue' },
|
|
772
|
-
hasResult: true,
|
|
773
|
-
result: { verdict: '通过', note: '重排后通过', evidence: [] },
|
|
774
|
-
session: { status: 'finished', verdict: '通过', runDir, steps: [] },
|
|
775
|
-
}),
|
|
776
|
-
}),
|
|
777
|
-
});
|
|
778
|
-
assert.equal(rerunPlaceholder.status, 'running');
|
|
779
|
-
assert.equal(rerunPlaceholder.note, '重新执行中');
|
|
780
|
-
assert.equal(final.status, 'done');
|
|
781
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'passed');
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
test('runner: 归档期间重排会继续执行,不把 queued 留在已结束批次', async () => {
|
|
785
|
-
const { config } = makeProject();
|
|
786
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
787
|
-
const batch = createBatch(config, { name: 'requeue-during-archive', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
788
|
-
const runDir = path.join(config.paths.runsDir, 'requeue-archive');
|
|
789
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
790
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'requeue-archive', status: 'passed', steps: [] }));
|
|
791
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
792
|
-
let flowCalls = 0;
|
|
793
|
-
let requeued = false;
|
|
794
|
-
const deps = quietDeps({
|
|
795
|
-
runFlowImpl: async () => {
|
|
796
|
-
flowCalls += 1;
|
|
797
|
-
return passedFlowResult(runDir, 'requeue-archive');
|
|
798
|
-
},
|
|
799
|
-
writeResultDoc: async (cfg, id) => {
|
|
800
|
-
if (!requeued) {
|
|
801
|
-
requeued = true;
|
|
802
|
-
const fresh = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] }).items[0];
|
|
803
|
-
const result = await requeueCase({ config, batchId: id, caseId: 'TC-LOGIN-001', panel: { ctx: {}, studioAborts: new Map() }, planItem: fresh });
|
|
804
|
-
assert.equal(result.started, true);
|
|
805
|
-
}
|
|
806
|
-
return { file: path.join(batchDir(cfg, id), 'result.md') };
|
|
807
|
-
},
|
|
808
|
-
});
|
|
809
|
-
|
|
810
|
-
const first = await runBatch({ config, batchId: batch.id, panel, deps });
|
|
811
|
-
assert.equal(first.status, 'queued', '收尾发现新排队时回到 queued');
|
|
812
|
-
const deadline = Date.now() + 4000;
|
|
813
|
-
let latest = readBatch(config, batch.id);
|
|
814
|
-
while (Date.now() < deadline && latest.status !== 'done') {
|
|
815
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
816
|
-
latest = readBatch(config, batch.id);
|
|
817
|
-
}
|
|
818
|
-
assert.equal(latest.status, 'done', '后台 runner 接手并执行完新排队项');
|
|
819
|
-
assert.equal(flowCalls, 2);
|
|
820
|
-
assert.equal(latest.queue.entries['TC-LOGIN-001'].attempt, 2);
|
|
821
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'passed');
|
|
822
|
-
assert.equal(listBatches(config).length, 1);
|
|
823
|
-
});
|
|
824
|
-
|
|
825
|
-
test('runner: 停止后归档期间重排会保留并拉起下一轮', async () => {
|
|
826
|
-
const { config } = makeProject();
|
|
827
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
828
|
-
const batch = createBatch(config, { name: 'requeue-after-stop', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
829
|
-
const runDir = path.join(config.paths.runsDir, 'requeue-after-stop');
|
|
830
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
831
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'requeue-after-stop', status: 'passed', steps: [] }));
|
|
832
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
833
|
-
let flowCalls = 0;
|
|
834
|
-
let requeued = false;
|
|
835
|
-
const deps = quietDeps({
|
|
836
|
-
runFlowImpl: async () => {
|
|
837
|
-
flowCalls += 1;
|
|
838
|
-
return passedFlowResult(runDir, 'requeue-after-stop');
|
|
839
|
-
},
|
|
840
|
-
writeResultDoc: async (cfg, id) => {
|
|
841
|
-
if (!requeued) {
|
|
842
|
-
requeued = true;
|
|
843
|
-
panel.studioAborts.get(id).stopped = true;
|
|
844
|
-
const fresh = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] }).items[0];
|
|
845
|
-
const result = await requeueCase({ config, batchId: id, caseId: 'TC-LOGIN-001', panel: { ctx: {}, studioAborts: new Map() }, planItem: fresh });
|
|
846
|
-
assert.equal(result.started, true);
|
|
847
|
-
}
|
|
848
|
-
return { file: path.join(batchDir(cfg, id), 'result.md') };
|
|
849
|
-
},
|
|
850
|
-
});
|
|
851
|
-
|
|
852
|
-
const first = await runBatch({ config, batchId: batch.id, panel, deps });
|
|
853
|
-
assert.equal(first.status, 'queued', '停止后仍有重排时回到 queued');
|
|
854
|
-
const deadline = Date.now() + 4000;
|
|
855
|
-
let latest = readBatch(config, batch.id);
|
|
856
|
-
while (Date.now() < deadline && latest.status !== 'done') {
|
|
857
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
858
|
-
latest = readBatch(config, batch.id);
|
|
859
|
-
}
|
|
860
|
-
assert.equal(latest.status, 'done', '后台 runner 接手重排项并执行完');
|
|
861
|
-
assert.equal(flowCalls, 2);
|
|
862
|
-
assert.equal(latest.queue.entries['TC-LOGIN-001'].attempt, 2);
|
|
863
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'passed');
|
|
864
|
-
assert.equal(listBatches(config).length, 1);
|
|
865
|
-
});
|
|
866
|
-
|
|
867
|
-
test('runner: 没有活动 runner 时停止 running 项收敛为 stopped', async () => {
|
|
868
|
-
const { config } = makeProject();
|
|
869
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
870
|
-
const batch = createBatch(config, { name: 'stale-running', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
871
|
-
const current = readBatch(config, batch.id);
|
|
872
|
-
saveBatch(config, {
|
|
873
|
-
...current,
|
|
874
|
-
queue: {
|
|
875
|
-
...current.queue,
|
|
876
|
-
entries: { ...current.queue.entries, 'TC-LOGIN-001': { ...current.queue.entries['TC-LOGIN-001'], state: 'running', startedAt: '2026-11-19T00:00:00.000Z' } },
|
|
877
|
-
},
|
|
878
|
-
});
|
|
879
|
-
saveCaseRun(config, batch.id, {
|
|
880
|
-
caseId: 'TC-LOGIN-001',
|
|
881
|
-
batchId: batch.id,
|
|
882
|
-
title: '登录成功',
|
|
883
|
-
mode: 'flow',
|
|
884
|
-
status: 'running',
|
|
885
|
-
startedAt: '2026-11-19T00:00:00.000Z',
|
|
886
|
-
endedAt: null,
|
|
887
|
-
durationMs: 0,
|
|
888
|
-
note: '正在执行',
|
|
889
|
-
steps: [],
|
|
890
|
-
evidence: [],
|
|
891
|
-
runDir: '',
|
|
892
|
-
});
|
|
893
|
-
|
|
894
|
-
const final = await requestCaseStopInBatch({ config, batchId: batch.id, caseId: 'TC-LOGIN-001', panel: { studioAborts: new Map() } });
|
|
895
|
-
assert.equal(final.queue.entries['TC-LOGIN-001'].state, 'stopped');
|
|
896
|
-
const run = readCaseRun(config, batch.id, 'TC-LOGIN-001');
|
|
897
|
-
assert.equal(run.status, 'stopped');
|
|
898
|
-
assert.equal(run.note, '已手动停止');
|
|
899
|
-
assert.equal(final.summary.stopped, 1);
|
|
900
|
-
});
|
|
901
|
-
|
|
902
|
-
test('runner: 停止排队不覆盖历史结果,停止项不写结果库', async () => {
|
|
903
|
-
const { config } = makeProject();
|
|
904
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
905
|
-
const batch = createBatch(config, { name: 'stop-queued', caseIds: plan.items.map((item) => item.caseId), plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
906
|
-
const historical = {
|
|
907
|
-
caseId: 'TC-LOGIN-001',
|
|
908
|
-
batchId: batch.id,
|
|
909
|
-
title: '登录成功',
|
|
910
|
-
mode: 'flow',
|
|
911
|
-
status: 'failed',
|
|
912
|
-
startedAt: '2026-01-01T00:00:00.000Z',
|
|
913
|
-
endedAt: '2026-01-01T00:00:01.000Z',
|
|
914
|
-
durationMs: 1000,
|
|
915
|
-
note: '历史失败',
|
|
916
|
-
steps: [{ id: 's1', action: 'goto', status: 'passed' }],
|
|
917
|
-
evidence: [{ kind: 'key', file: 'old.png' }],
|
|
918
|
-
};
|
|
919
|
-
saveCaseRun(config, batch.id, historical);
|
|
920
|
-
|
|
921
|
-
const stopped = await requestCaseStopInBatch({ config, batchId: batch.id, caseId: 'TC-LOGIN-001', panel: { studioAborts: new Map() } });
|
|
922
|
-
assert.equal(stopped.queue.entries['TC-LOGIN-001'].state, 'stopped');
|
|
923
|
-
const kept = readCaseRun(config, batch.id, 'TC-LOGIN-001');
|
|
924
|
-
assert.equal(kept.status, 'failed', '停止排队不覆盖上次结果');
|
|
925
|
-
assert.deepEqual(kept.steps, historical.steps);
|
|
926
|
-
assert.equal(stopped.summary.failed, 1);
|
|
927
|
-
assert.equal(stopped.summary.stopped, 0, '有历史结果时按上次结果计数');
|
|
928
|
-
const merged = mergeBatchRuns(stopped, readCaseRuns(config, batch.id));
|
|
929
|
-
assert.equal(merged.find((run) => run.caseId === 'TC-LOGIN-001').status, 'failed');
|
|
930
|
-
assert.equal(merged.find((run) => run.caseId === 'TC-LOGIN-001').queue.state, 'stopped');
|
|
931
|
-
|
|
932
|
-
const noHistory = await requestCaseStopInBatch({ config, batchId: batch.id, caseId: 'TC-LOGIN-002', panel: { studioAborts: new Map() } });
|
|
933
|
-
const placeholder = readCaseRun(config, batch.id, 'TC-LOGIN-002');
|
|
934
|
-
assert.equal(placeholder.status, 'stopped');
|
|
935
|
-
assert.equal(placeholder.note, '已手动停止,未执行');
|
|
936
|
-
assert.equal(noHistory.summary.stopped, 1);
|
|
937
|
-
assert.equal(noHistory.summary.failed, 1);
|
|
938
|
-
if (fs.existsSync(config.paths.resultsFile)) {
|
|
939
|
-
assert.doesNotMatch(fs.readFileSync(config.paths.resultsFile, 'utf8'), /TC-LOGIN-002/, '停止的用例不写结果库');
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
await assert.rejects(
|
|
943
|
-
() => requestCaseStopInBatch({ config, batchId: batch.id, caseId: 'TC-NOT-IN-BATCH', panel: { studioAborts: new Map() } }),
|
|
944
|
-
(error) => error.code === 'CASE_NOT_IN_BATCH',
|
|
945
|
-
);
|
|
946
|
-
await assert.rejects(
|
|
947
|
-
() => requestCasePrioritize({ config, batchId: batch.id, caseId: 'TC-LOGIN-001' }),
|
|
948
|
-
(error) => error.code === 'CASE_NOT_QUEUED',
|
|
949
|
-
);
|
|
950
|
-
});
|
|
951
|
-
|
|
952
|
-
test('runner: 优先只对 queued 生效并改变排队位置', async () => {
|
|
953
|
-
const { config } = makeProject();
|
|
954
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
955
|
-
const batch = createBatch(config, { name: 'prioritize', caseIds: plan.items.map((item) => item.caseId), plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
956
|
-
assert.equal(readBatch(config, batch.id).queue.entries['TC-LOGIN-001'].seq, 1);
|
|
957
|
-
assert.equal(readBatch(config, batch.id).queue.entries['TC-LOGIN-002'].seq, 2);
|
|
958
|
-
const prioritized = await requestCasePrioritize({ config, batchId: batch.id, caseId: 'TC-LOGIN-002' });
|
|
959
|
-
assert.equal(prioritized.queue.entries['TC-LOGIN-002'].priority, 1);
|
|
960
|
-
const merged = mergeBatchRuns(prioritized, []);
|
|
961
|
-
const item = merged.find((run) => run.caseId === 'TC-LOGIN-002');
|
|
962
|
-
assert.equal(item.queue.position, 1);
|
|
963
|
-
assert.equal(item.queue.ahead, 0);
|
|
964
|
-
assert.equal(merged.find((run) => run.caseId === 'TC-LOGIN-001').queue.ahead, 1);
|
|
965
|
-
});
|
|
966
|
-
|
|
967
|
-
test('runner: 并发 runBatch 在 claim 阶段只放行一轮', async () => {
|
|
968
|
-
const { config } = makeProject();
|
|
969
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
970
|
-
const batch = createBatch(config, { name: 'double-run', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
971
|
-
const runDir = path.join(config.paths.runsDir, 'double-run');
|
|
972
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
973
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'double-run', status: 'passed', steps: [] }));
|
|
974
|
-
let flowCalls = 0;
|
|
975
|
-
let release;
|
|
976
|
-
const gate = new Promise((resolve) => { release = resolve; });
|
|
977
|
-
let entered;
|
|
978
|
-
const enteredPromise = new Promise((resolve) => { entered = resolve; });
|
|
979
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
980
|
-
const deps = quietDeps({
|
|
981
|
-
runFlowImpl: async () => {
|
|
982
|
-
flowCalls += 1;
|
|
983
|
-
entered();
|
|
984
|
-
await gate;
|
|
985
|
-
return passedFlowResult(runDir, 'double-run');
|
|
986
|
-
},
|
|
987
|
-
});
|
|
988
|
-
const first = runBatch({ config, batchId: batch.id, panel, deps });
|
|
989
|
-
await enteredPromise;
|
|
990
|
-
const second = await runBatch({ config, batchId: batch.id, panel, deps });
|
|
991
|
-
assert.equal(second.status, 'running');
|
|
992
|
-
release();
|
|
993
|
-
const final = await first;
|
|
994
|
-
assert.equal(flowCalls, 1);
|
|
995
|
-
assert.equal(final.status, 'done');
|
|
996
|
-
});
|
|
997
|
-
|
|
998
|
-
test('runner: 认领后启动失败会清理 abort 并允许重排', async () => {
|
|
999
|
-
const { config } = makeProject();
|
|
1000
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1001
|
-
const batch = createBatch(config, { name: 'setup-fail', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1002
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
1003
|
-
const uitestDir = config.paths.uitestDir;
|
|
1004
|
-
fs.rmSync(uitestDir, { recursive: true, force: true });
|
|
1005
|
-
fs.writeFileSync(uitestDir, 'not-a-directory');
|
|
1006
|
-
|
|
1007
|
-
const failed = await runBatch({ config, batchId: batch.id, panel, deps: quietDeps({}) });
|
|
1008
|
-
|
|
1009
|
-
assert.equal(failed.status, 'failed');
|
|
1010
|
-
assert.equal(panel.studioAborts.has(batch.id), false, '启动失败后不能残留 abort');
|
|
1011
|
-
fs.rmSync(uitestDir, { force: true });
|
|
1012
|
-
const fresh = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] }).items[0];
|
|
1013
|
-
const again = await requeueCase({ config, batchId: batch.id, caseId: 'TC-LOGIN-001', panel, planItem: fresh });
|
|
1014
|
-
assert.equal(again.started, true, '失败批次仍然可以重新排队');
|
|
1015
|
-
});
|
|
1016
|
-
|
|
1017
|
-
test('runner: Agent 启动失败只写简短结论,完整报错进 technicalError', async () => {
|
|
1018
|
-
const md = [
|
|
1019
|
-
'## TC-CHROME-01 Agent 启动',
|
|
1020
|
-
'',
|
|
1021
|
-
'| 项 | 内容 |',
|
|
1022
|
-
'|---|---|',
|
|
1023
|
-
'| **前置条件** | 已登录 |',
|
|
1024
|
-
'| **操作步骤** | 打开页面 |',
|
|
1025
|
-
'| **预期结果** | 页面正常 |',
|
|
1026
|
-
].join('\n');
|
|
1027
|
-
const { config } = makeProject(md);
|
|
1028
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-CHROME-01'] });
|
|
1029
|
-
const batch = createBatch(config, { name: 'agent-launch-fail', caseIds: ['TC-CHROME-01'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1030
|
-
const raw = '{"error":"ProcessSingleton: 另一个程序正在使用此文件。 Lock file can not be created"}';
|
|
1031
|
-
const final = await runBatch({
|
|
1032
|
-
config,
|
|
1033
|
-
batchId: batch.id,
|
|
1034
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
1035
|
-
deps: quietDeps({
|
|
1036
|
-
startAgentJobImpl: async () => { throw new Error(raw); },
|
|
1037
|
-
}),
|
|
1038
|
-
});
|
|
1039
|
-
assert.equal(final.status, 'done');
|
|
1040
|
-
const run = readCaseRun(config, batch.id, 'TC-CHROME-01');
|
|
1041
|
-
assert.equal(run.status, 'blocked');
|
|
1042
|
-
assert.match(run.note, /测试浏览器 profile 被占用/);
|
|
1043
|
-
assert.doesNotMatch(run.note, /ProcessSingleton|Lock file/);
|
|
1044
|
-
assert.match(run.technicalError, /ProcessSingleton/);
|
|
1045
|
-
const live = JSON.parse(fs.readFileSync(agentLiveFile(config, batch.id, 'TC-CHROME-01'), 'utf8'));
|
|
1046
|
-
assert.equal(live.note, run.note, 'Agent 视图与结果使用同一条简短结论');
|
|
1047
|
-
assert.doesNotMatch(live.note, /ProcessSingleton|Lock file/);
|
|
1048
|
-
assert.equal(live.error, undefined, 'Agent 视图不重复整段错误');
|
|
1049
|
-
assert.equal(live.failed, true, 'Agent 视图要显示启动失败');
|
|
1050
|
-
assert.equal(live.phase, 'starting');
|
|
1051
|
-
assert.equal(live.active, false);
|
|
1052
|
-
});
|
|
1053
|
-
|
|
1054
|
-
test('runner: 浏览器包装错误保留 detail 原文给技术细节', async () => {
|
|
1055
|
-
const md = [
|
|
1056
|
-
'## TC-CHROME-02 Agent 启动包装',
|
|
1057
|
-
'',
|
|
1058
|
-
'| 项 | 内容 |',
|
|
1059
|
-
'|---|---|',
|
|
1060
|
-
'| **前置条件** | 已登录 |',
|
|
1061
|
-
'| **操作步骤** | 打开页面 |',
|
|
1062
|
-
'| **预期结果** | 页面正常 |',
|
|
1063
|
-
].join('\n');
|
|
1064
|
-
const { config } = makeProject(md);
|
|
1065
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-CHROME-02'] });
|
|
1066
|
-
const batch = createBatch(config, { name: 'agent-wrapped-fail', caseIds: ['TC-CHROME-02'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1067
|
-
const wrapped = new Error('测试浏览器 profile 被占用,浏览器没有启动。请等上一次执行结束后重试。');
|
|
1068
|
-
wrapped.code = 'BROWSER_PROFILE_LOCKED';
|
|
1069
|
-
wrapped.detail = 'ProcessSingleton: Lock file can not be created\nDevToolsActivePort missing';
|
|
1070
|
-
await runBatch({
|
|
1071
|
-
config,
|
|
1072
|
-
batchId: batch.id,
|
|
1073
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
1074
|
-
deps: quietDeps({ startAgentJobImpl: async () => { throw wrapped; } }),
|
|
1075
|
-
});
|
|
1076
|
-
const run = readCaseRun(config, batch.id, 'TC-CHROME-02');
|
|
1077
|
-
assert.match(run.note, /测试浏览器 profile 被占用/);
|
|
1078
|
-
assert.match(run.technicalError, /DevToolsActivePort missing/, 'detail 原文不能被短消息覆盖');
|
|
1079
|
-
});
|
|
1080
|
-
|
|
1081
|
-
test('runner: 浏览器准备完成后收到停止会关闭会话并按已停止收尾', async () => {
|
|
1082
|
-
const { config } = makeProject();
|
|
1083
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1084
|
-
const batch = createBatch(config, { name: 'stop-after-session', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1085
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
1086
|
-
let flowCalls = 0;
|
|
1087
|
-
let closed = false;
|
|
1088
|
-
const final = await runBatch({
|
|
1089
|
-
config,
|
|
1090
|
-
batchId: batch.id,
|
|
1091
|
-
panel,
|
|
1092
|
-
deps: quietDeps({
|
|
1093
|
-
launchSessionImpl: async () => {
|
|
1094
|
-
requestBatchStop(panel, batch.id);
|
|
1095
|
-
return { close: async () => { closed = true; } };
|
|
1096
|
-
},
|
|
1097
|
-
runFlowImpl: async () => { flowCalls += 1; return passedFlowResult(config.paths.runsDir, 'stop-after-session'); },
|
|
1098
|
-
}),
|
|
1099
|
-
});
|
|
1100
|
-
assert.equal(flowCalls, 0, '停止后不再启动 flow');
|
|
1101
|
-
assert.equal(closed, true, '刚启动的浏览器会话要关闭');
|
|
1102
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'stopped');
|
|
1103
|
-
assert.equal(final.status, 'stopped');
|
|
1104
|
-
});
|
|
1105
|
-
|
|
1106
|
-
test('runner: 批次停止会关闭执行中的 flow 会话并按已停止收尾', async () => {
|
|
1107
|
-
const { config } = makeProject();
|
|
1108
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1109
|
-
const batch = createBatch(config, { name: 'stop-in-flow', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1110
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
1111
|
-
let closed = false;
|
|
1112
|
-
const final = await runBatch({
|
|
1113
|
-
config,
|
|
1114
|
-
batchId: batch.id,
|
|
1115
|
-
panel,
|
|
1116
|
-
deps: quietDeps({
|
|
1117
|
-
launchSessionImpl: async () => ({ close: async () => { closed = true; } }),
|
|
1118
|
-
runFlowImpl: async () => {
|
|
1119
|
-
requestBatchStop(panel, batch.id);
|
|
1120
|
-
throw new Error('flow interrupted');
|
|
1121
|
-
},
|
|
1122
|
-
}),
|
|
1123
|
-
});
|
|
1124
|
-
assert.equal(closed, true, '停止批次要关闭执行中的 flow 会话');
|
|
1125
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'stopped');
|
|
1126
|
-
assert.equal(final.status, 'stopped');
|
|
1127
|
-
});
|
|
1128
|
-
|
|
1129
|
-
test('runner: flow 返回后落下的批次停止不丢已完成结果', async () => {
|
|
1130
|
-
const { config } = makeProject();
|
|
1131
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1132
|
-
const batch = createBatch(config, { name: 'stop-after-flow', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1133
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
1134
|
-
const runDir = path.join(config.paths.runsDir, 'stop-after-flow-run');
|
|
1135
|
-
fs.mkdirSync(path.join(runDir, 'screenshots'), { recursive: true });
|
|
1136
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'stop-after-flow-run', status: 'passed', steps: [] }));
|
|
1137
|
-
fs.writeFileSync(path.join(runDir, 'screenshots', 's1.png'), 'x');
|
|
1138
|
-
|
|
1139
|
-
const final = await runBatch({
|
|
1140
|
-
config,
|
|
1141
|
-
batchId: batch.id,
|
|
1142
|
-
panel,
|
|
1143
|
-
deps: quietDeps({
|
|
1144
|
-
runFlowImpl: async () => {
|
|
1145
|
-
requestBatchStop(panel, batch.id);
|
|
1146
|
-
return passedFlowResult(runDir, 'stop-after-flow-run');
|
|
1147
|
-
},
|
|
1148
|
-
}),
|
|
1149
|
-
});
|
|
1150
|
-
|
|
1151
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-001').status, 'passed', 'flow 已返回的完成结果不被停止覆盖');
|
|
1152
|
-
assert.equal(final.status, 'stopped', '批次仍按停止收尾');
|
|
1153
|
-
});
|
|
1154
|
-
|
|
1155
|
-
test('runner: Agent 作业完成后的停止不丢通过结论', async () => {
|
|
1156
|
-
const { config } = makeProject();
|
|
1157
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-002'] });
|
|
1158
|
-
const batch = createBatch(config, { name: 'agent-stop-window', caseIds: ['TC-LOGIN-002'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1159
|
-
const panel = { ctx: {}, studioAborts: new Map() };
|
|
1160
|
-
const runDir = path.join(config.paths.runsDir, 'agent-stop-window-run');
|
|
1161
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
1162
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'agent-stop-window-run', status: 'passed', steps: [] }));
|
|
1163
|
-
let reads = 0;
|
|
1164
|
-
|
|
1165
|
-
const final = await runBatch({
|
|
1166
|
-
config,
|
|
1167
|
-
batchId: batch.id,
|
|
1168
|
-
panel,
|
|
1169
|
-
deps: quietDeps({
|
|
1170
|
-
startAgentJobImpl: async () => ({ jobId: 'job-stop-window', sessionId: 'sess-stop-window', runDir }),
|
|
1171
|
-
readAgentJobImpl: async () => {
|
|
1172
|
-
reads += 1;
|
|
1173
|
-
if (reads === 1) {
|
|
1174
|
-
setTimeout(() => requestBatchStop(panel, batch.id), 0);
|
|
1175
|
-
return { job: { status: 'running', sessionId: 'sess-stop-window' }, hasResult: false, session: { status: 'running' } };
|
|
1176
|
-
}
|
|
1177
|
-
return {
|
|
1178
|
-
job: { status: 'done', sessionId: 'sess-stop-window' },
|
|
1179
|
-
hasResult: true,
|
|
1180
|
-
result: { verdict: '通过', note: 'Agent 核对通过' },
|
|
1181
|
-
session: { status: 'finished', verdict: '通过', note: 'Agent 核对通过', runDir, steps: [] },
|
|
1182
|
-
};
|
|
1183
|
-
},
|
|
1184
|
-
}),
|
|
1185
|
-
});
|
|
1186
|
-
|
|
1187
|
-
assert.equal(reads, 2);
|
|
1188
|
-
assert.equal(readCaseRun(config, batch.id, 'TC-LOGIN-002').status, 'passed', '轮询期间完成的作业仍记录通过');
|
|
1189
|
-
assert.equal(final.status, 'stopped', '批次仍按停止收尾');
|
|
1190
|
-
});
|
|
1191
|
-
|
|
1192
|
-
test('runner: 没有账号配置时浏览器启动失败按阻塞收尾', async () => {
|
|
1193
|
-
const { config } = makeProject();
|
|
1194
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1195
|
-
const batch = createBatch(config, { name: 'launch-fail-flow', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1196
|
-
let flowCalls = 0;
|
|
1197
|
-
const final = await runBatch({
|
|
1198
|
-
config,
|
|
1199
|
-
batchId: batch.id,
|
|
1200
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
1201
|
-
deps: quietDeps({
|
|
1202
|
-
launchSessionImpl: async () => { throw new Error('Auto-launch failed: Chrome exited early without writing DevToolsActivePort'); },
|
|
1203
|
-
runFlowImpl: async () => { flowCalls += 1; return passedFlowResult(config.paths.runsDir, 'launch-fail-flow'); },
|
|
1204
|
-
}),
|
|
1205
|
-
});
|
|
1206
|
-
const run = readCaseRun(config, batch.id, 'TC-LOGIN-001');
|
|
1207
|
-
assert.equal(flowCalls, 0, '浏览器没起来就不能继续跑 flow');
|
|
1208
|
-
assert.equal(run.status, 'blocked');
|
|
1209
|
-
assert.match(run.note, /浏览器准备失败/);
|
|
1210
|
-
assert.match(run.note, /DevToolsActivePort|Chrome 启动失败/);
|
|
1211
|
-
assert.match(run.technicalError, /DevToolsActivePort/);
|
|
1212
|
-
assert.equal(final.status, 'done');
|
|
1213
|
-
});
|
|
1214
|
-
|
|
1215
|
-
test('runner: 阻塞结论保留 Agent 自己的说明,作业错误只进技术细节', async () => {
|
|
1216
|
-
const { config } = makeProject();
|
|
1217
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-002'] });
|
|
1218
|
-
const batch = createBatch(config, { name: 'blocked-note', caseIds: ['TC-LOGIN-002'], plan: compactPlan(plan), options: { workers: 1, agentWorkers: 1, writeBack: 'none' } });
|
|
1219
|
-
const runDir = path.join(config.paths.runsDir, 'blocked-run');
|
|
1220
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
1221
|
-
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify({ label: 'blocked-run', status: 'blocked', steps: [] }));
|
|
1222
|
-
const raw = 'Auto-launch failed: Chrome exited early (exit code: 21) without writing DevToolsActivePort; Lock file can not be created';
|
|
1223
|
-
await runBatch({
|
|
1224
|
-
config,
|
|
1225
|
-
batchId: batch.id,
|
|
1226
|
-
panel: { ctx: {}, studioAborts: new Map() },
|
|
1227
|
-
deps: quietDeps({
|
|
1228
|
-
startAgentJobImpl: async () => ({ jobId: 'job-blocked', sessionId: 'sess-blocked', sessionName: 'uitest-agent-blocked', runDir }),
|
|
1229
|
-
readAgentJobImpl: async () => ({
|
|
1230
|
-
job: { status: 'done', sessionId: 'sess-blocked', message: raw },
|
|
1231
|
-
hasResult: true,
|
|
1232
|
-
result: { verdict: '阻塞', note: '找不到报表入口;已尝试从菜单和搜索进入,均无权限。' },
|
|
1233
|
-
session: { status: 'finished', verdict: '阻塞', runDir, steps: [] },
|
|
1234
|
-
}),
|
|
1235
|
-
}),
|
|
1236
|
-
});
|
|
1237
|
-
const run = readCaseRun(config, batch.id, 'TC-LOGIN-002');
|
|
1238
|
-
assert.equal(run.status, 'blocked');
|
|
1239
|
-
assert.equal(run.note, '找不到报表入口;已尝试从菜单和搜索进入,均无权限。');
|
|
1240
|
-
assert.match(run.technicalError, /DevToolsActivePort/);
|
|
1241
|
-
});
|
|
1242
|
-
|
|
1243
|
-
/* ------------------------------------------------------------------ */
|
|
1244
|
-
/* Host 路由:批次详情 summary、优先、停止、同批次重排 */
|
|
1245
|
-
/* ------------------------------------------------------------------ */
|
|
1246
|
-
|
|
1247
|
-
function makeHostConnection(ctxDir) {
|
|
1248
|
-
const routes = new Map();
|
|
1249
|
-
const ctx = {
|
|
1250
|
-
get(name) {
|
|
1251
|
-
if (name === 'workspaceRegistry') return { list: () => [{ path: ctxDir, title: 'test' }] };
|
|
1252
|
-
return undefined;
|
|
1253
|
-
},
|
|
1254
|
-
logger: { warn() {} },
|
|
1255
|
-
connection: {
|
|
1256
|
-
fetch: {
|
|
1257
|
-
register(spec) {
|
|
1258
|
-
routes.set(spec.path, spec);
|
|
1259
|
-
return () => {};
|
|
1260
|
-
},
|
|
1261
|
-
},
|
|
1262
|
-
},
|
|
1263
|
-
};
|
|
1264
|
-
return { ctx, routes };
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
function hostRequest(routes, pathName, { method = 'GET', body } = {}) {
|
|
1268
|
-
const pathname = pathName.split('?')[0];
|
|
1269
|
-
const route = routes.get('/api/uitest/studio' + pathname);
|
|
1270
|
-
assert.ok(route, 'route exists: ' + pathname);
|
|
1271
|
-
const init = { method, headers: {} };
|
|
1272
|
-
if (body !== undefined) {
|
|
1273
|
-
init.headers['content-type'] = 'application/json';
|
|
1274
|
-
init.body = JSON.stringify(body);
|
|
1275
|
-
}
|
|
1276
|
-
return route.fetch(new Request('http://dsh.internal/api/uitest/studio' + pathName, init));
|
|
1277
|
-
}
|
|
1278
|
-
|
|
1279
|
-
test('studio host: 批次详情按合并结果重算 summary 并返回 queue', async () => {
|
|
1280
|
-
const { dir, config } = makeProject();
|
|
1281
|
-
const { ctx, routes } = makeHostConnection(dir);
|
|
1282
|
-
const panel = createStudioPanel(ctx);
|
|
1283
|
-
registerStudioRoutes({ ctx, panel });
|
|
1284
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-002'] });
|
|
1285
|
-
const batch = createBatch(config, { name: 'summary', caseIds: ['TC-LOGIN-002'], plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
1286
|
-
saveCaseRun(config, batch.id, {
|
|
1287
|
-
caseId: 'TC-LOGIN-002',
|
|
1288
|
-
batchId: batch.id,
|
|
1289
|
-
title: '错误密码',
|
|
1290
|
-
mode: 'agent',
|
|
1291
|
-
status: 'failed',
|
|
1292
|
-
startedAt: new Date(Date.now() + 1000).toISOString(),
|
|
1293
|
-
endedAt: new Date(Date.now() + 1000).toISOString(),
|
|
1294
|
-
durationMs: 10,
|
|
1295
|
-
note: '失败',
|
|
1296
|
-
steps: [],
|
|
1297
|
-
evidence: [],
|
|
1298
|
-
});
|
|
1299
|
-
await patchBatch(config, batch.id, { summary: { total: 1, passed: 1, done: 1, percent: 100 } });
|
|
1300
|
-
const detail = await (await hostRequest(routes, '/batch?project=' + encodeURIComponent(dir) + '&id=' + batch.id)).json();
|
|
1301
|
-
assert.equal(detail.ok, true);
|
|
1302
|
-
assert.equal(detail.batch.summary.failed, 1, 'summary 由 merged runs 重算');
|
|
1303
|
-
assert.equal(detail.batch.summary.passed, 0);
|
|
1304
|
-
assert.ok(detail.batch.queue, '批次详情返回 queue');
|
|
1305
|
-
assert.equal(detail.batch.cases[0].status, 'failed');
|
|
1306
|
-
assert.equal(detail.batch.cases[0].queue.state, 'done');
|
|
1307
|
-
});
|
|
1308
|
-
|
|
1309
|
-
test('studio host: 优先、停止排队与同批次重排路由', async () => {
|
|
1310
|
-
const { dir, config } = makeProject();
|
|
1311
|
-
const { ctx, routes } = makeHostConnection(dir);
|
|
1312
|
-
const panel = createStudioPanel(ctx);
|
|
1313
|
-
registerStudioRoutes({ ctx, panel });
|
|
1314
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
1315
|
-
const batch = createBatch(config, { name: 'routes', caseIds: plan.items.map((item) => item.caseId), plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
1316
|
-
// 模拟 runner 正在执行:让重排只改队列,不后台启动 runBatch。
|
|
1317
|
-
panel.studioAborts.set(batch.id, { stopped: false, caseStops: new Map(), flowSessions: new Map() });
|
|
1318
|
-
|
|
1319
|
-
const prioritized = await (await hostRequest(routes, '/batch/case/prioritize', {
|
|
1320
|
-
method: 'POST',
|
|
1321
|
-
body: { project: dir, id: batch.id, caseId: 'TC-LOGIN-002' },
|
|
1322
|
-
})).json();
|
|
1323
|
-
assert.equal(prioritized.ok, true);
|
|
1324
|
-
const prioritizedItem = prioritized.batch.cases.find((item) => item.caseId === 'TC-LOGIN-002');
|
|
1325
|
-
assert.equal(prioritizedItem.queue.position, 1);
|
|
1326
|
-
assert.equal(prioritizedItem.queue.ahead, 0);
|
|
1327
|
-
|
|
1328
|
-
const stopped = await (await hostRequest(routes, '/batch/case/stop', {
|
|
1329
|
-
method: 'POST',
|
|
1330
|
-
body: { project: dir, id: batch.id, caseId: 'TC-LOGIN-001' },
|
|
1331
|
-
})).json();
|
|
1332
|
-
assert.equal(stopped.ok, true);
|
|
1333
|
-
assert.equal(stopped.batch.cases.find((item) => item.caseId === 'TC-LOGIN-001').queue.state, 'stopped');
|
|
1334
|
-
|
|
1335
|
-
const missing = await hostRequest(routes, '/batch/case/stop', {
|
|
1336
|
-
method: 'POST',
|
|
1337
|
-
body: { project: dir, id: batch.id, caseId: 'TC-NOPE' },
|
|
1338
|
-
});
|
|
1339
|
-
assert.equal(missing.status, 400);
|
|
1340
|
-
assert.equal((await missing.json()).error.code, 'CASE_NOT_IN_BATCH');
|
|
1341
|
-
|
|
1342
|
-
const notQueued = await hostRequest(routes, '/batch/case/prioritize', {
|
|
1343
|
-
method: 'POST',
|
|
1344
|
-
body: { project: dir, id: batch.id, caseId: 'TC-LOGIN-001' },
|
|
1345
|
-
});
|
|
1346
|
-
assert.equal(notQueued.status, 409);
|
|
1347
|
-
assert.equal((await notQueued.json()).error.code, 'CASE_NOT_QUEUED');
|
|
1348
|
-
|
|
1349
|
-
const requeued = await (await hostRequest(routes, '/case/run', {
|
|
1350
|
-
method: 'POST',
|
|
1351
|
-
body: { project: dir, id: 'TC-LOGIN-001', batchId: batch.id },
|
|
1352
|
-
})).json();
|
|
1353
|
-
assert.equal(requeued.ok, true);
|
|
1354
|
-
assert.equal(requeued.requeued, true);
|
|
1355
|
-
assert.equal(requeued.batch.id, batch.id);
|
|
1356
|
-
assert.equal(requeued.batch.queue.entries['TC-LOGIN-001'].state, 'queued');
|
|
1357
|
-
assert.equal(requeued.batch.queue.entries['TC-LOGIN-001'].attempt, 2);
|
|
1358
|
-
assert.equal(listBatches(config).length, 1, '同批次重排不创建新批次');
|
|
1359
|
-
|
|
1360
|
-
const active = await (await hostRequest(routes, '/case/run', {
|
|
1361
|
-
method: 'POST',
|
|
1362
|
-
body: { project: dir, id: 'TC-LOGIN-001', batchId: batch.id },
|
|
1363
|
-
})).json();
|
|
1364
|
-
assert.equal(active.ok, true);
|
|
1365
|
-
assert.equal(active.requeued, false);
|
|
1366
|
-
assert.equal(active.alreadyActive, true);
|
|
1367
|
-
assert.equal(listBatches(config).length, 1);
|
|
1368
|
-
});
|
|
1369
|
-
|
|
1370
|
-
test('studio host: 无活动 runner 时停止批次收敛为 stopped', async () => {
|
|
1371
|
-
const { dir, config } = makeProject();
|
|
1372
|
-
const { ctx, routes } = makeHostConnection(dir);
|
|
1373
|
-
const panel = createStudioPanel(ctx);
|
|
1374
|
-
registerStudioRoutes({ ctx, panel });
|
|
1375
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
1376
|
-
const batch = createBatch(config, { name: 'stop-inactive', caseIds: plan.items.map((item) => item.caseId), plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
1377
|
-
|
|
1378
|
-
const payload = await (await hostRequest(routes, '/batch/stop', {
|
|
1379
|
-
method: 'POST',
|
|
1380
|
-
body: { project: dir, id: batch.id },
|
|
1381
|
-
})).json();
|
|
1382
|
-
assert.equal(payload.ok, true);
|
|
1383
|
-
assert.equal(payload.batch.status, 'stopped');
|
|
1384
|
-
assert.ok(payload.batch.cases.every((item) => item.queue?.state === 'stopped'), '剩余排队项都标记停止');
|
|
1385
|
-
const saved = readBatch(config, batch.id);
|
|
1386
|
-
assert.equal(saved.status, 'stopped');
|
|
1387
|
-
assert.equal(saved.stopRequested, false);
|
|
1388
|
-
assert.ok(saved.endedAt, '停止时补结束时间');
|
|
1389
|
-
assert.equal(saved.summary.stopped, payload.batch.cases.length, '停止后的汇总落盘,列表不会继续显示排队中');
|
|
1390
|
-
assert.equal(saved.summary.queued, 0);
|
|
1391
|
-
assert.equal(payload.batch.stages.execute.status, 'stopped', '详情步进器停在已停止');
|
|
1392
|
-
assert.equal(saved.stages.execute.status, 'stopped');
|
|
1393
|
-
});
|
|
1394
|
-
|
|
1395
|
-
test('studio host: 有活动批次时拒绝新建批次', async () => {
|
|
1396
|
-
const { dir, config } = makeProject();
|
|
1397
|
-
const { ctx, routes } = makeHostConnection(dir);
|
|
1398
|
-
const panel = createStudioPanel(ctx);
|
|
1399
|
-
registerStudioRoutes({ ctx, panel });
|
|
1400
|
-
const plan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1401
|
-
const active = createBatch(config, { name: '活动批次', caseIds: ['TC-LOGIN-001'], plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
1402
|
-
await patchBatch(config, active.id, { status: 'running' });
|
|
1403
|
-
|
|
1404
|
-
const response = await hostRequest(routes, '/batch', {
|
|
1405
|
-
method: 'POST',
|
|
1406
|
-
body: { project: dir, caseIds: ['TC-LOGIN-002'] },
|
|
1407
|
-
});
|
|
1408
|
-
assert.equal(response.status, 409);
|
|
1409
|
-
const payload = await response.json();
|
|
1410
|
-
assert.equal(payload.ok, false);
|
|
1411
|
-
assert.equal(payload.error.code, 'BATCH_ACTIVE');
|
|
1412
|
-
assert.equal(payload.error.batchId, active.id);
|
|
1413
|
-
assert.equal(listBatches(config).length, 1, '没有创建第二个批次');
|
|
1414
|
-
|
|
1415
|
-
const single = await hostRequest(routes, '/case/run', {
|
|
1416
|
-
method: 'POST',
|
|
1417
|
-
body: { project: dir, id: 'TC-LOGIN-002' },
|
|
1418
|
-
});
|
|
1419
|
-
assert.equal(single.status, 409);
|
|
1420
|
-
assert.equal((await single.json()).error.code, 'BATCH_ACTIVE');
|
|
1421
|
-
});
|
|
1422
|
-
|
|
1423
|
-
test('studio host: 有其它活动批次时拒绝重排已结束批次', async () => {
|
|
1424
|
-
const { dir, config } = makeProject();
|
|
1425
|
-
const { ctx, routes } = makeHostConnection(dir);
|
|
1426
|
-
const panel = createStudioPanel(ctx);
|
|
1427
|
-
registerStudioRoutes({ ctx, panel });
|
|
1428
|
-
const donePlan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-001'] });
|
|
1429
|
-
const doneBatch = createBatch(config, { name: '已结束批次', caseIds: ['TC-LOGIN-001'], plan: compactPlan(donePlan), options: { writeBack: 'none' } });
|
|
1430
|
-
await patchBatch(config, doneBatch.id, { status: 'done' });
|
|
1431
|
-
const runningPlan = buildStudioPlan(config, { caseIds: ['TC-LOGIN-002'] });
|
|
1432
|
-
const runningBatch = createBatch(config, { name: '活动批次', caseIds: ['TC-LOGIN-002'], plan: compactPlan(runningPlan), options: { writeBack: 'none' } });
|
|
1433
|
-
await patchBatch(config, runningBatch.id, { status: 'running' });
|
|
1434
|
-
|
|
1435
|
-
const response = await hostRequest(routes, '/case/run', {
|
|
1436
|
-
method: 'POST',
|
|
1437
|
-
body: { project: dir, id: 'TC-LOGIN-001', batchId: doneBatch.id },
|
|
1438
|
-
});
|
|
1439
|
-
assert.equal(response.status, 409);
|
|
1440
|
-
const payload = await response.json();
|
|
1441
|
-
assert.equal(payload.error.code, 'BATCH_ACTIVE');
|
|
1442
|
-
assert.equal(payload.error.batchId, runningBatch.id);
|
|
1443
|
-
assert.equal(readBatch(config, doneBatch.id).status, 'done', '已结束批次没有被重新拉起');
|
|
1444
|
-
assert.equal(listBatches(config).length, 2, '没有创建新批次');
|
|
1445
|
-
});
|
|
1446
|
-
|
|
1447
|
-
test('studio host: 停止落在 runner 认领之后时补发停止信号', async () => {
|
|
1448
|
-
const { dir, config } = makeProject();
|
|
1449
|
-
const { ctx } = makeHostConnection(dir);
|
|
1450
|
-
const panel = createStudioPanel(ctx);
|
|
1451
|
-
const plan = buildStudioPlan(config, { caseIds: [] });
|
|
1452
|
-
const batch = createBatch(config, { name: 'stop-race', caseIds: plan.items.map((item) => item.caseId), plan: compactPlan(plan), options: { writeBack: 'none' } });
|
|
1453
|
-
const abort = { stopped: false };
|
|
1454
|
-
panel.studioAborts.set(batch.id, abort);
|
|
1455
|
-
|
|
1456
|
-
await stopInactiveBatch(config, batch.id, panel);
|
|
1457
|
-
|
|
1458
|
-
assert.equal(abort.stopped, true, 'runner 在停止期间登记时收到停止信号');
|
|
1459
|
-
const saved = readBatch(config, batch.id);
|
|
1460
|
-
assert.equal(saved.status, 'stopped');
|
|
1461
|
-
assert.equal(saved.stopRequested, true);
|
|
1462
|
-
});
|