























@@ -3,6 +3,25 @@ import { describe, expect, it, vi } from "vitest";
33import { buildTeamsFileInfoCard } from "./graph-chat.js";
44import { resolveGraphChatId, uploadToOneDrive, uploadToSharePoint } from "./graph-upload.js";
556+type FetchCall = [string, { method?: string; headers?: Record<string, string> } | undefined];
7+8+function requireFetchCall(fetchFn: ReturnType<typeof vi.fn>, index = 0): FetchCall {
9+const call = fetchFn.mock.calls[index] as unknown as FetchCall | undefined;
10+if (!call) {
11+throw new Error(`fetch call ${index} missing`);
12+}
13+return call;
14+}
15+16+function expectGraphUploadFetch(fetchFn: ReturnType<typeof vi.fn>, expectedUrl: string): void {
17+const [url, init] = requireFetchCall(fetchFn);
18+expect(url).toBe(expectedUrl);
19+expect(init?.method).toBe("PUT");
20+expect(init?.headers?.Authorization).toBe("Bearer graph-token");
21+expect(init?.headers?.["Content-Type"]).toBe("application/octet-stream");
22+expect(init?.headers?.["User-Agent"]).toMatch(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/);
23+}
24+625describe("graph upload helpers", () => {
726const tokenProvider = {
827getAccessToken: vi.fn(async () => "graph-token"),
@@ -27,16 +46,9 @@ describe("graph upload helpers", () => {
2746fetchFn: withFetchPreconnect(fetchFn),
2847});
294830-expect(fetchFn).toHaveBeenCalledWith(
49+expectGraphUploadFetch(
50+fetchFn,
3151"https://graph.microsoft.com/v1.0/me/drive/root:/OpenClawShared/a.txt:/content",
32-expect.objectContaining({
33-method: "PUT",
34-headers: expect.objectContaining({
35-Authorization: "Bearer graph-token",
36-"Content-Type": "application/octet-stream",
37-"User-Agent": expect.stringMatching(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/),
38-}),
39-}),
4052);
4153expect(result).toEqual({
4254id: "item-1",
@@ -65,16 +77,9 @@ describe("graph upload helpers", () => {
6577fetchFn: withFetchPreconnect(fetchFn),
6678});
677968-expect(fetchFn).toHaveBeenCalledWith(
80+expectGraphUploadFetch(
81+fetchFn,
6982"https://graph.microsoft.com/v1.0/sites/site-123/drive/root:/OpenClawShared/b.txt:/content",
70-expect.objectContaining({
71-method: "PUT",
72-headers: expect.objectContaining({
73-Authorization: "Bearer graph-token",
74-"Content-Type": "application/octet-stream",
75-"User-Agent": expect.stringMatching(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/),
76-}),
77-}),
7883);
7984expect(result).toEqual({
8085id: "item-2",
@@ -137,20 +142,10 @@ describe("resolveGraphChatId", () => {
137142fetchFn: withFetchPreconnect(fetchFn),
138143});
139144140-expect(fetchFn).toHaveBeenCalledWith(
141-expect.stringContaining("/me/chats"),
142-expect.objectContaining({
143-headers: expect.objectContaining({
144-Authorization: "Bearer graph-token",
145-"User-Agent": expect.stringMatching(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/),
146-}),
147-}),
148-);
149-const firstCall = fetchFn.mock.calls[0];
150-if (!firstCall) {
151-throw new Error("expected Graph chat lookup request");
152-}
153-const [callUrlRaw] = firstCall as unknown as [string, RequestInit?];
145+expect(fetchFn).toHaveBeenCalledTimes(1);
146+const [callUrlRaw, init] = requireFetchCall(fetchFn);
147+expect(init?.headers?.Authorization).toBe("Bearer graph-token");
148+expect(init?.headers?.["User-Agent"]).toMatch(/^teams\.ts\[apps\]\/.+ OpenClaw\/.+$/);
154149const callUrl = new URL(callUrlRaw);
155150expect(callUrl.origin).toBe("https://graph.microsoft.com");
156151expect(callUrl.pathname).toBe("/v1.0/me/chats");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。