From 9da1f7b8d596ce91fcba93c4f2e92cd3d41df2e4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 2 Mar 2020 10:54:14 +0000 Subject: [PATCH 1/6] Fix unhomoglyph import to make browser-matrix.js happy once more --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index ec2113a91..c55534325 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -20,7 +20,7 @@ limitations under the License. * @module utils */ -import unhomoglyph from 'unhomoglyph'; +import * as unhomoglyph from 'unhomoglyph'; /** * Encode a dictionary of query parameters. From 7f32d7d32085c5ab939fc364ad985cc22fd661de Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 15 May 2020 16:55:00 +0100 Subject: [PATCH 2/6] revert Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index ed173a982..350c48ae5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -20,7 +20,7 @@ limitations under the License. * @module utils */ -import * as unhomoglyph from 'unhomoglyph'; +import unhomoglyph from 'unhomoglyph'; import {ConnectionError} from "./http-api"; /** From f806e4342ea026900db2257bcb0bbd9682dc9773 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 15 May 2020 18:56:09 +0100 Subject: [PATCH 3/6] Add simple browserify sync test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- package.json | 5 +- spec/browserify/sync-browserify.spec.js | 91 +++++++++++++++++++++++++ spec/setupTests.js | 18 +++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 spec/browserify/sync-browserify.spec.js create mode 100644 spec/setupTests.js diff --git a/package.json b/package.json index 5fa997dae..1956b2814 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,9 @@ "typescript": "^3.7.3" }, "jest": { - "testEnvironment": "node" + "testEnvironment": "node", + "setupFilesAfterEnv": [ + "/spec/setupTests.js" + ] } } diff --git a/spec/browserify/sync-browserify.spec.js b/spec/browserify/sync-browserify.spec.js new file mode 100644 index 000000000..825fcecdd --- /dev/null +++ b/spec/browserify/sync-browserify.spec.js @@ -0,0 +1,91 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import "../../dist/browser-matrix"; // uses browser-matrix instead of the src +import {MockStorageApi} from "../MockStorageApi"; +import {WebStorageSessionStore} from "../../src/store/session/webstorage"; +import MockHttpBackend from "matrix-mock-request"; +import {LocalStorageCryptoStore} from "../../src/crypto/store/localStorage-crypto-store"; +import * as utils from "../test-utils"; + +const USER_ID = "@user:test.server"; +const DEVICE_ID = "device_id"; +const ACCESS_TOKEN = "access_token"; +const ROOM_ID = "!room_id:server.test"; + +/* global matrixcs */ + +describe("Browserify Test", function() { + async function createTestClient() { + const sessionStoreBackend = new MockStorageApi(); + const sessionStore = new WebStorageSessionStore(sessionStoreBackend); + const httpBackend = new MockHttpBackend(); + + const options = { + baseUrl: "http://" + USER_ID + ".test.server", + userId: USER_ID, + accessToken: ACCESS_TOKEN, + deviceId: DEVICE_ID, + sessionStore: sessionStore, + request: httpBackend.requestFn, + cryptoStore: new LocalStorageCryptoStore(sessionStoreBackend), + }; + + const client = matrixcs.createClient(options); + + httpBackend.when("GET", "/pushrules").respond(200, {}); + httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" }); + + return { client, httpBackend }; + } + + it("Sync", async function() { + const {client, httpBackend} = await createTestClient(); + + const event = utils.mkMembership({ + room: ROOM_ID, + mship: "join", + user: "@other_user:server.test", + name: "Displayname", + }); + + const syncData = { + next_batch: "batch1", + rooms: { + join: {}, + }, + }; + syncData.rooms.join[ROOM_ID] = { + timeline: { + events: [ + event, + ], + limited: false, + }, + }; + + httpBackend.when("GET", "/sync").respond(200, syncData); + await Promise.race([ + Promise.all([ + httpBackend.flushAllExpected(), + client.startClient(), + ]), + new Promise((_, reject) => { + client.once("sync.unexpectedError", reject); + }), + ]); + }, 10000); +}); diff --git a/spec/setupTests.js b/spec/setupTests.js new file mode 100644 index 000000000..c0c38fb95 --- /dev/null +++ b/spec/setupTests.js @@ -0,0 +1,18 @@ +/* +Copyright 2020 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// stub for browser-matrix browserify tests +global.XMLHttpRequest = jest.fn(); From e381b1901ed33eac81dcaaf82541c43220ce7afd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 18 May 2020 19:39:30 +0100 Subject: [PATCH 4/6] clean up test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/browserify/sync-browserify.spec.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/spec/browserify/sync-browserify.spec.js b/spec/browserify/sync-browserify.spec.js index 825fcecdd..d11a8f69d 100644 --- a/spec/browserify/sync-browserify.spec.js +++ b/spec/browserify/sync-browserify.spec.js @@ -29,6 +29,9 @@ const ROOM_ID = "!room_id:server.test"; /* global matrixcs */ describe("Browserify Test", function() { + let client; + let httpBackend; + async function createTestClient() { const sessionStoreBackend = new MockStorageApi(); const sessionStore = new WebStorageSessionStore(sessionStoreBackend); @@ -52,9 +55,17 @@ describe("Browserify Test", function() { return { client, httpBackend }; } - it("Sync", async function() { - const {client, httpBackend} = await createTestClient(); + beforeEach(async () => { + ({client, httpBackend} = await createTestClient()); + await client.startClient(); + }); + afterEach(async () => { + client.stopClient(); + await httpBackend.stop(); + }); + + it("Sync", async function() { const event = utils.mkMembership({ room: ROOM_ID, mship: "join", @@ -81,7 +92,6 @@ describe("Browserify Test", function() { await Promise.race([ Promise.all([ httpBackend.flushAllExpected(), - client.startClient(), ]), new Promise((_, reject) => { client.once("sync.unexpectedError", reject); From b9352cfcc109cad2cddfcbaf35351ade8bd9b674 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 18 May 2020 20:03:04 +0100 Subject: [PATCH 5/6] Fix tests Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- package.json | 5 +---- spec/{ => browserify}/setupTests.js | 0 spec/browserify/sync-browserify.spec.js | 7 +++++++ 3 files changed, 8 insertions(+), 4 deletions(-) rename spec/{ => browserify}/setupTests.js (100%) diff --git a/package.json b/package.json index 1956b2814..5fa997dae 100644 --- a/package.json +++ b/package.json @@ -92,9 +92,6 @@ "typescript": "^3.7.3" }, "jest": { - "testEnvironment": "node", - "setupFilesAfterEnv": [ - "/spec/setupTests.js" - ] + "testEnvironment": "node" } } diff --git a/spec/setupTests.js b/spec/browserify/setupTests.js similarity index 100% rename from spec/setupTests.js rename to spec/browserify/setupTests.js diff --git a/spec/browserify/sync-browserify.spec.js b/spec/browserify/sync-browserify.spec.js index d11a8f69d..91d430c0b 100644 --- a/spec/browserify/sync-browserify.spec.js +++ b/spec/browserify/sync-browserify.spec.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +// load XmlHttpRequest mock +import "./setupTests"; import "../../dist/browser-matrix"; // uses browser-matrix instead of the src import {MockStorageApi} from "../MockStorageApi"; import {WebStorageSessionStore} from "../../src/store/session/webstorage"; @@ -65,6 +67,11 @@ describe("Browserify Test", function() { await httpBackend.stop(); }); + afterAll(() => { + // clean up XMLHttpRequest mock + global.XMLHttpRequest = undefined; + }); + it("Sync", async function() { const event = utils.mkMembership({ room: ROOM_ID, From 661901b00d419162ee1bde55c20f8f57f3f71bdd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 18 May 2020 20:11:32 +0100 Subject: [PATCH 6/6] tidy up Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/browserify/setupTests.js | 5 +++++ spec/browserify/sync-browserify.spec.js | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/browserify/setupTests.js b/spec/browserify/setupTests.js index c0c38fb95..16120f78a 100644 --- a/spec/browserify/setupTests.js +++ b/spec/browserify/setupTests.js @@ -16,3 +16,8 @@ limitations under the License. // stub for browser-matrix browserify tests global.XMLHttpRequest = jest.fn(); + +afterAll(() => { + // clean up XMLHttpRequest mock + global.XMLHttpRequest = undefined; +}); diff --git a/spec/browserify/sync-browserify.spec.js b/spec/browserify/sync-browserify.spec.js index 91d430c0b..c2054d7f1 100644 --- a/spec/browserify/sync-browserify.spec.js +++ b/spec/browserify/sync-browserify.spec.js @@ -67,11 +67,6 @@ describe("Browserify Test", function() { await httpBackend.stop(); }); - afterAll(() => { - // clean up XMLHttpRequest mock - global.XMLHttpRequest = undefined; - }); - it("Sync", async function() { const event = utils.mkMembership({ room: ROOM_ID,