1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-07 23:02:56 +03:00

Apply prettier formatting

This commit is contained in:
Michael Weimann
2022-12-09 09:38:20 +01:00
parent 08a9073bd5
commit 349c2c2587
239 changed files with 22004 additions and 21928 deletions

View File

@@ -14,21 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import MockHttpBackend from 'matrix-mock-request';
import { indexedDB as fakeIndexedDB } from 'fake-indexeddb';
import MockHttpBackend from "matrix-mock-request";
import { indexedDB as fakeIndexedDB } from "fake-indexeddb";
import { IndexedDBStore, MatrixEvent, MemoryStore, Room } from "../../src";
import { MatrixClient } from "../../src/client";
import { ToDeviceBatch } from '../../src/models/ToDeviceMessage';
import { logger } from '../../src/logger';
import { IStore } from '../../src/store';
import { flushPromises } from '../test-utils/flushPromises';
import { ToDeviceBatch } from "../../src/models/ToDeviceMessage";
import { logger } from "../../src/logger";
import { IStore } from "../../src/store";
import { flushPromises } from "../test-utils/flushPromises";
import { removeElement } from "../../src/utils";
const FAKE_USER = "@alice:example.org";
const FAKE_DEVICE_ID = "AAAAAAAA";
const FAKE_PAYLOAD = {
"foo": 42,
foo: 42,
};
const EXPECTED_BODY = {
messages: {
@@ -45,8 +45,8 @@ const FAKE_MSG = {
};
enum StoreType {
Memory = 'Memory',
IndexedDB = 'IndexedDB',
Memory = "Memory",
IndexedDB = "IndexedDB",
}
async function flushAndRunTimersUntil(cond: () => boolean) {
@@ -57,13 +57,11 @@ async function flushAndRunTimersUntil(cond: () => boolean) {
}
}
describe.each([
[StoreType.Memory], [StoreType.IndexedDB],
])("queueToDevice (%s store)", function(storeType) {
describe.each([[StoreType.Memory], [StoreType.IndexedDB]])("queueToDevice (%s store)", function (storeType) {
let httpBackend: MockHttpBackend;
let client: MatrixClient;
beforeEach(async function() {
beforeEach(async function () {
jest.runOnlyPendingTimers();
jest.useRealTimers();
httpBackend = new MockHttpBackend();
@@ -85,23 +83,22 @@ describe.each([
});
});
afterEach(function() {
afterEach(function () {
jest.useRealTimers();
client.stopClient();
});
it("sends a to-device message", async function() {
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).check((request) => {
expect(request.data).toEqual(EXPECTED_BODY);
}).respond(200, {});
it("sends a to-device message", async function () {
httpBackend
.when("PUT", "/sendToDevice/org.example.foo/")
.check((request) => {
expect(request.data).toEqual(EXPECTED_BODY);
})
.respond(200, {});
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
await httpBackend.flushAllExpected();
@@ -111,24 +108,21 @@ describe.each([
await flushPromises();
});
it("retries on error", async function() {
it("retries on error", async function () {
jest.useFakeTimers();
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(500);
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(500);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).check((request) => {
expect(request.data).toEqual(EXPECTED_BODY);
}).respond(200, {});
httpBackend
.when("PUT", "/sendToDevice/org.example.foo/")
.check((request) => {
expect(request.data).toEqual(EXPECTED_BODY);
})
.respond(200, {});
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
await flushAndRunTimersUntil(() => httpBackend.requests.length > 0);
expect(httpBackend.flushSync(undefined, 1)).toEqual(1);
@@ -141,18 +135,14 @@ describe.each([
await flushPromises();
});
it("stops retrying on 4xx errors", async function() {
it("stops retrying on 4xx errors", async function () {
jest.useFakeTimers();
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(400);
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(400);
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
await flushAndRunTimersUntil(() => httpBackend.requests.length > 0);
expect(httpBackend.flushSync(undefined, 1)).toEqual(1);
@@ -166,29 +156,23 @@ describe.each([
expect(httpBackend.requests.length).toEqual(0);
});
it("honours ratelimiting", async function() {
it("honours ratelimiting", async function () {
jest.useFakeTimers();
// pick something obscure enough it's unlikley to clash with a
// retry delay the algorithm uses anyway
const retryDelay = 279 * 1000;
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(429, {
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(429, {
errcode: "M_LIMIT_EXCEEDED",
retry_after_ms: retryDelay,
});
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(200, {});
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(200, {});
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
await flushAndRunTimersUntil(() => httpBackend.requests.length > 0);
expect(httpBackend.flushSync(undefined, 1)).toEqual(1);
@@ -209,26 +193,20 @@ describe.each([
expect(httpBackend.flushSync(undefined, 1)).toEqual(1);
});
it("retries on retryImmediately()", async function() {
it("retries on retryImmediately()", async function () {
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
versions: ["r0.0.1"],
});
await Promise.all([client.startClient(), httpBackend.flush(undefined, 1, 20)]);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(500);
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(500);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(200, {});
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(200, {});
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
expect(await httpBackend.flush(undefined, 1, 1)).toEqual(1);
await flushPromises();
@@ -239,26 +217,20 @@ describe.each([
expect(await httpBackend.flush(undefined, 1, 3000)).toEqual(1);
});
it("retries on when client is started", async function() {
it("retries on when client is started", async function () {
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
versions: ["r0.0.1"],
});
await Promise.all([client.startClient(), httpBackend.flush("/_matrix/client/versions", 1, 20)]);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(500);
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(500);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(200, {});
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(200, {});
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
expect(await httpBackend.flush(undefined, 1, 1)).toEqual(1);
await flushPromises();
@@ -269,26 +241,20 @@ describe.each([
expect(await httpBackend.flush(undefined, 1, 20)).toEqual(1);
});
it("retries when a message is retried", async function() {
it("retries when a message is retried", async function () {
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
versions: ["r0.0.1"],
});
await Promise.all([client.startClient(), httpBackend.flush(undefined, 1, 20)]);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(500);
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(500);
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).respond(200, {});
httpBackend.when("PUT", "/sendToDevice/org.example.foo/").respond(200, {});
await client.queueToDevice({
eventType: "org.example.foo",
batch: [
FAKE_MSG,
],
batch: [FAKE_MSG],
});
expect(await httpBackend.flush(undefined, 1, 20)).toEqual(1);
@@ -305,7 +271,7 @@ describe.each([
expect(await httpBackend.flush(undefined, 1, 20)).toEqual(1);
});
it("splits many messages into multiple HTTP requests", async function() {
it("splits many messages into multiple HTTP requests", async function () {
const batch: ToDeviceBatch = {
eventType: "org.example.foo",
batch: [],
@@ -320,16 +286,20 @@ describe.each([
}
const expectedCounts = [20, 1];
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).check((request) => {
expect(removeElement(expectedCounts, c => c === Object.keys(request.data.messages).length)).toBeTruthy();
}).respond(200, {});
httpBackend.when(
"PUT", "/sendToDevice/org.example.foo/",
).check((request) => {
expect(Object.keys(request.data.messages).length).toEqual(1);
}).respond(200, {});
httpBackend
.when("PUT", "/sendToDevice/org.example.foo/")
.check((request) => {
expect(
removeElement(expectedCounts, (c) => c === Object.keys(request.data.messages).length),
).toBeTruthy();
})
.respond(200, {});
httpBackend
.when("PUT", "/sendToDevice/org.example.foo/")
.check((request) => {
expect(Object.keys(request.data.messages).length).toEqual(1);
})
.respond(200, {});
await client.queueToDevice(batch);
await httpBackend.flushAllExpected();