2023-06-29 18:26:45 +02:00
|
|
|
import o from "@tutao/otest"
|
2024-07-01 17:56:41 +02:00
|
|
|
import { Notifications } from "../../../src/common/gui/Notifications.js"
|
2025-02-10 15:59:28 +01:00
|
|
|
import { Spy, spy, verify } from "@tutao/tutanota-test-utils"
|
2024-08-07 08:38:58 +02:00
|
|
|
import { MailSetKind, OperationType } from "../../../src/common/api/common/TutanotaConstants.js"
|
2025-10-14 17:54:14 +02:00
|
|
|
import {
|
|
|
|
BodyTypeRef,
|
|
|
|
Mail,
|
|
|
|
MailDetailsTypeRef,
|
|
|
|
MailFolderTypeRef,
|
|
|
|
MailSetEntryTypeRef,
|
|
|
|
MailTypeRef,
|
|
|
|
} from "../../../src/common/api/entities/tutanota/TypeRefs.js"
|
2024-07-01 17:56:41 +02:00
|
|
|
import { EntityClient } from "../../../src/common/api/common/EntityClient.js"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { EntityRestClientMock } from "../api/worker/rest/EntityRestClientMock.js"
|
|
|
|
import { downcast } from "@tutao/tutanota-utils"
|
2024-07-01 17:56:41 +02:00
|
|
|
import { LoginController } from "../../../src/common/api/main/LoginController.js"
|
2024-08-20 18:03:03 +02:00
|
|
|
import { instance, matchers, object, when } from "testdouble"
|
2024-07-01 17:56:41 +02:00
|
|
|
import { UserController } from "../../../src/common/api/main/UserController.js"
|
2023-11-10 16:59:39 +01:00
|
|
|
import { createTestEntity } from "../TestUtils.js"
|
2025-07-04 13:37:36 +02:00
|
|
|
import { EntityUpdateData, PrefetchStatus } from "../../../src/common/api/common/utils/EntityUpdateUtils.js"
|
2025-06-13 17:27:15 +02:00
|
|
|
import { MailboxModel } from "../../../src/common/mailFunctionality/MailboxModel.js"
|
2024-08-07 08:38:58 +02:00
|
|
|
import { getElementId, getListId } from "../../../src/common/api/common/utils/EntityUtils.js"
|
2024-08-20 18:03:03 +02:00
|
|
|
import { MailModel } from "../../../src/mail-app/mail/model/MailModel.js"
|
|
|
|
import { EventController } from "../../../src/common/api/main/EventController.js"
|
|
|
|
import { MailFacade } from "../../../src/common/api/worker/facades/lazy/MailFacade.js"
|
2025-05-16 16:03:24 +02:00
|
|
|
import { ClientModelInfo } from "../../../src/common/api/common/EntityFunctions"
|
2025-10-14 15:53:36 +02:00
|
|
|
import { InboxRuleHandler } from "../../../src/mail-app/mail/model/InboxRuleHandler"
|
|
|
|
import { SpamClassificationHandler } from "../../../src/mail-app/mail/model/SpamClassificationHandler"
|
|
|
|
import { SpamClassifier } from "../../../src/mail-app/workerUtils/spamClassification/SpamClassifier"
|
|
|
|
|
|
|
|
const { anything } = matchers
|
2018-10-22 12:02:13 +02:00
|
|
|
|
|
|
|
o.spec("MailModelTest", function () {
|
2021-12-23 14:03:23 +01:00
|
|
|
let notifications: Partial<Notifications>
|
2018-10-22 12:02:13 +02:00
|
|
|
let showSpy: Spy
|
2024-08-20 18:03:03 +02:00
|
|
|
let model: MailModel
|
2025-02-06 10:42:22 +01:00
|
|
|
const inboxFolder = createTestEntity(MailFolderTypeRef, { _id: ["folderListId", "inboxId"] })
|
2024-08-07 08:38:58 +02:00
|
|
|
inboxFolder.folderType = MailSetKind.INBOX
|
2025-02-06 10:42:22 +01:00
|
|
|
const anotherFolder = createTestEntity(MailFolderTypeRef, { _id: ["folderListId", "archiveId"] })
|
2024-08-07 08:38:58 +02:00
|
|
|
anotherFolder.folderType = MailSetKind.ARCHIVE
|
2022-11-08 17:06:42 +01:00
|
|
|
let logins: LoginController
|
2025-02-10 15:59:28 +01:00
|
|
|
let mailFacade: MailFacade
|
2024-08-07 08:38:58 +02:00
|
|
|
const restClient: EntityRestClientMock = new EntityRestClientMock()
|
2023-05-17 16:49:56 +02:00
|
|
|
|
2018-10-22 12:02:13 +02:00
|
|
|
o.beforeEach(function () {
|
|
|
|
notifications = {}
|
2024-08-20 18:03:03 +02:00
|
|
|
const mailboxModel = instance(MailboxModel)
|
|
|
|
const eventController = instance(EventController)
|
2025-02-10 15:59:28 +01:00
|
|
|
mailFacade = instance(MailFacade)
|
2018-10-22 12:02:13 +02:00
|
|
|
showSpy = notifications.showNotification = spy()
|
2022-11-08 17:06:42 +01:00
|
|
|
logins = object()
|
2023-08-31 16:31:00 +02:00
|
|
|
let userController = object<UserController>()
|
|
|
|
when(userController.isUpdateForLoggedInUserInstance(matchers.anything(), matchers.anything())).thenReturn(false)
|
|
|
|
when(logins.getUserController()).thenReturn(userController)
|
|
|
|
|
2025-05-16 16:03:24 +02:00
|
|
|
model = new MailModel(
|
|
|
|
downcast({}),
|
|
|
|
mailboxModel,
|
|
|
|
eventController,
|
|
|
|
new EntityClient(restClient, ClientModelInfo.getNewInstanceForTestsOnly()),
|
|
|
|
logins,
|
|
|
|
mailFacade,
|
|
|
|
null,
|
2025-10-14 15:53:36 +02:00
|
|
|
() => object(),
|
2025-06-10 18:17:24 +02:00
|
|
|
() => null,
|
2025-05-16 16:03:24 +02:00
|
|
|
)
|
2018-10-22 12:02:13 +02:00
|
|
|
})
|
2025-10-14 15:53:36 +02:00
|
|
|
|
2020-09-16 14:36:28 +02:00
|
|
|
o("doesn't send notification for another folder", async function () {
|
2025-02-06 10:42:22 +01:00
|
|
|
const mailSetEntry = createTestEntity(MailSetEntryTypeRef, { _id: [anotherFolder.entries, "mailSetEntryId"] })
|
|
|
|
restClient.addListInstances(mailSetEntry)
|
2024-08-20 18:03:03 +02:00
|
|
|
await model.entityEventsReceived([
|
|
|
|
makeUpdate({
|
2025-08-08 13:46:06 +02:00
|
|
|
instanceListId: getListId(mailSetEntry) as NonEmptyString,
|
2025-02-06 10:42:22 +01:00
|
|
|
instanceId: getElementId(mailSetEntry),
|
2024-08-20 18:03:03 +02:00
|
|
|
operation: OperationType.CREATE,
|
|
|
|
}),
|
|
|
|
])
|
2018-10-22 12:02:13 +02:00
|
|
|
o(showSpy.invocations.length).equals(0)
|
|
|
|
})
|
2020-09-16 14:36:28 +02:00
|
|
|
o("doesn't send notification for move operation", async function () {
|
2025-02-06 10:42:22 +01:00
|
|
|
const mailSetEntry = createTestEntity(MailSetEntryTypeRef, { _id: [inboxFolder.entries, "mailSetEntryId"] })
|
|
|
|
restClient.addListInstances(mailSetEntry)
|
2024-08-20 18:03:03 +02:00
|
|
|
await model.entityEventsReceived([
|
|
|
|
makeUpdate({
|
2025-08-08 13:46:06 +02:00
|
|
|
instanceListId: getListId(mailSetEntry) as NonEmptyString,
|
2025-02-06 10:42:22 +01:00
|
|
|
instanceId: getElementId(mailSetEntry),
|
2024-08-20 18:03:03 +02:00
|
|
|
operation: OperationType.DELETE,
|
|
|
|
}),
|
|
|
|
makeUpdate({
|
2025-08-08 13:46:06 +02:00
|
|
|
instanceListId: getListId(mailSetEntry) as NonEmptyString,
|
2025-02-06 10:42:22 +01:00
|
|
|
instanceId: getElementId(mailSetEntry),
|
2024-08-20 18:03:03 +02:00
|
|
|
operation: OperationType.CREATE,
|
|
|
|
}),
|
|
|
|
])
|
2018-10-22 12:02:13 +02:00
|
|
|
o(showSpy.invocations.length).equals(0)
|
|
|
|
})
|
2025-02-10 15:59:28 +01:00
|
|
|
o("markMails", async function () {
|
2025-02-10 16:43:42 +01:00
|
|
|
const mailId1: IdTuple = ["mailbag id1", "mail id1"]
|
|
|
|
const mailId2: IdTuple = ["mailbag id2", "mail id2"]
|
|
|
|
const mailId3: IdTuple = ["mailbag id3", "mail id3"]
|
|
|
|
await model.markMails([mailId1, mailId2, mailId3], true)
|
|
|
|
verify(mailFacade.markMails([mailId1, mailId2, mailId3], true))
|
2025-02-10 15:59:28 +01:00
|
|
|
})
|
|
|
|
|
2025-10-14 15:53:36 +02:00
|
|
|
o.spec("Inbox rule and spam prediction", () => {
|
|
|
|
let inboxRuleHandler: InboxRuleHandler
|
|
|
|
let spamClassificationHandler: SpamClassificationHandler
|
|
|
|
let spamClassifier: SpamClassifier
|
|
|
|
let mailboxModel: MailboxModel
|
|
|
|
|
|
|
|
o.beforeEach(async () => {
|
|
|
|
const entityClient = new EntityClient(restClient, ClientModelInfo.getNewInstanceForTestsOnly())
|
|
|
|
mailboxModel = instance(MailboxModel)
|
|
|
|
inboxRuleHandler = object<InboxRuleHandler>()
|
|
|
|
spamClassifier = object<SpamClassifier>()
|
|
|
|
spamClassificationHandler = new SpamClassificationHandler(object(), spamClassifier, entityClient, object())
|
|
|
|
|
|
|
|
model = new MailModel(
|
|
|
|
downcast({}),
|
|
|
|
mailboxModel,
|
|
|
|
instance(EventController),
|
|
|
|
entityClient,
|
|
|
|
logins,
|
|
|
|
mailFacade,
|
|
|
|
null,
|
|
|
|
() => spamClassificationHandler,
|
|
|
|
() => inboxRuleHandler,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
o("does not try to apply inbox rule when downloading of mail fails on create mail event", async function () {
|
|
|
|
when(spamClassificationHandler.downloadMail(matchers.anything())).thenResolve(null)
|
|
|
|
|
|
|
|
const mailCreateEvent = makeUpdate({ instanceListId: "mailListId", instanceId: "mailId", operation: OperationType.CREATE })
|
|
|
|
await model.entityEventsReceived([mailCreateEvent])
|
|
|
|
|
2025-10-14 16:35:33 +02:00
|
|
|
verify(inboxRuleHandler.findAndApplyMatchingRule(anything(), anything(), anything()), { times: 0 })
|
2025-10-14 15:53:36 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
o("does not try to do spam classification when downloading of mail fails on create mail event", async function () {
|
|
|
|
when(spamClassificationHandler.downloadMail(matchers.anything())).thenResolve(null)
|
|
|
|
|
|
|
|
const mailCreateEvent = makeUpdate({ instanceListId: "mailListId", instanceId: "mailId", operation: OperationType.CREATE })
|
|
|
|
await model.entityEventsReceived([mailCreateEvent])
|
|
|
|
|
|
|
|
verify(spamClassificationHandler.predictSpamForNewMail(anything(), anything(), anything()), { times: 0 })
|
|
|
|
})
|
|
|
|
|
2025-10-14 17:54:14 +02:00
|
|
|
o("no spam prediction if inbox rule is applied", async () => {
|
|
|
|
const mailDetails = createTestEntity(MailDetailsTypeRef, {
|
|
|
|
_id: "mailDetail",
|
|
|
|
body: createTestEntity(BodyTypeRef, { text: "some text" }),
|
|
|
|
})
|
|
|
|
const mail = createTestEntity(MailTypeRef, { _id: ["mailListId", "mailId"], mailDetails: ["detailsList", mailDetails._id] })
|
|
|
|
when(spamClassificationHandler.downloadMail(matchers.anything())).thenResolve(mail)
|
|
|
|
when(spamClassificationHandler.downloadMailDetails(mail)).thenResolve(mailDetails)
|
2025-10-14 15:53:36 +02:00
|
|
|
|
2025-10-14 17:54:14 +02:00
|
|
|
const inboxRuleTargetFolder = createTestEntity(MailFolderTypeRef, { _id: ["folderListId", "inboxRuleTarget"] })
|
2025-10-14 16:35:33 +02:00
|
|
|
when(inboxRuleHandler.findAndApplyMatchingRule(anything(), anything(), anything())).thenResolve(inboxRuleTargetFolder)
|
2025-10-14 15:53:36 +02:00
|
|
|
|
|
|
|
const mailCreateEvent = makeUpdate({ instanceListId: "mailListId", instanceId: "mailId", operation: OperationType.CREATE })
|
|
|
|
await model.entityEventsReceived([mailCreateEvent])
|
|
|
|
|
|
|
|
verify(spamClassificationHandler.predictSpamForNewMail(anything(), mail, anything()), { times: 1 })
|
|
|
|
verify(spamClassifier.predict(anything()), { times: 0 })
|
|
|
|
})
|
|
|
|
|
|
|
|
o("Do spam prediction if inbox rule is not applied", async () => {
|
2025-10-14 17:54:14 +02:00
|
|
|
const mailDetails = createTestEntity(MailDetailsTypeRef, {
|
|
|
|
_id: "mailDetail",
|
|
|
|
body: createTestEntity(BodyTypeRef, { text: "some text" }),
|
|
|
|
})
|
|
|
|
const mail = createTestEntity(MailTypeRef, { _id: ["mailListId", "mailId"], mailDetails: ["detailsList", mailDetails._id] })
|
|
|
|
when(spamClassificationHandler.downloadMail(matchers.anything())).thenResolve(mail)
|
|
|
|
when(spamClassificationHandler.downloadMailDetails(mail)).thenResolve(mailDetails)
|
2025-10-14 15:53:36 +02:00
|
|
|
|
2025-10-14 16:35:33 +02:00
|
|
|
when(inboxRuleHandler.findAndApplyMatchingRule(anything(), anything(), anything())).thenResolve(null)
|
2025-10-14 15:53:36 +02:00
|
|
|
when(spamClassifier.predict(anything())).thenResolve(null)
|
|
|
|
|
|
|
|
const mailCreateEvent = makeUpdate({ instanceListId: "mailListId", instanceId: "mailId", operation: OperationType.CREATE })
|
|
|
|
await model.entityEventsReceived([mailCreateEvent])
|
|
|
|
|
|
|
|
verify(spamClassifier.predict(anything()), { times: 1 })
|
|
|
|
})
|
|
|
|
|
|
|
|
o("do not do spam prediction for draft mail", async () => {
|
|
|
|
const mail = createTestEntity(MailTypeRef, { _id: ["mailListId", "mailId"], mailDetailsDraft: ["draftListId", "draftId"], mailDetails: null })
|
2025-10-14 17:54:14 +02:00
|
|
|
|
2025-10-14 15:53:36 +02:00
|
|
|
const inboxRuleTargetFolder = createTestEntity(MailFolderTypeRef, { _id: ["folderListId", "inboxRuleTarget"] })
|
|
|
|
|
|
|
|
when(spamClassificationHandler.downloadMail(anything())).thenResolve(mail)
|
2025-10-14 18:11:25 +02:00
|
|
|
when(inboxRuleHandler.findAndApplyMatchingRule(anything(), mail, anything())).thenResolve(inboxRuleTargetFolder)
|
2025-10-14 15:53:36 +02:00
|
|
|
|
|
|
|
const mailCreateEvent = makeUpdate({ instanceListId: "mailListId", instanceId: "mailId", operation: OperationType.CREATE })
|
|
|
|
await model.entityEventsReceived([mailCreateEvent])
|
|
|
|
|
|
|
|
verify(spamClassificationHandler.predictSpamForNewMail(anything(), mail, anything()), { times: 1 })
|
|
|
|
verify(spamClassifier.predict(anything()), { times: 0 })
|
|
|
|
})
|
2025-10-14 18:11:25 +02:00
|
|
|
|
|
|
|
o("deletes a training datum for deleted mail event", async () => {
|
|
|
|
const mail = createTestEntity(MailTypeRef, {
|
|
|
|
_id: ["mailListId", "mailId"],
|
|
|
|
_ownerGroup: "owner",
|
|
|
|
mailDetailsDraft: ["draftListId", "draftId"],
|
|
|
|
mailDetails: null,
|
|
|
|
})
|
|
|
|
when(spamClassificationHandler.downloadMail(anything())).thenResolve(mail)
|
|
|
|
when(inboxRuleHandler.findAndApplyMatchingRule(anything(), anything(), anything())).thenResolve(null)
|
|
|
|
|
|
|
|
const mailDeleteEvent = makeUpdate({ instanceListId: "mailListId", instanceId: "mailId", operation: OperationType.DELETE })
|
|
|
|
await model.entityEventsReceived([mailDeleteEvent])
|
|
|
|
|
|
|
|
verify(spamClassifier.deleteSpamClassification("owner", mail._id), { times: 0 })
|
|
|
|
})
|
2025-10-14 15:53:36 +02:00
|
|
|
})
|
|
|
|
|
2025-08-08 13:46:06 +02:00
|
|
|
function makeUpdate({
|
|
|
|
instanceId,
|
|
|
|
instanceListId,
|
|
|
|
operation,
|
|
|
|
}: {
|
|
|
|
instanceListId: NonEmptyString
|
|
|
|
instanceId: Id
|
|
|
|
operation: OperationType
|
|
|
|
}): EntityUpdateData<Mail> {
|
2025-05-16 16:03:24 +02:00
|
|
|
return {
|
|
|
|
typeRef: MailTypeRef,
|
|
|
|
operation,
|
|
|
|
instanceListId,
|
|
|
|
instanceId,
|
2025-06-13 17:27:15 +02:00
|
|
|
instance: null,
|
|
|
|
patches: null,
|
2025-07-04 13:37:36 +02:00
|
|
|
prefetchStatus: PrefetchStatus.NotPrefetched,
|
2025-05-16 16:03:24 +02:00
|
|
|
}
|
2018-10-22 12:02:13 +02:00
|
|
|
}
|
2022-12-27 15:37:40 +01:00
|
|
|
})
|