76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
runChatRepositoryTransaction: vi.fn(),
|
|
tx: { teamChat: { findFirst: vi.fn() } },
|
|
}));
|
|
|
|
vi.mock("./repository.server.js", () => ({
|
|
runChatRepositoryTransaction: (...args: unknown[]) => mocks.runChatRepositoryTransaction(...args),
|
|
}));
|
|
|
|
import { findMemberProjectChatLink } from "./project-link.server.js";
|
|
|
|
const context = {
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
permissions: ["chat.read_own_channels"],
|
|
projectId: "project_1",
|
|
} as const;
|
|
|
|
describe("member project chat link", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.runChatRepositoryTransaction.mockImplementation(
|
|
async (_context: unknown, _permission: string, callback: (tx: typeof mocks.tx) => Promise<unknown>) => callback(mocks.tx)
|
|
);
|
|
});
|
|
|
|
it("prefers the active member channel and preserves read-only project history state", async () => {
|
|
mocks.tx.teamChat.findFirst.mockResolvedValueOnce({
|
|
id: "chat_active",
|
|
archivedAt: null,
|
|
members: [{ writeRevokedAt: new Date("2026-07-15T12:00:00.000Z") }],
|
|
});
|
|
|
|
await expect(findMemberProjectChatLink(context)).resolves.toEqual({
|
|
chatId: "chat_active",
|
|
archivedAtIso: null,
|
|
canWrite: false,
|
|
});
|
|
expect(mocks.runChatRepositoryTransaction).toHaveBeenCalledWith(context, "chat.read_own_channels", expect.any(Function));
|
|
expect(mocks.tx.teamChat.findFirst).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("falls back to the newest archived channel without exposing another user's membership", async () => {
|
|
const archivedAt = new Date("2026-07-15T12:00:00.000Z");
|
|
mocks.tx.teamChat.findFirst.mockResolvedValueOnce(null).mockResolvedValueOnce({
|
|
id: "chat_archived",
|
|
archivedAt,
|
|
members: [{ writeRevokedAt: null }],
|
|
});
|
|
|
|
await expect(findMemberProjectChatLink(context)).resolves.toEqual({
|
|
chatId: "chat_archived",
|
|
archivedAtIso: archivedAt.toISOString(),
|
|
canWrite: false,
|
|
});
|
|
expect(mocks.tx.teamChat.findFirst).toHaveBeenNthCalledWith(
|
|
2,
|
|
expect.objectContaining({
|
|
where: expect.objectContaining({
|
|
tenantId: "tenant_1",
|
|
projektId: "project_1",
|
|
archivedAt: { not: null },
|
|
members: { some: { tenantId: "tenant_1", userId: "user_1", leftAt: null } },
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("returns null when no readable project chat exists", async () => {
|
|
mocks.tx.teamChat.findFirst.mockResolvedValue(null);
|
|
await expect(findMemberProjectChatLink(context)).resolves.toBeNull();
|
|
});
|
|
});
|