feat(notifications): inbox query + mark-read mutations
This commit is contained in:
@@ -7,6 +7,12 @@ import schema from '../../convex/schema';
|
||||
|
||||
const modules = import.meta.glob('../../convex/**/*.*s');
|
||||
|
||||
const authed = (t: ReturnType<typeof convexTest>, userId: string) =>
|
||||
t.withIdentity({
|
||||
subject: `${userId}|session`,
|
||||
issuer: 'https://convex.test',
|
||||
});
|
||||
|
||||
const countNotifications = async (
|
||||
t: ReturnType<typeof convexTest>,
|
||||
userId: Id<'users'>,
|
||||
@@ -370,3 +376,122 @@ describe('emit points', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('inbox query + mark-read mutations', () => {
|
||||
const seedInbox = async (t: ReturnType<typeof convexTest>) =>
|
||||
await t.run(async (ctx) => {
|
||||
const userA = await ctx.db.insert('users', {
|
||||
email: 'inbox-a@example.com',
|
||||
name: 'Inbox A',
|
||||
});
|
||||
const userB = await ctx.db.insert('users', {
|
||||
email: 'inbox-b@example.com',
|
||||
name: 'Inbox B',
|
||||
});
|
||||
const base = 1_000_000;
|
||||
// Three notifications for A with ascending createdAt (a3 is newest).
|
||||
const a1 = await ctx.db.insert('notifications', {
|
||||
userId: userA,
|
||||
kind: 'agent_turn_finished',
|
||||
title: 'A one',
|
||||
body: 'first',
|
||||
createdAt: base + 1,
|
||||
});
|
||||
const a2 = await ctx.db.insert('notifications', {
|
||||
userId: userA,
|
||||
kind: 'agent_turn_finished',
|
||||
title: 'A two',
|
||||
body: 'second',
|
||||
createdAt: base + 2,
|
||||
});
|
||||
const a3 = await ctx.db.insert('notifications', {
|
||||
userId: userA,
|
||||
kind: 'agent_needs_input',
|
||||
title: 'A three',
|
||||
body: 'third',
|
||||
createdAt: base + 3,
|
||||
});
|
||||
// One notification for B, which must never leak to A.
|
||||
const b1 = await ctx.db.insert('notifications', {
|
||||
userId: userB,
|
||||
kind: 'sync_failed',
|
||||
title: 'B one',
|
||||
body: 'b first',
|
||||
createdAt: base + 4,
|
||||
});
|
||||
return { userA, userB, a1, a2, a3, b1 };
|
||||
});
|
||||
|
||||
test('listMine returns only the caller notifications, newest-first, capped by limit', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const { userA, a1, a2, a3 } = await seedInbox(t);
|
||||
|
||||
const all = await authed(t, userA).query(api.notifications.listMine, {});
|
||||
expect(all.map((n) => n._id)).toEqual([a3, a2, a1]);
|
||||
// No notification belongs to any other user.
|
||||
expect(all.every((n) => n.userId === userA)).toBe(true);
|
||||
|
||||
const limited = await authed(t, userA).query(api.notifications.listMine, {
|
||||
limit: 2,
|
||||
});
|
||||
expect(limited.map((n) => n._id)).toEqual([a3, a2]);
|
||||
});
|
||||
|
||||
test('unreadCount counts only the caller unread notifications', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const { userA, userB } = await seedInbox(t);
|
||||
|
||||
expect(await authed(t, userA).query(api.notifications.unreadCount, {})).toBe(
|
||||
3,
|
||||
);
|
||||
// B has a single unread notification; A's three do not leak in.
|
||||
expect(await authed(t, userB).query(api.notifications.unreadCount, {})).toBe(
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
test('markRead marks the caller notification and drops unreadCount', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const { userA, a1 } = await seedInbox(t);
|
||||
|
||||
await authed(t, userA).mutation(api.notifications.markRead, {
|
||||
notificationId: a1,
|
||||
});
|
||||
|
||||
const notification = await t.run(async (ctx) => await ctx.db.get(a1));
|
||||
expect(typeof notification?.readAt).toBe('number');
|
||||
expect(await authed(t, userA).query(api.notifications.unreadCount, {})).toBe(
|
||||
2,
|
||||
);
|
||||
});
|
||||
|
||||
test('markRead rejects a notification owned by another user', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const { userA, b1 } = await seedInbox(t);
|
||||
|
||||
await expect(
|
||||
authed(t, userA).mutation(api.notifications.markRead, {
|
||||
notificationId: b1,
|
||||
}),
|
||||
).rejects.toThrow('Notification not found.');
|
||||
|
||||
// B's notification is untouched and B still has one unread.
|
||||
const notification = await t.run(async (ctx) => await ctx.db.get(b1));
|
||||
expect(notification?.readAt).toBeUndefined();
|
||||
});
|
||||
|
||||
test('markAllRead zeroes the caller unread count but leaves other users untouched', async () => {
|
||||
const t = convexTest(schema, modules);
|
||||
const { userA, userB } = await seedInbox(t);
|
||||
|
||||
await authed(t, userA).mutation(api.notifications.markAllRead, {});
|
||||
|
||||
expect(await authed(t, userA).query(api.notifications.unreadCount, {})).toBe(
|
||||
0,
|
||||
);
|
||||
// B's unread notification is left alone.
|
||||
expect(await authed(t, userB).query(api.notifications.unreadCount, {})).toBe(
|
||||
1,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user