Skip to content

Commit a69b5bd

Browse files
committed
feat(scenario): write-serial command
1 parent 6224464 commit a69b5bd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ExpectPinCommand } from './scenario/ExpectPinCommand.js';
1717
import { SetControlCommand } from './scenario/SetControlCommand.js';
1818
import { WaitSerialCommand } from './scenario/WaitSerialCommand.js';
1919
import { uploadFirmware } from './uploadFirmware.js';
20+
import { WriteSerialCommand } from './scenario/WriteSerialCommand.js';
2021

2122
const millis = 1_000_000;
2223

@@ -143,6 +144,7 @@ async function main() {
143144
'expect-pin': new ExpectPinCommand(),
144145
'set-control': new SetControlCommand(),
145146
'wait-serial': new WaitSerialCommand(expectEngine),
147+
'write-serial': new WriteSerialCommand(),
146148
});
147149
scenario.validate();
148150
}

src/scenario/WriteSerialCommand.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { APIClient } from '../APIClient.js';
2+
import type { IScenarioCommand, TestScenario } from '../TestScenario.js';
3+
4+
const encoder = new TextEncoder();
5+
6+
export class WriteSerialCommand implements IScenarioCommand {
7+
validate(value: string | number[]) {
8+
if (value instanceof Array) {
9+
if (value.length === 0) {
10+
throw new Error(`Array must contain at least one number`);
11+
}
12+
if (value.some((v) => typeof v !== 'number')) {
13+
throw new Error(`Array must contain only numbers`);
14+
}
15+
return;
16+
}
17+
if (typeof value !== 'string') {
18+
throw new Error(`Value must be a string or an array of numbers`);
19+
}
20+
}
21+
22+
async run(scenario: TestScenario, client: APIClient, text: string | number[]) {
23+
const data = text instanceof Array ? new Uint8Array(text) : encoder.encode(text);
24+
await client.serialMonitorWrite(data);
25+
await scenario.resume();
26+
}
27+
}

0 commit comments

Comments
 (0)