|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { OpenClawConfig } from "../runtime-api.js"; |
| 3 | +import { getMemberInfoMSTeams } from "./graph-members.js"; |
| 4 | + |
| 5 | +const mockState = vi.hoisted(() => ({ |
| 6 | + resolveGraphToken: vi.fn(), |
| 7 | + fetchGraphJson: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("./graph.js", async (importOriginal) => { |
| 11 | + const actual = await importOriginal<typeof import("./graph.js")>(); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + resolveGraphToken: mockState.resolveGraphToken, |
| 15 | + fetchGraphJson: mockState.fetchGraphJson, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +const TOKEN = "test-graph-token"; |
| 20 | + |
| 21 | +describe("getMemberInfoMSTeams", () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + mockState.resolveGraphToken.mockResolvedValue(TOKEN); |
| 25 | + }); |
| 26 | + |
| 27 | + it("fetches user profile and maps all fields", async () => { |
| 28 | + mockState.fetchGraphJson.mockResolvedValue({ |
| 29 | + id: "user-123", |
| 30 | + displayName: "Alice Smith", |
| 31 | + |
| 32 | + jobTitle: "Engineer", |
| 33 | + userPrincipalName: "[email protected]", |
| 34 | + officeLocation: "Building 1", |
| 35 | + }); |
| 36 | + |
| 37 | + const result = await getMemberInfoMSTeams({ |
| 38 | + cfg: {} as OpenClawConfig, |
| 39 | + userId: "user-123", |
| 40 | + }); |
| 41 | + |
| 42 | + expect(result).toEqual({ |
| 43 | + user: { |
| 44 | + id: "user-123", |
| 45 | + displayName: "Alice Smith", |
| 46 | + |
| 47 | + jobTitle: "Engineer", |
| 48 | + userPrincipalName: "[email protected]", |
| 49 | + officeLocation: "Building 1", |
| 50 | + }, |
| 51 | + }); |
| 52 | + expect(mockState.fetchGraphJson).toHaveBeenCalledWith({ |
| 53 | + token: TOKEN, |
| 54 | + path: `/users/${encodeURIComponent("user-123")}?$select=id,displayName,mail,jobTitle,userPrincipalName,officeLocation`, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("handles sparse data with some fields undefined", async () => { |
| 59 | + mockState.fetchGraphJson.mockResolvedValue({ |
| 60 | + id: "user-456", |
| 61 | + displayName: "Bob", |
| 62 | + }); |
| 63 | + |
| 64 | + const result = await getMemberInfoMSTeams({ |
| 65 | + cfg: {} as OpenClawConfig, |
| 66 | + userId: "user-456", |
| 67 | + }); |
| 68 | + |
| 69 | + expect(result).toEqual({ |
| 70 | + user: { |
| 71 | + id: "user-456", |
| 72 | + displayName: "Bob", |
| 73 | + mail: undefined, |
| 74 | + jobTitle: undefined, |
| 75 | + userPrincipalName: undefined, |
| 76 | + officeLocation: undefined, |
| 77 | + }, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("propagates Graph API errors", async () => { |
| 82 | + mockState.fetchGraphJson.mockRejectedValue(new Error("Graph API 404: user not found")); |
| 83 | + |
| 84 | + await expect( |
| 85 | + getMemberInfoMSTeams({ |
| 86 | + cfg: {} as OpenClawConfig, |
| 87 | + userId: "nonexistent-user", |
| 88 | + }), |
| 89 | + ).rejects.toThrow("Graph API 404: user not found"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments