1
11import { describe, it, expect } from "vitest";
12import { normalizeWorkspaceMember } from "../api";
13
14describe("normalizeWorkspaceMember (flat wire → nested model)", () => {
15 it("maps userId → user.id, name/email straight, and avatarUrl → image", () => {
16 const m = normalizeWorkspaceMember({
17 userId: "usr_1",
18 email: "demo@factory.local",
19 name: "Demo User",
20 displayName: "Demo",
21 avatarUrl: "https://x/img.png",
22 role: "OWNER",
23 joinedAt: "2024-01-02T03:04:05.000Z"
24 });
25 expect(m.userId).toBe("usr_1");
26 expect(m.user).toEqual({ id: "usr_1", name: "Demo User", email: "demo@factory.local", image: "https://x/img.png" });
27 expect(m.joinedAt).toBe("2024-01-02T03:04:05.000Z");
28 });
29
30 it("lowercases the UPPERCASE role enum into the core lowercase type", () => {
31 for (const [wire, core] of [
32 ["OWNER", "owner"],
33 ["ADMIN", "admin"],
34 ["MEMBER", "member"],
35 ["GUEST", "guest"]
36 ] as const) {
37 expect(normalizeWorkspaceMember({ userId: "u", email: "e@x", name: "n", role: wire, joinedAt: null }).role).toBe(core);
38 }
39 });
40
41 it("nulls an absent avatarUrl (image → null, not undefined)", () => {
42 const m = normalizeWorkspaceMember({ userId: "u", email: "e@x", name: "n", avatarUrl: null, role: "member", joinedAt: "2024-05-05T00:00:00.000Z" });
43 expect(m.user.image).toBeNull();
44 });
45
46 it("coerces a null joinedAt to the epoch ISO string (never undefined — consumers format it)", () => {
47 const m = normalizeWorkspaceMember({ userId: "u", email: "e@x", name: "n", role: "owner", joinedAt: null });
48 expect(m.joinedAt).toBe(new Date(0).toISOString());
49 });
50});
51