1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Add local echo tests.

This commit is contained in:
Kegan Dougal
2015-06-25 15:24:56 +01:00
parent 21df000604
commit c4c68a0287
2 changed files with 195 additions and 9 deletions

View File

@@ -0,0 +1,173 @@
"use strict";
var sdk = require("../..");
var EventStatus = sdk.EventStatus;
var HttpBackend = require("../mock-request");
var utils = require("../test-utils");
describe("MatrixClient room timelines", function() {
var baseUrl = "http://localhost.or.something";
var client, httpBackend;
var userId = "@alice:localhost";
var accessToken = "aseukfgwef";
var roomId = "!foo:bar";
var otherUserId = "@bob:localhost";
var eventData;
var initialSync = {
end: "s_5_3",
presence: [],
rooms: [{
membership: "join",
room_id: roomId,
messages: {
start: "f_1_1",
end: "f_2_2",
chunk: [
utils.mkMessage({
room: roomId, user: otherUserId, msg: "hello"
})
]
},
state: [
utils.mkEvent({
type: "m.room.name", room: roomId, user: otherUserId,
content: {
name: "Old room name"
}
}),
utils.mkMembership({
room: roomId, mship: "join", user: otherUserId, name: "Bob"
}),
utils.mkMembership({
room: roomId, mship: "join", user: userId, name: "Alice"
}),
utils.mkEvent({
type: "m.room.create", room: roomId, user: userId,
content: {
creator: userId
}
})
]
}]
};
beforeEach(function() {
utils.beforeEach(this);
httpBackend = new HttpBackend();
sdk.request(httpBackend.requestFn);
client = sdk.createClient({
baseUrl: baseUrl,
userId: userId,
accessToken: accessToken
});
eventData = {
chunk: [],
end: "end_",
start: "start_"
};
httpBackend.when("GET", "/initialSync").respond(200, initialSync);
httpBackend.when("GET", "/events").respond(200, eventData);
});
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
});
describe("local echo events", function() {
var sendEvent = utils.mkMessage({
room: roomId, user: otherUserId, msg: "hello"
});
it("should be added immediately after calling MatrixClient.sendEvent "+
"with EventStatus.SENDING and the right event.sender", function(done) {
client.on("syncComplete", function() {
var room = client.getRoom(roomId);
expect(room.timeline.length).toEqual(1);
var promise = client.sendTextMessage(roomId, "I am a fish", "txn1");
// check it was added
expect(room.timeline.length).toEqual(2);
// check status
expect(room.timeline[1].status).toEqual(EventStatus.SENDING);
// check member
var member = room.timeline[1].sender;
expect(member.userId).toEqual(userId);
expect(member.name).toEqual("Alice");
httpBackend.flush("/events", 1).done(function() {
done();
})
});
client.startClient();
httpBackend.flush("/initialSync", 1);
});
it("should be updated correctly when the send request finishes "+
"BEFORE the event comes down the event stream", function(done) {
var eventId = "$foo:bar";
httpBackend.when("PUT", "/txn1").respond(200, {
event_id: eventId
});
eventData.chunk = [
utils.mkMessage({
body: "I am a fish", user: userId, room: roomId
})
];
eventData.chunk[0].event_id = eventId;
client.on("syncComplete", function() {
var room = client.getRoom(roomId);
client.sendTextMessage(roomId, "I am a fish", "txn1").done(
function() {
expect(room.timeline[1].getId()).toEqual(eventId);
httpBackend.flush("/events", 1).done(function() {
expect(room.timeline[1].getId()).toEqual(eventId);
done();
});
});
httpBackend.flush("/txn1", 1);
});
client.startClient();
httpBackend.flush("/initialSync", 1);
});
it("should be updated correctly when the send request finishes "+
"AFTER the event comes down the event stream", function(done) {
var eventId = "$foo:bar";
httpBackend.when("PUT", "/txn1").respond(200, {
event_id: eventId
});
eventData.chunk = [
utils.mkMessage({
body: "I am a fish", user: userId, room: roomId
})
];
eventData.chunk[0].event_id = eventId;
client.on("syncComplete", function() {
var room = client.getRoom(roomId);
var promise = client.sendTextMessage(roomId, "I am a fish", "txn1");
httpBackend.flush("/events", 1).done(function() {
// expect 3rd msg, it doesn't know this is the request is just did
expect(room.timeline.length).toEqual(3);
httpBackend.flush("/txn1", 1);
promise.done(function() {
expect(room.timeline.length).toEqual(2);
expect(room.timeline[1].getId()).toEqual(eventId);
done();
});
});
});
client.startClient();
httpBackend.flush("/initialSync", 1);
});
});
describe("paginated events", function() {
});
describe("new events", function() {
});
});

View File

@@ -19,25 +19,35 @@ function HttpBackend() {
HttpBackend.prototype = { HttpBackend.prototype = {
/** /**
* Respond to all of the requests (flush the queue). * Respond to all of the requests (flush the queue).
* @param {string} path The path to flush (optional) default: all.
* @param {integer} numToFlush The number of things to flush (optional), default: all.
* @return {Promise} resolved when there is nothing left to flush. * @return {Promise} resolved when there is nothing left to flush.
*/ */
flush: function() { flush: function(path, numToFlush) {
var defer = q.defer(); var defer = q.defer();
var self = this; var self = this;
console.log("HTTP backend flushing..."); var flushed = 0;
console.log("HTTP backend flushing... (path=%s numToFlush=%s)", path, numToFlush);
var tryFlush = function() { var tryFlush = function() {
// if there's more real requests and more expected requests, flush 'em. // if there's more real requests and more expected requests, flush 'em.
console.log( console.log(
" trying to flush queue => reqs=%s expected=%s", " trying to flush queue => reqs=%s expected=%s [%s]",
self.requests.length, self.expectedRequests.length self.requests.length, self.expectedRequests.length, path
); );
if (self._takeFromQueue()) { if (self._takeFromQueue(path)) {
// try again on the next tick. // try again on the next tick.
console.log(" flushed. Trying for more."); console.log(" flushed. Trying for more. [%s]", path);
setTimeout(tryFlush, 0); flushed += 1;
if (numToFlush && flushed === numToFlush) {
console.log(" [%s] Flushed assigned amount: %s", path, numToFlush);
defer.resolve();
}
else {
setTimeout(tryFlush, 0);
}
} }
else { else {
console.log(" no more flushes."); console.log(" no more flushes. [%s]", path);
defer.resolve(); defer.resolve();
} }
}; };
@@ -49,9 +59,10 @@ HttpBackend.prototype = {
/** /**
* Attempts to resolve requests/expected requests. * Attempts to resolve requests/expected requests.
* @param {string} path The path to flush (optional) default: all.
* @return {boolean} true if something was resolved. * @return {boolean} true if something was resolved.
*/ */
_takeFromQueue: function() { _takeFromQueue: function(path) {
var req = null; var req = null;
var i, j; var i, j;
var matchingReq, expectedReq, testResponse = null; var matchingReq, expectedReq, testResponse = null;
@@ -59,6 +70,7 @@ HttpBackend.prototype = {
req = this.requests[i]; req = this.requests[i];
for (j = 0; j < this.expectedRequests.length; j++) { for (j = 0; j < this.expectedRequests.length; j++) {
expectedReq = this.expectedRequests[j]; expectedReq = this.expectedRequests[j];
if (path && path !== expectedReq.path) { continue; }
if (expectedReq.method === req.method && if (expectedReq.method === req.method &&
req.path.indexOf(expectedReq.path) !== -1) { req.path.indexOf(expectedReq.path) !== -1) {
if (!expectedReq.data || (JSON.stringify(expectedReq.data) === if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
@@ -84,6 +96,7 @@ HttpBackend.prototype = {
req.callback( req.callback(
testResponse.err, testResponse.response, testResponse.body testResponse.err, testResponse.response, testResponse.body
); );
matchingReq = null;
} }
} }
if (testResponse) { // flushed something if (testResponse) { // flushed something