-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
155 lines (137 loc) · 4.39 KB
/
run.ts
File metadata and controls
155 lines (137 loc) · 4.39 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
/**
* Subprocess entry point for run command
*
* Executes scenarios and streams progress via TCP IPC.
* Communication is via CBOR over TCP (not stdin/stdout).
*
* @module
* @internal
*/
import { applySelectors } from "@probitas/core/selector";
import { loadScenarios } from "@probitas/core/loader";
import { getLogger } from "@logtape/logtape";
import { Runner } from "@probitas/runner";
import {
createReporter,
type RunCommandInput,
type RunOutput,
serializeError,
serializeRunResult,
} from "./run_protocol.ts";
import { configureLogging, runSubprocess, writeOutput } from "./utils.ts";
const logger = getLogger(["probitas", "cli", "run", "subprocess"]);
// Global AbortController for manual cancellation (Ctrl-C, --fail-fast, etc.)
let globalAbortController: AbortController | null = null;
// Handle unhandled promise rejections from Deno's node:http2 compatibility layer.
// The "Bad resource ID" error occurs during HTTP/2 stream cleanup and doesn't
// affect test correctness, but causes the subprocess to crash without this handler.
globalThis.addEventListener(
"unhandledrejection",
(event: PromiseRejectionEvent) => {
event.preventDefault();
const error = event.reason;
const errorMessage = error instanceof Error ? error.message : String(error);
// Silently ignore "Bad resource ID" errors from node:http2
if (
errorMessage.includes("Bad resource ID") ||
(error instanceof Error && error.stack?.includes("node:http2"))
) {
return;
}
// Log other unhandled rejections
logger.error`Unhandled promise rejection in subprocess: ${error}`;
},
);
/**
* Execute all scenarios
*
* This handler manages its own error handling because it needs to:
* 1. Clean up the abort controller on error
* 2. Write structured error output for scenario execution failures
*/
runSubprocess<RunCommandInput>(async (ipc, input) => {
const {
filePaths,
selectors,
maxConcurrency,
maxFailures,
timeout,
stepOptions,
logLevel,
failedFilter,
} = input;
// Create abort controller for this run
globalAbortController = new AbortController();
// Configure logging in subprocess
await configureLogging(logLevel);
try {
// Load scenarios from files and apply selectors
let scenarios = applySelectors(
await loadScenarios(filePaths, {
onImportError: (file, err) => {
const m = err instanceof Error ? err.message : String(err);
throw new Error(`Failed to import scenario file ${file}: ${m}`);
},
}),
selectors,
);
// Apply failed filter if provided (for --failed flag)
if (failedFilter && failedFilter.length > 0) {
const failedSet = new Set(
failedFilter.map((f) => `${f.name}|${f.file}`),
);
scenarios = scenarios.filter((s) => {
const scenarioFile = s.origin?.path ?? "unknown";
// Check both absolute path and relative path matching
return failedSet.has(`${s.name}|${scenarioFile}`) ||
failedFilter.some((f) =>
f.name === s.name && scenarioFile.endsWith(f.file)
);
});
logger.debug("Applied failed filter", {
filterCount: failedFilter.length,
matchedCount: scenarios.length,
});
}
// Check if aborted during loading phase
if (globalAbortController.signal.aborted) {
throw new Error("Aborted during scenario loading");
}
// Log warning if no scenarios found after filtering
if (scenarios.length === 0) {
logger.info("No scenarios found after applying selectors", {
filePaths,
selectors,
failedFilter: failedFilter?.length ?? 0,
});
}
// Create reporter that streams events to IPC
const reporter = createReporter((output) => writeOutput(ipc, output));
// Run all scenarios using Runner
const runner = new Runner(reporter);
const runResult = await runner.run(scenarios, {
maxConcurrency,
maxFailures,
timeout: timeout > 0 ? timeout : undefined,
signal: globalAbortController.signal,
stepOptions,
});
await writeOutput(
ipc,
{
type: "result",
result: serializeRunResult(runResult),
} satisfies RunOutput,
);
} catch (error) {
await writeOutput(
ipc,
{
type: "error",
error: serializeError(error),
} satisfies RunOutput,
);
} finally {
globalAbortController = null;
}
});