Files
speckle-server/packages/server/modules/notifications/tests/notificationsPreferences.spec.ts
T
Daniel Gak Anagrov 3ca4a11ca3 feat(notifications): basic listener structure, notification record, delayed mechanism (#5432)
* feat: basic notification listener sturcuture

* feat: clean up generated gql

* chore: edited structure

* feat: added basic repo

* feat: ported comment email to job queue

* feat: ported stream access request accepted

* feat: added notification insertion

* fix: minor typings

* feat: delayed notifications

* updated types

* feat: fixed gql

* notifications are listed

* index on notifications

* feat: while loop skiping for update locked

* delayed notification for access request

* take into account user prefrences

* on comment view, notification is marked as read

* feat: added gql notifications

* feat: avoid raising errors

* fix: error added scopes

* fix: mr comments

* fix: cursor and service method

* feat: added stronger types to notifications and versioning logic

* minor: rows updated
2025-10-06 12:19:12 +01:00

101 lines
3.6 KiB
TypeScript

import { truncateTables } from '@/test/hooks'
import { UserNotificationPreferences, Users } from '@/modules/core/dbSchema'
import type { BasicTestUser } from '@/test/authHelper'
import { createTestUsers } from '@/test/authHelper'
import { expect } from 'chai'
import { NotificationType } from '@speckle/shared/notifications'
import { NotificationChannel } from '@/modules/notifications/helpers/types'
import { BaseError } from '@/modules/shared/errors'
import {
getUserNotificationPreferencesFactory,
updateNotificationPreferencesFactory
} from '@/modules/notifications/services/notificationPreferences'
import {
getSavedUserNotificationPreferencesFactory,
saveUserNotificationPreferencesFactory
} from '@/modules/notifications/repositories/userNotificationPreferences'
import { db } from '@/db/knex'
const getSavedUserNotificationPreferences = getSavedUserNotificationPreferencesFactory({
db
})
const getUserNotificationPreferences = getUserNotificationPreferencesFactory({
getSavedUserNotificationPreferences
})
const updateNotificationPreferences = updateNotificationPreferencesFactory({
saveUserNotificationPreferences: saveUserNotificationPreferencesFactory({ db })
})
const cleanup = async () => {
await truncateTables([Users.name, UserNotificationPreferences.name])
}
describe('User notification preferences @notifications', () => {
const userA: BasicTestUser = {
name: 'd1',
email: 'd.1@speckle.systems',
id: ''
}
before(async () => {
await cleanup()
await createTestUsers([userA])
})
describe('services', () => {
it('gets default preferences if none saved', async () => {
const savedPreferences = await getSavedUserNotificationPreferences(userA.id)
expect(savedPreferences).to.deep.equal({})
expect(savedPreferences).to.be.empty
const preferences = await getUserNotificationPreferences(userA.id)
expect(preferences).to.not.be.empty
for (const val of Object.values(preferences)) {
for (const setting of Object.values(val)) {
expect(setting).to.be.true
}
}
})
it('store notification settings', async () => {
await updateNotificationPreferences(userA.id, {
activityDigest: { email: false }
})
let preferences = await getUserNotificationPreferences(userA.id)
expect(preferences).to.not.be.empty
expect(preferences.activityDigest?.email).to.be.false
await updateNotificationPreferences(userA.id, {
activityDigest: { email: true }
})
preferences = await getUserNotificationPreferences(userA.id)
expect(preferences.activityDigest?.email).to.be.true
})
it("doesn't store invalid preference keys", async () => {
const invalidKeys = <const>[
[NotificationType.ActivityDigest, 'mailPigeon', true],
['birthdayParty', NotificationChannel.Email, false],
[
NotificationType.MentionedInComment,
NotificationChannel.Email,
'PleaseDontSpamMe'
]
]
for (const [nt, nc, value] of invalidKeys) {
try {
const preferences: Partial<Record<string, Partial<Record<string, boolean>>>> =
{}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
preferences[nt] = {}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
preferences[nt][nc] = value
await updateNotificationPreferences(userA.id, preferences)
} catch (err) {
expect(err instanceof BaseError)
const error = err as BaseError
expect(error.message).to.contain('Notification preferences input')
}
}
})
})
})