1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00
Files
matrix-js-sdk/spec/unit/matrix-client.spec.js
Kegan Dougal 5d782a317c Add some sync emission tests. Emit after starting timers.
We want to emit AFTER starting the timers so tests can speed
up time. We also want to do this because clients may want to
retryImmediately() on sync errors (which would be lost unless
the timer had already been started)
2015-11-04 16:09:30 +00:00

236 lines
7.4 KiB
JavaScript

"use strict";
var q = require("q");
var sdk = require("../..");
var MatrixClient = sdk.MatrixClient;
var utils = require("../test-utils");
describe("MatrixClient", function() {
var userId = "@alice:bar";
var client, store, scheduler;
var initialSyncData = {
end: "s_5_3",
presence: [],
rooms: []
};
var eventData = {
start: "s_START",
end: "s_END",
chunk: []
};
var PUSH_RULES_RESPONSE = {
method: "GET", path: "/pushrules/", data: {}
};
var httpLookups = [
// items are objects which look like:
// {
// method: "GET",
// path: "/initialSync",
// data: {},
// error: { errcode: M_FORBIDDEN } // if present will reject promise
// }
// items are popped off when processed and block if no items left.
];
var pendingLookup = null;
function httpReq(cb, method, path, qp, data, prefix) {
var next = httpLookups.shift();
var logLine = (
"MatrixClient[UT] RECV " + method + " " + path + " " +
"EXPECT " + (next ? next.method : next) + " " + (next ? next.path : next)
);
console.log(logLine);
if (!next) { // no more things to return
if (pendingLookup) {
// >1 pending thing, whine.
expect(false).toBe(
true, ">1 pending request. You should probably handle them. " +
"PENDING: " + JSON.stringify(pendingLookup) + " JUST GOT: " +
method + " " + path
);
}
pendingLookup = {
promise: q.defer().promise,
method: method,
path: path
};
return pendingLookup.promise;
}
if (next.path === path && next.method === method) {
console.log(
"MatrixClient[UT] Matched. Returning " +
(next.error ? "BAD" : "GOOD") + " response"
);
if (next.error) {
return q.reject({
errcode: next.error.errcode,
name: next.error.errcode,
message: "Expected testing error",
data: next.error
});
}
return q(next.data);
}
expect(true).toBe(false, "Expected different request. " + logLine);
return q.defer().promise;
}
beforeEach(function() {
utils.beforeEach(this);
jasmine.Clock.useMock();
scheduler = jasmine.createSpyObj("scheduler", [
"getQueueForEvent", "queueEvent", "removeEventFromQueue",
"setProcessFunction"
]);
store = jasmine.createSpyObj("store", [
"getRoom", "getRooms", "getUser", "getSyncToken", "scrollback",
"setSyncToken", "storeEvents", "storeRoom", "storeUser"
]);
client = new MatrixClient({
baseUrl: "https://my.home.server",
accessToken: "my.access.token",
request: function() {}, // NOP
store: store,
scheduler: scheduler,
userId: userId
});
// FIXME: We shouldn't be yanking _http like this.
client._http = jasmine.createSpyObj("httpApi", [
"authedRequest", "authedRequestWithPrefix", "getContentUri",
"request", "requestWithPrefix", "uploadContent"
]);
client._http.authedRequest.andCallFake(httpReq);
client._http.authedRequestWithPrefix.andCallFake(httpReq);
// set reasonable working defaults
pendingLookup = null;
httpLookups = [];
httpLookups.push(PUSH_RULES_RESPONSE);
httpLookups.push({
method: "GET", path: "/initialSync", data: initialSyncData
});
httpLookups.push({
method: "GET", path: "/events", data: eventData
});
});
describe("getSyncState", function() {
it("should return null if the client isn't started", function() {
expect(client.getSyncState()).toBeNull();
});
it("should return the same sync state as emitted sync events", function(done) {
client.on("sync", function(state) {
expect(state).toEqual(client.getSyncState());
if (state === "SYNCING") {
done();
}
});
client.startClient();
});
});
describe("retryImmediately", function() {
it("should return false if there is no request waiting", function() {
});
it("should return true if there is a request waiting", function() {
});
it("should work on /initialSync", function() {
});
it("should work on /events", function() {
});
it("should work on /pushrules", function() {
});
});
describe("emitted sync events", function() {
it("should transition null -> PREPARED after /initialSync", function(done) {
// the first sync emitted should be null > prep
client.once("sync", function(state, old) {
expect(state).toEqual("PREPARED");
expect(old).toBeNull();
done();
});
client.startClient();
});
it("should transition null -> ERROR after a failed /initialSync", function(done) {
httpLookups = [];
httpLookups.push(PUSH_RULES_RESPONSE);
httpLookups.push({
method: "GET", path: "/initialSync", error: { errcode: "NOPE_NOPE_NOPE" }
});
// the first sync emitted should be null > prep
client.once("sync", function(state, old) {
expect(state).toEqual("ERROR");
expect(old).toBeNull();
done();
// FIXME: need to make next req tick else it pollutes other tests
jasmine.Clock.tick(10000);
});
client.startClient();
});
it("should transition ERROR -> PREPARED after /initialSync if prev failed",
function(done) {
httpLookups = [];
httpLookups.push(PUSH_RULES_RESPONSE);
httpLookups.push({
method: "GET", path: "/initialSync", error: { errcode: "NOPE_NOPE_NOPE" }
});
httpLookups.push({
method: "GET", path: "/initialSync", data: initialSyncData
});
var states = [
// current, old
["ERROR", null],
["PREPARED", "ERROR"]
];
client.on("sync", function(state, old) {
var expected = states.shift();
if (!expected) {
done();
return;
}
expect(state).toEqual(expected[0]);
expect(old).toEqual(expected[1]);
if (expected.length === 0) {
done();
}
jasmine.Clock.tick(10000);
});
client.startClient();
});
it("should transition PREPARED -> SYNCING after /initialSync", function() {
});
it("should transition SYNCING -> ERROR after a failed /events", function() {
});
it("should transition ERROR -> SYNCING after /events if prev failed", function() {
});
it("should transition ERROR -> ERROR if multiple /events fails", function() {
});
});
});