227 lines
7.9 KiB
TypeScript
227 lines
7.9 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { buildChatChannelCreateVM } from "@hivegrid/view-models";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
loadChatContext: vi.fn(),
|
|
loadCreatePage: vi.fn(),
|
|
createChatChannel: vi.fn(),
|
|
validateCSRFToken: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/locale/i18next.server", () => ({
|
|
getTranslations: vi.fn(async () => ({ t: (key: string) => key })),
|
|
}));
|
|
vi.mock("~/modules/chat/routes/chatAccess.server", () => ({
|
|
loadChatContext: mocks.loadChatContext,
|
|
}));
|
|
vi.mock("~/modules/chat/services/ChatChannelCreateService.server", () => ({
|
|
loadChatChannelCreatePage: mocks.loadCreatePage,
|
|
createChatChannel: mocks.createChatChannel,
|
|
}));
|
|
vi.mock("~/utils/session.server", () => ({
|
|
validateCSRFToken: mocks.validateCSRFToken,
|
|
}));
|
|
vi.mock("~/utils/data/useRootData", () => ({
|
|
useRootData: () => ({ csrf: "csrf_test" }),
|
|
}));
|
|
vi.mock("~/components/screens/chat/ChatChannelCreatePage", () => ({
|
|
ChatChannelCreatePage: () => null,
|
|
}));
|
|
vi.mock("~/components/ui/errors/ServerError", () => ({ default: () => null }));
|
|
|
|
import { action, loader } from "./chat.new";
|
|
|
|
const context = {
|
|
tenantId: "tenant_1",
|
|
userId: "user_current",
|
|
tenantUserId: "tenant_user_current",
|
|
permissions: ["chat.create_channel", "chat.create_dm"],
|
|
hasModule: true as const,
|
|
};
|
|
const CREATION_REQUEST_ID = "chat-create-request-0001";
|
|
|
|
describe("AK-P15b chat create route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.loadChatContext.mockResolvedValue(context);
|
|
mocks.validateCSRFToken.mockResolvedValue(undefined);
|
|
mocks.createChatChannel.mockResolvedValue({ chatId: "chat_created" });
|
|
mocks.loadCreatePage.mockResolvedValue(createVm("dm"));
|
|
});
|
|
|
|
it("loads the requested create mode through the tenant-scoped chat context", async () => {
|
|
const request = new Request("https://app.example.test/demo/chat/new?kind=dm");
|
|
const response = await loader({ request, params: { tenant: "demo" }, context: {}, unstable_pattern: "app.$tenant/chat.new" });
|
|
const data = await response.json();
|
|
|
|
expect(mocks.loadChatContext).toHaveBeenCalledWith({ request, params: { tenant: "demo" } });
|
|
expect(mocks.loadCreatePage).toHaveBeenCalledWith({ context, requestedMode: "dm" });
|
|
expect(data).toEqual(expect.objectContaining({ tenantSlug: "demo", requestId: expect.any(String), vm: createVm("dm") }));
|
|
});
|
|
|
|
it("returns a 404 when neither create permission is available", async () => {
|
|
mocks.loadCreatePage.mockResolvedValue(
|
|
buildChatChannelCreateVM({
|
|
currentUserId: "user_current",
|
|
tenantUsers: [],
|
|
permissions: { canCreateGroup: false, canCreateDm: false },
|
|
})
|
|
);
|
|
|
|
await expect(
|
|
loader({
|
|
request: new Request("https://app.example.test/demo/chat/new"),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.new",
|
|
})
|
|
).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
|
|
it("validates CSRF and creates a group with unique initial members", async () => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_test");
|
|
form.set("requestId", CREATION_REQUEST_ID);
|
|
form.set("mode", "group");
|
|
form.set("name", " Montage Nord ");
|
|
form.set("description", " Abstimmung ");
|
|
form.set("privacy", "private");
|
|
form.append("initialUserIds", "user_mara");
|
|
form.append("initialUserIds", "user_noah");
|
|
form.append("initialGroupIds", "group_south");
|
|
form.set("syncGroupMembership", "true");
|
|
const request = postRequest(form);
|
|
|
|
const response = await action({ request, params: { tenant: "demo" }, context: {}, unstable_pattern: "app.$tenant/chat.new" });
|
|
|
|
expect(mocks.validateCSRFToken).toHaveBeenCalledWith(request, { formData: expect.anything() });
|
|
expect(mocks.createChatChannel).toHaveBeenCalledWith({
|
|
context,
|
|
requestId: CREATION_REQUEST_ID,
|
|
submission: {
|
|
mode: "group",
|
|
name: "Montage Nord",
|
|
description: "Abstimmung",
|
|
privacy: "private",
|
|
initialUserIds: ["user_mara", "user_noah"],
|
|
initialGroupIds: ["group_south"],
|
|
syncGroupMembership: true,
|
|
},
|
|
});
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("location")).toBe("/demo/chat/chat_created?toast=chat.toast.groupCreated");
|
|
});
|
|
|
|
it("strictly rejects duplicate group members before the lifecycle call", async () => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_test");
|
|
form.set("requestId", CREATION_REQUEST_ID);
|
|
form.set("mode", "group");
|
|
form.set("name", "Montage Nord");
|
|
form.set("description", "");
|
|
form.set("privacy", "public");
|
|
form.set("syncGroupMembership", "false");
|
|
form.append("initialUserIds", "user_mara");
|
|
form.append("initialUserIds", "user_mara");
|
|
|
|
const response = await action({
|
|
request: postRequest(form),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.new",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(await response.json()).toEqual({ errorKey: "chat.channelCreate.validation.duplicateInitialUsers" });
|
|
expect(mocks.createChatChannel).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects automatic group sync without a selected permission group", async () => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_test");
|
|
form.set("requestId", CREATION_REQUEST_ID);
|
|
form.set("mode", "group");
|
|
form.set("name", "Team Süd");
|
|
form.set("description", "");
|
|
form.set("privacy", "private");
|
|
form.set("syncGroupMembership", "true");
|
|
|
|
const response = await action({
|
|
request: postRequest(form),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.new",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(await response.json()).toEqual({ errorKey: "chat.channelCreate.validation.groupUnavailable" });
|
|
expect(mocks.createChatChannel).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("parses exactly one DM peer and redirects to the canonical chat", async () => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_test");
|
|
form.set("requestId", CREATION_REQUEST_ID);
|
|
form.set("mode", "dm");
|
|
form.set("peerUserId", "user_mara");
|
|
|
|
const response = await action({
|
|
request: postRequest(form),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.new",
|
|
});
|
|
|
|
expect(mocks.createChatChannel).toHaveBeenCalledWith({
|
|
context,
|
|
requestId: CREATION_REQUEST_ID,
|
|
submission: { mode: "dm", peerUserId: "user_mara" },
|
|
});
|
|
expect(response.headers.get("location")).toBe("/demo/chat/chat_created?toast=chat.toast.directOpened");
|
|
});
|
|
|
|
it("rejects a create submission without the stable browser request id", async () => {
|
|
const form = new FormData();
|
|
form.set("csrf", "csrf_test");
|
|
form.set("mode", "dm");
|
|
form.set("peerUserId", "user_mara");
|
|
|
|
const response = await action({
|
|
request: postRequest(form),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat.new",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(await response.json()).toEqual({ errorKey: "chat.errors.invalidInput" });
|
|
expect(mocks.createChatChannel).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
function createVm(requestedMode: "group" | "dm") {
|
|
return buildChatChannelCreateVM({
|
|
currentUserId: "user_current",
|
|
requestedMode,
|
|
tenantUsers: [
|
|
{
|
|
userId: "user_mara",
|
|
displayName: "Mara Klein",
|
|
email: "mara@example.test",
|
|
avatar: { imageUrl: null, fallbackLabel: "MK" },
|
|
},
|
|
],
|
|
permissions: { canCreateGroup: true, canCreateDm: true },
|
|
});
|
|
}
|
|
|
|
function postRequest(formData: FormData): Request {
|
|
const body = new URLSearchParams();
|
|
for (const [key, value] of formData.entries()) body.append(key, value.toString());
|
|
return new Request("https://app.example.test/demo/chat/new", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
body,
|
|
});
|
|
}
|