summaryrefslogtreecommitdiff
path: root/tst/email.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tst/email.test.ts')
-rw-r--r--tst/email.test.ts100
1 files changed, 100 insertions, 0 deletions
diff --git a/tst/email.test.ts b/tst/email.test.ts
new file mode 100644
index 0000000..dfab0ff
--- /dev/null
+++ b/tst/email.test.ts
@@ -0,0 +1,100 @@
+import { test, expect, jest } from '@jest/globals';
+import { Email, Outbox, Inbox, IRecv, ISend } from '@emprespresso/uptime';
+import { LogMetricTraceable, Either } from '@emprespresso/pengueno';
+import { FetchMessageObject, MailboxLockObject } from 'imapflow';
+
+const createMockRecv = (): IRecv => ({
+ connect: jest.fn(),
+ logout: jest.fn(),
+ getMailboxLock: jest.fn(),
+ mailboxClose: jest.fn(),
+ fetchAll: jest.fn(),
+ messageDelete: jest.fn(),
+});
+
+const createMockSend = (): ISend => ({
+ sendMail: jest.fn(),
+});
+
+const from = {
+ send_port: 465,
+ email: 'test@localhost',
+ username: 'test',
+ password: 'password',
+ send_host: 'localhost',
+};
+
+const to = {
+ read_port: 993,
+ email: 'test@localhost',
+ username: 'test',
+ password: 'password',
+ read_host: 'localhost',
+};
+
+test('failure to send message goes left', async () => {
+ const send = createMockSend();
+
+ send.sendMail.mockRejectedValue(new Error('fail'));
+
+ const outbox = new Outbox(send);
+
+ const email = LogMetricTraceable.of<Email>({
+ from: from.email,
+ to: to.email,
+ subject: 'test',
+ text: 'test',
+ });
+
+ const result = await outbox.send(email);
+
+ expect(result.left().present()).toBe(true);
+ expect(result.left().get().message).toBe('fail');
+});
+
+test('goes left when message not ever received', async () => {
+ const recv = createMockRecv();
+
+ recv.fetchAll.mockResolvedValue([]);
+ const lock: MailboxLockObject = { path: 'INBOX', release: jest.fn() };
+
+ const inbox = new Inbox(recv, LogMetricTraceable.of(lock));
+
+ const email = LogMetricTraceable.of<Email>({
+ from: from.email,
+ to: to.email,
+ subject: 'test',
+ text: 'test',
+ });
+
+ const result = await inbox.find(email);
+
+ expect(result.left().present()).toBe(true);
+});
+
+test('releases lock on failure', async () => {
+ const recv = createMockRecv();
+
+ recv.fetchAll.mockResolvedValue([]);
+ const lock: MailboxLockObject = { path: 'INBOX', release: jest.fn() };
+
+ // Mock mailboxClose and logout to resolve properly
+ recv.mailboxClose.mockResolvedValue(true);
+ recv.logout.mockResolvedValue();
+
+ const inbox = new Inbox(recv, LogMetricTraceable.of(lock));
+
+ const email = LogMetricTraceable.of<Email>({
+ from: from.email,
+ to: to.email,
+ subject: 'test',
+ text: 'test',
+ });
+
+ await inbox.find(email);
+ await inbox.close();
+
+ expect(lock.release).toHaveBeenCalledTimes(1);
+ expect(recv.mailboxClose).toHaveBeenCalledTimes(1);
+ expect(recv.logout).toHaveBeenCalledTimes(1);
+});