-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathheatstroke.ts
More file actions
367 lines (334 loc) · 11.6 KB
/
heatstroke.ts
File metadata and controls
367 lines (334 loc) · 11.6 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import type { EventEmitter } from "node:events";
import type { ContractInterfaceFunction } from "@stacks/clarinet-sdk-wasm";
import { green } from "ansicolor";
import type {
InvariantCounterExample,
RunDetails,
Statistics,
StatisticsTreeOptions,
TestCounterExample,
} from "./heatstroke.types";
import type { FalsifiedInvariantError } from "./invariant";
import type { PropertyTestError } from "./property";
import { getContractNameFromContractId } from "./shared";
/**
* Heatstrokes Reporter
*
* Provides a detailed report when a test run test ends. In case of failure,
* includes the contract, functions, arguments, outputs, and the specific
* invariant or property test that failed.
* @param runDetails The details of the test run provided by fast-check.
* @property `runDetails.failed`: Indicates if the test run failed.
* @property `runDetails.counterexample`: The input that caused the failure.
* @property `runDetails.numRuns`: The number of test cases that were run.
* @property `runDetails.seed`: The replay seed for reproducing the failure.
* @property `runDetails.path`: The replay path for reproducing the failure.
* @property `runDetails.error`: The error thrown during the test.
* @param radio The event emitter to log messages.
* @param type The type of test that failed: invariant or property.
* @returns void
*/
export const reporter = (
runDetails: RunDetails,
radio: EventEmitter,
type: "invariant" | "test",
statistics: Statistics,
) => {
const { counterexample, failed, numRuns, path, seed } = runDetails;
if (failed) {
const error = runDetails.errorInstance || runDetails.error;
// Extract the actual Clarity error once for both error types.
const clarityError =
(error as FalsifiedInvariantError | PropertyTestError)?.clarityError ||
error?.message ||
error?.toString() ||
"Unknown error";
// Report general run data.
radio.emit(
"logFailure",
`\nError: Property failed after ${numRuns} tests.`,
);
radio.emit("logFailure", `Seed : ${seed}`);
if (path) {
radio.emit("logFailure", `Path : ${path}`);
}
switch (type) {
case "invariant": {
const ce = counterexample[0] as InvariantCounterExample;
// Report specific run data for the invariant testing type.
radio.emit("logFailure", `\nCounterexample:`);
radio.emit(
"logFailure",
`- Contract : ${getContractNameFromContractId(
ce.rendezvousContractId,
)}`,
);
radio.emit(
"logFailure",
`- Functions: ${ce.selectedFunctions
.map(
(selectedFunction: ContractInterfaceFunction) =>
selectedFunction.name,
)
.join(", ")} (${ce.selectedFunctions
.map(
(selectedFunction: ContractInterfaceFunction) =>
selectedFunction.access,
)
.join(", ")})`,
);
radio.emit(
"logFailure",
`- Arguments: ${ce.selectedFunctionsArgsList
.map((selectedFunctionArgs: any[]) =>
JSON.stringify(selectedFunctionArgs),
)
.join(", ")}`,
);
radio.emit(
"logFailure",
`- Callers : ${ce.sutCallers
.map((sutCaller: [string, string]) => sutCaller[0])
.join(", ")}`,
);
radio.emit(
"logFailure",
`- Outputs : ${ce.selectedFunctions
.map((selectedFunction: ContractInterfaceFunction) =>
JSON.stringify(selectedFunction.outputs),
)
.join(", ")}`,
);
radio.emit(
"logFailure",
`- Invariant: ${ce.selectedInvariant.name} (${ce.selectedInvariant.access})`,
);
radio.emit(
"logFailure",
`- Arguments: ${JSON.stringify(ce.invariantArgs)}`,
);
radio.emit("logFailure", `- Caller : ${ce.invariantCaller[0]}`);
radio.emit(
"logFailure",
`\nWhat happened? Rendezvous went on a rampage and found a weak spot:\n`,
);
const formattedError = `The invariant "${
ce.selectedInvariant.name
}" returned:\n\n${clarityError
.toString()
.split("\n")
.map((line: string) => " " + line)
.join("\n")}\n`;
radio.emit("logFailure", formattedError);
break;
}
case "test": {
const ce = counterexample[0] as TestCounterExample;
// Report specific run data for the property testing type.
radio.emit("logFailure", `\nCounterexample:`);
radio.emit(
"logFailure",
`- Contract : ${getContractNameFromContractId(
ce.rendezvousContractId,
)}`,
);
radio.emit(
"logFailure",
`- Test Function : ${ce.selectedTestFunction.name} (${ce.selectedTestFunction.access})`,
);
radio.emit(
"logFailure",
`- Arguments : ${JSON.stringify(ce.functionArgs)}`,
);
radio.emit("logFailure", `- Caller : ${ce.testCaller[0]}`);
radio.emit(
"logFailure",
`- Outputs : ${JSON.stringify(ce.selectedTestFunction.outputs)}`,
);
radio.emit(
"logFailure",
`\nWhat happened? Rendezvous went on a rampage and found a weak spot:\n`,
);
const formattedError = `The test function "${
ce.selectedTestFunction.name
}" returned:\n\n${clarityError
.toString()
.split("\n")
.map((line: string) => " " + line)
.join("\n")}\n`;
radio.emit("logFailure", formattedError);
break;
}
}
// Set non-zero exit code to properly signal test failure to shells,
// scripts, CI systems, and other tools that check process exit status.
process.exitCode = 1;
} else {
radio.emit(
"logMessage",
green(
`\nOK, ${
type === "invariant" ? "invariants" : "properties"
} passed after ${numRuns} runs.\n`,
),
);
}
reportStatistics(statistics, type, radio);
radio.emit("logMessage", "\n");
};
const ARROW = "->";
const SUCCESS_SYMBOL = "+";
const FAIL_SYMBOL = "-";
const WARN_SYMBOL = "!";
/**
* Reports execution statistics in a tree-like format.
* @param statistics The statistics object containing test execution data.
* @param type The type of test being reported.
* @param radio The event emitter for logging messages.
*/
const reportStatistics = (
statistics: Statistics,
type: "invariant" | "test",
radio: EventEmitter,
): void => {
if (
(type === "invariant" && (!statistics.invariant || !statistics.sut)) ||
(type === "test" && !statistics.test)
) {
radio.emit("logMessage", "No statistics available for this run");
return;
}
radio.emit("logMessage", `\nEXECUTION STATISTICS\n`);
switch (type) {
case "invariant": {
radio.emit("logMessage", "│ PUBLIC FUNCTION CALLS");
radio.emit("logMessage", "│");
radio.emit("logMessage", `├─ ${SUCCESS_SYMBOL} SUCCESSFUL`);
logAsTree(Object.fromEntries(statistics.sut!.successful), radio);
radio.emit("logMessage", "│");
radio.emit("logMessage", `├─ ${FAIL_SYMBOL} IGNORED`);
logAsTree(Object.fromEntries(statistics.sut!.failed), radio);
radio.emit("logMessage", "│");
radio.emit("logMessage", "│ INVARIANT CHECKS");
radio.emit("logMessage", "│");
radio.emit("logMessage", `├─ ${SUCCESS_SYMBOL} PASSED`);
logAsTree(Object.fromEntries(statistics.invariant!.successful), radio);
radio.emit("logMessage", "│");
radio.emit("logMessage", `└─ ${FAIL_SYMBOL} FAILED`);
logAsTree(Object.fromEntries(statistics.invariant!.failed), radio, {
isLastSection: true,
});
radio.emit("logMessage", "\nLEGEND:\n");
radio.emit(
"logMessage",
" SUCCESSFUL calls executed and advanced the test",
);
radio.emit(
"logMessage",
" IGNORED calls failed but did not affect the test",
);
radio.emit(
"logMessage",
" PASSED invariants maintained system integrity",
);
radio.emit(
"logMessage",
" FAILED invariants indicate contract vulnerabilities",
);
if (computeTotalCount(statistics.invariant!.failed) > 0) {
radio.emit(
"logFailure",
"\n! FAILED invariants require immediate attention as they indicate that your contract can enter an invalid state under certain conditions.",
);
}
break;
}
case "test": {
radio.emit("logMessage", "│ PROPERTY TEST CALLS");
radio.emit("logMessage", "│");
radio.emit("logMessage", `├─ ${SUCCESS_SYMBOL} PASSED`);
logAsTree(Object.fromEntries(statistics.test!.successful), radio);
radio.emit("logMessage", "│");
radio.emit("logMessage", `├─ ${WARN_SYMBOL} DISCARDED`);
logAsTree(Object.fromEntries(statistics.test!.discarded), radio);
radio.emit("logMessage", "│");
radio.emit("logMessage", `└─ ${FAIL_SYMBOL} FAILED`);
logAsTree(Object.fromEntries(statistics.test!.failed), radio, {
isLastSection: true,
});
radio.emit("logMessage", "\nLEGEND:\n");
radio.emit(
"logMessage",
" PASSED properties verified for given inputs",
);
radio.emit(
"logMessage",
" DISCARDED skipped due to invalid preconditions",
);
radio.emit(
"logMessage",
" FAILED property violations or unexpected behavior",
);
if (computeTotalCount(statistics.test!.failed) > 0) {
radio.emit(
"logFailure",
"\n! FAILED tests indicate that your function properties don't hold for all inputs. Review the counterexamples above for debugging.",
);
}
break;
}
}
};
/**
* Displays a tree structure of data.
* @param tree The object to display as a tree.
* @param radio The event emitter for logging messages.
* @param options Configuration options for tree display.
*/
const logAsTree = (
tree: Record<string, any>,
radio: EventEmitter,
options: StatisticsTreeOptions = {},
): void => {
const { isLastSection = false, baseIndent = " " } = options;
const printTree = (
node: Record<string, any>,
radioEmitter: EventEmitter,
indent: string = baseIndent,
isLastParent = true,
): void => {
const keys = Object.keys(node);
keys.forEach((key, index) => {
const isLast = index === keys.length - 1;
const connector = isLast ? "└─" : "├─";
const nextIndent = indent + (isLastParent ? " " : "│ ");
const leadingChar = isLastSection ? " " : "│";
if (typeof node[key] === "object" && node[key] !== null) {
radioEmitter.emit(
"logMessage",
`${leadingChar} ${indent}${connector} ${ARROW} ${key}`,
);
printTree(node[key], radioEmitter, nextIndent, isLast);
} else {
const count = node[key] as number;
radioEmitter.emit(
"logMessage",
`${leadingChar} ${indent}${connector} ${key}: x${count}`,
);
}
});
};
printTree(tree, radio, baseIndent);
};
/**
* Computes the total number of failures from a failure map.
* @param failedMap Map containing failure counts by test name
* @returns The sum of all failure counts
*/
const computeTotalCount = (failedMap: Map<string, number>): number => {
let totalFailures = 0;
for (const count of failedMap.values()) {
totalFailures += count;
}
return totalFailures;
};