2016-03-18 07:23:53 +08:00
|
|
|
import _ from 'underscore';
|
|
|
|
import {
|
2017-03-31 08:38:07 +08:00
|
|
|
DatabaseWriter,
|
2016-03-18 07:23:53 +08:00
|
|
|
SyncbackDraftTask,
|
|
|
|
SyncbackMetadataTask,
|
|
|
|
DatabaseStore,
|
|
|
|
AccountStore,
|
|
|
|
TaskQueue,
|
|
|
|
Contact,
|
|
|
|
Message,
|
|
|
|
Account,
|
|
|
|
Actions,
|
|
|
|
Task,
|
|
|
|
APIError,
|
|
|
|
NylasAPI,
|
2016-11-30 08:32:23 +08:00
|
|
|
NylasAPIRequest,
|
2016-03-18 07:23:53 +08:00
|
|
|
} from 'nylas-exports';
|
|
|
|
|
|
|
|
const inboxError = {
|
|
|
|
message: "No draft with public id bvn4aydxuyqlbmzowh4wraysg",
|
|
|
|
type: "invalid_request_error",
|
|
|
|
};
|
|
|
|
|
|
|
|
const testData = {
|
|
|
|
to: [new Contact({name: "Ben Gotow", email: "benthis.nylas.com"})],
|
|
|
|
from: [new Contact({name: "Evan Morikawa", email: "evanthis.nylas.com"})],
|
2016-10-18 08:59:33 +08:00
|
|
|
date: new Date(),
|
2016-03-18 07:23:53 +08:00
|
|
|
draft: true,
|
|
|
|
subject: "Test",
|
|
|
|
accountId: "abc123",
|
|
|
|
body: '<body>123</body>',
|
|
|
|
};
|
|
|
|
|
|
|
|
const localDraft = () => new Message(_.extend({}, testData, {
|
|
|
|
clientId: "local-id",
|
|
|
|
}));
|
|
|
|
|
|
|
|
const remoteDraft = () => new Message(_.extend({}, testData, {
|
|
|
|
clientId: "local-id",
|
|
|
|
serverId: "remoteid1234",
|
|
|
|
threadId: '1234',
|
|
|
|
version: 2,
|
|
|
|
}));
|
|
|
|
|
fix(spec): add support for async specs and disable misbehaving ones
More spec fixes
replace process.nextTick with setTimeout(fn, 0) for specs
Also added an unspy in the afterEach
Temporarily disable specs
fix(spec): start fixing specs
Summary:
This is the WIP fix to our spec runner.
Several tests have been completely commented out that will require
substantially more work to fix. These have been added to our sprint
backlog.
Other tests have been fixed to update to new APIs or to deal with genuine
bugs that were introduced without our knowing!
The most common non-trivial change relates to observing the `NylasAPI` and
`NylasAPIRequest`. We used to observe the arguments to `makeRequest`.
Unfortunately `NylasAPIRequest.run` is argumentless. Instead you can do:
`NylasAPIRequest.prototype.run.mostRecentCall.object.options` to get the
`options` passed into the object. the `.object` property grabs the context
of the spy when it was last called.
Fixing these tests uncovered several concerning issues with our test
runner. I spent a while tracking down why our participant-text-field-spec
was failling every so often. I chose that spec because it was the first
spec to likely fail, thereby requiring looking at the least number of
preceding files. I tried binary searching, turning on and off, several
files beforehand only to realize that the failure rate was not determined
by a particular preceding test, but rather the existing and quantity of
preceding tests, AND the number of console.log statements I had. There is
some processor-dependent race condition going on that needs further
investigation.
I also discovered an issue with the file-download-spec. We were getting
errors about it accessing a file, which was very suspicious given the code
stubs out all fs access. This was caused due to a spec that called an
async function outside ot a `waitsForPromise` block or a `waitsFor` block.
The test completed, the spies were cleaned up, but the downstream async
chain was still running. By the time the async chain finished the runner
was already working on the next spec and the spies had been restored
(causing the real fs access to run).
Juan had an idea to kill the specs once one fails to prevent cascading
failures. I'll implement this in the next diff update
Test Plan: npm test
Reviewers: juan, halla, jackie
Differential Revision: https://phab.nylas.com/D3501
Disable other specs
Disable more broken specs
All specs turned off till passing state
Use async-safe versions of spec functions
Add async test spec
Remove unused package code
Remove canary spec
2016-12-13 04:12:20 +08:00
|
|
|
xdescribe('SyncbackDraftTask', function syncbackDraftTask() {
|
2016-03-18 07:23:53 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(AccountStore, "accountForEmail").andCallFake((email) =>
|
|
|
|
new Account({clientId: 'local-abc123', serverId: 'abc123', emailAddress: email})
|
|
|
|
);
|
|
|
|
|
|
|
|
spyOn(DatabaseStore, "run").andCallFake((query) => {
|
|
|
|
const clientId = query.matcherValueForModelKey('clientId')
|
|
|
|
if (clientId === "localDraftId") {
|
|
|
|
return Promise.resolve(localDraft());
|
|
|
|
}
|
|
|
|
if (clientId === "remoteDraftId") {
|
|
|
|
return Promise.resolve(remoteDraft());
|
|
|
|
}
|
|
|
|
if (clientId === "missingDraftId") {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(NylasAPI, 'incrementRemoteChangeLock');
|
|
|
|
spyOn(NylasAPI, 'decrementRemoteChangeLock');
|
2017-03-31 08:38:07 +08:00
|
|
|
spyOn(DatabaseWriter.prototype, "persistModel").andReturn(Promise.resolve());
|
2016-03-18 07:23:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("queueing multiple tasks", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
this.taskA = new SyncbackDraftTask("draft-123");
|
|
|
|
this.taskB = new SyncbackDraftTask("draft-123");
|
|
|
|
this.taskC = new SyncbackDraftTask("draft-123");
|
|
|
|
this.taskOther = new SyncbackDraftTask("draft-456");
|
|
|
|
|
|
|
|
this.taskA.sequentialId = 0;
|
|
|
|
this.taskB.sequentialId = 1;
|
|
|
|
this.taskC.sequentialId = 2;
|
|
|
|
TaskQueue._queue = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
it("dequeues other SyncbackDraftTasks that haven't started yet", () => {
|
|
|
|
// Task A is taking forever, B is waiting on it, and C gets queued.
|
|
|
|
for (const t of [this.taskA, this.taskB, this.taskOther]) {
|
|
|
|
t.queueState.localComplete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// taskA has already started This should NOT get dequeued
|
|
|
|
this.taskA.queueState.isProcessing = true;
|
|
|
|
|
|
|
|
// taskB hasn't started yet! This should get dequeued
|
|
|
|
this.taskB.queueState.isProcessing = false;
|
|
|
|
|
|
|
|
// taskOther, while unstarted, doesn't match the draftId and should
|
|
|
|
// not get dequeued
|
|
|
|
this.taskOther.queueState.isProcessing = false;
|
|
|
|
|
|
|
|
TaskQueue._queue = [this.taskA, this.taskB, this.taskOther];
|
|
|
|
spyOn(this.taskC, "runLocal").andReturn(Promise.resolve());
|
|
|
|
|
|
|
|
TaskQueue.enqueue(this.taskC);
|
|
|
|
|
|
|
|
// Note that taskB is gone, taskOther was untouched, and taskC was
|
|
|
|
// added.
|
|
|
|
expect(TaskQueue._queue).toEqual = [this.taskA, this.taskOther, this.taskC];
|
|
|
|
|
|
|
|
expect(this.taskC.runLocal).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("waits for any other inflight tasks to finish or error", () => {
|
|
|
|
this.taskA.queueState.localComplete = true;
|
|
|
|
this.taskA.queueState.isProcessing = true;
|
|
|
|
this.taskB.queueState.localComplete = true;
|
|
|
|
spyOn(this.taskB, "runRemote").andReturn(Promise.resolve());
|
|
|
|
|
|
|
|
TaskQueue._queue = [this.taskA, this.taskB];
|
|
|
|
|
|
|
|
// Since taskA has isProcessing set to true, it will just be passed
|
|
|
|
// over. We expect taskB to fail the `_taskIsBlocked` test
|
|
|
|
TaskQueue._processQueue();
|
|
|
|
advanceClock(100);
|
|
|
|
expect(TaskQueue._queue).toEqual([this.taskA, this.taskB]);
|
|
|
|
expect(this.taskA.queueState.isProcessing).toBe(true);
|
|
|
|
expect(this.taskB.queueState.isProcessing).toBe(false);
|
|
|
|
expect(this.taskB.runRemote).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("performRemote", () => {
|
|
|
|
beforeEach(() => {
|
2016-11-30 08:32:23 +08:00
|
|
|
spyOn(NylasAPIRequest.prototype, 'run').andReturn(Promise.resolve(remoteDraft().toJSON()))
|
2016-03-18 07:23:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does nothing if no draft can be found in the db", () => {
|
|
|
|
const task = new SyncbackDraftTask("missingDraftId");
|
|
|
|
waitsForPromise(() => task.performRemote().then(() => {
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(NylasAPIRequest.prototype.run).not.toHaveBeenCalled();
|
2016-03-18 07:23:53 +08:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should start an API request with the Message JSON", () => {
|
|
|
|
const task = new SyncbackDraftTask("localDraftId")
|
|
|
|
waitsForPromise(() => task.performRemote().then(() => {
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(NylasAPIRequest.prototype.run).toHaveBeenCalled();
|
|
|
|
const reqBody = NylasAPIRequest.prototype.run.mostRecentCall.args[0].body;
|
2016-03-18 07:23:53 +08:00
|
|
|
expect(reqBody.subject).toEqual(testData.subject);
|
|
|
|
expect(reqBody.body).toEqual(testData.body);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should do a PUT when the draft has already been saved", () => {
|
|
|
|
const task = new SyncbackDraftTask("remoteDraftId")
|
|
|
|
waitsForPromise(() => task.performRemote().then(() => {
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(NylasAPIRequest.prototype.run).toHaveBeenCalled();
|
|
|
|
const options = NylasAPIRequest.prototype.run.mostRecentCall.args[0];
|
2016-03-18 07:23:53 +08:00
|
|
|
expect(options.path).toBe("/drafts/remoteid1234");
|
|
|
|
expect(options.accountId).toBe("abc123");
|
|
|
|
expect(options.method).toBe('PUT');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should do a POST when the draft is unsaved", () => {
|
|
|
|
const task = new SyncbackDraftTask("localDraftId");
|
|
|
|
waitsForPromise(() => task.performRemote().then(() => {
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(NylasAPIRequest.prototype.run).toHaveBeenCalled();
|
|
|
|
const options = NylasAPIRequest.prototype.run.mostRecentCall.args[0];
|
2016-03-18 07:23:53 +08:00
|
|
|
expect(options.path).toBe("/drafts");
|
|
|
|
expect(options.accountId).toBe("abc123");
|
|
|
|
expect(options.method).toBe('POST');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should apply the server ID, thread ID and version to the draft", () => {
|
|
|
|
const task = new SyncbackDraftTask("localDraftId");
|
|
|
|
waitsForPromise(() => task.performRemote().then(() => {
|
2017-03-31 08:38:07 +08:00
|
|
|
expect(DatabaseWriter.prototype.persistModel).toHaveBeenCalled();
|
|
|
|
const saved = DatabaseWriter.prototype.persistModel.calls[0].args[0];
|
2016-03-18 07:23:53 +08:00
|
|
|
const remote = remoteDraft();
|
|
|
|
expect(saved.threadId).toEqual(remote.threadId);
|
|
|
|
expect(saved.serverId).toEqual(remote.serverId);
|
|
|
|
expect(saved.version).toEqual(remote.version);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2016-04-12 05:32:54 +08:00
|
|
|
it("should save metadata associated with the draft when the draft has been already saved to the api", () => {
|
2016-03-18 07:23:53 +08:00
|
|
|
const draft = remoteDraft();
|
|
|
|
draft.pluginMetadata = [{pluginId: 1, value: {a: 1}}];
|
|
|
|
const task = new SyncbackDraftTask(draft.clientId);
|
|
|
|
spyOn(task, 'refreshDraftReference').andCallFake(() => {
|
|
|
|
task.draft = draft;
|
|
|
|
return Promise.resolve(draft)
|
|
|
|
});
|
|
|
|
spyOn(Actions, 'queueTask');
|
|
|
|
waitsForPromise(() => task.applyResponseToDraft(draft).then(() => {
|
2016-04-12 05:32:54 +08:00
|
|
|
expect(Actions.queueTask).toHaveBeenCalled();
|
2016-03-18 07:23:53 +08:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2016-04-12 05:32:54 +08:00
|
|
|
it("should save metadata associated with the draft when the draft is syncbacked for the first time", () => {
|
2016-03-18 07:23:53 +08:00
|
|
|
const draft = localDraft();
|
|
|
|
draft.pluginMetadata = [{pluginId: 1, value: {a: 1}}];
|
|
|
|
const task = new SyncbackDraftTask(draft.clientId);
|
|
|
|
spyOn(task, 'refreshDraftReference').andCallFake(() => {
|
|
|
|
task.draft = draft;
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
spyOn(Actions, 'queueTask');
|
|
|
|
waitsForPromise(() => task.applyResponseToDraft(draft).then(() => {
|
|
|
|
const metadataTask = Actions.queueTask.mostRecentCall.args[0];
|
|
|
|
expect(metadataTask instanceof SyncbackMetadataTask).toBe(true);
|
|
|
|
expect(metadataTask.clientId).toEqual(draft.clientId);
|
|
|
|
expect(metadataTask.modelClassName).toEqual('Message');
|
|
|
|
expect(metadataTask.pluginId).toEqual(1);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("When the api throws errors", () => {
|
|
|
|
const stubAPI = (code, method) => {
|
2016-11-30 08:32:23 +08:00
|
|
|
spyOn(NylasAPIRequest.prototype, "run").andReturn(Promise.reject(
|
2016-03-18 07:23:53 +08:00
|
|
|
new APIError({
|
|
|
|
error: inboxError,
|
|
|
|
response: {statusCode: code},
|
|
|
|
body: inboxError,
|
|
|
|
requestOptions: {method},
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
this.task = new SyncbackDraftTask("removeDraftId")
|
|
|
|
spyOn(this.task, 'refreshDraftReference').andCallFake(() => {
|
|
|
|
this.task.draft = remoteDraft();
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
NylasAPI.PermanentErrorCodes.forEach((code) => {
|
|
|
|
it(`fails on API status code ${code}`, () => {
|
|
|
|
stubAPI(code, "PUT");
|
|
|
|
waitsForPromise(() => this.task.performRemote().then(([status, err]) => {
|
|
|
|
expect(status).toBe(Task.Status.Failed);
|
|
|
|
expect(this.task.refreshDraftReference).toHaveBeenCalled();
|
|
|
|
expect(this.task.refreshDraftReference.calls.length).toBe(1);
|
|
|
|
expect(err.statusCode).toBe(code);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-05 06:05:48 +08:00
|
|
|
NylasAPI.TimeoutErrorCodes.forEach((code) => {
|
2016-03-18 07:23:53 +08:00
|
|
|
it(`retries on status code ${code}`, () => {
|
|
|
|
stubAPI(code, "PUT");
|
|
|
|
waitsForPromise(() => this.task.performRemote().then((status) => {
|
|
|
|
expect(status).toBe(Task.Status.Retry);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fails on other JavaScript errors", () => {
|
2016-11-30 08:32:23 +08:00
|
|
|
spyOn(NylasAPIRequest.prototype, "run").andReturn(Promise.reject(new TypeError()));
|
2016-03-18 07:23:53 +08:00
|
|
|
waitsForPromise(() => this.task.performRemote().then(([status]) => {
|
|
|
|
expect(status).toBe(Task.Status.Failed);
|
|
|
|
expect(this.task.refreshDraftReference).toHaveBeenCalled();
|
|
|
|
expect(this.task.refreshDraftReference.calls.length).toBe(1);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|