|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import OneDriveFileSystem from "./onedrive"; |
| 3 | +import { LocalStorageDAO } from "@App/app/repo/localStorage"; |
| 4 | + |
| 5 | +function createMockResponse(options: { ok?: boolean; status?: number; text?: string; json?: any }): Response { |
| 6 | + const { ok = true, status = 200, text = "", json = {} } = options; |
| 7 | + return { |
| 8 | + ok, |
| 9 | + status, |
| 10 | + text: vi.fn().mockResolvedValue(text), |
| 11 | + json: vi.fn().mockResolvedValue(json), |
| 12 | + headers: new Headers(), |
| 13 | + } as unknown as Response; |
| 14 | +} |
| 15 | + |
| 16 | +describe("OneDriveFileSystem", () => { |
| 17 | + const localStorageDAO = new LocalStorageDAO(); |
| 18 | + let originalFetch: typeof fetch; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + await chrome.storage.local.clear(); |
| 23 | + originalFetch = globalThis.fetch; |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.stubGlobal("fetch", originalFetch); |
| 28 | + }); |
| 29 | + |
| 30 | + it("request should return retry result after token refresh", async () => { |
| 31 | + await localStorageDAO.saveValue("netdisk:token:onedrive", { |
| 32 | + accessToken: "expired-token", |
| 33 | + refreshToken: "refresh-token", |
| 34 | + createtime: Date.now(), |
| 35 | + }); |
| 36 | + |
| 37 | + const fs = new OneDriveFileSystem("/", "expired-token"); |
| 38 | + const fetchMock = vi |
| 39 | + .fn() |
| 40 | + .mockResolvedValueOnce( |
| 41 | + createMockResponse({ |
| 42 | + ok: true, |
| 43 | + status: 200, |
| 44 | + json: { |
| 45 | + error: { |
| 46 | + code: "InvalidAuthenticationToken", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }) |
| 50 | + ) |
| 51 | + .mockResolvedValueOnce({ |
| 52 | + json: vi.fn().mockResolvedValue({ |
| 53 | + code: 0, |
| 54 | + data: { |
| 55 | + token: { |
| 56 | + access_token: "fresh-token", |
| 57 | + refresh_token: "fresh-refresh-token", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }), |
| 61 | + } as unknown as Response) |
| 62 | + .mockResolvedValueOnce( |
| 63 | + createMockResponse({ |
| 64 | + ok: true, |
| 65 | + status: 200, |
| 66 | + json: { |
| 67 | + value: [ |
| 68 | + { |
| 69 | + name: "ok.txt", |
| 70 | + size: 1, |
| 71 | + eTag: "tag", |
| 72 | + createdDateTime: new Date().toISOString(), |
| 73 | + lastModifiedDateTime: new Date().toISOString(), |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + }) |
| 78 | + ); |
| 79 | + vi.stubGlobal("fetch", fetchMock); |
| 80 | + |
| 81 | + const data = await fs.request("https://graph.microsoft.com/v1.0/me/drive/special/approot/children"); |
| 82 | + |
| 83 | + expect(data.value).toHaveLength(1); |
| 84 | + expect(fetchMock).toHaveBeenCalledTimes(3); |
| 85 | + }); |
| 86 | + |
| 87 | + it("delete should be idempotent on 404", async () => { |
| 88 | + const fs = new OneDriveFileSystem("/", "token"); |
| 89 | + vi.spyOn(fs, "request").mockResolvedValue({ |
| 90 | + status: 404, |
| 91 | + text: vi.fn().mockResolvedValue("not found"), |
| 92 | + } as unknown as Response); |
| 93 | + |
| 94 | + await expect(fs.delete("missing.txt")).resolves.toBeUndefined(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments