|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const { |
| 4 | + searchGraphUsersMock, |
| 5 | + listTeamsByNameMock, |
| 6 | + listChannelsForTeamMock, |
| 7 | + normalizeQueryMock, |
| 8 | + resolveGraphTokenMock, |
| 9 | +} = vi.hoisted(() => { |
| 10 | + return { |
| 11 | + searchGraphUsersMock: vi.fn(), |
| 12 | + listTeamsByNameMock: vi.fn(), |
| 13 | + listChannelsForTeamMock: vi.fn(), |
| 14 | + normalizeQueryMock: vi.fn((value?: string | null) => value?.trim() ?? ""), |
| 15 | + resolveGraphTokenMock: vi.fn(), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +vi.mock("./graph-users.js", () => { |
| 20 | + return { searchGraphUsers: searchGraphUsersMock }; |
| 21 | +}); |
| 22 | + |
| 23 | +vi.mock("./graph.js", () => { |
| 24 | + return { |
| 25 | + listTeamsByName: listTeamsByNameMock, |
| 26 | + listChannelsForTeam: listChannelsForTeamMock, |
| 27 | + normalizeQuery: normalizeQueryMock, |
| 28 | + resolveGraphToken: resolveGraphTokenMock, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +import { listMSTeamsDirectoryGroupsLive, listMSTeamsDirectoryPeersLive } from "./directory-live.js"; |
| 33 | + |
| 34 | +describe("msteams directory live", () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + normalizeQueryMock.mockImplementation((value?: string | null) => value?.trim() ?? ""); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns normalized peer entries and skips users without ids", async () => { |
| 41 | + resolveGraphTokenMock.mockResolvedValue("graph-token"); |
| 42 | + searchGraphUsersMock.mockResolvedValue([ |
| 43 | + { |
| 44 | + id: "user-1", |
| 45 | + displayName: "Alice", |
| 46 | + userPrincipalName: "[email protected]", |
| 47 | + }, |
| 48 | + { |
| 49 | + id: "user-2", |
| 50 | + displayName: "Bob", |
| 51 | + |
| 52 | + }, |
| 53 | + { |
| 54 | + displayName: "Missing Id", |
| 55 | + }, |
| 56 | + ]); |
| 57 | + |
| 58 | + await expect( |
| 59 | + listMSTeamsDirectoryPeersLive({ |
| 60 | + cfg: {}, |
| 61 | + query: " ali ", |
| 62 | + }), |
| 63 | + ).resolves.toEqual([ |
| 64 | + { |
| 65 | + kind: "user", |
| 66 | + id: "user:user-1", |
| 67 | + name: "Alice", |
| 68 | + |
| 69 | + raw: { |
| 70 | + id: "user-1", |
| 71 | + displayName: "Alice", |
| 72 | + userPrincipalName: "[email protected]", |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + kind: "user", |
| 77 | + id: "user:user-2", |
| 78 | + name: "Bob", |
| 79 | + |
| 80 | + raw: { |
| 81 | + id: "user-2", |
| 82 | + displayName: "Bob", |
| 83 | + |
| 84 | + }, |
| 85 | + }, |
| 86 | + ]); |
| 87 | + |
| 88 | + expect(searchGraphUsersMock).toHaveBeenCalledWith({ |
| 89 | + token: "graph-token", |
| 90 | + query: "ali", |
| 91 | + top: 20, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("returns team entries without channel queries and honors limits", async () => { |
| 96 | + resolveGraphTokenMock.mockResolvedValue("graph-token"); |
| 97 | + listTeamsByNameMock.mockResolvedValue([ |
| 98 | + { id: "team-1", displayName: "Platform" }, |
| 99 | + { id: "team-2", displayName: "Infra" }, |
| 100 | + ]); |
| 101 | + |
| 102 | + await expect( |
| 103 | + listMSTeamsDirectoryGroupsLive({ |
| 104 | + cfg: {}, |
| 105 | + query: "platform", |
| 106 | + limit: 1, |
| 107 | + }), |
| 108 | + ).resolves.toEqual([ |
| 109 | + { |
| 110 | + kind: "group", |
| 111 | + id: "team:team-1", |
| 112 | + name: "Platform", |
| 113 | + handle: "#Platform", |
| 114 | + raw: { id: "team-1", displayName: "Platform" }, |
| 115 | + }, |
| 116 | + ]); |
| 117 | + }); |
| 118 | + |
| 119 | + it("searches channels within matching teams when a team/channel query is used", async () => { |
| 120 | + resolveGraphTokenMock.mockResolvedValue("graph-token"); |
| 121 | + listTeamsByNameMock.mockResolvedValue([ |
| 122 | + { id: "team-1", displayName: "Platform" }, |
| 123 | + { id: "team-2", displayName: "Infra" }, |
| 124 | + ]); |
| 125 | + listChannelsForTeamMock |
| 126 | + .mockResolvedValueOnce([ |
| 127 | + { id: "chan-1", displayName: "Deployments" }, |
| 128 | + { id: "chan-2", displayName: "General" }, |
| 129 | + ]) |
| 130 | + .mockResolvedValueOnce([{ id: "chan-3", displayName: "Deployments-West" }]); |
| 131 | + |
| 132 | + await expect( |
| 133 | + listMSTeamsDirectoryGroupsLive({ |
| 134 | + cfg: {}, |
| 135 | + query: "plat / deploy", |
| 136 | + }), |
| 137 | + ).resolves.toEqual([ |
| 138 | + { |
| 139 | + kind: "group", |
| 140 | + id: "conversation:chan-1", |
| 141 | + name: "Platform/Deployments", |
| 142 | + handle: "#Deployments", |
| 143 | + raw: { id: "chan-1", displayName: "Deployments" }, |
| 144 | + }, |
| 145 | + { |
| 146 | + kind: "group", |
| 147 | + id: "conversation:chan-3", |
| 148 | + name: "Infra/Deployments-West", |
| 149 | + handle: "#Deployments-West", |
| 150 | + raw: { id: "chan-3", displayName: "Deployments-West" }, |
| 151 | + }, |
| 152 | + ]); |
| 153 | + |
| 154 | + expect(listTeamsByNameMock).toHaveBeenCalledWith("graph-token", "plat"); |
| 155 | + }); |
| 156 | +}); |
0 commit comments