Skip to content

Commit c34ffa0

Browse files
Add test for switching lobbies
1 parent 3df6a8a commit c34ffa0

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

features/basic.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,18 @@ Feature: Players can create and connect a network of players
7575
When "yellow" leaves the lobby
7676
Then "yellow" receives the network event "left"
7777
Then "blue" receives the network event "disconnected" with the argument "[Peer: h5yzwyizlwao]"
78+
79+
Scenario: A player changes lobbies
80+
Given "blue" is connected as "1u8fw4aph5ypt" and ready for game "4307bd86-e1df-41b8-b9df-e22afcf084bd"
81+
And "yellow" is connected as "h5yzwyizlwao" and ready for game "4307bd86-e1df-41b8-b9df-e22afcf084bd"
82+
And "green" is connected as "19yrzmetd2bn7" and ready for game "4307bd86-e1df-41b8-b9df-e22afcf084bd"
83+
84+
Given "blue,yellow" are joined in a lobby
85+
And "green" creates a lobby
86+
When "yellow" leaves the lobby
87+
Then "yellow" receives the network event "left"
88+
Then "blue" receives the network event "disconnected" with the argument "[Peer: h5yzwyizlwao]"
89+
90+
When "yellow" connects to the lobby "prb67ouj837u"
91+
And "green" receives the network event "connected" with the argument "[Peer: h5yzwyizlwao]"
92+
And "blue" has not seen a new "connected" event

features/reconnect.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Feature: Players can create and connect a network of players
2222

2323
And "blue" boardcasts "Hello, world!" over the reliable channel
2424
And "yellow" receives the network event "message" with the arguments "[Peer: 1u8fw4aph5ypt]", "reliable" and "Hello, world!"
25-
And "yellow" has not seen the "reconnecting" event
25+
And "yellow" has not seen any "reconnecting" event
2626

2727

2828
Scenario: Connections are reconnected when rtc is disconnected

features/support/steps/network.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,27 @@ Then('{string} should have received only these lobbies:', function (this: World,
324324
}
325325
})
326326

327-
When('{string} has not seen the {string} event', function (this: World, playerName: string, eventName: string) {
327+
// "has not seen any" checks all events ever received.
328+
When('{string} has not seen any {string} event', function (this: World, playerName: string, eventName: string) {
328329
const player = this.players.get(playerName)
329330
if (player == null) {
330331
throw new Error('no such player')
331332
}
332-
if (player.findEvent(eventName) !== undefined) {
333-
throw new Error(`${playerName} has recieved a ${eventName} event`)
333+
const event = player.findEvent(eventName)
334+
if (event !== undefined) {
335+
throw new Error(`${playerName} has recieved a ${eventName} event: ${event.eventPayload[0]}`)
336+
}
337+
})
338+
339+
// "has not seen a new" only checks new events since the last "receives the network event".
340+
When('{string} has not seen a new {string} event', function (this: World, playerName: string, eventName: string) {
341+
const player = this.players.get(playerName)
342+
if (player == null) {
343+
throw new Error('no such player')
344+
}
345+
const event = player.findNewEvent(eventName)
346+
if (event !== undefined) {
347+
throw new Error(`${playerName} has recieved a ${eventName} event: ${event.eventPayload[0]}`)
334348
}
335349
})
336350

features/support/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ export class Player {
2929
network.on('rtcerror', _ => {})
3030
}
3131

32+
// findEvent finds the first event matching the eventName and matchArguments.
3233
findEvent (eventName: string, matchArguments: any[] = []): RecordedEvent | undefined {
3334
return this.events.find(e => matchEvent(e, eventName, matchArguments))
3435
}
3536

37+
// findRecentEvent finds events starting after the last consumed event.
38+
findNewEvent (eventName: string, matchArguments: any[] = []): RecordedEvent | undefined {
39+
return this.events.slice(this.scanIndex).find(e => matchEvent(e, eventName, matchArguments))
40+
}
41+
42+
// waitForEvent waits for an event to be received, matching the eventName and matchArguments.
43+
// If consume is true, the event will be consumed and the scanIndex will be incremented.
3644
async waitForEvent (eventName: string, matchArguments: any[] = [], consume: boolean = true): Promise<RecordedEvent> {
3745
if (!allEvents.includes(eventName)) {
3846
throw new Error(`Event type ${eventName} not tracked, add to allEvents in types.ts`)

0 commit comments

Comments
 (0)