117 lines
4.4 KiB
TypeScript
117 lines
4.4 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { CHAT_PERMISSIONS } from "./permissions.js";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
publishAuditEventViaOutbox: vi.fn(),
|
|
runChatRepositoryTransaction: vi.fn(),
|
|
tx: {
|
|
teamChatMember: {
|
|
findFirst: vi.fn(),
|
|
updateMany: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/audit-events.server", () => ({
|
|
publishAuditEventViaOutbox: (...args: unknown[]) => mocks.publishAuditEventViaOutbox(...args),
|
|
}));
|
|
|
|
vi.mock("./repository.server.js", () => ({
|
|
runChatRepositoryTransaction: (...args: unknown[]) => mocks.runChatRepositoryTransaction(...args),
|
|
}));
|
|
|
|
import { setChatMemberPreferences } from "./member-preferences.server.js";
|
|
|
|
const context = {
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
permissions: [CHAT_PERMISSIONS.readOwnChannels],
|
|
requestId: "request_1",
|
|
} as const;
|
|
|
|
describe("chat member preferences", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.runChatRepositoryTransaction.mockImplementation(
|
|
async (_input: unknown, _permission: string, callback: (tx: typeof mocks.tx) => Promise<unknown>) => callback(mocks.tx)
|
|
);
|
|
mocks.tx.teamChatMember.findFirst.mockResolvedValue({ id: "member_1", isPinned: false, notificationPref: "all_messages" });
|
|
mocks.tx.teamChatMember.updateMany.mockResolvedValue({ count: 1 });
|
|
});
|
|
|
|
it("sets personal pin and notification preferences inside the fresh RLS repository boundary", async () => {
|
|
await expect(
|
|
setChatMemberPreferences({ ...context, chatId: " chat_1 ", isPinned: true, notificationPreference: "muted" })
|
|
).resolves.toEqual({ chatId: "chat_1", isPinned: true, notificationPreference: "muted", changed: true });
|
|
|
|
expect(mocks.runChatRepositoryTransaction).toHaveBeenCalledWith(
|
|
expect.objectContaining({ chatId: " chat_1 " }),
|
|
CHAT_PERMISSIONS.readOwnChannels,
|
|
expect.any(Function)
|
|
);
|
|
expect(mocks.tx.teamChatMember.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
tenantId: "tenant_1",
|
|
chatId: "chat_1",
|
|
userId: "user_1",
|
|
leftAt: null,
|
|
chat: { tenantId: "tenant_1" },
|
|
},
|
|
select: { id: true, isPinned: true, notificationPref: true },
|
|
});
|
|
expect(mocks.tx.teamChatMember.updateMany).toHaveBeenCalledWith({
|
|
where: { tenantId: "tenant_1", id: "member_1", chatId: "chat_1", userId: "user_1", leftAt: null },
|
|
data: { isPinned: true, notificationPref: "muted" },
|
|
});
|
|
expect(mocks.publishAuditEventViaOutbox).toHaveBeenCalledWith(
|
|
mocks.tx,
|
|
expect.objectContaining({
|
|
action: "chat.member.preferences_updated",
|
|
requestId: "request_1",
|
|
targetId: "chat_1",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("is idempotent when the requested preference already matches", async () => {
|
|
mocks.tx.teamChatMember.findFirst.mockResolvedValue({ id: "member_1", isPinned: true, notificationPref: "muted" });
|
|
|
|
await expect(setChatMemberPreferences({ ...context, chatId: "chat_1", isPinned: true })).resolves.toMatchObject({
|
|
changed: false,
|
|
isPinned: true,
|
|
notificationPreference: "muted",
|
|
});
|
|
expect(mocks.tx.teamChatMember.updateMany).not.toHaveBeenCalled();
|
|
expect(mocks.publishAuditEventViaOutbox).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fails closed for a non-member without attempting a mutation", async () => {
|
|
mocks.tx.teamChatMember.findFirst.mockResolvedValue(null);
|
|
|
|
await expect(setChatMemberPreferences({ ...context, chatId: "chat_hidden", isPinned: true })).rejects.toMatchObject({
|
|
code: "foundation.not_found",
|
|
});
|
|
expect(mocks.tx.teamChatMember.updateMany).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
{ chatId: "", isPinned: true },
|
|
{ chatId: "chat_1" },
|
|
{ chatId: "chat_1", notificationPreference: "invalid" },
|
|
])("rejects malformed input before opening a transaction", async (candidate) => {
|
|
await expect(
|
|
setChatMemberPreferences({ ...context, ...candidate } as Parameters<typeof setChatMemberPreferences>[0])
|
|
).rejects.toMatchObject({ code: "chat.invalid_input" });
|
|
expect(mocks.runChatRepositoryTransaction).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fails closed when the guarded update loses its active member row", async () => {
|
|
mocks.tx.teamChatMember.updateMany.mockResolvedValue({ count: 0 });
|
|
|
|
await expect(setChatMemberPreferences({ ...context, chatId: "chat_1", notificationPreference: "mentions_only" })).rejects.toMatchObject({
|
|
code: "chat.integrity_failure",
|
|
});
|
|
});
|
|
});
|