Skip to content

Commit aeedda0

Browse files
committed
fix(scenario): consecutive "wait-serial" commands sometimes fail matching #13
close #13
1 parent 9ae8ab6 commit aeedda0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/ExpectEngine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class ExpectEngine extends EventEmitter {
1717
for (const byte of bytes) {
1818
const char = String.fromCharCode(byte);
1919
if (char === '\n') {
20+
this.emit('line', this.currentLine);
2021
this.testMatches();
2122
this.currentLine = '';
2223
} else {

src/scenario/WaitSerialCommand.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@ import type { IScenarioCommand, TestScenario } from '../TestScenario.js';
55
import { promiseAndResolver } from '../utils/promise.js';
66

77
export class WaitSerialCommand implements IScenarioCommand {
8-
constructor(readonly expectEngine: ExpectEngine) {}
8+
readonly buffer: string[] = [];
9+
10+
constructor(readonly expectEngine: ExpectEngine) {
11+
expectEngine.on('line', (line) => {
12+
this.buffer.push(line);
13+
});
14+
}
915

1016
async run(scenario: TestScenario, client: APIClient, text: string) {
17+
for (let i = 0; i < this.buffer.length; i++) {
18+
const line = this.buffer[i];
19+
if (line.includes(text)) {
20+
this.buffer.splice(0, i + 1);
21+
scenario.log(chalkTemplate`Expected text matched: {green "${text}"}`);
22+
return;
23+
}
24+
}
25+
1126
this.expectEngine.expectTexts.push(text);
1227
const { promise, resolve } = promiseAndResolver();
1328
this.expectEngine.once('match', () => {
@@ -16,6 +31,7 @@ export class WaitSerialCommand implements IScenarioCommand {
1631
if (textIndex >= 0) {
1732
this.expectEngine.expectTexts.splice(textIndex, 1);
1833
}
34+
this.buffer.length = 0;
1935
resolve();
2036
});
2137
await Promise.all([scenario.resume(), promise]);

0 commit comments

Comments
 (0)