@@ -3,19 +3,20 @@ import {
33 LATEST_VERSION ,
44 asyncSafety ,
55 getSnapshotForComparison ,
6- sleep ,
76} from "@cursorless/common" ;
87import { getRecordedTestsDirPath , loadFixture } from "@cursorless/node-common" ;
98import {
109 getCursorlessApi ,
1110 runCursorlessCommand ,
11+ type SpyWebViewEvent ,
1212} from "@cursorless/vscode-common" ;
13+ import { isEqual } from "lodash-es" ;
1314import assert from "node:assert" ;
1415import path from "path" ;
1516import sinon from "sinon" ;
1617import { commands } from "vscode" ;
17- import { endToEndTestSetup , sleepWithBackoff } from "../../endToEndTestSetup" ;
18- import { isEqual } from "lodash-es " ;
18+ import { endToEndTestSetup } from "../../endToEndTestSetup" ;
19+ import { waitFor } from "../waitFor " ;
1920
2021suite ( "tutorial" , async function ( ) {
2122 const { getSpy } = endToEndTestSetup ( this ) ;
@@ -73,56 +74,42 @@ async function runBasicTutorialTest(spyIde: SpyIDE) {
7374 ) ;
7475 await checkStepSetup ( fixtures [ 0 ] ) ;
7576
76- // Allow for debounce
77- await sleep ( 100 ) ;
78-
79- // Another sleep just in case
80- await sleepWithBackoff ( 50 ) ;
81-
8277 // We allow duplicate messages because they're idempotent. Not sure why some
8378 // platforms get the init message twice but it doesn't matter.
84- const result = getTutorialWebviewEventLog ( ) ;
79+
8580 // This is the initial message that the webview sends to the extension.
8681 // Seeing this means that the javascript in the webview successfully loaded.
87- assert (
88- result . some ( ( e ) =>
89- isEqual ( e , {
90- type : "messageReceived" ,
91- data : {
92- type : "getInitialState" ,
93- } ,
94- } ) ,
95- ) ,
82+ await waitForEvent (
83+ getTutorialWebviewEventLog ,
84+ ( e ) => e . type === "messageReceived" && e . data . type === "getInitialState" ,
9685 ) ;
9786
9887 // This is the response from the extension to the webview's initial message.
99- assert (
100- result . some ( ( e ) =>
101- isEqual ( e , {
102- type : "messageSent" ,
103- data : {
104- type : "doingTutorial" ,
105- hasErrors : false ,
106- id : "tutorial-1-basics" ,
107- stepNumber : 0 ,
108- stepContent : [
109- [
110- {
111- type : "string" ,
112- value : "Say " ,
113- } ,
114- {
115- type : "command" ,
116- value : "take cap" ,
117- } ,
118- ] ,
88+ await waitForEvent ( getTutorialWebviewEventLog , ( e ) =>
89+ isEqual ( e , {
90+ type : "messageSent" ,
91+ data : {
92+ type : "doingTutorial" ,
93+ hasErrors : false ,
94+ id : "tutorial-1-basics" ,
95+ stepNumber : 0 ,
96+ stepContent : [
97+ [
98+ {
99+ type : "string" ,
100+ value : "Say " ,
101+ } ,
102+ {
103+ type : "command" ,
104+ value : "take cap" ,
105+ } ,
119106 ] ,
120- stepCount : 11 ,
121- title : "Introduction" ,
122- preConditionsMet : true ,
123- } ,
124- } ) ,
125- ) ,
107+ ] ,
108+ stepCount : 11 ,
109+ title : "Introduction" ,
110+ preConditionsMet : true ,
111+ } ,
112+ } ) ,
126113 ) ;
127114
128115 // Check that we focus the tutorial webview when the user starts the tutorial
@@ -149,18 +136,10 @@ async function runBasicTutorialTest(spyIde: SpyIDE) {
149136 usePrePhraseSnapshot : false ,
150137 } ) ;
151138
152- // Allow for debounce
153- await sleep ( 100 ) ;
154-
155- // Another sleep just in case
156- await sleepWithBackoff ( 50 ) ;
157-
158- assert (
159- getTutorialWebviewEventLog ( ) . some (
160- ( message ) =>
161- message . type === "messageSent" &&
162- message . data . preConditionsMet === false ,
163- ) ,
139+ await waitForEvent (
140+ getTutorialWebviewEventLog ,
141+ ( event ) =>
142+ event . type === "messageSent" && event . data . preConditionsMet === false ,
164143 ) ;
165144
166145 // Test resuming tutorial
@@ -234,3 +213,15 @@ const runNoOpCursorlessCommand = () =>
234213 } ,
235214 usePrePhraseSnapshot : false ,
236215 } ) ;
216+
217+ async function waitForEvent (
218+ getTutorialWebviewEventLog : ( ) => SpyWebViewEvent [ ] ,
219+ predicate : ( event : SpyWebViewEvent ) => boolean ,
220+ ) {
221+ const success = await waitFor ( ( ) =>
222+ getTutorialWebviewEventLog ( ) . some ( predicate ) ,
223+ ) ;
224+ if ( ! success ) {
225+ assert . fail ( "Timed out waiting for tutorial event log" ) ;
226+ }
227+ }
0 commit comments