-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_protocol.ts
More file actions
344 lines (316 loc) · 8.04 KB
/
run_protocol.ts
File metadata and controls
344 lines (316 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* Subprocess communication protocol for scenario execution
*
* @module
*/
import type {
Reporter,
RunResult,
ScenarioResult,
StepResult,
} from "@probitas/runner";
import type {
ScenarioMetadata,
StepMetadata,
StepOptions,
} from "@probitas/core";
import type { LogLevel } from "@logtape/logtape";
import { isErrorObject } from "@core/errorutil/error-object";
import {
createOutputValidator,
deserializeError,
type ErrorObject,
serializeError,
} from "./utils.ts";
// Re-export for backward compatibility with consumers
export { deserializeError, type ErrorObject, serializeError } from "./utils.ts";
/**
* Message sent from CLI to subprocess
*/
export type RunInput =
| RunCommandInput
| RunAbortInput;
/**
* Failed scenario identifier for filtering
*/
export interface FailedScenarioFilter {
/** Scenario name */
readonly name: string;
/** File path (relative or absolute) */
readonly file: string;
}
/**
* Run scenarios command
*/
export interface RunCommandInput {
readonly type: "run";
/** File paths to load scenarios from */
readonly filePaths: readonly string[];
/** Selectors to filter scenarios (empty array = no filtering) */
readonly selectors: readonly string[];
/** Maximum number of scenarios to run concurrently (0 = no limit) */
readonly maxConcurrency: number;
/** Maximum number of failures before stopping (0 = no limit) */
readonly maxFailures: number;
/** Timeout in milliseconds (0 = no timeout) */
readonly timeout: number;
/** Default step options (timeout, retry) applied to all steps */
readonly stepOptions?: StepOptions;
/** Log level for subprocess logging */
readonly logLevel: LogLevel;
/** Filter to only run these failed scenarios (name + file pairs) */
readonly failedFilter?: readonly FailedScenarioFilter[];
}
/**
* Abort running scenarios
*/
export interface RunAbortInput {
readonly type: "abort";
}
/**
* Message sent from subprocess to CLI
*/
export type RunOutput =
| RunResultOutput
| RunErrorOutput
| RunStartOutput
| RunEndOutput
| RunScenarioStartOutput
| RunScenarioEndOutput
| RunStepStartOutput
| RunStepEndOutput;
/**
* Run started
*/
export interface RunStartOutput {
readonly type: "run_start";
/** Scenario metadata list (serializable) */
readonly scenarios: readonly ScenarioMetadata[];
}
/**
* Run completed
*/
export interface RunEndOutput {
readonly type: "run_end";
/** Scenario metadata list (serializable) */
readonly scenarios: readonly ScenarioMetadata[];
/** Run result */
readonly result: RunResult;
}
/**
* All scenarios execution completed successfully
*/
export interface RunResultOutput {
readonly type: "result";
/** Run result with all scenario results */
readonly result: RunResult;
}
/**
* Scenario execution failed with error
*/
export interface RunErrorOutput {
readonly type: "error";
/** Serialized error information */
readonly error: ErrorObject;
}
/**
* Scenario execution started
*/
export interface RunScenarioStartOutput {
readonly type: "scenario_start";
/** Scenario metadata (serializable) */
readonly scenario: ScenarioMetadata;
}
/**
* Scenario execution completed
*/
export interface RunScenarioEndOutput {
readonly type: "scenario_end";
/** Scenario metadata (serializable) */
readonly scenario: ScenarioMetadata;
/** Scenario execution result */
readonly result: ScenarioResult;
}
/**
* Step execution started
*/
export interface RunStepStartOutput {
readonly type: "step_start";
/** Scenario metadata (serializable) */
readonly scenario: ScenarioMetadata;
/** Step metadata (serializable) */
readonly step: StepMetadata;
}
/**
* Step execution completed
*/
export interface RunStepEndOutput {
readonly type: "step_end";
/** Scenario metadata (serializable) */
readonly scenario: ScenarioMetadata;
/** Step metadata (serializable) */
readonly step: StepMetadata;
/** Step execution result */
readonly result: StepResult;
}
/**
* Serialize StepResult for transmission
*
* Handles error serialization (for failed/skipped) to ensure proper
* cross-process Error transmission. Passed step values are transmitted
* as-is since CBOR streaming handles all complex types.
*/
export function serializeStepResult(result: StepResult): StepResult {
if (result.status === "passed") {
// Value is transmitted directly - CBOR streaming handles all types
return result;
}
return {
...result,
error: serializeError(result.error),
};
}
/**
* Serialize ScenarioResult errors for transmission
*/
export function serializeScenarioResult(
result: ScenarioResult,
): ScenarioResult {
const steps = result.steps.map(serializeStepResult);
if (result.status === "passed") {
return { ...result, steps };
}
return {
...result,
steps,
error: serializeError(result.error),
};
}
/**
* Deserialize StepResult from transmission
*
* Handles error deserialization (for failed/skipped) to restore
* Error instances from serialized ErrorObject. Passed step values
* are already deserialized by CBOR streaming.
*/
export function deserializeStepResult(result: StepResult): StepResult {
if (result.status === "passed") {
// Value is already deserialized by CBOR streaming
return result;
}
if (isErrorObject(result.error)) {
return {
...result,
error: deserializeError(result.error),
};
}
return result;
}
/**
* Deserialize ScenarioResult errors from transmission
*/
export function deserializeScenarioResult(
result: ScenarioResult,
): ScenarioResult {
const steps = result.steps.map(deserializeStepResult);
if (result.status === "passed") {
return { ...result, steps };
}
if (isErrorObject(result.error)) {
return {
...result,
steps,
error: deserializeError(result.error),
};
}
return { ...result, steps };
}
/**
* Serialize RunResult errors for transmission
*/
export function serializeRunResult(result: RunResult): RunResult {
return {
...result,
scenarios: result.scenarios.map(serializeScenarioResult),
};
}
/**
* Deserialize RunResult errors from transmission
*/
export function deserializeRunResult(result: RunResult): RunResult {
return {
...result,
scenarios: result.scenarios.map(deserializeScenarioResult),
};
}
/**
* Create a Reporter that sends events via the provided output function
*
* This factory eliminates duplication between Worker and Subprocess reporters
* by extracting the common serialization logic.
*
* The output function can be async - all reporter methods will await it.
*/
export function createReporter(
output: (message: RunOutput) => void | Promise<void>,
): Reporter {
return {
async onRunStart(scenarios: readonly ScenarioMetadata[]): Promise<void> {
await output({ type: "run_start", scenarios });
},
async onRunEnd(
scenarios: readonly ScenarioMetadata[],
result: RunResult,
): Promise<void> {
await output({
type: "run_end",
scenarios,
result: serializeRunResult(result),
});
},
async onScenarioStart(scenario: ScenarioMetadata): Promise<void> {
await output({ type: "scenario_start", scenario });
},
async onScenarioEnd(
scenario: ScenarioMetadata,
result: ScenarioResult,
): Promise<void> {
await output({
type: "scenario_end",
scenario,
result: serializeScenarioResult(result),
});
},
async onStepStart(
scenario: ScenarioMetadata,
step: StepMetadata,
): Promise<void> {
await output({ type: "step_start", scenario, step });
},
async onStepEnd(
scenario: ScenarioMetadata,
step: StepMetadata,
result: StepResult,
): Promise<void> {
await output({
type: "step_end",
scenario,
step,
result: serializeStepResult(result),
});
},
};
}
/**
* Type guard to check if a value is a valid RunOutput
*/
export const isRunOutput = createOutputValidator<RunOutput>([
"result",
"error",
"run_start",
"run_end",
"scenario_start",
"scenario_end",
"step_start",
"step_end",
]);