Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/view-models/src/chat/channel-create.vm.test.ts
T

152 lines
6.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
buildChatChannelCreateVM,
ChatChannelCreateVMSchema,
chatChannelCreateSubmissionSchema,
type ChatTenantUserOptionInput,
} from "./channel-create.vm.js";
function user(userId: string, displayName: string): ChatTenantUserOptionInput {
return {
userId,
displayName,
email: `${userId}@example.test`,
avatar: { imageUrl: null, fallbackLabel: displayName.slice(0, 2).toUpperCase() },
};
}
const USERS = [user("viewer", "Raffael"), user("peer-1", "Akeshan"), user("peer-2", "Luna")];
describe("buildChatChannelCreateVM", () => {
it("derives allowed modes from permissions and excludes the current user", () => {
const vm = buildChatChannelCreateVM({
currentUserId: "viewer",
tenantUsers: [...USERS, user("peer-1", "Duplicate")],
permissions: { canCreateGroup: false, canCreateDm: true },
requestedMode: "group",
dmDraft: { peerUserId: "peer-1" },
});
if (vm.status !== "ready") throw new Error("expected ready");
expect(vm.data.allowedModes).toEqual(["dm"]);
expect(vm.data.selectedMode).toBe("dm");
expect(vm.data.userOptions.map(({ userId }) => userId)).toEqual(["peer-1", "peer-2"]);
expect(vm.data.form).toMatchObject({ mode: "dm", peerUserId: "peer-1", canSubmit: true });
expect(vm.data.keys.usersLoadingKey).toBe("chat.channelCreate.users.loading");
});
it("builds a public/private group draft and reports required or stale values by key", () => {
const vm = buildChatChannelCreateVM({
currentUserId: "viewer",
tenantUsers: USERS,
permissions: { canCreateGroup: true, canCreateDm: false },
tenantGroups: [
{ groupId: "group-1", name: "Team Süd", description: "Montage", memberCount: 4 },
{ groupId: "group-2", name: "Lehrlinge", description: "", memberCount: 2 },
],
groupDraft: {
name: " ",
description: "Montage-Team",
privacy: "private",
initialUserIds: ["peer-1", "peer-1", "missing", "viewer"],
initialGroupIds: ["group-1", "group-1", "missing-group"],
syncGroupMembership: true,
},
});
if (vm.status !== "ready" || vm.data.form?.mode !== "group") throw new Error("expected group form");
expect(vm.data.form).toMatchObject({
privacy: "private",
initialUserIds: ["peer-1"],
initialGroupIds: ["group-1"],
syncGroupMembership: true,
canSubmit: false,
});
expect(vm.data.form.validationErrorKeys).toEqual([
"chat.channelCreate.validation.groupNameRequired",
"chat.channelCreate.validation.duplicateInitialUsers",
"chat.channelCreate.validation.userUnavailable",
"chat.channelCreate.validation.duplicateInitialGroups",
"chat.channelCreate.validation.groupUnavailable",
]);
expect(vm.data.groupOptions).toEqual([
{ groupId: "group-1", name: "Team Süd", description: "Montage", memberCount: 4 },
{ groupId: "group-2", name: "Lehrlinge", description: "", memberCount: 2 },
]);
});
it("keeps DM structurally limited to one peer and exposes missing/unavailable validation", () => {
const missing = buildChatChannelCreateVM({
currentUserId: "viewer",
tenantUsers: USERS,
permissions: { canCreateGroup: false, canCreateDm: true },
requestedMode: "dm",
});
const self = buildChatChannelCreateVM({
currentUserId: "viewer",
tenantUsers: USERS,
permissions: { canCreateGroup: false, canCreateDm: true },
requestedMode: "dm",
dmDraft: { peerUserId: "viewer" },
});
if (missing.status !== "ready" || self.status !== "ready") throw new Error("expected ready");
expect(missing.data.form).toMatchObject({ peerUserId: null, validationErrorKeys: ["chat.channelCreate.validation.dmPeerRequired"] });
expect(self.data.form).toMatchObject({ peerUserId: "viewer", validationErrorKeys: ["chat.channelCreate.validation.dmPeerUnavailable"] });
});
it("returns an explicit permission-empty state when neither mode is allowed", () => {
const vm = buildChatChannelCreateVM({
currentUserId: "viewer",
tenantUsers: USERS,
permissions: { canCreateGroup: false, canCreateDm: false },
});
if (vm.status !== "ready") throw new Error("expected ready");
expect(vm.data).toMatchObject({ allowedModes: [], selectedMode: null, form: null });
expect(vm.data.keys.noAllowedModesKey).toBe("chat.channelCreate.permissions.none");
});
it("validates strict action and render contracts", () => {
expect(
chatChannelCreateSubmissionSchema.safeParse({ mode: "dm", peerUserId: "peer-1", peerUserIds: ["peer-2"] }).success,
).toBe(false);
const duplicateGroup = chatChannelCreateSubmissionSchema.safeParse({
mode: "group",
name: "Montage",
description: null,
privacy: "public",
initialUserIds: ["peer-1", "peer-1"],
initialGroupIds: [],
syncGroupMembership: false,
});
expect(duplicateGroup.success).toBe(false);
if (!duplicateGroup.success) expect(duplicateGroup.error.issues[0]?.message).toBe("chat.channelCreate.validation.duplicateInitialUsers");
const vm = buildChatChannelCreateVM({
currentUserId: "viewer",
tenantUsers: USERS,
permissions: { canCreateGroup: true, canCreateDm: true },
groupDraft: { name: "Montage", initialUserIds: ["peer-1"] },
});
expect(ChatChannelCreateVMSchema.parse(JSON.parse(JSON.stringify(vm)) as unknown)).toEqual(vm);
expect(ChatChannelCreateVMSchema.safeParse({ ...vm, unexpected: true }).success).toBe(false);
});
it("requires a selected permission group when automatic group sync is enabled", () => {
const parsed = chatChannelCreateSubmissionSchema.safeParse({
mode: "group",
name: "Team Süd",
description: null,
privacy: "private",
initialUserIds: [],
initialGroupIds: [],
syncGroupMembership: true,
});
expect(parsed.success).toBe(false);
if (!parsed.success) expect(parsed.error.issues[0]?.message).toBe("chat.channelCreate.validation.groupUnavailable");
});
});