@remit/drizzle-service 0.0.48 → 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/drizzle-service",
3
- "version": "0.0.48",
3
+ "version": "0.0.49",
4
4
  "description": "Drizzle ORM service over SQLite",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -0,0 +1,79 @@
1
+ import assert from "node:assert/strict";
2
+ import { randomUUID } from "node:crypto";
3
+ import { after, before, describe, test } from "node:test";
4
+ import { mailboxSpecialUseTable, mailboxTable } from "../schema.js";
5
+ import { createSqliteTestDb } from "../test-db-sqlite.js";
6
+ import { MailboxRepo } from "./i4-mailbox.js";
7
+ import { MailboxSpecialUseRepo } from "./i4-mailbox-special-use.js";
8
+
9
+ const makeMailboxInput = (accountId: string, fullPath: string) => ({
10
+ accountId,
11
+ namespaceType: "personal" as const,
12
+ namespacePrefix: "",
13
+ hierarchyDelimiter: "/",
14
+ fullPath,
15
+ uidValidity: 1,
16
+ uidNext: 1,
17
+ highestModseq: "0",
18
+ messageCount: 0,
19
+ unseenCount: 0,
20
+ deletedCount: 0,
21
+ totalSize: 0,
22
+ lastSyncUid: 0,
23
+ highWaterMarkUid: 0,
24
+ lastMessageSyncAt: Date.now(),
25
+ });
26
+
27
+ describe("MailboxSpecialUseRepo.findJunkMailbox (sqlite)", () => {
28
+ let close: () => Promise<void>;
29
+ let repo: MailboxSpecialUseRepo;
30
+ let mailboxes: MailboxRepo;
31
+
32
+ before(async () => {
33
+ const testDb = await createSqliteTestDb({
34
+ mailbox: mailboxTable,
35
+ mailboxSpecialUse: mailboxSpecialUseTable,
36
+ });
37
+ close = testDb.close;
38
+ repo = new MailboxSpecialUseRepo(testDb.db as never);
39
+ mailboxes = new MailboxRepo(testDb.db as never);
40
+ });
41
+
42
+ after(async () => {
43
+ await close();
44
+ });
45
+
46
+ test("resolves an INBOX-nested Junk folder that advertises \\Junk", async () => {
47
+ const accountId = randomUUID();
48
+ await mailboxes.create(makeMailboxInput(accountId, "INBOX"));
49
+ const spam = await mailboxes.create(
50
+ makeMailboxInput(accountId, "INBOX/Spam"),
51
+ );
52
+ await repo.create(spam.mailboxId, "Junk");
53
+
54
+ const found = await repo.findJunkMailbox(accountId);
55
+ assert.equal(found?.mailboxId, spam.mailboxId);
56
+ });
57
+
58
+ test("resolves an INBOX-nested Junk folder that advertises no special use", async () => {
59
+ // The name fallback used to compare the whole path against a list of
60
+ // bare names, so `INBOX/Spam` resolved to nothing on a server that
61
+ // advertises no \Junk.
62
+ const accountId = randomUUID();
63
+ await mailboxes.create(makeMailboxInput(accountId, "INBOX"));
64
+ const spam = await mailboxes.create(
65
+ makeMailboxInput(accountId, "INBOX/Spam"),
66
+ );
67
+
68
+ const found = await repo.findJunkMailbox(accountId);
69
+ assert.equal(found?.mailboxId, spam.mailboxId);
70
+ });
71
+
72
+ test("answers null when the account has no Junk folder at all", async () => {
73
+ const accountId = randomUUID();
74
+ await mailboxes.create(makeMailboxInput(accountId, "INBOX"));
75
+ await mailboxes.create(makeMailboxInput(accountId, "INBOX/Work"));
76
+
77
+ assert.equal(await repo.findJunkMailbox(accountId), null);
78
+ });
79
+ });
@@ -3,6 +3,7 @@ import type {
3
3
  MailboxSpecialUseItem,
4
4
  MailboxSpecialUseValue,
5
5
  } from "@remit/data-ports";
6
+ import { resolveMailboxByLeafName } from "@remit/data-ports/mailbox-name";
6
7
  import { eq } from "drizzle-orm";
7
8
  import type { Db } from "../db.js";
8
9
  import { randomId } from "../id.js";
@@ -10,6 +11,14 @@ import { mailboxSpecialUseTable, mailboxTable } from "../schema/i4-mailbox.js";
10
11
 
11
12
  type DB = Db<Record<string, unknown>>;
12
13
 
14
+ const JUNK_FOLDER_NAMES = [
15
+ "junk",
16
+ "spam",
17
+ "junk e-mail",
18
+ "junk email",
19
+ "bulk mail",
20
+ ];
21
+
13
22
  function rowToSpecialUse(
14
23
  row: typeof mailboxSpecialUseTable.$inferSelect,
15
24
  ): MailboxSpecialUseItem {
@@ -155,9 +164,7 @@ export class MailboxSpecialUseRepo implements IMailboxSpecialUseRepository {
155
164
  .select()
156
165
  .from(mailboxTable)
157
166
  .where(eq(mailboxTable.accountId, accountId));
158
-
159
- const names = ["junk", "spam", "bulk mail", "junk e-mail", "[gmail]/spam"];
160
- const found = rows.find((r) => names.includes(r.fullPath.toLowerCase()));
167
+ const found = resolveMailboxByLeafName(rows, JUNK_FOLDER_NAMES);
161
168
  return found
162
169
  ? { mailboxId: found.mailboxId, fullPath: found.fullPath }
163
170
  : null;