-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.ts
286 lines (247 loc) · 8.71 KB
/
main.ts
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
import arg from 'arg';
import chalkTemplate from 'chalk-template';
import { createWriteStream, existsSync, readFileSync, writeFileSync } from 'fs';
import path, { join } from 'path';
import YAML from 'yaml';
import { APIClient } from './APIClient.js';
import type { APIEvent, ChipsLogPayload, SerialMonitorDataPayload } from './APITypes.js';
import { EventManager } from './EventManager.js';
import { ExpectEngine } from './ExpectEngine.js';
import { TestScenario } from './TestScenario.js';
import { parseConfig } from './config.js';
import { cliHelp } from './help.js';
import { loadChips } from './loadChips.js';
import { readVersion } from './readVersion.js';
import { DelayCommand } from './scenario/DelayCommand.js';
import { ExpectPinCommand } from './scenario/ExpectPinCommand.js';
import { SetControlCommand } from './scenario/SetControlCommand.js';
import { WaitSerialCommand } from './scenario/WaitSerialCommand.js';
import { WaitPinCommand } from './scenario/WaitPinCommand.js';
import { WaitPinChangeCommand } from './scenario/WaitPinChangeCommand.js';
import { uploadFirmware } from './uploadFirmware.js';
const millis = 1_000_000;
async function main() {
const args = arg(
{
'--help': Boolean,
'--quiet': Boolean,
'--version': Boolean,
'--elf': String,
'--expect-text': String,
'--fail-text': String,
'--interactive': Boolean,
'--serial-log-file': String,
'--scenario': String,
'--screenshot-part': String,
'--screenshot-file': String,
'--screenshot-time': Number,
'--timeout': Number,
'--timeout-exit-code': Number,
'-h': '--help',
'-q': '--quiet',
},
{ argv: process.argv.slice(2) },
);
const quiet = args['--quiet'];
const elf = args['--elf'];
const expectText = args['--expect-text'];
const failText = args['--fail-text'];
const interactive = args['--interactive'];
const serialLogFile = args['--serial-log-file'];
const scenarioFile = args['--scenario'];
const timeout = args['--timeout'] ?? 30000;
const screenshotPart = args['--screenshot-part'];
const screenshotTime = args['--screenshot-time'];
const screenshotFile = args['--screenshot-file'] ?? 'screenshot.png';
const timeoutExitCode = args['--timeout-exit-code'] ?? 42;
const timeoutNanos = timeout * millis;
if (!quiet) {
const { sha, version } = readVersion();
console.log(`Wokwi CLI v${version} (${sha})`);
}
if (args['--help']) {
cliHelp();
process.exit(0);
}
const token = process.env.WOKWI_CLI_TOKEN;
if (token == null || token.length === 0) {
console.error(
`Error: Missing WOKWI_CLI_TOKEN environment variable. Please set it to your Wokwi token.\nGet your token at https://wokwi.com/dashboard/ci.`,
);
process.exit(1);
}
const rootDir = args._[0] || '.';
const configPath = `${rootDir}/wokwi.toml`;
const diagramFile = `${rootDir}/diagram.json`;
const configExists = existsSync(configPath);
if (!elf && !configExists) {
console.error(`Error: wokwi.toml not found in ${path.resolve(rootDir)}`);
process.exit(1);
}
if (!existsSync(diagramFile)) {
console.error(`Error: diagram.json not found in ${path.resolve(rootDir)}`);
process.exit(1);
}
let firmwarePath;
let elfPath;
let config;
if (configExists) {
const configData = readFileSync(configPath, 'utf8');
config = await parseConfig(configData, rootDir);
firmwarePath = elf ?? join(rootDir, config.wokwi.firmware);
elfPath = elf ?? join(rootDir, config.wokwi.elf);
} else if (elf) {
firmwarePath = elf;
elfPath = elf;
} else {
throw new Error('Internal error: neither elf nor config exists');
}
if (!existsSync(firmwarePath)) {
console.error(`Error: firmware file not found: ${path.resolve(firmwarePath)}`);
process.exit(1);
}
if (!existsSync(elfPath)) {
console.error(`Error: ELF file not found: ${path.resolve(elfPath)}`);
process.exit(1);
}
const diagram = readFileSync(diagramFile, 'utf8');
const chips = loadChips(config?.chip ?? [], rootDir);
const resolvedScenarioFile = scenarioFile ? path.resolve(rootDir, scenarioFile) : null;
if (resolvedScenarioFile && !existsSync(resolvedScenarioFile)) {
console.error(`Error: scenario file not found: ${path.resolve(resolvedScenarioFile)}`);
process.exit(1);
}
const eventManager = new EventManager();
const expectEngine = new ExpectEngine();
let scenario;
if (resolvedScenarioFile) {
scenario = new TestScenario(
YAML.parse(readFileSync(resolvedScenarioFile, 'utf-8')),
eventManager,
);
scenario.registerCommands({
delay: new DelayCommand(eventManager),
'expect-pin': new ExpectPinCommand(),
'set-control': new SetControlCommand(),
'wait-serial': new WaitSerialCommand(expectEngine),
'wait-pin': new WaitPinCommand(),
'wait-pin-change': new WaitPinChangeCommand(),
});
scenario.validate();
}
const serialLogStream = serialLogFile ? createWriteStream(serialLogFile) : null;
if (expectText) {
expectEngine.expectTexts.push(expectText);
expectEngine.on('match', (text) => {
if (text !== expectText) {
return;
}
if (!quiet) {
console.log(chalkTemplate`\n\nExpected text found: {green "${expectText}"}`);
console.log('TEST PASSED.');
}
client.close();
process.exit(0);
});
}
if (failText) {
expectEngine.failTexts.push(failText);
expectEngine.on('fail', (text) => {
if (text !== failText) {
return;
}
console.error(chalkTemplate`\n\n{red Error:} Unexpected text found: {yellow "${text}"}`);
console.error('TEST FAILED.');
client.close();
process.exit(1);
});
}
const client = new APIClient(token);
client.onConnected = (hello) => {
if (!quiet) {
console.log(`Connected to Wokwi Simulation API ${hello.appVersion}`);
}
};
client.onError = (error) => {
console.error('API Error:', error.message);
process.exit(1);
};
try {
await client.connected;
await client.fileUpload('diagram.json', diagram);
const firmwareName = await uploadFirmware(client, firmwarePath);
await client.fileUpload('firmware.elf', readFileSync(elfPath));
for (const chip of chips) {
await client.fileUpload(`${chip.name}.chip.json`, readFileSync(chip.jsonPath, 'utf-8'));
await client.fileUpload(`${chip.name}.chip.wasm`, readFileSync(chip.wasmPath));
}
if (!quiet) {
console.log('Starting simulation...');
}
const scenarioPromise = scenario?.start(client);
if (timeoutNanos) {
eventManager.at(timeoutNanos, () => {
// We are using setImmediate to make sure other events (e.g. screen shot) are processed first
setImmediate(() => {
void eventManager.eventHandlersInProgress.then(() => {
console.error(`Timeout: simulation did not finish in ${timeout}ms`);
client.close();
process.exit(timeoutExitCode);
});
});
});
}
if (screenshotPart != null && screenshotTime != null) {
eventManager.at(screenshotTime * millis, async (t) => {
const result = await client.framebufferRead(screenshotPart);
writeFileSync(screenshotFile, result.png, 'base64');
});
}
await client.serialMonitorListen();
const { timeToNextEvent } = eventManager;
client.onEvent = (event) => {
if (event.event === 'sim:pause') {
eventManager.processEvents(event.nanos);
if (eventManager.timeToNextEvent >= 0) {
void client.simResume(eventManager.timeToNextEvent);
}
}
if (event.event === 'serial-monitor:data') {
const { bytes } = (event as APIEvent<SerialMonitorDataPayload>).payload;
for (const byte of bytes) {
process.stdout.write(String.fromCharCode(byte));
}
serialLogStream?.write(Buffer.from(bytes));
expectEngine.feed(bytes);
}
if (event.event === 'chips:log') {
const { message, chip } = (event as APIEvent<ChipsLogPayload>).payload;
console.log(chalkTemplate`[{magenta ${chip}}] ${message}`);
}
};
await client.simStart({
elf: 'test.elf',
firmware: firmwareName,
chips: chips.map((chip) => chip.name),
pause: timeToNextEvent >= 0,
});
if (interactive) {
process.stdin.pipe(client.serialMonitorWritable());
}
if (timeToNextEvent > 0) {
await client.simResume(timeToNextEvent);
}
if (scenarioPromise) {
await scenarioPromise;
} else {
// wait forever - until there's an error, timeout, serial output match, or the user presses Ctrl+C
await new Promise(() => {});
}
} finally {
client.close();
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});