Mailspring/packages/isomorphic-core/spec/message-factory-spec.js

123 lines
5.3 KiB
JavaScript
Raw Normal View History

Revert "Revert "[feat] Add support for send later"" Arc land messed up and landed a not fully merged branch. (Seriously – I had merged a copy of my branch with master to see how easy it would be. Because I didn't want to merge the whole thing, I blindly committed my changes and switched back to my real branch). To my great surprise, arc decided to use the wrong branch when landing it. Original commit message: Summary: Finally, here it is! Send later, with support for open tracking but without support for attachments yet. It took me some time to find the right way to do things. **The send later dilemna** There's two ways we could handle send later: 1. do everything on the client 2. process the message in the cloud 1. is very tempting because it would make the cloud server very simple. Unfortunately, it has some serious limitations, for example, setting the "Date" message header. That's why I chose to go with 2. When a user presses the "Send Later" button, we save the open/link tracking metadata and fills in all the required fields. I added a custom endpoint to the K2 API to do this, `/drafts/build`. After that, we save the JSON contents of the message as metadata. When we process metadata, we simply create a MIME message from the JSON and send it. **Limitations** Right now, send later doesn't support and attachments. There's also some minor code duplication which needs to be refactored away. Test Plan: Tested manually. Checked that regular send still worked, too. Reviewers: mark, spang, halla, juan, evan Reviewed By: evan Differential Revision: https://phab.nylas.com/D4054
2017-03-08 08:27:08 +08:00
const {parseFromImap, parseSnippet, parseContacts} = require('../src/message-factory');
const {forEachJSONFixture, forEachHTMLAndTXTFixture, ACCOUNT_ID, getTestDatabase} = require('../helpers');
xdescribe('MessageFactory', function MessageFactorySpecs() {
beforeEach(() => {
waitsForPromise(async () => {
const db = await getTestDatabase()
const folder = await db.Folder.create({
id: 'test-folder-id',
accountId: ACCOUNT_ID,
version: 1,
name: 'Test Folder',
role: null,
});
this.options = { accountId: ACCOUNT_ID, db, folder };
})
})
describe("parseFromImap", () => {
forEachJSONFixture('MessageFactory/parseFromImap', (filename, json) => {
it(`should correctly build message properties for ${filename}`, () => {
const {imapMessage, desiredParts, result} = json;
// requiring these to match makes it overly arduous to generate test
// cases from real accounts
const excludeKeys = new Set(['id', 'accountId', 'folderId', 'folder', 'labels']);
waitsForPromise(async () => {
const actual = await parseFromImap(imapMessage, desiredParts, this.options);
for (const key of Object.keys(result)) {
if (!excludeKeys.has(key)) {
expect(actual[key]).toEqual(result[key]);
}
}
});
});
})
});
});
const snippetTestCases = [{
purpose: 'trim whitespace in basic plaintext',
body: '<pre>The quick brown fox\n\n\tjumps over the lazy</pre>',
snippet: 'The quick brown fox jumps over the lazy',
}, {
purpose: 'truncate long plaintext without breaking words',
body: '<pre>The quick brown fox jumps over the lazy dog and then the lazy dog rolls over and sighs. The fox turns around in a circle and then jumps onto a bush! It grins wickedly and wags its fat tail. As the lazy dog puts its head on its paws and cracks a sleepy eye open, a slow grin forms on its face. The fox has fallen into the bush and is yelping and squeaking.</pre>',
snippet: 'The quick brown fox jumps over the lazy dog and then the lazy dog rolls over and sighs. The fox turns',
}, {
purpose: 'process basic HTML correctly',
body: '<html><title>All About Ponies</title><h1>PONIES AND RAINBOWS AND UNICORNS</h1><p>Unicorns are native to the hillsides of Flatagonia.</p></html>',
snippet: 'PONIES AND RAINBOWS AND UNICORNS Unicorns are native to the hillsides of Flatagonia.',
}, {
purpose: 'properly strip rogue styling inside of <body> and trim whitespace in HTML',
body: '<html>\n <head></head>\n <body>\n <style>\n body { width: 100% !important; min-width: 100%; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; margin: 0; padding: 0; background: #fafafa;\n </style>\n <p>Look ma, no CSS!</p></body></html>',
snippet: 'Look ma, no CSS!',
}, {
purpose: 'properly process <br/> and <div/>',
body: '<p>Unicorns are <div>native</div>to the<br/>hillsides of<br/>Flatagonia.</p>',
snippet: 'Unicorns are native to the hillsides of Flatagonia.',
}, {
purpose: 'properly strip out HTML comments',
body: '<p>Unicorns are<!-- an HTML comment! -->native to the</p>',
snippet: 'Unicorns are native to the',
}, {
purpose: "don't add extraneous spaces after text format markup",
body: `
<td style="padding: 0px 10px">
Hey there, <b>Nylas</b>!<br>
You have a new follower on Product Hunt.
</td>`,
snippet: 'Hey there, Nylas! You have a new follower on Product Hunt.',
},
]
const contactsTestCases = [{
purpose: "not erroneously split contact names on commas",
// NOTE: inputs must be in same format as output by mimelib.parseHeader
input: ['"Little Bo Peep, The Hill" <bopeep@example.com>'],
output: [{name: "Little Bo Peep, The Hill", email: "bopeep@example.com"}],
}, {
purpose: "extract two separate contacts, removing quotes properly & respecing unicode",
input: ['AppleBees Zé <a@example.com>, "Tiger Zen" b@example.com'],
output: [
{name: 'AppleBees Zé', email: 'a@example.com'},
{name: 'Tiger Zen', email: 'b@example.com'},
],
}, {
purpose: "correctly concatenate multiple array elements (from multiple header lines)",
input: ['Yubi Key <yubi@example.com>', 'Smokey the Bear <smokey@example.com>'],
output: [
{name: 'Yubi Key', email: 'yubi@example.com'},
{name: 'Smokey the Bear', email: 'smokey@example.com'},
],
},
]
describe('MessageFactoryHelpers', function MessageFactoryHelperSpecs() {
describe('parseSnippet (basic)', () => {
snippetTestCases.forEach(({purpose, body, snippet}) => {
it(`should ${purpose}`, () => {
const parsedSnippet = parseSnippet(body);
expect(parsedSnippet).toEqual(snippet);
});
});
});
describe('parseSnippet (real world)', () => {
forEachHTMLAndTXTFixture('MessageFactory/parseSnippet', (filename, html, txt) => {
it(`should correctly extract the snippet from the html`, () => {
const parsedSnippet = parseSnippet(html);
expect(parsedSnippet).toEqual(txt);
});
});
});
describe('parseContacts (basic)', () => {
contactsTestCases.forEach(({purpose, input, output}) => {
it(`should ${purpose}`, () => {
const parsedContacts = parseContacts(input);
expect(parsedContacts).toEqual(output);
});
});
});
});