import assert from "node:assert/strict"; import { test } from "node:test"; import { AccountAuthType, CanonicalMailboxRole, LabelColor, MessageCategory, } from "@remit/domain-enums"; import { AccountSchema, AddressFlagsEntrySchema, FilterSchema, FolderOverrideSchema, LabelSchema, MessageDecisionsSchema, ReaderConfigDocumentSchema, } from "./document.js"; import { readGoldenConfigDocument } from "./fixtures.js"; const goldenAccount = () => structuredClone( (readGoldenConfigDocument() as { accounts: unknown[] }).accounts[0], ) as Record; test("the account schema is strict at every level it defines", () => { for (const mutate of [ (account: Record) => { account.legacyField = 1; }, (account: Record) => { (account.imap as Record).compress = true; }, (account: Record) => { (account.signature as Record).markdown = ""; }, ]) { const account = goldenAccount(); mutate(account); assert.equal(AccountSchema.safeParse(account).success, false); } }); test("an account declares which credential it needs, and never more", () => { const account = goldenAccount(); account.credentials = { required: "certificate" }; assert.equal(AccountSchema.safeParse(account).success, false); }); test("enum values come from the domain, so a stale spelling is rejected", () => { const account = goldenAccount(); account.authType = "oauthGoogle"; assert.equal(AccountSchema.safeParse(account).success, false); account.authType = AccountAuthType.OauthMicrosoft; assert.equal(AccountSchema.safeParse(account).success, true); assert.equal( LabelSchema.safeParse({ name: "x", color: "Chartreuse" }).success, false, ); assert.equal( LabelSchema.safeParse({ name: "x", color: LabelColor.Teal }).success, true, ); }); test("folder roles and overrides travel by IMAP path", () => { const account = goldenAccount(); account.folderRoles = [ { role: CanonicalMailboxRole.Archive, mailboxId: "not-a-path" }, ]; assert.equal(AccountSchema.safeParse(account).success, false); assert.equal( FolderOverrideSchema.safeParse({ folderPath: "INBOX.Lists", displayName: "", muted: null, }).success, true, ); }); test("a filter names its label by name and its folder by account and path", () => { const filter = { name: "keep", scope: "Standing", expiresAt: null, rule: { version: 1, test: { test: "header", match: "Is", header: "ListId", key: "x.example", }, }, priority: 0, stop: false, actionLabelName: "Facturen", actionFolder: { accountId: "6f4c2c309a2c4a7f9f9f1f2c3", folderPath: "INBOX.Facturen", }, }; assert.equal(FilterSchema.safeParse(filter).success, true); assert.equal( FilterSchema.safeParse({ ...filter, actionLabelId: "Facturen" }).success, false, ); assert.equal( FilterSchema.safeParse({ ...filter, actionFolder: { mailboxId: "6f4c2c309a2c4a7f9f9f1f2c3" }, }).success, false, ); }); test("an address flag keeps its whole payload", () => { const entry = { normalizedEmail: "post@bank.example", displayName: "Bank", flags: { blocked: { value: true, setAt: 1750000000000, setBy: "web-client", expiresAt: 1790000000000, reason: "phishing", }, category: { value: MessageCategory.transactional, setAt: 1750000000000, }, }, }; const parsed = AddressFlagsEntrySchema.parse(entry); assert.deepEqual(parsed, entry); assert.equal( AddressFlagsEntrySchema.safeParse({ ...entry, flags: { blocked: true }, }).success, false, ); }); test("an unknown flag key is refused, so a flag is added deliberately", () => { assert.equal( AddressFlagsEntrySchema.safeParse({ normalizedEmail: "a@b.example", displayName: "", flags: { shouted: { value: true, setAt: 1 } }, }).success, false, ); }); test("the two derived flags have no place in the file", () => { for (const flag of ["wellknown", "junkOnly"]) { assert.equal( AddressFlagsEntrySchema.safeParse({ normalizedEmail: "a@b.example", displayName: "", flags: { [flag]: { value: true, setAt: 1 } }, }).success, false, ); } }); test("the reserved decisions slot is defined and empty", () => { assert.equal(MessageDecisionsSchema.safeParse([]).success, true); assert.equal(MessageDecisionsSchema.safeParse([{}]).success, false); }); test("an id is the stored 25-character form, never the RFC 4122 spelling", () => { // Every id in this database is a UUID re-encoded base36 to 25 characters, // and `UUID` in the API contract means exactly that. A schema that took the // dashed spelling would refuse every id a real export produces, so the // dashed one has to fail here for the stored one to be the contract. const account = goldenAccount(); account.accountId = "6f4c2c30-9a2c-4a7f-9f9f-1f2c3d4e5a6b"; assert.equal(AccountSchema.safeParse(account).success, false); assert.equal( AccountSchema.safeParse({ ...goldenAccount(), accountId: "6f4c2c309a2c4a7f9f9f1f2c3", }).success, true, ); }); test("the composer format is a server setting, not a browser one", () => { // It lives in an AccountSetting row the server owns and a database drop // takes with it, so it rides `accountConfig`. Carrying it in the block the // server ignores would have the file record it and the import discard it. const source = readGoldenConfigDocument() as { accountConfig: Record; clientPreferences: Record; }; assert.equal(source.accountConfig.defaultComposerFormat, "markdown"); assert.equal( ReaderConfigDocumentSchema.safeParse({ ...source, clientPreferences: { ...source.clientPreferences, defaultComposerFormat: "markdown", }, }).success, false, ); }); test("the document schema refuses an unknown top-level section", () => { const source = readGoldenConfigDocument() as Record; assert.equal( ReaderConfigDocumentSchema.safeParse({ ...source, signatures: [] }).success, false, ); });