Files
spoon/packages/backend/tests/unit/notifications.test.ts
T

73 lines
2.4 KiB
TypeScript

import { convexTest } from 'convex-test';
import { describe, expect, test } from 'vitest';
import schema from '../../convex/schema';
const modules = import.meta.glob('../../convex/**/*.*s');
describe('notifications schema', () => {
test('inserts and reads back a notifications row', async () => {
const t = convexTest(schema, modules);
const { notificationId, userId } = await t.run(async (ctx) => {
const userId = await ctx.db.insert('users', {
email: 'a@b.c',
name: 'A',
});
const now = Date.now();
const notificationId = await ctx.db.insert('notifications', {
userId,
kind: 'agent_turn_finished',
title: 'Agent finished',
body: 'Your agent completed its turn.',
link: '/threads/abc',
createdAt: now,
});
return { notificationId, userId };
});
const notification = await t.run(
async (ctx) => await ctx.db.get(notificationId),
);
expect(notification).not.toBeNull();
expect(notification?.userId).toBe(userId);
expect(notification?.kind).toBe('agent_turn_finished');
expect(notification?.title).toBe('Agent finished');
expect(notification?.body).toBe('Your agent completed its turn.');
expect(notification?.link).toBe('/threads/abc');
expect(notification?.readAt).toBeUndefined();
expect(notification?.emailedAt).toBeUndefined();
expect(typeof notification?.createdAt).toBe('number');
});
test('inserts and reads back a notificationPreferences row', async () => {
const t = convexTest(schema, modules);
const { preferenceId, userId } = await t.run(async (ctx) => {
const userId = await ctx.db.insert('users', {
email: 'prefs@b.c',
name: 'Prefs',
});
const preferenceId = await ctx.db.insert('notificationPreferences', {
userId,
emailEnabled: true,
emailSyncFailed: false,
updatedAt: Date.now(),
});
return { preferenceId, userId };
});
const preference = await t.run(
async (ctx) => await ctx.db.get(preferenceId),
);
expect(preference).not.toBeNull();
expect(preference?.userId).toBe(userId);
expect(preference?.emailEnabled).toBe(true);
expect(preference?.emailSyncFailed).toBe(false);
expect(preference?.emailMaintenance).toBeUndefined();
expect(typeof preference?.updatedAt).toBe('number');
});
});