@midscene/test 0.0.0 → 1.10.4-beta-20260715034325.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/LICENSE +21 -0
- package/bin/midscene-workflow +3 -0
- package/dist/es/cli/index.mjs +1118 -0
- package/dist/es/cli/index.mjs.map +1 -0
- package/dist/es/index.mjs +843 -0
- package/dist/es/index.mjs.map +1 -0
- package/dist/es/midscene/index.mjs +198 -0
- package/dist/es/midscene/index.mjs.map +1 -0
- package/dist/es/workflow-cli.mjs +1160 -0
- package/dist/es/workflow-cli.mjs.map +1 -0
- package/dist/lib/cli/index.js +1170 -0
- package/dist/lib/cli/index.js.map +1 -0
- package/dist/lib/index.js +952 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/midscene/index.js +232 -0
- package/dist/lib/midscene/index.js.map +1 -0
- package/dist/lib/workflow-cli.js +1166 -0
- package/dist/lib/workflow-cli.js.map +1 -0
- package/dist/types/cli/index.d.ts +282 -0
- package/dist/types/index.d.ts +425 -0
- package/dist/types/midscene/index.d.ts +175 -0
- package/dist/types/workflow-cli.d.ts +1 -0
- package/package.json +60 -8
- package/README.md +0 -3
- package/index.js +0 -3
|
@@ -0,0 +1,1118 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { globSync } from "tinyglobby";
|
|
5
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
6
|
+
import { JSON_SCHEMA, load } from "js-yaml";
|
|
7
|
+
function _define_property(obj, key, value) {
|
|
8
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
9
|
+
value: value,
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true
|
|
13
|
+
});
|
|
14
|
+
else obj[key] = value;
|
|
15
|
+
return obj;
|
|
16
|
+
}
|
|
17
|
+
class WorkflowError extends Error {
|
|
18
|
+
toJSON() {
|
|
19
|
+
return {
|
|
20
|
+
name: this.name,
|
|
21
|
+
message: this.message,
|
|
22
|
+
code: this.code,
|
|
23
|
+
...void 0 === this.details ? {} : {
|
|
24
|
+
details: this.details
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
constructor(message, options = {}){
|
|
29
|
+
super(message, {
|
|
30
|
+
cause: options.cause
|
|
31
|
+
}), _define_property(this, "code", void 0), _define_property(this, "details", void 0);
|
|
32
|
+
this.name = new.target.name;
|
|
33
|
+
this.code = options.code ?? 'WORKFLOW_ERROR';
|
|
34
|
+
this.details = options.details;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class WorkflowParseError extends WorkflowError {
|
|
38
|
+
constructor(message, details, cause){
|
|
39
|
+
super(message, {
|
|
40
|
+
code: 'WORKFLOW_PARSE_ERROR',
|
|
41
|
+
details,
|
|
42
|
+
cause
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
class NodeDefinitionError extends WorkflowError {
|
|
47
|
+
constructor(message, details){
|
|
48
|
+
super(message, {
|
|
49
|
+
code: 'NODE_DEFINITION_ERROR',
|
|
50
|
+
details
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
class DuplicateNodeError extends WorkflowError {
|
|
55
|
+
constructor(node){
|
|
56
|
+
super(`Node "${node}" is already registered.`, {
|
|
57
|
+
code: 'DUPLICATE_NODE',
|
|
58
|
+
details: {
|
|
59
|
+
node
|
|
60
|
+
}
|
|
61
|
+
}), _define_property(this, "node", void 0);
|
|
62
|
+
this.node = node;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
class NodeNotFoundError extends WorkflowError {
|
|
66
|
+
constructor(node){
|
|
67
|
+
super(`Node "${node}" is not registered.`, {
|
|
68
|
+
code: 'NODE_NOT_FOUND',
|
|
69
|
+
details: {
|
|
70
|
+
node
|
|
71
|
+
}
|
|
72
|
+
}), _define_property(this, "node", void 0);
|
|
73
|
+
this.node = node;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class StepTimeoutError extends WorkflowError {
|
|
77
|
+
constructor(timeoutMs, node){
|
|
78
|
+
super(node ? `Node "${node}" timed out after ${timeoutMs}ms.` : `Step timed out after ${timeoutMs}ms.`, {
|
|
79
|
+
code: 'STEP_TIMEOUT',
|
|
80
|
+
details: {
|
|
81
|
+
timeoutMs,
|
|
82
|
+
...void 0 === node ? {} : {
|
|
83
|
+
node
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}), _define_property(this, "timeoutMs", void 0), _define_property(this, "node", void 0);
|
|
87
|
+
this.timeoutMs = timeoutMs;
|
|
88
|
+
this.node = node;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
class NodeExecutionError extends WorkflowError {
|
|
92
|
+
constructor(node, cause){
|
|
93
|
+
const causeMessage = cause instanceof Error ? cause.message : String(cause ?? 'Unknown error');
|
|
94
|
+
super(`Node "${node}" failed: ${causeMessage}`, {
|
|
95
|
+
code: 'NODE_EXECUTION_ERROR',
|
|
96
|
+
details: {
|
|
97
|
+
node
|
|
98
|
+
},
|
|
99
|
+
cause
|
|
100
|
+
}), _define_property(this, "node", void 0);
|
|
101
|
+
this.node = node;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
class WorkflowDocumentSetupError extends WorkflowError {
|
|
105
|
+
constructor(cause, details){
|
|
106
|
+
const causeMessage = cause instanceof Error ? cause.message : String(cause ?? 'Unknown error');
|
|
107
|
+
super(`Workflow document setup failed: ${causeMessage}`, {
|
|
108
|
+
code: 'WORKFLOW_DOCUMENT_SETUP_ERROR',
|
|
109
|
+
details,
|
|
110
|
+
cause
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
class WorkflowDocumentTeardownError extends WorkflowError {
|
|
115
|
+
constructor(cause, details){
|
|
116
|
+
const causeMessage = cause instanceof Error ? cause.message : String(cause ?? 'Unknown error');
|
|
117
|
+
super(`Workflow document teardown failed: ${causeMessage}`, {
|
|
118
|
+
code: 'WORKFLOW_DOCUMENT_TEARDOWN_ERROR',
|
|
119
|
+
details,
|
|
120
|
+
cause
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
class WorkflowLifecycleError extends WorkflowError {
|
|
125
|
+
constructor(message, details){
|
|
126
|
+
super(message, {
|
|
127
|
+
code: 'WORKFLOW_LIFECYCLE_ERROR',
|
|
128
|
+
details
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function normalizeNodeExecutionError(error, node) {
|
|
133
|
+
return error instanceof WorkflowError ? error : new NodeExecutionError(node, error);
|
|
134
|
+
}
|
|
135
|
+
const validateDefinition = (options)=>{
|
|
136
|
+
if (!options || 'object' != typeof options) throw new NodeDefinitionError('Node definition must be an object.');
|
|
137
|
+
if ('string' != typeof options.name || 0 === options.name.trim().length) throw new NodeDefinitionError('Node name must be a non-empty string.');
|
|
138
|
+
if ('function' != typeof options.execute) throw new NodeDefinitionError(`Node "${options.name}" must provide an execute function.`, {
|
|
139
|
+
node: options.name
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
function defineNode(options) {
|
|
143
|
+
validateDefinition(options);
|
|
144
|
+
return options;
|
|
145
|
+
}
|
|
146
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
147
|
+
if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
148
|
+
}
|
|
149
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
150
|
+
if (descriptor.get) return descriptor.get.call(receiver);
|
|
151
|
+
return descriptor.value;
|
|
152
|
+
}
|
|
153
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
154
|
+
if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
155
|
+
return privateMap.get(receiver);
|
|
156
|
+
}
|
|
157
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
158
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
159
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
160
|
+
}
|
|
161
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
162
|
+
_check_private_redeclaration(obj, privateMap);
|
|
163
|
+
privateMap.set(obj, value);
|
|
164
|
+
}
|
|
165
|
+
var _nodes = /*#__PURE__*/ new WeakMap();
|
|
166
|
+
class NodeRegistry {
|
|
167
|
+
register(node) {
|
|
168
|
+
defineNode(node);
|
|
169
|
+
if (_class_private_field_get(this, _nodes).has(node.name)) throw new DuplicateNodeError(node.name);
|
|
170
|
+
_class_private_field_get(this, _nodes).set(node.name, node);
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
get(name) {
|
|
174
|
+
return _class_private_field_get(this, _nodes).get(name);
|
|
175
|
+
}
|
|
176
|
+
require(name) {
|
|
177
|
+
const node = this.get(name);
|
|
178
|
+
if (!node) throw new NodeNotFoundError(name);
|
|
179
|
+
return node;
|
|
180
|
+
}
|
|
181
|
+
has(name) {
|
|
182
|
+
return _class_private_field_get(this, _nodes).has(name);
|
|
183
|
+
}
|
|
184
|
+
names() {
|
|
185
|
+
return [
|
|
186
|
+
..._class_private_field_get(this, _nodes).keys()
|
|
187
|
+
];
|
|
188
|
+
}
|
|
189
|
+
constructor(nodes = []){
|
|
190
|
+
_class_private_field_init(this, _nodes, {
|
|
191
|
+
writable: true,
|
|
192
|
+
value: new Map()
|
|
193
|
+
});
|
|
194
|
+
for (const node of nodes)this.register(node);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const defineWorkflowProject = (definition)=>definition;
|
|
198
|
+
const unwrapProjectDefinition = (loaded)=>{
|
|
199
|
+
if ('object' == typeof loaded && null !== loaded && 'default' in loaded) return loaded.default;
|
|
200
|
+
return loaded;
|
|
201
|
+
};
|
|
202
|
+
const isAbsolutePattern = (pattern)=>isAbsolute(pattern) || /^[A-Za-z]:[\\/]/.test(pattern) || pattern.startsWith('\\\\');
|
|
203
|
+
const validatePatterns = (value, field)=>{
|
|
204
|
+
if (!Array.isArray(value)) throw new TypeError(`Workflow config files.${field} must be an array.`);
|
|
205
|
+
if ('include' === field && 0 === value.length) throw new TypeError('Workflow config files.include must be a non-empty array.');
|
|
206
|
+
return value.map((pattern, index)=>{
|
|
207
|
+
if ('string' != typeof pattern || 0 === pattern.trim().length) throw new TypeError(`Workflow config files.${field}[${index}] must be a non-empty string.`);
|
|
208
|
+
if (isAbsolutePattern(pattern)) throw new TypeError(`Workflow config files.${field}[${index}] must be relative to the project root.`);
|
|
209
|
+
if (pattern.split(/[\\/]/).includes('..')) throw new TypeError(`Workflow config files.${field}[${index}] must not contain a ".." path segment.`);
|
|
210
|
+
if (pattern.includes('\\')) throw new TypeError(`Workflow config files.${field}[${index}] must use POSIX path separators (/).`);
|
|
211
|
+
if (pattern.startsWith('!')) {
|
|
212
|
+
if ('include' === field) throw new TypeError(`Workflow config files.include[${index}] must not be a negated pattern. Use files.exclude instead.`);
|
|
213
|
+
throw new TypeError(`Workflow config files.exclude[${index}] must not be a negated pattern.`);
|
|
214
|
+
}
|
|
215
|
+
return pattern;
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
const validateWorkflowFileSelection = (value)=>{
|
|
219
|
+
if (void 0 === value) return;
|
|
220
|
+
if ('object' != typeof value || null === value || Array.isArray(value)) throw new TypeError('Workflow config files must be an object.');
|
|
221
|
+
const files = value;
|
|
222
|
+
const include = validatePatterns(files.include, 'include');
|
|
223
|
+
const exclude = void 0 === files.exclude ? void 0 : validatePatterns(files.exclude, 'exclude');
|
|
224
|
+
return {
|
|
225
|
+
include,
|
|
226
|
+
...exclude ? {
|
|
227
|
+
exclude
|
|
228
|
+
} : {}
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
function loadWorkflowProjectSync(configPath) {
|
|
232
|
+
let definition = {
|
|
233
|
+
nodes: []
|
|
234
|
+
};
|
|
235
|
+
if (configPath) {
|
|
236
|
+
const absolutePath = resolve(configPath);
|
|
237
|
+
try {
|
|
238
|
+
definition = unwrapProjectDefinition(createRequire(absolutePath)(absolutePath));
|
|
239
|
+
} catch (error) {
|
|
240
|
+
throw new Error(`Failed to load workflow config "${absolutePath}". Use a CommonJS config (.cjs).`, {
|
|
241
|
+
cause: error
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if ('object' != typeof definition || null === definition || !('nodes' in definition) || !Array.isArray(definition.nodes)) throw new TypeError('Workflow config must export an object with a nodes array.');
|
|
246
|
+
const projectDefinition = definition;
|
|
247
|
+
if ('setupWorkflow' in definition) throw new TypeError('Workflow config setupWorkflow is no longer supported. Use setupDocument instead.');
|
|
248
|
+
if (void 0 !== projectDefinition.root && ('string' != typeof projectDefinition.root || 0 === projectDefinition.root.trim().length)) throw new TypeError('Workflow config root must be a non-empty string.');
|
|
249
|
+
const files = validateWorkflowFileSelection(projectDefinition.files);
|
|
250
|
+
if (void 0 !== projectDefinition.setupDocument && 'function' != typeof projectDefinition.setupDocument) throw new TypeError('Workflow config setupDocument must be a function.');
|
|
251
|
+
const nodes = new NodeRegistry(projectDefinition.nodes);
|
|
252
|
+
return {
|
|
253
|
+
...projectDefinition.root ? {
|
|
254
|
+
root: projectDefinition.root
|
|
255
|
+
} : {},
|
|
256
|
+
...files ? {
|
|
257
|
+
files
|
|
258
|
+
} : {},
|
|
259
|
+
nodes,
|
|
260
|
+
...projectDefinition.setupDocument ? {
|
|
261
|
+
setupDocument: projectDefinition.setupDocument
|
|
262
|
+
} : {},
|
|
263
|
+
resolveNode: (name)=>nodes.get(name)
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
const supportedMetaKeys = new Set([
|
|
267
|
+
'timeout',
|
|
268
|
+
'continue-on-error'
|
|
269
|
+
]);
|
|
270
|
+
function isMapping(value) {
|
|
271
|
+
return 'object' == typeof value && null !== value && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || null === Object.getPrototypeOf(value));
|
|
272
|
+
}
|
|
273
|
+
function formatStep(index) {
|
|
274
|
+
return `step ${index + 1}`;
|
|
275
|
+
}
|
|
276
|
+
function normalizeMeta(value, index) {
|
|
277
|
+
if (void 0 === value) return {
|
|
278
|
+
continueOnError: false
|
|
279
|
+
};
|
|
280
|
+
if (!isMapping(value)) throw new WorkflowParseError(`${formatStep(index)} "$" must be a mapping.`, {
|
|
281
|
+
index
|
|
282
|
+
});
|
|
283
|
+
for (const key of Object.keys(value))if (!supportedMetaKeys.has(key)) throw new WorkflowParseError(`${formatStep(index)} has unsupported engine metadata "${key}".`, {
|
|
284
|
+
index,
|
|
285
|
+
key
|
|
286
|
+
});
|
|
287
|
+
const timeout = value.timeout;
|
|
288
|
+
if (void 0 !== timeout && ('number' != typeof timeout || !Number.isFinite(timeout) || timeout <= 0)) throw new WorkflowParseError(`${formatStep(index)} "$.timeout" must be a positive number of milliseconds.`, {
|
|
289
|
+
index,
|
|
290
|
+
timeout
|
|
291
|
+
});
|
|
292
|
+
const continueOnError = value['continue-on-error'];
|
|
293
|
+
if (void 0 !== continueOnError && 'boolean' != typeof continueOnError) throw new WorkflowParseError(`${formatStep(index)} "$.continue-on-error" must be a boolean.`, {
|
|
294
|
+
index,
|
|
295
|
+
continueOnError
|
|
296
|
+
});
|
|
297
|
+
return {
|
|
298
|
+
...void 0 === timeout ? {} : {
|
|
299
|
+
timeoutMs: timeout
|
|
300
|
+
},
|
|
301
|
+
continueOnError: continueOnError ?? false
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function validateCommonNodeInput(input, index) {
|
|
305
|
+
if (void 0 !== input.prompt && 'string' != typeof input.prompt) throw new WorkflowParseError(`${formatStep(index)} "prompt" must be a string.`, {
|
|
306
|
+
index,
|
|
307
|
+
prompt: input.prompt
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
function normalizeStep(value, index = 0) {
|
|
311
|
+
if (!isMapping(value)) throw new WorkflowParseError(`${formatStep(index)} must be a mapping.`, {
|
|
312
|
+
index
|
|
313
|
+
});
|
|
314
|
+
const entries = Object.entries(value);
|
|
315
|
+
if (1 !== entries.length) throw new WorkflowParseError(`${formatStep(index)} must contain exactly one node.`, {
|
|
316
|
+
index,
|
|
317
|
+
nodes: Object.keys(value)
|
|
318
|
+
});
|
|
319
|
+
const [node, rawValue] = entries[0];
|
|
320
|
+
if (0 === node.trim().length) throw new WorkflowParseError(`${formatStep(index)} node name must not be empty.`, {
|
|
321
|
+
index
|
|
322
|
+
});
|
|
323
|
+
if ('string' == typeof rawValue) return {
|
|
324
|
+
node,
|
|
325
|
+
input: {
|
|
326
|
+
prompt: rawValue
|
|
327
|
+
},
|
|
328
|
+
meta: {
|
|
329
|
+
continueOnError: false
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
if (!isMapping(rawValue)) throw new WorkflowParseError(`${formatStep(index)} value must be a string shorthand or a mapping.`, {
|
|
333
|
+
index,
|
|
334
|
+
node
|
|
335
|
+
});
|
|
336
|
+
const { $: rawMeta, ...input } = rawValue;
|
|
337
|
+
validateCommonNodeInput(input, index);
|
|
338
|
+
return {
|
|
339
|
+
node,
|
|
340
|
+
input,
|
|
341
|
+
meta: normalizeMeta(rawMeta, index)
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function normalizeSteps(steps) {
|
|
345
|
+
if (!Array.isArray(steps)) throw new WorkflowParseError('Steps must be an array.');
|
|
346
|
+
return steps.map((step, index)=>normalizeStep(step, index));
|
|
347
|
+
}
|
|
348
|
+
function validateNodeOutput(output, node) {
|
|
349
|
+
if (void 0 === output) return;
|
|
350
|
+
if ('object' != typeof output || null === output || Array.isArray(output)) throw new NodeExecutionError(node, new TypeError('Node output must be an object or undefined.'));
|
|
351
|
+
if ('summary' in output && void 0 !== output.summary && 'string' != typeof output.summary) throw new NodeExecutionError(node, new TypeError('Node output summary must be a string.'));
|
|
352
|
+
return output;
|
|
353
|
+
}
|
|
354
|
+
function createStepResultBase(step, startedAt, phase, stepIndex) {
|
|
355
|
+
const endedAt = new Date();
|
|
356
|
+
return {
|
|
357
|
+
node: step.node,
|
|
358
|
+
phase,
|
|
359
|
+
stepIndex,
|
|
360
|
+
input: step.input,
|
|
361
|
+
meta: step.meta,
|
|
362
|
+
startedAt: startedAt.toISOString(),
|
|
363
|
+
endedAt: endedAt.toISOString(),
|
|
364
|
+
durationMs: Math.max(0, endedAt.getTime() - startedAt.getTime())
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
async function executeNode(step, execute, phase, stepIndex) {
|
|
368
|
+
validateCommonNodeInput(step.input, stepIndex);
|
|
369
|
+
const startedAt = new Date();
|
|
370
|
+
const abortController = new AbortController();
|
|
371
|
+
const timeoutMs = step.meta.timeoutMs;
|
|
372
|
+
let timeout;
|
|
373
|
+
let timeoutError;
|
|
374
|
+
const execution = Promise.resolve().then(()=>execute(abortController.signal));
|
|
375
|
+
const timedExecution = void 0 === timeoutMs ? execution : Promise.race([
|
|
376
|
+
execution,
|
|
377
|
+
new Promise((_, reject)=>{
|
|
378
|
+
timeout = setTimeout(()=>{
|
|
379
|
+
timeoutError = new StepTimeoutError(timeoutMs, step.node);
|
|
380
|
+
abortController.abort(timeoutError);
|
|
381
|
+
reject(timeoutError);
|
|
382
|
+
}, timeoutMs);
|
|
383
|
+
})
|
|
384
|
+
]);
|
|
385
|
+
try {
|
|
386
|
+
const output = validateNodeOutput(await timedExecution, step.node);
|
|
387
|
+
return {
|
|
388
|
+
...createStepResultBase(step, startedAt, phase, stepIndex),
|
|
389
|
+
status: 'success',
|
|
390
|
+
continuedAfterError: false,
|
|
391
|
+
...void 0 === output ? {} : {
|
|
392
|
+
output
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
} catch (error) {
|
|
396
|
+
return {
|
|
397
|
+
...createStepResultBase(step, startedAt, phase, stepIndex),
|
|
398
|
+
status: 'failed',
|
|
399
|
+
continuedAfterError: step.meta.continueOnError,
|
|
400
|
+
error: normalizeNodeExecutionError(timeoutError ?? error, step.node)
|
|
401
|
+
};
|
|
402
|
+
} finally{
|
|
403
|
+
if (void 0 !== timeout) clearTimeout(timeout);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
async function executeStep(step, node, target, context) {
|
|
407
|
+
const phase = 'case' === target.scope ? target.case.phase : target.document.phase;
|
|
408
|
+
const stepIndex = 'case' === target.scope ? target.case.stepIndex : target.document.stepIndex;
|
|
409
|
+
return executeNode(step, (signal)=>{
|
|
410
|
+
const common = {
|
|
411
|
+
input: step.input,
|
|
412
|
+
$: step.meta,
|
|
413
|
+
signal,
|
|
414
|
+
context
|
|
415
|
+
};
|
|
416
|
+
return 'case' === target.scope ? node.execute({
|
|
417
|
+
...common,
|
|
418
|
+
scope: 'case',
|
|
419
|
+
case: target.case
|
|
420
|
+
}) : node.execute({
|
|
421
|
+
...common,
|
|
422
|
+
scope: 'document',
|
|
423
|
+
document: target.document
|
|
424
|
+
});
|
|
425
|
+
}, phase, stepIndex);
|
|
426
|
+
}
|
|
427
|
+
function createDocumentRuntime(document, options) {
|
|
428
|
+
const documentRunId = options.createDocumentRunId?.() ?? randomUUID();
|
|
429
|
+
const startedAt = new Date();
|
|
430
|
+
const beforeAll = [];
|
|
431
|
+
const afterAll = [];
|
|
432
|
+
const completedNodes = [];
|
|
433
|
+
const nodes = {
|
|
434
|
+
beforeAll: document.lifecycle.beforeAll.map((step)=>options.resolveNode(step.node)),
|
|
435
|
+
afterAll: document.lifecycle.afterAll.map((step)=>options.resolveNode(step.node))
|
|
436
|
+
};
|
|
437
|
+
const info = {
|
|
438
|
+
documentId: document.documentId,
|
|
439
|
+
documentRunId,
|
|
440
|
+
projectId: document.projectId,
|
|
441
|
+
sourcePath: document.sourcePath,
|
|
442
|
+
cases: document.cases.map((caseDefinition)=>caseDefinition.definition),
|
|
443
|
+
lifecycle: document.lifecycle,
|
|
444
|
+
env: Object.freeze({
|
|
445
|
+
...process.env
|
|
446
|
+
})
|
|
447
|
+
};
|
|
448
|
+
const teardownStack = [];
|
|
449
|
+
let acceptingTeardowns = void 0 !== options.setupDocument;
|
|
450
|
+
let context;
|
|
451
|
+
let setupError;
|
|
452
|
+
let started = false;
|
|
453
|
+
let finishedResult;
|
|
454
|
+
const createResult = (teardownErrors = [])=>{
|
|
455
|
+
const endedAt = new Date();
|
|
456
|
+
return {
|
|
457
|
+
documentId: document.documentId,
|
|
458
|
+
documentRunId,
|
|
459
|
+
projectId: document.projectId,
|
|
460
|
+
sourcePath: document.sourcePath,
|
|
461
|
+
status: setupError || completedNodes.some((step)=>'failed' === step.status) || teardownErrors.length > 0 ? 'failed' : 'success',
|
|
462
|
+
startedAt: startedAt.toISOString(),
|
|
463
|
+
endedAt: endedAt.toISOString(),
|
|
464
|
+
durationMs: Math.max(0, endedAt.getTime() - startedAt.getTime()),
|
|
465
|
+
beforeAll: [
|
|
466
|
+
...beforeAll
|
|
467
|
+
],
|
|
468
|
+
afterAll: [
|
|
469
|
+
...afterAll
|
|
470
|
+
],
|
|
471
|
+
...setupError ? {
|
|
472
|
+
setupError
|
|
473
|
+
} : {},
|
|
474
|
+
...teardownErrors.length > 0 ? {
|
|
475
|
+
teardownErrors
|
|
476
|
+
} : {}
|
|
477
|
+
};
|
|
478
|
+
};
|
|
479
|
+
const runPhase = async (phase, results)=>{
|
|
480
|
+
for (const [stepIndex, step] of document.lifecycle[phase].entries()){
|
|
481
|
+
const documentContext = {
|
|
482
|
+
documentId: document.documentId,
|
|
483
|
+
documentRunId,
|
|
484
|
+
projectId: document.projectId,
|
|
485
|
+
sourcePath: document.sourcePath,
|
|
486
|
+
phase,
|
|
487
|
+
stepIndex,
|
|
488
|
+
completedNodes: Object.freeze([
|
|
489
|
+
...completedNodes
|
|
490
|
+
])
|
|
491
|
+
};
|
|
492
|
+
const stepInfo = {
|
|
493
|
+
scope: 'document',
|
|
494
|
+
node: step.node,
|
|
495
|
+
stepCount: document.lifecycle[phase].length,
|
|
496
|
+
document: documentContext
|
|
497
|
+
};
|
|
498
|
+
await options.onStepStart?.(stepInfo);
|
|
499
|
+
const result = await executeStep(step, nodes[phase][stepIndex], {
|
|
500
|
+
scope: 'document',
|
|
501
|
+
document: documentContext
|
|
502
|
+
}, context);
|
|
503
|
+
await options.onStepResult?.(stepInfo, result);
|
|
504
|
+
results.push(result);
|
|
505
|
+
completedNodes.push(result);
|
|
506
|
+
if ('failed' === result.status && !result.continuedAfterError) break;
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
const onTeardown = (teardown)=>{
|
|
510
|
+
if (!acceptingTeardowns) throw new WorkflowLifecycleError('onTeardown() can only be called while setupDocument is running.', {
|
|
511
|
+
documentId: document.documentId,
|
|
512
|
+
documentRunId
|
|
513
|
+
});
|
|
514
|
+
if ('function' != typeof teardown) throw new WorkflowLifecycleError('onTeardown() requires a teardown function.', {
|
|
515
|
+
documentId: document.documentId,
|
|
516
|
+
documentRunId
|
|
517
|
+
});
|
|
518
|
+
teardownStack.push({
|
|
519
|
+
registrationIndex: teardownStack.length,
|
|
520
|
+
teardown
|
|
521
|
+
});
|
|
522
|
+
};
|
|
523
|
+
return {
|
|
524
|
+
get context () {
|
|
525
|
+
return context;
|
|
526
|
+
},
|
|
527
|
+
get canRunCases () {
|
|
528
|
+
return started && !setupError && !beforeAll.some((step)=>'failed' === step.status);
|
|
529
|
+
},
|
|
530
|
+
async start () {
|
|
531
|
+
if (started) throw new WorkflowLifecycleError('Workflow document runtime has already started.', {
|
|
532
|
+
documentId: document.documentId,
|
|
533
|
+
documentRunId
|
|
534
|
+
});
|
|
535
|
+
started = true;
|
|
536
|
+
if (options.setupDocument) try {
|
|
537
|
+
context = await options.setupDocument({
|
|
538
|
+
...info,
|
|
539
|
+
onTeardown
|
|
540
|
+
});
|
|
541
|
+
} catch (error) {
|
|
542
|
+
setupError = new WorkflowDocumentSetupError(error, {
|
|
543
|
+
documentId: document.documentId,
|
|
544
|
+
documentRunId
|
|
545
|
+
});
|
|
546
|
+
} finally{
|
|
547
|
+
acceptingTeardowns = false;
|
|
548
|
+
}
|
|
549
|
+
if (!setupError) await runPhase('beforeAll', beforeAll);
|
|
550
|
+
return createResult();
|
|
551
|
+
},
|
|
552
|
+
async finish () {
|
|
553
|
+
if (finishedResult) return finishedResult;
|
|
554
|
+
if (!started) throw new WorkflowLifecycleError('Workflow document runtime must start before it can finish.', {
|
|
555
|
+
documentId: document.documentId,
|
|
556
|
+
documentRunId
|
|
557
|
+
});
|
|
558
|
+
if (!setupError) await runPhase('afterAll', afterAll);
|
|
559
|
+
const statusBeforeTeardown = createResult().status;
|
|
560
|
+
const teardownErrors = [];
|
|
561
|
+
for (const { registrationIndex, teardown } of teardownStack.reverse())try {
|
|
562
|
+
await teardown({
|
|
563
|
+
...info,
|
|
564
|
+
beforeAll: Object.freeze([
|
|
565
|
+
...beforeAll
|
|
566
|
+
]),
|
|
567
|
+
afterAll: Object.freeze([
|
|
568
|
+
...afterAll
|
|
569
|
+
]),
|
|
570
|
+
status: statusBeforeTeardown,
|
|
571
|
+
...setupError ? {
|
|
572
|
+
setupError
|
|
573
|
+
} : {}
|
|
574
|
+
});
|
|
575
|
+
} catch (error) {
|
|
576
|
+
teardownErrors.push(new WorkflowDocumentTeardownError(error, {
|
|
577
|
+
documentId: document.documentId,
|
|
578
|
+
documentRunId,
|
|
579
|
+
registrationIndex
|
|
580
|
+
}));
|
|
581
|
+
}
|
|
582
|
+
finishedResult = createResult(teardownErrors);
|
|
583
|
+
await options.onResult?.(finishedResult);
|
|
584
|
+
return finishedResult;
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
async function runCollectedCase(collectedCase, options) {
|
|
589
|
+
const phases = {
|
|
590
|
+
beforeEach: options.beforeEach ?? [],
|
|
591
|
+
steps: collectedCase.definition.steps,
|
|
592
|
+
afterEach: options.afterEach ?? []
|
|
593
|
+
};
|
|
594
|
+
const nodes = {
|
|
595
|
+
beforeEach: phases.beforeEach.map((step)=>options.resolveNode(step.node)),
|
|
596
|
+
steps: phases.steps.map((step)=>options.resolveNode(step.node)),
|
|
597
|
+
afterEach: phases.afterEach.map((step)=>options.resolveNode(step.node))
|
|
598
|
+
};
|
|
599
|
+
const runId = options.createRunId?.() ?? randomUUID();
|
|
600
|
+
const startedAt = new Date();
|
|
601
|
+
const beforeEach = [];
|
|
602
|
+
const steps = [];
|
|
603
|
+
const afterEach = [];
|
|
604
|
+
const completedNodes = [];
|
|
605
|
+
const runPhase = async (phase, results)=>{
|
|
606
|
+
for (const [stepIndex, step] of phases[phase].entries()){
|
|
607
|
+
const caseContext = {
|
|
608
|
+
caseId: collectedCase.caseId,
|
|
609
|
+
runId,
|
|
610
|
+
name: collectedCase.definition.name,
|
|
611
|
+
sourcePath: collectedCase.sourcePath,
|
|
612
|
+
caseIndex: collectedCase.caseIndex,
|
|
613
|
+
phase,
|
|
614
|
+
stepIndex,
|
|
615
|
+
completedSteps: Object.freeze([
|
|
616
|
+
...steps
|
|
617
|
+
]),
|
|
618
|
+
completedNodes: Object.freeze([
|
|
619
|
+
...completedNodes
|
|
620
|
+
])
|
|
621
|
+
};
|
|
622
|
+
const stepInfo = {
|
|
623
|
+
scope: 'case',
|
|
624
|
+
node: step.node,
|
|
625
|
+
stepCount: phases[phase].length,
|
|
626
|
+
case: caseContext
|
|
627
|
+
};
|
|
628
|
+
await options.onStepStart?.(stepInfo);
|
|
629
|
+
const result = await executeStep(step, nodes[phase][stepIndex], {
|
|
630
|
+
scope: 'case',
|
|
631
|
+
case: caseContext
|
|
632
|
+
}, options.context);
|
|
633
|
+
await options.onStepResult?.(stepInfo, result);
|
|
634
|
+
results.push(result);
|
|
635
|
+
completedNodes.push(result);
|
|
636
|
+
if ('failed' === result.status && !result.continuedAfterError) break;
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
await runPhase('beforeEach', beforeEach);
|
|
640
|
+
if (!beforeEach.some((step)=>'failed' === step.status)) await runPhase('steps', steps);
|
|
641
|
+
await runPhase('afterEach', afterEach);
|
|
642
|
+
const endedAt = new Date();
|
|
643
|
+
const result = {
|
|
644
|
+
caseId: collectedCase.caseId,
|
|
645
|
+
runId,
|
|
646
|
+
name: collectedCase.definition.name,
|
|
647
|
+
sourcePath: collectedCase.sourcePath,
|
|
648
|
+
caseIndex: collectedCase.caseIndex,
|
|
649
|
+
status: completedNodes.some((step)=>'failed' === step.status) ? 'failed' : 'success',
|
|
650
|
+
beforeEach,
|
|
651
|
+
steps,
|
|
652
|
+
afterEach,
|
|
653
|
+
startedAt: startedAt.toISOString(),
|
|
654
|
+
endedAt: endedAt.toISOString(),
|
|
655
|
+
durationMs: Math.max(0, endedAt.getTime() - startedAt.getTime())
|
|
656
|
+
};
|
|
657
|
+
await options.onResult?.(result);
|
|
658
|
+
return result;
|
|
659
|
+
}
|
|
660
|
+
const asNotRun = (collectedCase, reason)=>({
|
|
661
|
+
caseId: collectedCase.caseId,
|
|
662
|
+
name: collectedCase.definition.name,
|
|
663
|
+
sourcePath: collectedCase.sourcePath,
|
|
664
|
+
caseIndex: collectedCase.caseIndex,
|
|
665
|
+
status: 'not-run',
|
|
666
|
+
notRunReason: reason
|
|
667
|
+
});
|
|
668
|
+
async function runWorkflowDocument(document, options) {
|
|
669
|
+
const createDocumentRunId = options.createDocumentRunId;
|
|
670
|
+
const createCaseRunId = options.createCaseRunId;
|
|
671
|
+
const runtime = createDocumentRuntime(document, {
|
|
672
|
+
resolveNode: options.resolveNode,
|
|
673
|
+
setupDocument: options.setupDocument,
|
|
674
|
+
onStepStart: options.onStepStart,
|
|
675
|
+
onStepResult: options.onStepResult,
|
|
676
|
+
onResult: options.onDocumentResult,
|
|
677
|
+
createDocumentRunId: createDocumentRunId ? ()=>createDocumentRunId(document) : void 0
|
|
678
|
+
});
|
|
679
|
+
const cases = [];
|
|
680
|
+
let runtimeStarted = false;
|
|
681
|
+
let documentResult;
|
|
682
|
+
let executionError;
|
|
683
|
+
try {
|
|
684
|
+
runtimeStarted = true;
|
|
685
|
+
await runtime.start();
|
|
686
|
+
if (runtime.canRunCases) for (const collectedCase of document.cases){
|
|
687
|
+
if (options.shouldStop?.()) {
|
|
688
|
+
cases.push(asNotRun(collectedCase, 'interrupted'));
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
await options.onCaseStart?.(collectedCase);
|
|
692
|
+
const run = await runCollectedCase(collectedCase, {
|
|
693
|
+
resolveNode: options.resolveNode,
|
|
694
|
+
beforeEach: document.lifecycle.beforeEach,
|
|
695
|
+
afterEach: document.lifecycle.afterEach,
|
|
696
|
+
context: runtime.context,
|
|
697
|
+
onStepStart: options.onStepStart,
|
|
698
|
+
onStepResult: options.onStepResult,
|
|
699
|
+
onResult: options.onCaseResult,
|
|
700
|
+
createRunId: createCaseRunId ? ()=>createCaseRunId(collectedCase) : void 0
|
|
701
|
+
});
|
|
702
|
+
cases.push({
|
|
703
|
+
caseId: collectedCase.caseId,
|
|
704
|
+
name: collectedCase.definition.name,
|
|
705
|
+
sourcePath: collectedCase.sourcePath,
|
|
706
|
+
caseIndex: collectedCase.caseIndex,
|
|
707
|
+
status: run.status,
|
|
708
|
+
run
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
else cases.push(...document.cases.map((collectedCase)=>asNotRun(collectedCase, 'document-start-failed')));
|
|
712
|
+
} catch (error) {
|
|
713
|
+
executionError = error;
|
|
714
|
+
} finally{
|
|
715
|
+
if (runtimeStarted) try {
|
|
716
|
+
documentResult = await runtime.finish();
|
|
717
|
+
} catch (error) {
|
|
718
|
+
executionError ??= error;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (executionError) throw executionError;
|
|
722
|
+
if (!documentResult) throw new Error(`Workflow document "${document.sourcePath}" did not produce a result.`);
|
|
723
|
+
return {
|
|
724
|
+
document: documentResult,
|
|
725
|
+
cases
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
const collect_isMapping = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
729
|
+
const rejectUnknownKeys = (value, allowed, label)=>{
|
|
730
|
+
const allowedSet = new Set(allowed);
|
|
731
|
+
const unknown = Object.keys(value).filter((key)=>!allowedSet.has(key));
|
|
732
|
+
if (unknown.length) throw new WorkflowParseError(`${label} has unsupported field "${unknown[0]}".`, {
|
|
733
|
+
field: unknown[0]
|
|
734
|
+
});
|
|
735
|
+
};
|
|
736
|
+
const createCaseId = (projectId, sourcePath, caseIndex)=>createHash('sha256').update(JSON.stringify([
|
|
737
|
+
projectId,
|
|
738
|
+
sourcePath,
|
|
739
|
+
caseIndex
|
|
740
|
+
])).digest('hex');
|
|
741
|
+
const createWorkflowDocumentId = (projectId, sourcePath)=>createHash('sha256').update(JSON.stringify([
|
|
742
|
+
projectId,
|
|
743
|
+
sourcePath
|
|
744
|
+
])).digest('hex');
|
|
745
|
+
function collectWorkflowDocument(source, options) {
|
|
746
|
+
let parsed;
|
|
747
|
+
try {
|
|
748
|
+
parsed = load(readFileSync(source.absolutePath, 'utf8'), {
|
|
749
|
+
schema: JSON_SCHEMA
|
|
750
|
+
});
|
|
751
|
+
} catch (error) {
|
|
752
|
+
throw new WorkflowParseError(`Failed to parse workflow document "${source.sourcePath}".`, {
|
|
753
|
+
sourcePath: source.sourcePath
|
|
754
|
+
}, error);
|
|
755
|
+
}
|
|
756
|
+
if (!collect_isMapping(parsed)) throw new WorkflowParseError('Workflow document must be a mapping.');
|
|
757
|
+
rejectUnknownKeys(parsed, [
|
|
758
|
+
'beforeAll',
|
|
759
|
+
'beforeEach',
|
|
760
|
+
'cases',
|
|
761
|
+
'afterEach',
|
|
762
|
+
'afterAll'
|
|
763
|
+
], 'Workflow document');
|
|
764
|
+
if (!Array.isArray(parsed.cases) || 0 === parsed.cases.length) throw new WorkflowParseError('Workflow document must contain a non-empty cases array.');
|
|
765
|
+
const sourcePath = source.sourcePath.split(sep).join('/');
|
|
766
|
+
const normalizeLifecycle = (phase)=>{
|
|
767
|
+
const value = parsed[phase];
|
|
768
|
+
if (void 0 === value) return [];
|
|
769
|
+
if (!Array.isArray(value)) throw new WorkflowParseError(`Workflow document ${phase} must be an array.`, {
|
|
770
|
+
phase
|
|
771
|
+
});
|
|
772
|
+
return normalizeSteps(value).map((normalized, stepIndex)=>{
|
|
773
|
+
const resolved = options.resolveNode(normalized.node);
|
|
774
|
+
if (!resolved) throw new WorkflowParseError(`Workflow document ${phase} step ${stepIndex + 1} references unknown node "${normalized.node}".`, {
|
|
775
|
+
phase,
|
|
776
|
+
stepIndex,
|
|
777
|
+
node: normalized.node
|
|
778
|
+
});
|
|
779
|
+
return normalized;
|
|
780
|
+
});
|
|
781
|
+
};
|
|
782
|
+
const lifecycle = {
|
|
783
|
+
beforeAll: normalizeLifecycle('beforeAll'),
|
|
784
|
+
beforeEach: normalizeLifecycle('beforeEach'),
|
|
785
|
+
afterEach: normalizeLifecycle('afterEach'),
|
|
786
|
+
afterAll: normalizeLifecycle('afterAll')
|
|
787
|
+
};
|
|
788
|
+
const ids = new Set();
|
|
789
|
+
const cases = parsed.cases.map((definition, caseIndex)=>{
|
|
790
|
+
if (!collect_isMapping(definition)) throw new WorkflowParseError(`Case ${caseIndex + 1} must be a mapping.`, {
|
|
791
|
+
caseIndex
|
|
792
|
+
});
|
|
793
|
+
rejectUnknownKeys(definition, [
|
|
794
|
+
'name',
|
|
795
|
+
'steps'
|
|
796
|
+
], `Case ${caseIndex + 1}`);
|
|
797
|
+
if ('string' != typeof definition.name || 0 === definition.name.trim().length) throw new WorkflowParseError(`Case ${caseIndex + 1} name must be a non-empty string.`, {
|
|
798
|
+
caseIndex
|
|
799
|
+
});
|
|
800
|
+
if (!Array.isArray(definition.steps) || 0 === definition.steps.length) throw new WorkflowParseError(`Case ${caseIndex + 1} steps must be a non-empty array.`, {
|
|
801
|
+
caseIndex
|
|
802
|
+
});
|
|
803
|
+
const steps = normalizeSteps(definition.steps).map((normalized, stepIndex)=>{
|
|
804
|
+
if (!options.resolveNode(normalized.node)) throw new WorkflowParseError(`Case ${caseIndex + 1} step ${stepIndex + 1} references unknown node "${normalized.node}".`, {
|
|
805
|
+
caseIndex,
|
|
806
|
+
stepIndex,
|
|
807
|
+
node: normalized.node
|
|
808
|
+
});
|
|
809
|
+
return normalized;
|
|
810
|
+
});
|
|
811
|
+
const caseId = createCaseId(source.projectId, sourcePath, caseIndex);
|
|
812
|
+
if (ids.has(caseId)) throw new WorkflowParseError(`Case id collision: ${caseId}.`, {
|
|
813
|
+
caseId
|
|
814
|
+
});
|
|
815
|
+
ids.add(caseId);
|
|
816
|
+
return {
|
|
817
|
+
caseId,
|
|
818
|
+
projectId: source.projectId,
|
|
819
|
+
sourcePath,
|
|
820
|
+
caseIndex,
|
|
821
|
+
definition: {
|
|
822
|
+
name: definition.name,
|
|
823
|
+
steps
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
});
|
|
827
|
+
return {
|
|
828
|
+
documentId: createWorkflowDocumentId(source.projectId, sourcePath),
|
|
829
|
+
projectId: source.projectId,
|
|
830
|
+
sourcePath,
|
|
831
|
+
lifecycle,
|
|
832
|
+
cases
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
const writeJson = (path, value)=>{
|
|
836
|
+
mkdirSync(dirname(path), {
|
|
837
|
+
recursive: true
|
|
838
|
+
});
|
|
839
|
+
writeFileSync(path, JSON.stringify(value, null, 2));
|
|
840
|
+
};
|
|
841
|
+
const toPosix = (value)=>value.split(sep).join('/');
|
|
842
|
+
const caseRunResultPath = (result)=>toPosix(join('runs', result.caseId, `${result.runId}.json`));
|
|
843
|
+
const workflowDocumentRunResultPath = (result)=>toPosix(join('documents', result.documentId, `${result.documentRunId}.json`));
|
|
844
|
+
const collectionErrorPath = (sourcePath)=>{
|
|
845
|
+
const id = createHash('sha256').update(sourcePath).digest('hex');
|
|
846
|
+
return toPosix(join('collection-errors', `${id}.json`));
|
|
847
|
+
};
|
|
848
|
+
const writeWorkflowDocumentRunResult = (resultDir, result)=>{
|
|
849
|
+
writeJson(join(resultDir, workflowDocumentRunResultPath(result)), result);
|
|
850
|
+
};
|
|
851
|
+
const writeCaseRunResult = (resultDir, result)=>{
|
|
852
|
+
writeJson(join(resultDir, caseRunResultPath(result)), result);
|
|
853
|
+
};
|
|
854
|
+
const writeCollectionError = (resultDir, collectionError)=>{
|
|
855
|
+
writeJson(join(resultDir, collectionErrorPath(collectionError.sourcePath)), {
|
|
856
|
+
kind: 'collection-error',
|
|
857
|
+
...collectionError
|
|
858
|
+
});
|
|
859
|
+
};
|
|
860
|
+
const writeWorkflowProjectRunResult = (resultDir, options)=>{
|
|
861
|
+
const { result } = options;
|
|
862
|
+
writeJson(join(resultDir, 'project.json'), {
|
|
863
|
+
version: 3,
|
|
864
|
+
projectId: options.projectId,
|
|
865
|
+
projectRoot: options.projectRoot,
|
|
866
|
+
...options.configPath ? {
|
|
867
|
+
configPath: options.configPath
|
|
868
|
+
} : {},
|
|
869
|
+
fileSelection: {
|
|
870
|
+
include: [
|
|
871
|
+
...options.fileSelection.include
|
|
872
|
+
],
|
|
873
|
+
...options.fileSelection.exclude ? {
|
|
874
|
+
exclude: [
|
|
875
|
+
...options.fileSelection.exclude
|
|
876
|
+
]
|
|
877
|
+
} : {}
|
|
878
|
+
},
|
|
879
|
+
sources: options.sources,
|
|
880
|
+
status: result.status,
|
|
881
|
+
exitCode: result.exitCode,
|
|
882
|
+
resultDir: result.resultDir,
|
|
883
|
+
summary: result.summary,
|
|
884
|
+
cases: result.cases.map((caseResult)=>({
|
|
885
|
+
caseId: caseResult.caseId,
|
|
886
|
+
name: caseResult.name,
|
|
887
|
+
sourcePath: caseResult.sourcePath,
|
|
888
|
+
caseIndex: caseResult.caseIndex,
|
|
889
|
+
status: caseResult.status,
|
|
890
|
+
...caseResult.run ? {
|
|
891
|
+
resultFile: caseRunResultPath(caseResult.run)
|
|
892
|
+
} : {},
|
|
893
|
+
...caseResult.notRunReason ? {
|
|
894
|
+
notRunReason: caseResult.notRunReason
|
|
895
|
+
} : {}
|
|
896
|
+
})),
|
|
897
|
+
documents: result.documents.map((document)=>({
|
|
898
|
+
documentId: document.documentId,
|
|
899
|
+
sourcePath: document.sourcePath,
|
|
900
|
+
status: document.status,
|
|
901
|
+
resultFile: workflowDocumentRunResultPath(document)
|
|
902
|
+
})),
|
|
903
|
+
collectionErrors: result.collectionErrors.map((error)=>({
|
|
904
|
+
sourcePath: error.sourcePath,
|
|
905
|
+
errorFile: collectionErrorPath(error.sourcePath)
|
|
906
|
+
}))
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
const CONFIG_NAMES = [
|
|
910
|
+
'midscene.workflow.config.cjs',
|
|
911
|
+
'midscene.workflow.config.js'
|
|
912
|
+
];
|
|
913
|
+
const ALWAYS_IGNORED_PATTERNS = [
|
|
914
|
+
'.git/**',
|
|
915
|
+
'.midscene/**',
|
|
916
|
+
'node_modules/**',
|
|
917
|
+
'**/.git/**',
|
|
918
|
+
'**/.midscene/**',
|
|
919
|
+
'**/node_modules/**'
|
|
920
|
+
];
|
|
921
|
+
const DEFAULT_WORKFLOW_FILE_SELECTION = {
|
|
922
|
+
include: [
|
|
923
|
+
'**/*.{yaml,yml}'
|
|
924
|
+
]
|
|
925
|
+
};
|
|
926
|
+
const workflow_runner_toPosix = (value)=>value.split(sep).join('/');
|
|
927
|
+
const discoverWorkflowFiles = (projectRoot, selection = DEFAULT_WORKFLOW_FILE_SELECTION)=>{
|
|
928
|
+
const root = resolve(projectRoot);
|
|
929
|
+
const normalized = validateWorkflowFileSelection(selection);
|
|
930
|
+
if (!normalized) throw new TypeError('Workflow file selection is required.');
|
|
931
|
+
const files = globSync(normalized.include, {
|
|
932
|
+
absolute: true,
|
|
933
|
+
caseSensitiveMatch: false,
|
|
934
|
+
cwd: root,
|
|
935
|
+
dot: true,
|
|
936
|
+
expandDirectories: false,
|
|
937
|
+
followSymbolicLinks: false,
|
|
938
|
+
ignore: [
|
|
939
|
+
...ALWAYS_IGNORED_PATTERNS,
|
|
940
|
+
...normalized.exclude ?? []
|
|
941
|
+
],
|
|
942
|
+
onlyFiles: true
|
|
943
|
+
}).filter((file)=>/\.ya?ml$/i.test(file));
|
|
944
|
+
return [
|
|
945
|
+
...new Set(files.map((file)=>resolve(file)))
|
|
946
|
+
].sort((a, b)=>{
|
|
947
|
+
const relativeA = workflow_runner_toPosix(relative(root, a));
|
|
948
|
+
const relativeB = workflow_runner_toPosix(relative(root, b));
|
|
949
|
+
return relativeA < relativeB ? -1 : relativeA > relativeB ? 1 : 0;
|
|
950
|
+
});
|
|
951
|
+
};
|
|
952
|
+
const discoverWorkflowConfig = (projectRoot)=>CONFIG_NAMES.map((name)=>join(resolve(projectRoot), name)).find(existsSync);
|
|
953
|
+
const defaultResultDir = (projectRoot)=>join(projectRoot, '.midscene', 'workflow-results', `${Date.now()}-${process.pid}`);
|
|
954
|
+
const assertDirectory = (path, label)=>{
|
|
955
|
+
if (!existsSync(path) || !statSync(path).isDirectory()) throw new Error(`${label} does not exist or is not a directory: ${path}`);
|
|
956
|
+
};
|
|
957
|
+
const asCollectionError = (sourcePath, error)=>({
|
|
958
|
+
sourcePath,
|
|
959
|
+
error: error instanceof WorkflowError ? error : new WorkflowParseError(`Failed to collect workflow document "${sourcePath}": ${error instanceof Error ? error.message : String(error)}`, {
|
|
960
|
+
sourcePath
|
|
961
|
+
}, error)
|
|
962
|
+
});
|
|
963
|
+
const workflow_runner_asNotRun = (collectedCase, reason)=>({
|
|
964
|
+
caseId: collectedCase.caseId,
|
|
965
|
+
name: collectedCase.definition.name,
|
|
966
|
+
sourcePath: collectedCase.sourcePath,
|
|
967
|
+
caseIndex: collectedCase.caseIndex,
|
|
968
|
+
status: 'not-run',
|
|
969
|
+
notRunReason: reason
|
|
970
|
+
});
|
|
971
|
+
const summarize = (cases, documents, collectionErrors)=>({
|
|
972
|
+
total: cases.length,
|
|
973
|
+
passed: cases.filter((caseResult)=>'success' === caseResult.status).length,
|
|
974
|
+
failed: cases.filter((caseResult)=>'failed' === caseResult.status).length,
|
|
975
|
+
notRun: cases.filter((caseResult)=>'not-run' === caseResult.status).length,
|
|
976
|
+
collectionErrors: collectionErrors.length,
|
|
977
|
+
documentFailures: documents.filter((document)=>'failed' === document.status).length
|
|
978
|
+
});
|
|
979
|
+
const stepPosition = (info)=>'case' === info.scope ? info.case : info.document;
|
|
980
|
+
const workflow_runner_formatStep = (info)=>{
|
|
981
|
+
const position = stepPosition(info);
|
|
982
|
+
const phase = 'steps' === position.phase ? 'step' : position.phase;
|
|
983
|
+
return `${phase} ${position.stepIndex + 1}/${info.stepCount}: ${info.node}`;
|
|
984
|
+
};
|
|
985
|
+
const formatStepResult = (info, result)=>{
|
|
986
|
+
const indent = 'case' === info.scope ? ' ' : ' ';
|
|
987
|
+
const symbol = 'success' === result.status ? '✓' : '✗';
|
|
988
|
+
const error = result.error ? ` — ${result.error.message}` : '';
|
|
989
|
+
const continuation = result.continuedAfterError ? '; continuing' : '';
|
|
990
|
+
return `${indent}${symbol} ${workflow_runner_formatStep(info)} (${result.durationMs} ms)${error}${continuation}`;
|
|
991
|
+
};
|
|
992
|
+
const formatNotRunReason = (reason)=>'document-start-failed' === reason ? 'document start failed' : 'run interrupted';
|
|
993
|
+
async function runWorkflowProject(options = {}) {
|
|
994
|
+
const cwd = resolve(options.cwd ?? process.cwd());
|
|
995
|
+
assertDirectory(cwd, 'Workflow working directory');
|
|
996
|
+
const cliProjectRoot = options.projectRoot ? resolve(cwd, options.projectRoot) : void 0;
|
|
997
|
+
if (cliProjectRoot) assertDirectory(cliProjectRoot, 'Workflow project directory');
|
|
998
|
+
const configSearchRoot = cliProjectRoot ?? cwd;
|
|
999
|
+
const configPath = options.configPath ? resolve(configSearchRoot, options.configPath) : discoverWorkflowConfig(configSearchRoot);
|
|
1000
|
+
if (options.configPath && (!configPath || !existsSync(configPath))) throw new Error(`Workflow config does not exist: ${configPath}`);
|
|
1001
|
+
const project = loadWorkflowProjectSync(configPath);
|
|
1002
|
+
const projectRoot = cliProjectRoot ? cliProjectRoot : project.root ? resolve(configPath ? dirname(configPath) : configSearchRoot, project.root) : cwd;
|
|
1003
|
+
assertDirectory(projectRoot, 'Workflow project root');
|
|
1004
|
+
const fileSelection = project.files ?? DEFAULT_WORKFLOW_FILE_SELECTION;
|
|
1005
|
+
const files = discoverWorkflowFiles(projectRoot, fileSelection);
|
|
1006
|
+
if (0 === files.length) throw new Error(`No workflow YAML files found in ${projectRoot}.`);
|
|
1007
|
+
const resultDir = options.resultDir ? resolve(cwd, options.resultDir) : defaultResultDir(projectRoot);
|
|
1008
|
+
mkdirSync(resultDir, {
|
|
1009
|
+
recursive: true
|
|
1010
|
+
});
|
|
1011
|
+
const projectId = basename(projectRoot);
|
|
1012
|
+
const sources = files.map((absolutePath)=>({
|
|
1013
|
+
projectId,
|
|
1014
|
+
sourcePath: workflow_runner_toPosix(relative(projectRoot, absolutePath)),
|
|
1015
|
+
absolutePath
|
|
1016
|
+
}));
|
|
1017
|
+
const collectedDocuments = [];
|
|
1018
|
+
const collectionErrors = [];
|
|
1019
|
+
const collectedCaseIds = new Set();
|
|
1020
|
+
const progress = options.onProgress ?? (()=>{});
|
|
1021
|
+
for (const source of sources)try {
|
|
1022
|
+
const document = collectWorkflowDocument(source, {
|
|
1023
|
+
resolveNode: project.resolveNode
|
|
1024
|
+
});
|
|
1025
|
+
const collided = document.cases.find((collectedCase)=>collectedCaseIds.has(collectedCase.caseId));
|
|
1026
|
+
if (collided) throw new WorkflowParseError(`Case id collision: ${collided.caseId}.`, {
|
|
1027
|
+
caseId: collided.caseId,
|
|
1028
|
+
sourcePath: source.sourcePath
|
|
1029
|
+
});
|
|
1030
|
+
for (const collectedCase of document.cases)collectedCaseIds.add(collectedCase.caseId);
|
|
1031
|
+
collectedDocuments.push({
|
|
1032
|
+
document
|
|
1033
|
+
});
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
const collectionError = asCollectionError(source.sourcePath, error);
|
|
1036
|
+
collectionErrors.push(collectionError);
|
|
1037
|
+
writeCollectionError(resultDir, collectionError);
|
|
1038
|
+
}
|
|
1039
|
+
const collectedCaseCount = collectedDocuments.reduce((total, { document })=>total + document.cases.length, 0);
|
|
1040
|
+
progress(`midscene-workflow: collected ${collectedDocuments.length} documents, ${collectedCaseCount} cases, ${collectionErrors.length} collection errors`);
|
|
1041
|
+
for (const collectionError of collectionErrors)progress(`✗ collection: ${collectionError.sourcePath} — ${collectionError.error.message}`);
|
|
1042
|
+
const cases = [];
|
|
1043
|
+
const documents = [];
|
|
1044
|
+
let interrupted = false;
|
|
1045
|
+
const handleSignal = ()=>{
|
|
1046
|
+
interrupted = true;
|
|
1047
|
+
};
|
|
1048
|
+
process.on('SIGINT', handleSignal);
|
|
1049
|
+
process.on('SIGTERM', handleSignal);
|
|
1050
|
+
try {
|
|
1051
|
+
for (const [documentIndex, { document }] of collectedDocuments.entries()){
|
|
1052
|
+
if (interrupted) {
|
|
1053
|
+
progress(`[document ${documentIndex + 1}/${collectedDocuments.length}] ${document.sourcePath}`);
|
|
1054
|
+
const notRunCases = document.cases.map((collectedCase)=>workflow_runner_asNotRun(collectedCase, 'interrupted'));
|
|
1055
|
+
for (const outcome of notRunCases)progress(` - case ${outcome.caseIndex + 1}/${document.cases.length}: ${outcome.name} (not run: run interrupted)`);
|
|
1056
|
+
progress(`- document ${documentIndex + 1}/${collectedDocuments.length}: ${document.sourcePath} (not run: run interrupted)`);
|
|
1057
|
+
cases.push(...notRunCases);
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
progress(`[document ${documentIndex + 1}/${collectedDocuments.length}] ${document.sourcePath}`);
|
|
1061
|
+
const execution = await runWorkflowDocument(document, {
|
|
1062
|
+
resolveNode: project.nodes.require.bind(project.nodes),
|
|
1063
|
+
setupDocument: project.setupDocument,
|
|
1064
|
+
shouldStop: ()=>interrupted,
|
|
1065
|
+
onCaseStart: (collectedCase)=>{
|
|
1066
|
+
progress(` [case ${collectedCase.caseIndex + 1}/${document.cases.length}] ${collectedCase.definition.name}`);
|
|
1067
|
+
},
|
|
1068
|
+
onStepStart: (info)=>{
|
|
1069
|
+
const indent = 'case' === info.scope ? ' ' : ' ';
|
|
1070
|
+
progress(`${indent}→ ${workflow_runner_formatStep(info)}`);
|
|
1071
|
+
},
|
|
1072
|
+
onStepResult: (info, result)=>{
|
|
1073
|
+
progress(formatStepResult(info, result));
|
|
1074
|
+
},
|
|
1075
|
+
onCaseResult: (run)=>{
|
|
1076
|
+
writeCaseRunResult(resultDir, run);
|
|
1077
|
+
const symbol = 'success' === run.status ? '✓' : '✗';
|
|
1078
|
+
progress(` ${symbol} case ${run.caseIndex + 1}/${document.cases.length}: ${run.name} (${run.durationMs} ms)`);
|
|
1079
|
+
},
|
|
1080
|
+
onDocumentResult: (documentResult)=>writeWorkflowDocumentRunResult(resultDir, documentResult)
|
|
1081
|
+
});
|
|
1082
|
+
for (const outcome of execution.cases)if ('not-run' === outcome.status && outcome.notRunReason) progress(` - case ${outcome.caseIndex + 1}/${document.cases.length}: ${outcome.name} (not run: ${formatNotRunReason(outcome.notRunReason)})`);
|
|
1083
|
+
const documentSymbol = 'success' === execution.document.status ? '✓' : '✗';
|
|
1084
|
+
const documentError = execution.document.setupError ?? execution.document.teardownErrors?.[0];
|
|
1085
|
+
progress(`${documentSymbol} document ${documentIndex + 1}/${collectedDocuments.length}: ${document.sourcePath} (${execution.document.durationMs} ms)${documentError ? ` — ${documentError.message}` : ''}`);
|
|
1086
|
+
cases.push(...execution.cases);
|
|
1087
|
+
documents.push(execution.document);
|
|
1088
|
+
}
|
|
1089
|
+
} finally{
|
|
1090
|
+
process.off('SIGINT', handleSignal);
|
|
1091
|
+
process.off('SIGTERM', handleSignal);
|
|
1092
|
+
}
|
|
1093
|
+
const summary = summarize(cases, documents, collectionErrors);
|
|
1094
|
+
const failed = interrupted || summary.failed > 0 || summary.notRun > 0 || summary.collectionErrors > 0 || summary.documentFailures > 0;
|
|
1095
|
+
const result = {
|
|
1096
|
+
status: failed ? 'failed' : 'success',
|
|
1097
|
+
exitCode: failed ? 1 : 0,
|
|
1098
|
+
resultDir,
|
|
1099
|
+
summary,
|
|
1100
|
+
cases,
|
|
1101
|
+
documents,
|
|
1102
|
+
collectionErrors
|
|
1103
|
+
};
|
|
1104
|
+
writeWorkflowProjectRunResult(resultDir, {
|
|
1105
|
+
projectId,
|
|
1106
|
+
projectRoot,
|
|
1107
|
+
...configPath ? {
|
|
1108
|
+
configPath
|
|
1109
|
+
} : {},
|
|
1110
|
+
fileSelection,
|
|
1111
|
+
sources,
|
|
1112
|
+
result
|
|
1113
|
+
});
|
|
1114
|
+
return result;
|
|
1115
|
+
}
|
|
1116
|
+
export { DEFAULT_WORKFLOW_FILE_SELECTION, defineWorkflowProject, discoverWorkflowConfig, discoverWorkflowFiles, loadWorkflowProjectSync, runWorkflowProject, validateWorkflowFileSelection };
|
|
1117
|
+
|
|
1118
|
+
//# sourceMappingURL=index.mjs.map
|