|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { OpenClawConfig } from "../runtime-api.js"; |
| 3 | +import { getChannelInfoMSTeams, listChannelsMSTeams } from "./graph-teams.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("listChannelsMSTeams", () => { |
| 22 | + beforeEach(() => { |
| 23 | + mockState.resolveGraphToken.mockReset().mockResolvedValue(TOKEN); |
| 24 | + mockState.fetchGraphJson.mockReset(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns channels with all fields mapped", async () => { |
| 28 | + mockState.fetchGraphJson.mockResolvedValue({ |
| 29 | + value: [ |
| 30 | + { |
| 31 | + id: "ch-1", |
| 32 | + displayName: "General", |
| 33 | + description: "The default channel", |
| 34 | + membershipType: "standard", |
| 35 | + }, |
| 36 | + { |
| 37 | + id: "ch-2", |
| 38 | + displayName: "Engineering", |
| 39 | + description: "Engineering discussions", |
| 40 | + membershipType: "private", |
| 41 | + }, |
| 42 | + ], |
| 43 | + }); |
| 44 | + |
| 45 | + const result = await listChannelsMSTeams({ |
| 46 | + cfg: {} as OpenClawConfig, |
| 47 | + teamId: "team-abc", |
| 48 | + }); |
| 49 | + |
| 50 | + expect(result.channels).toEqual([ |
| 51 | + { |
| 52 | + id: "ch-1", |
| 53 | + displayName: "General", |
| 54 | + description: "The default channel", |
| 55 | + membershipType: "standard", |
| 56 | + }, |
| 57 | + { |
| 58 | + id: "ch-2", |
| 59 | + displayName: "Engineering", |
| 60 | + description: "Engineering discussions", |
| 61 | + membershipType: "private", |
| 62 | + }, |
| 63 | + ]); |
| 64 | + expect(mockState.fetchGraphJson).toHaveBeenCalledWith({ |
| 65 | + token: TOKEN, |
| 66 | + path: `/teams/${encodeURIComponent("team-abc")}/channels?$select=id,displayName,description,membershipType`, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns empty array when team has no channels", async () => { |
| 71 | + mockState.fetchGraphJson.mockResolvedValue({ value: [] }); |
| 72 | + |
| 73 | + const result = await listChannelsMSTeams({ |
| 74 | + cfg: {} as OpenClawConfig, |
| 75 | + teamId: "team-empty", |
| 76 | + }); |
| 77 | + |
| 78 | + expect(result.channels).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("returns empty array when value is undefined", async () => { |
| 82 | + mockState.fetchGraphJson.mockResolvedValue({}); |
| 83 | + |
| 84 | + const result = await listChannelsMSTeams({ |
| 85 | + cfg: {} as OpenClawConfig, |
| 86 | + teamId: "team-no-value", |
| 87 | + }); |
| 88 | + |
| 89 | + expect(result.channels).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + it("follows @odata.nextLink across multiple pages", async () => { |
| 93 | + mockState.fetchGraphJson |
| 94 | + .mockResolvedValueOnce({ |
| 95 | + value: [ |
| 96 | + { id: "ch-1", displayName: "General", description: null, membershipType: "standard" }, |
| 97 | + ], |
| 98 | + "@odata.nextLink": |
| 99 | + "https://graph.microsoft.com/v1.0/teams/team-paged/channels?$select=id,displayName,description,membershipType&$skip=1", |
| 100 | + }) |
| 101 | + .mockResolvedValueOnce({ |
| 102 | + value: [ |
| 103 | + { id: "ch-2", displayName: "Random", description: "Fun", membershipType: "standard" }, |
| 104 | + ], |
| 105 | + "@odata.nextLink": |
| 106 | + "https://graph.microsoft.com/v1.0/teams/team-paged/channels?$select=id,displayName,description,membershipType&$skip=2", |
| 107 | + }) |
| 108 | + .mockResolvedValueOnce({ |
| 109 | + value: [ |
| 110 | + { id: "ch-3", displayName: "Private", description: null, membershipType: "private" }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + |
| 114 | + const result = await listChannelsMSTeams({ |
| 115 | + cfg: {} as OpenClawConfig, |
| 116 | + teamId: "team-paged", |
| 117 | + }); |
| 118 | + |
| 119 | + expect(result.channels).toHaveLength(3); |
| 120 | + expect(result.channels.map((ch) => ch.id)).toEqual(["ch-1", "ch-2", "ch-3"]); |
| 121 | + expect(result.truncated).toBe(false); |
| 122 | + expect(mockState.fetchGraphJson).toHaveBeenCalledTimes(3); |
| 123 | + |
| 124 | + // Second call should use the relative path stripped from the nextLink |
| 125 | + const secondCallPath = mockState.fetchGraphJson.mock.calls[1]?.[0]?.path; |
| 126 | + expect(secondCallPath).toBe( |
| 127 | + "/teams/team-paged/channels?$select=id,displayName,description,membershipType&$skip=1", |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it("stops after 10 pages to avoid runaway pagination", async () => { |
| 132 | + for (let i = 0; i < 11; i++) { |
| 133 | + mockState.fetchGraphJson.mockResolvedValueOnce({ |
| 134 | + value: [ |
| 135 | + { |
| 136 | + id: `ch-${i}`, |
| 137 | + displayName: `Channel ${i}`, |
| 138 | + description: null, |
| 139 | + membershipType: "standard", |
| 140 | + }, |
| 141 | + ], |
| 142 | + "@odata.nextLink": `https://graph.microsoft.com/v1.0/teams/team-huge/channels?$skip=${i + 1}`, |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + const result = await listChannelsMSTeams({ |
| 147 | + cfg: {} as OpenClawConfig, |
| 148 | + teamId: "team-huge", |
| 149 | + }); |
| 150 | + |
| 151 | + // Should stop at 10 pages even though more nextLinks are available |
| 152 | + expect(result.channels).toHaveLength(10); |
| 153 | + expect(mockState.fetchGraphJson).toHaveBeenCalledTimes(10); |
| 154 | + expect(result.truncated).toBe(true); |
| 155 | + }); |
| 156 | +}); |
| 157 | + |
| 158 | +describe("getChannelInfoMSTeams", () => { |
| 159 | + beforeEach(() => { |
| 160 | + mockState.resolveGraphToken.mockReset().mockResolvedValue(TOKEN); |
| 161 | + mockState.fetchGraphJson.mockReset(); |
| 162 | + }); |
| 163 | + |
| 164 | + it("returns channel with all fields", async () => { |
| 165 | + mockState.fetchGraphJson.mockResolvedValue({ |
| 166 | + id: "ch-1", |
| 167 | + displayName: "General", |
| 168 | + description: "The default channel", |
| 169 | + membershipType: "standard", |
| 170 | + webUrl: "https://teams.microsoft.com/l/channel/ch-1/General", |
| 171 | + createdDateTime: "2026-01-15T09:00:00Z", |
| 172 | + }); |
| 173 | + |
| 174 | + const result = await getChannelInfoMSTeams({ |
| 175 | + cfg: {} as OpenClawConfig, |
| 176 | + teamId: "team-abc", |
| 177 | + channelId: "ch-1", |
| 178 | + }); |
| 179 | + |
| 180 | + expect(result.channel).toEqual({ |
| 181 | + id: "ch-1", |
| 182 | + displayName: "General", |
| 183 | + description: "The default channel", |
| 184 | + membershipType: "standard", |
| 185 | + webUrl: "https://teams.microsoft.com/l/channel/ch-1/General", |
| 186 | + createdDateTime: "2026-01-15T09:00:00Z", |
| 187 | + }); |
| 188 | + expect(mockState.fetchGraphJson).toHaveBeenCalledWith({ |
| 189 | + token: TOKEN, |
| 190 | + path: `/teams/${encodeURIComponent("team-abc")}/channels/${encodeURIComponent("ch-1")}?$select=id,displayName,description,membershipType,webUrl,createdDateTime`, |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it("handles missing optional fields gracefully", async () => { |
| 195 | + mockState.fetchGraphJson.mockResolvedValue({ |
| 196 | + id: "ch-2", |
| 197 | + displayName: "Private Channel", |
| 198 | + }); |
| 199 | + |
| 200 | + const result = await getChannelInfoMSTeams({ |
| 201 | + cfg: {} as OpenClawConfig, |
| 202 | + teamId: "team-abc", |
| 203 | + channelId: "ch-2", |
| 204 | + }); |
| 205 | + |
| 206 | + expect(result.channel).toEqual({ |
| 207 | + id: "ch-2", |
| 208 | + displayName: "Private Channel", |
| 209 | + description: undefined, |
| 210 | + membershipType: undefined, |
| 211 | + webUrl: undefined, |
| 212 | + createdDateTime: undefined, |
| 213 | + }); |
| 214 | + }); |
| 215 | +}); |
0 commit comments