Skip to content

Commit 1e15431

Browse files
committed
feat(scenario): expect-pin command
1 parent 58f35b7 commit 1e15431

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/APIClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
APIResponse,
88
APIResultError,
99
APISimStartParams,
10+
PinReadResponse,
1011
} from './APITypes';
1112

1213
const DEFAULT_SERVER = process.env.WOKWI_CLI_SERVER ?? 'wss://wokwi.com/api/ws/beta';
@@ -135,6 +136,10 @@ export class APIClient {
135136
return await this.sendCommand('control:set', { part: partId, control, value });
136137
}
137138

139+
async pinRead(partId: string, pin: string) {
140+
return await this.sendCommand<PinReadResponse>('pin:read', { part: partId, pin });
141+
}
142+
138143
async sendCommand<T = unknown>(command: string, params?: any) {
139144
return await new Promise<T>((resolve, reject) => {
140145
const id = this.lastId++;

src/APITypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ export interface APISimStartParams {
5353
pause?: boolean;
5454
chips?: string[];
5555
}
56+
57+
export interface PinReadResponse {
58+
pin: string;
59+
value: number;
60+
}

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { readVersion } from './readVersion';
1515
import { DelayCommand } from './scenario/DelayCommand';
1616
import { SetControlCommand } from './scenario/SetControlCommand';
1717
import { WaitSerialCommand } from './scenario/WaitSerialCommand';
18+
import { ExpectPinCommand } from './scenario/ExpectPinCommand';
1819

1920
const millis = 1_000_000;
2021

@@ -117,6 +118,7 @@ async function main() {
117118
);
118119
scenario.registerCommands({
119120
delay: new DelayCommand(eventManager),
121+
'expect-pin': new ExpectPinCommand(),
120122
'set-control': new SetControlCommand(),
121123
'wait-serial': new WaitSerialCommand(expectEngine),
122124
});

src/scenario/ExpectPinCommand.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import chalk from 'chalk';
2+
import type { APIClient } from '../APIClient';
3+
import type { IScenarioCommand, TestScenario } from '../TestScenario';
4+
5+
export interface IExpectPinParams {
6+
'part-id': string;
7+
pin: string;
8+
value: number;
9+
}
10+
11+
export class ExpectPinCommand implements IScenarioCommand {
12+
async run(scenario: TestScenario, client: APIClient, params: IExpectPinParams) {
13+
const partId = params['part-id'];
14+
const pinName = params.pin;
15+
const expectedValue = params.value;
16+
scenario.log(
17+
chalk`expect-pin {yellow ${partId}}:{magenta ${pinName}} == {yellow ${expectedValue}}`
18+
);
19+
const value = (await client.pinRead(partId, pinName))?.value ? 1 : 0;
20+
if (value !== expectedValue) {
21+
scenario.fail(
22+
chalk`GPIO {yellow ${partId}}:{magenta ${pinName}} expected to be {yellow ${expectedValue}} but was {red ${value}}`
23+
);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)