-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.ts
More file actions
171 lines (149 loc) · 4.13 KB
/
app.ts
File metadata and controls
171 lines (149 loc) · 4.13 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
#!/usr/bin/env node
import { EventEmitter } from "node:events";
import { existsSync } from "node:fs";
import { join, resolve } from "node:path";
import { initSimnet } from "@stacks/clarinet-sdk";
import { red, yellow } from "ansicolor";
import {
helpMessage,
logRunConfig,
logWarnings,
parseCli,
type RunConfig,
} from "./cli";
import { resolveAccounts } from "./config";
import { checkInvariants } from "./invariant";
import { checkProperties } from "./property";
import {
getContractNameFromContractId,
getFunctionsFromContractInterfaces,
getSimnetDeployerContractsInterfaces,
} from "./shared";
const logger = (log: string, logLevel: "log" | "error" | "info" = "log") => {
console[logLevel](log);
};
/**
* Gets the manifest file name for a Clarinet project.
* If a custom manifest exists (`Clarinet-<contract-name>.toml`), it is used.
* Otherwise, the default `Clarinet.toml` is returned.
* @param manifestDir The relative path to the Clarinet project directory.
* @param targetContractName The target contract name.
* @returns The manifest file name.
*/
export const getManifestFileName = (
manifestDir: string,
targetContractName: string,
) => {
const isCustomManifest = existsSync(
resolve(manifestDir, `Clarinet-${targetContractName}.toml`),
);
if (isCustomManifest) {
return `Clarinet-${targetContractName}.toml`;
}
return "Clarinet.toml";
};
const parseCliOrExit = (
argv: string[],
radio: EventEmitter,
): RunConfig | undefined => {
try {
return parseCli(argv);
} catch (err: unknown) {
radio.emit(
"logFailure",
`\n${err instanceof Error ? err.message : String(err)}`,
);
radio.emit("logMessage", helpMessage);
process.exit(1);
}
};
export const main = async () => {
const radio = new EventEmitter();
radio.on("logMessage", (log) => logger(log));
radio.on("logInfo", (log) => logger(yellow(log), "info"));
radio.on("logFailure", (log) => logger(red(log), "error"));
const runConfig = parseCliOrExit(process.argv.slice(2), radio);
if (!runConfig) {
radio.emit("logMessage", helpMessage);
return;
}
logWarnings(radio, runConfig.warnings);
const manifestPath = join(
runConfig.manifestDir,
getManifestFileName(runConfig.manifestDir, runConfig.sutContractName),
);
logRunConfig(radio, runConfig, manifestPath);
const simnet = await initSimnet(manifestPath);
const { eligibleAccounts, allAddresses } = resolveAccounts(
simnet.getAccounts(),
runConfig.accounts,
runConfig.accountsMode,
);
const resetSession = async () => {
await initSimnet(manifestPath);
radio.emit("logMessage", "Simnet session reset.");
};
/**
* The list of contract IDs for the SUT contract names, as per the simnet.
*/
const rendezvousList = [
...getSimnetDeployerContractsInterfaces(simnet).keys(),
].filter(
(deployedContract) =>
getContractNameFromContractId(deployedContract) ===
runConfig.sutContractName,
);
if (rendezvousList.length === 0) {
radio.emit(
"logFailure",
`\nContract "${runConfig.sutContractName}" not found among project contracts.\n`,
);
return;
}
const rendezvousAllFunctions = getFunctionsFromContractInterfaces(
new Map(
[...getSimnetDeployerContractsInterfaces(simnet)].filter(([contractId]) =>
rendezvousList.includes(contractId),
),
),
);
// Select the testing routine based on `type`.
switch (runConfig.type) {
case "invariant": {
await checkInvariants(
simnet,
resetSession,
rendezvousList,
rendezvousAllFunctions,
runConfig.seed,
runConfig.runs,
runConfig.dial,
runConfig.bail,
runConfig.regr,
radio,
eligibleAccounts,
allAddresses,
);
break;
}
case "test": {
await checkProperties(
simnet,
resetSession,
rendezvousList,
rendezvousAllFunctions,
runConfig.seed,
runConfig.runs,
runConfig.bail,
runConfig.regr,
radio,
eligibleAccounts,
allAddresses,
);
break;
}
}
};
if (require.main === module) {
main();
}