154 lines
6.2 KiB
TypeScript
154 lines
6.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
loadChatContext: vi.fn(),
|
|
loadMemberChatChannels: vi.fn(),
|
|
joinPublicChatChannel: vi.fn(),
|
|
updateOwnChatChannelPreferences: 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/ChatService.server", () => ({
|
|
loadMemberChatChannels: mocks.loadMemberChatChannels,
|
|
joinPublicChatChannel: mocks.joinPublicChatChannel,
|
|
updateOwnChatChannelPreferences: mocks.updateOwnChatChannelPreferences,
|
|
}));
|
|
vi.mock("~/utils/session.server", () => ({ validateCSRFToken: mocks.validateCSRFToken }));
|
|
vi.mock("~/utils/data/useRootData", () => ({ useRootData: () => ({ csrf: "csrf_1" }) }));
|
|
vi.mock("~/components/screens/chat/ChatChannelListPage", () => ({ ChatChannelListPage: () => null }));
|
|
vi.mock("~/components/ui/errors/ServerError", () => ({ default: () => null }));
|
|
|
|
import { action, loader } from "./chat._index";
|
|
|
|
const context = { tenantId: "tenant_1", userId: "user_1", permissions: ["chat.read_own_channels"] };
|
|
|
|
describe("public chat join action", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.loadChatContext.mockResolvedValue(context);
|
|
mocks.validateCSRFToken.mockResolvedValue(undefined);
|
|
mocks.joinPublicChatChannel.mockResolvedValue({ chatId: "chat_public", changed: true });
|
|
mocks.updateOwnChatChannelPreferences.mockResolvedValue({
|
|
chatId: "chat_public",
|
|
isPinned: true,
|
|
notificationPreference: "all_messages",
|
|
changed: true,
|
|
});
|
|
mocks.loadMemberChatChannels.mockResolvedValue({ status: "ready", data: {} });
|
|
});
|
|
|
|
it("loads the URL cursor with the canonical filter and fixed page limit", async () => {
|
|
const request = new Request("https://app.test/demo/chat?q=Montage&view=groups&archived=1&cursor=opaque_cursor");
|
|
const response = await loader({ request, params: { tenant: "demo" }, context: {}, unstable_pattern: "app.$tenant/chat._index" });
|
|
|
|
expect(mocks.loadMemberChatChannels).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
tenantSlug: "demo",
|
|
filter: { query: "Montage", view: "groups", showArchived: true },
|
|
afterCursor: "opaque_cursor",
|
|
limit: 50,
|
|
})
|
|
);
|
|
await expect(response.json()).resolves.toMatchObject({ pageCursor: "opaque_cursor" });
|
|
});
|
|
|
|
it("joins through the server service and redirects only after the lifecycle boundary succeeds", async () => {
|
|
const formData = new FormData();
|
|
formData.set("intent", "join");
|
|
formData.set("chatId", "chat_public");
|
|
formData.set("csrf", "csrf_1");
|
|
const request = postRequest(formData);
|
|
|
|
const response = await action({ request, params: { tenant: "demo" }, context: {}, unstable_pattern: "app.$tenant/chat._index" });
|
|
|
|
expect(mocks.validateCSRFToken).toHaveBeenCalledWith(request, { formData: expect.anything() });
|
|
expect(mocks.joinPublicChatChannel).toHaveBeenCalledWith({
|
|
context,
|
|
chatId: "chat_public",
|
|
requestId: expect.any(String),
|
|
});
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("location")).toBe("/demo/chat/chat_public?toast=chat.toast.channelJoined");
|
|
});
|
|
|
|
it("rejects malformed join intents before the lifecycle mutation", async () => {
|
|
const formData = new FormData();
|
|
formData.set("intent", "join");
|
|
const response = await action({
|
|
request: postRequest(formData),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat._index",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({ errorKey: "chat.errors.invalidInput" });
|
|
expect(mocks.joinPublicChatChannel).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("updates the current member pin through a CSRF-protected action", async () => {
|
|
const formData = new FormData();
|
|
formData.set("intent", "set-pin");
|
|
formData.set("chatId", "chat_public");
|
|
formData.set("requestId", "request_pin_1");
|
|
formData.set("isPinned", "true");
|
|
formData.set("csrf", "csrf_1");
|
|
const request = postRequest(formData);
|
|
|
|
const response = await action({ request, params: { tenant: "demo" }, context: {}, unstable_pattern: "app.$tenant/chat._index" });
|
|
|
|
expect(mocks.updateOwnChatChannelPreferences).toHaveBeenCalledWith({
|
|
context,
|
|
chatId: "chat_public",
|
|
requestId: "request_pin_1",
|
|
isPinned: true,
|
|
});
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
errorKey: null,
|
|
mutation: { chatId: "chat_public", isPinned: true },
|
|
});
|
|
});
|
|
|
|
it("updates notification preferences and rejects malformed preference values", async () => {
|
|
const valid = new FormData();
|
|
valid.set("intent", "set-notification");
|
|
valid.set("chatId", "chat_public");
|
|
valid.set("requestId", "request_notification_1");
|
|
valid.set("notificationPreference", "muted");
|
|
const validRequest = postRequest(valid);
|
|
await action({ request: validRequest, params: { tenant: "demo" }, context: {}, unstable_pattern: "app.$tenant/chat._index" });
|
|
expect(mocks.updateOwnChatChannelPreferences).toHaveBeenCalledWith({
|
|
context,
|
|
chatId: "chat_public",
|
|
requestId: "request_notification_1",
|
|
notificationPreference: "muted",
|
|
});
|
|
|
|
const invalid = new FormData();
|
|
invalid.set("intent", "set-notification");
|
|
invalid.set("chatId", "chat_public");
|
|
invalid.set("requestId", "request_notification_2");
|
|
invalid.set("notificationPreference", "everything");
|
|
const response = await action({
|
|
request: postRequest(invalid),
|
|
params: { tenant: "demo" },
|
|
context: {},
|
|
unstable_pattern: "app.$tenant/chat._index",
|
|
});
|
|
expect(response.status).toBe(400);
|
|
expect(mocks.updateOwnChatChannelPreferences).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
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.test/demo/chat", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
body,
|
|
});
|
|
}
|