diff --git a/internal/api/js.go b/internal/api/js.go index 48ad2ac..3c98e64 100644 --- a/internal/api/js.go +++ b/internal/api/js.go @@ -168,12 +168,16 @@ func (c *JSClient) UserID() string { // Tests should call stopSyncing() at the end of the test. func (c *JSClient) StartSyncing(t *testing.T) (stopSyncing func()) { t.Logf("%s is starting to sync", c.userID) - chrome.MustExecute(t, c.ctx, fmt.Sprintf(`window.__client.on("sync", function(state) { - if (state !== "PREPARED") { - return; - } - console.log("%s"+"sync||{\"type\":\"sync\",\"content\":{}}"); - });`, CONSOLE_LOG_CONTROL_STRING)) + chrome.MustExecute(t, c.ctx, fmt.Sprintf(` + var fn; + fn = function(state) { + if (state !== "SYNCING") { + return; + } + console.log("%s"+"sync||{\"type\":\"sync\",\"content\":{}}"); + window.__client.off("sync", fn); + }; + window.__client.on("sync", fn);`, CONSOLE_LOG_CONTROL_STRING)) ch := make(chan struct{}) cancel := c.listenForUpdates(func(roomID string, ev Event) { if roomID != "sync" { @@ -189,7 +193,6 @@ func (c *JSClient) StartSyncing(t *testing.T) (stopSyncing func()) { } cancel() t.Logf("%s is now syncing", c.userID) - time.Sleep(500 * time.Millisecond) // race condition means we don't query keys yet return func() { chrome.AwaitExecute(t, c.ctx, `window.__client.stopClient();`) } diff --git a/tests/membership_acls_test.go b/tests/membership_acls_test.go index 9cbaae3..eeaf2fc 100644 --- a/tests/membership_acls_test.go +++ b/tests/membership_acls_test.go @@ -52,13 +52,16 @@ func TestAliceBobEncryptionWorks(t *testing.T) { // SDK testing below // ----------------- + + // login both clients first, so OTKs etc are uploaded. alice := MustLoginClient(t, clientTypeA, api.FromComplementClient(csapiAlice, "complement-crypto-password"), ss) defer alice.Close(t) + bob := MustLoginClient(t, clientTypeB, api.FromComplementClient(csapiBob, "complement-crypto-password"), ss) + defer bob.Close(t) // Alice starts syncing aliceStopSyncing := alice.StartSyncing(t) defer aliceStopSyncing() - time.Sleep(time.Second) // TODO: find another way to wait until initial sync is done wantMsgBody := "Hello world" @@ -68,11 +71,8 @@ func TestAliceBobEncryptionWorks(t *testing.T) { must.Equal(t, isEncrypted, true, "room is not encrypted when it should be") // Bob starts syncing - bob := MustLoginClient(t, clientTypeB, api.FromComplementClient(csapiBob, "complement-crypto-password"), ss) - defer bob.Close(t) bobStopSyncing := bob.StartSyncing(t) defer bobStopSyncing() - time.Sleep(time.Second) // TODO: find another way to wait until initial sync is done isEncrypted, err = bob.IsRoomEncrypted(t, roomID) must.NotError(t, "failed to check if room is encrypted", err)