From 6f45fe5a10174fd33932d17dc056898f06466067 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 15 Dec 2024 12:29:30 -0800 Subject: [PATCH] close the mailbox --- src/email.ts | 15 ++++++++++++--- tst/email.spec.ts | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/email.ts b/src/email.ts index cf588f4..337572c 100644 --- a/src/email.ts +++ b/src/email.ts @@ -26,6 +26,7 @@ interface ImapClientI { opts: Record, ) => Promise; logout: () => Promise; + mailboxClose: () => Promise; } type Email = { @@ -287,7 +288,10 @@ export const perform = ( if (O.isSome(e.imap)) { const imap = e.imap.value; return pipe( - TE.tryCatch(() => imap.logout(), toError), + TE.tryCatch( + () => imap.mailboxClose().then(() => imap.logout()), + toError, + ), TE.flatMap(() => TE.left(e)), ); } @@ -295,8 +299,13 @@ export const perform = ( }, ({ mailboxLock, deleted, imap }) => { mailboxLock.release(); - imap.logout(); - return TE.right(deleted); + return pipe( + TE.tryCatch( + () => imap.mailboxClose().then(() => imap.logout()), + toError, + ), + TE.flatMap(() => TE.right(deleted)), + ); }, ), ); diff --git a/tst/email.spec.ts b/tst/email.spec.ts index e966030..79c21cc 100644 --- a/tst/email.spec.ts +++ b/tst/email.spec.ts @@ -32,6 +32,7 @@ const getMocks = () => { getMailboxLock: mock(() => Promise.resolve(lock)), messageDelete: mock(() => Promise.resolve(true)), logout: mock(() => Promise.resolve()), + mailboxClose: mock(() => Promise.resolve()), }; const mockDependencies: Partial = { @@ -116,6 +117,7 @@ test("releases lock on left", async () => { TE.mapLeft(() => { expect(imap.getMailboxLock).toHaveBeenCalledTimes(1); expect(lock.release).toHaveBeenCalledTimes(1); + expect(imap.mailboxClose).toHaveBeenCalledTimes(1); expect(imap.logout).toHaveBeenCalledTimes(1); }), )(); @@ -133,6 +135,7 @@ test("releases lock on right", async () => { expect(imap.getMailboxLock).toHaveBeenCalledTimes(1); expect(lock.release).toHaveBeenCalledTimes(1); expect(imap.logout).toHaveBeenCalledTimes(1); + expect(imap.mailboxClose).toHaveBeenCalledTimes(1); }), TE.mapLeft(() => expect(false).toBeTruthy()), )();