Commit graph

490 commits

Author SHA1 Message Date
Evan Morikawa 16cab4272b feat(send)
refactor(send): split delivery from sent folder stuffing

Summary:
See explanation in https://phab.nylas.com/D3577

Depends on D3577

Test Plan: manual

Reviewers: jackie, juan, halla

Reviewed By: juan, halla

Differential Revision: https://phab.nylas.com/D3578
2017-01-04 15:42:19 -08:00
Evan Morikawa 487dac31b3 fix(spec): fix asynchronous specs
Summary:
I believe I discovered why our tests intermittently fail, why those
failures cause a cascade of failures, and how to fix it.

The bug is subtle and it's helpful to get a quick refresher on how various
parts of our testing system work:

First a deeper dive into how Promises work:

1. Upon creating a new Promise, the "executor" block (the one that gets
   passed `resolve` and `reject`), gets synchronously run.
2. You eventually call `resolve()`.
3. The minute you call `resolve()`, Bluebird's `async.js` queues up
   whatever's downstream (the next `then` block)
4. The queue gets processed on every "tick".
5. Once the "tick" happens, our queue processes and downstream `then`
   blocks get run.
6. If any more Promises come in before the "tick", they get added to the
   queue. Nothing gets processed until the "tick" happens.

The important takeaway here is this "tick" in step 4. This "tick" is the
core of what makes Promises asynchronous. By default, Bluebird in our
Node-like environment uses `setImmediate` as the underlying
implementation.

Our test environment is different. We do NOT use `setImmediate` in our test
environment.

We use Bluebird's `Promise.setScheduler` API to implement our own "tick".  This
gives us much greater control over when Promises advance. Node's `setImmediate`
puts you at the whim of the underlying event loop.

Before today, our test "tick" implementation used `setTimeout` and
`advanceClock` triggered by `process.nextTick`.

Let me also quickly explain `setTimeout` in our test environment.

We globally override `setTimeout` in our test environment to not be based on
actual time at all. There are places in our code where we wait for several
hundred miliseconds, or have timeouts places. Instead of "sleeping" some amount
of time and hoping for the best, we gain absolute control over "time". "Time"
is just an integer value that only moves forward when you manually call
`advanceClock()` and pass it a certain number of "milliseconds".

At the beginning of each test, we reset time back to zero, we record
setTimeouts that come in, and as advanceClock gets called, we know if we need
to run any of the setTimeout callbacks.

Back to the Promise "tick" implementation. Before today, our testing "tick"
implementation relied our our stubbed `setTimeout` and our control of time.

This almost always works fine. Unfortunately tests would sometimes
intermittently fail, and furthermore cause a cascade of failures down the road.

We've been plauged with this for as long as I can remember. I think I finally
found how all of this comes together to cause these intermittent failures and
how to fix it.

The issue arises from a test like the one in query-subscription-pool-spec.  We
have tests here (and subtly in other yet unknown places) that go
something like this:

```
it("TEST A", () => {
  Foo.add(new Thing())
  expect(Foo._things.length).toBe(1)
})

it("TEST B", () => {
  expect(true).toBe(true)
})
```

At the surface this test looks straightforward. The problem is that
`Foo.add` may down the line call something like `trigger()`, which may
have listeners setup, which might try and asynchronously do all kinds of
things (like read the fs or read/write to the database).

The biggest issue with this is that the test 'finishes' immediately after
that `expect` block and immediately moves onto the next test. If `Foo.add`
is asynchronous, by the time whatever downstream effects of `Foo.add` take
place we may be in a completely different test. Furthremore if those
downstream function errors, those errors will be raised, but Jasmine will
catch them in the wrong test sending you down a rabbit hole of dispair.

It gets worse.

At the start of each test, we reset our `setTimeout` stub time back to
zero. This is problematic when combined with the last issue.

Suppose `Foo.add` ends up queuing a downstream Promsie. Before today, that
downstream Promise used `setTimeout(0)` to trigger the `then` block.

Suppose TEST A finishes before `process.nextTick` in our custom scheduler
can call `advanceClock` and run the downstream Promise.

Once Test B starts, it will reset our `setTimeout` stub time back to zero.

`process.nextTick` comes back after Test B has started and calls
`advanceClock` like it's supposed to.

Unfortunately, because our stub time has been reset, advanceClock will NOT
find the original callback function that would have resolved `Foo.add`'s
downstream Promise!!!

This means that Bluebird is now stuck waiting for a "tick" that will never
come anymore.

Since Bluebird thinks it's waiting for a "tick", all future Promises will
get queued, but never called (see Step 6 of the Promise description
above).

This is why once one test fails, downstream ones never complete and
Jasmine times out.

The failure is intermittent because `process.nextTick` is racing agianst a
test finishing, the next one starting, and how many and how far downstream
promises are setup.

Okay. So how do we fix this? First I tried to simply not reset the time back to
zero again in our stubbed time-override. This doesn't work because it simply
exposes the diasterous consequences of downstream Promises resolving after a
test has completed. When a test completes we cleanup objects, unmount React
components. Those downstream promises and timeouts come back and throw all
kinds of errors like: "can't read property x of undefined" and "can't find a
match for component X".

The fix that works the best is to simply MAKE PROMISES FULLY SYCNRHONOUS.
Now if you look at our custom Promise Test Scheduler in time-override,
you'll see that it immediately and sychronously calls the function. This
means that all downstream promises will run BEFORE the test ends.

Note that this is designed as a safeguard. The best way to make a more
robust test is to declare that your funtion body is asynchronous. If you
call a method that has downstream effects, it's your responsibility to
wait for them to finish. I would consider the test example above a very,
very subtle bug. Unfortunately it's so subtle that it's unreasonable to
expect that we'll always catch them. Making everything in our testing
environment synchronous ensures that test setup and cleanup happen when we
intuitively expect them to.

Addendum:

The full Promise call chain looks something like this:

-> `Promise::_resolveCallback`
-> `Promise::_fulfill`
-> `Promise::_async.settlePromises`
-> `AsyncSettlePromises`
-> `Async::_queueTick`
-> CHECK `Async::_isTickUsed`
-> `Async::_schedule`
-> `TimeOverride.scheduler`
-> `setTimeout`
-> `process.nextTick`
-> `advanceClock`
-> `Async::_drainQueues`
-> `Async::_drainQueue`
-> `THEN BLOCK RUNS`
-> `Maybe more things get added to queue`
-> `Async::_reset`
-> `Async::_queueTick` works again.

Test Plan: They now work

Reviewers: halla, mark, spang, juan, jackie

Reviewed By: juan, jackie

Differential Revision: https://phab.nylas.com/D3538
2016-12-20 16:47:26 -08:00
Christine Spang 63a95c2096 fix(rendering): Fix quote stripping of many plaintext emails
Summary:
Since we changed the way we mark up plaintext emails to wrap them in <pre>
tags, the quote stripper has been failing to restore bodies in the cases
where a plaintext email starts with an inline quote (and thus the whole
contents of the <pre> tag is detected as a quote string).

This diff also fixes the HTML sanitizer to not strip off our custom
"nylas-plaintext" class, which isn't currently used for anything but
allows us to definitively know which tags we added on for marking up
plaintext. Might be useful at some point!

Test Plan: unit test included, manual inspection of message display

Reviewers: evan

Differential Revision: https://phab.nylas.com/D3519
2016-12-15 18:40:34 -08:00
Juan Tejada d92506d8f1 fix(sync-status): Change old per model status, in favor of per folder
Summary:
Addresses T7275
Previously, we kept track of the sync status of each API model, and the progress
we'd made syncing all available models (e.g. all threads, messages, events, etc)

Given K2's set up, we are now keeping track of sync status per folder, i.e. what
percent of the folder's messages we've synced. This status is now reported from K2
to N1 via folder object deltas, and this commit rewrites the
NylasSyncStatusStore (in ES6) to reflect that.

The new Store keeps the sync state per account, which is the merged state of
per folder sync state, and delta connections state.
We also got rid of `CategoryStore.whenCategoriesReady` in favor of
`whenCategoryListSynced`, which is derived from the fact hat as long as we've
started syncing one folder, we've already synced the entire list of of folders/labels.

There are a couple of TODOs to be addressed in upcoming diffs:
- T7329 Restore the sidebar component to show sync progress, which was previously removed
- T7330 Figure out how to report sync progress per label, specifically, we are interested in knowing how much of the inbox we've synced, which is a label in Gmail. (This might be a non-issue if we sync the inbox very fast, first)

Depends on D3514

Test Plan: Manual

Reviewers: mark, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3515
2016-12-15 11:17:20 -08:00
Evan Morikawa d1c587a01c 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-15 13:02:00 -05:00
Halla Moore 1f36388b93 update(send): Pass in the upload data that local-sync expects
Since local-sync can access the same targetPaths as N1, there's
no need to create actual File instances anymore. Pass just the
upload data in the API request, and remove SyncbackDraftFilesTask
since it is no longer necessary.
2016-12-07 14:19:07 -08:00
Evan Morikawa f170d14b8d fix(errors): properly rethrow api error 2016-12-06 17:21:37 -08:00
Jackie Luo 0e388d5c8c rm(send-successful): Remove NotifyPluginsOfSendTask 2016-12-06 16:45:32 -08:00
Jackie Luo 8026d1e1a5 fix(spelling): Correct references 2016-12-06 16:05:05 -08:00
Evan Morikawa ace9b94053 feat(key): use unified key manager 2016-12-05 17:19:19 -08:00
Jackie Luo 871eb524ec fix(nylas-api): Correct some imports and remove tests for plugin auth 2016-12-05 11:37:27 -08:00
Jackie Luo ad5a1a6316 refactor(nylas-api): Make requests with helpers 2016-12-02 15:51:38 -08:00
Ben Gotow fb13abe8f4 fix(deltas): Process all deltas of each class in a single pass
Also changed the DeltaProcessor so it doesn’t query for models before sending out `Actions.didPassivelyReceiveCreateDeltas`, and renames it to be more clear it’s about deltas.
2016-12-02 11:41:49 -08:00
Ben Gotow 6d85a8ef81 lint(*): Fix linter errors within K2, update eslint grunt task 2016-12-01 15:38:16 -08:00
Ben Gotow ce14e3b12e fix(specs): Fix AccountStore specs 2016-12-01 13:50:31 -08:00
Jackie Luo 8818ad1957 spec(nylas-api): Update tests to use NylasAPIRequest 2016-11-29 16:32:23 -08:00
Evan Morikawa 02c3b78d94 fix(lint): fix linter import error 2016-11-18 11:13:21 -08:00
Juan Tejada 2f116b8181 fix(specs) Fix remaining tests 2016-11-17 15:37:44 -08:00
Juan Tejada 710dc1a4c4 fix(db-transaction): Make sure we mark as not bg-able query objects
When forwarding query operations to the database (e.g. find, findAll)
from the transaction,
we didn't check if those methods actually returned a ModelQuery object
(e.g. modelify returns a regular Promise), so this would break in cases
where it wasn't defined (`markNotBackgroundable` would be undefined)

This commit fixes that and fixes related specs
2016-11-17 14:03:06 -08:00
Jackie Luo e3e8c28c57 lint(eslint): Update to new rule names 2016-11-15 16:49:11 -08:00
Evan Morikawa e43de77708 fix(lint): update files to for new linter version 2016-11-15 10:20:39 -08:00
Evan Morikawa 9cde226598 refactor(registry): move all registries into src/registries 2016-11-14 14:01:00 -08:00
Evan Morikawa 5c81f083ce fix(quote): fix more cases of unwrapped signature detection 2016-11-14 11:55:37 -08:00
Evan Morikawa ecb7a0e4aa fix(quote): catch Office 365 quoted text case 2016-11-14 11:55:31 -08:00
Ben Gotow 25c32ec5d9 specs(spellcheck): Ignore on linux due to dict downloads 2016-11-09 15:58:57 -08:00
Ben Gotow 83adb8e74f fix(spellcheck): Wait longer, electron-spellchecker actually downloads dicts 2016-11-09 15:07:30 -08:00
Ben Gotow d819479613 fix(mailto): Improve handling of invalid urls 2016-11-07 14:09:47 -08:00
Juan Tejada 69161e8f04 fix(specs) Fix date-utils specs 2016-11-07 12:15:47 -08:00
Evan Morikawa dbc81a87a4 feat(quote): improved quoted text detection for trailing signatures 2016-11-04 20:45:25 -07:00
Evan Morikawa c0b28456a9 fix(quote): properly detect "wrote: " strings with trailing space 2016-11-04 18:39:37 -07:00
Evan Morikawa 4a40074cd1 convert(es6): quoted-html-transformer to es6 2016-11-04 18:28:11 -07:00
Juan Tejada 189b15e586 feat(attachments): Generate and display thumbnail previews for files (mac only)
Summary: Adds option to view preview thumbnails for attachments. This commit updates the FileDownloadStore to generate file thumbnail previews for attachments via `qlmanage` and displays them in the AttachmentItem component.

Test Plan: Manual

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D3393
2016-11-02 17:27:53 -07:00
Halla Moore 6f571a323f fix(decaffeination) Remove second arg from some slice calls
Decaffination replaces someString[index..-1] with
someString.slice(index, -1 + 1), which is bizzare. This commit changes those
instances to someString.slice(index).
2016-11-02 12:40:01 -07:00
Juan Tejada 13586d2886 fix(specs): Add regression test for list-selection 2016-11-02 11:46:28 -07:00
Juan Tejada 5fa379bccf fix(es6): Convert FileDownloadStore to JS 2016-11-01 18:12:24 -07:00
Evan Morikawa 8ab3fba7da fix(es6): fix linter and compile error in es6 conversion 2016-10-28 09:59:41 -04:00
Ben Gotow 8e533a3854 es6(*): Actions, ConfigSchema => ES2016 2016-10-27 18:48:33 -07:00
Ben Gotow a9b0472030 es6(*): Misc components, nylas-exports => ES2016 2016-10-27 18:25:30 -07:00
Ben Gotow 0a56d30a22 es6(composer): Composer extensions => ES2016 2016-10-27 17:28:36 -07:00
Ben Gotow e7c22eacbc fix(spec): Apply jasmine styles to windowed specs 2016-10-27 12:44:22 -07:00
Evan Morikawa 0adc1261f6 fix(spec): let ./N1.sh --test=window work 2016-10-27 15:24:13 -04:00
Ben Gotow 781d061ee8 es6(*): Misc src to ES2016 2016-10-27 12:08:59 -07:00
Ben Gotow 642977126f es6(models): Remaining models => ES2016
# Conflicts:
#	src/flux/models/contact.coffee
2016-10-27 12:08:59 -07:00
Juan Tejada 645a032a50 fix(specs): Fix remaining broken specs 2016-10-27 11:44:19 -07:00
Juan Tejada da48d8433e fix(specs): Fix date input specs 2016-10-27 11:35:37 -07:00
Juan Tejada f2e7ea4c4c feat(reminders): Add send reminders functionality
Summary: Add reminders plugin which lets you set reminder if you don't get a reply for a message within a specified time in the future

Test Plan: TODO

Reviewers: halla, bengotow, evan

Reviewed By: halla, bengotow, evan

Differential Revision: https://phab.nylas.com/D3356
2016-10-27 08:49:29 -07:00
Juan Tejada 5d837ffd02 feat(undo-send): Add undo send
Summary:
Add ability to undo send. We decided to make undo send completely client side for a couple of reasons. If we rely on send-later for undo-send, we would be giving /all/ send load to our send-later backend. If this increases the send-later load too much, it might cause delays in the regular send-later functionality and potentially other plugins like snooze that run under the same service. We would also need to rely on the network to be able to cancel a send, which would make it unusable offline or hard to debug if that specific request fails for any given reason.

This commit also refactors the way `ComposerExtension.sendActionConfig` works. The method has been renamed and now must return an array of send actions. Also, all of the business logic to handle different send actions registered by extensions has been pieced apart from the SendActionButton and into a new SendActionStore. This also enables undo send to undo custom send actions registered by extensions.
Along the way, this also fixes a pending TODO to show all registered custom send actions in the preferences for choosing the preferred send action for sending.

Undo send works via a task, so in case N1 closes before send goes through, it will still be persisted to the task queue and restored when opened again. Undoing a send means dequeuing this task.

Test Plan: Manual

Reviewers: jackie, bengotow, halla, evan

Reviewed By: bengotow, halla, evan

Differential Revision: https://phab.nylas.com/D3361
2016-10-26 20:40:10 -07:00
Ben Gotow 9ee3087d10 feat(tokens): Multi-select for tokenized text fields
Test Plan: Lots of tests, mostly updated to enzyme. Not many new ones.

Reviewers: evan, juan, jackie

Reviewed By: juan, jackie

Subscribers: juan

Differential Revision: https://phab.nylas.com/D3374
2016-10-26 13:58:57 -07:00
Ben Gotow ded52ce101 rm(grim): We’re not using Grim for deprecations 2016-10-25 11:36:20 -07:00
Ben Gotow d6f7984c82 fix(mailto): Support body with \n or \r characters
Related to #2923
2016-10-18 10:37:27 -07:00