|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { LocalStorageDAO } from "@App/app/repo/localStorage"; |
| 3 | +import { FileSystemError, isAuthError, isConflictError, isNotFoundError, isRateLimitError } from "../error"; |
2 | 4 | import GoogleDriveFileSystem from "./googledrive"; |
3 | 5 |
|
| 6 | +function createMockResponse(options: { ok?: boolean; status?: number; text?: string; json?: any }): Response { |
| 7 | + const { ok = true, status = 200, text = "", json = {} } = options; |
| 8 | + return { |
| 9 | + ok, |
| 10 | + status, |
| 11 | + text: vi.fn().mockResolvedValue(text), |
| 12 | + json: vi.fn().mockResolvedValue(json), |
| 13 | + headers: new Headers(), |
| 14 | + } as unknown as Response; |
| 15 | +} |
| 16 | + |
4 | 17 | describe("GoogleDriveFileSystem", () => { |
5 | | - beforeEach(() => { |
| 18 | + const localStorageDAO = new LocalStorageDAO(); |
| 19 | + let originalFetch: typeof fetch; |
| 20 | + |
| 21 | + beforeEach(async () => { |
6 | 22 | vi.clearAllMocks(); |
| 23 | + await chrome.storage.local.clear(); |
| 24 | + originalFetch = globalThis.fetch; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.stubGlobal("fetch", originalFetch); |
7 | 29 | }); |
8 | 30 |
|
9 | 31 | it("delete should be idempotent when file id is missing", async () => { |
@@ -59,4 +81,195 @@ describe("GoogleDriveFileSystem", () => { |
59 | 81 | expect(findSpy).toHaveBeenCalledWith("file.txt", "base-id"); |
60 | 82 | expect(requestSpy).toHaveBeenCalledTimes(1); |
61 | 83 | }); |
| 84 | + |
| 85 | + it("request should return retry result after token refresh", async () => { |
| 86 | + await localStorageDAO.saveValue("netdisk:token:googledrive", { |
| 87 | + accessToken: "expired-token", |
| 88 | + refreshToken: "refresh-token", |
| 89 | + createtime: Date.now(), |
| 90 | + }); |
| 91 | + |
| 92 | + const fs = new GoogleDriveFileSystem("/", "expired-token"); |
| 93 | + const fetchMock = vi |
| 94 | + .fn() |
| 95 | + .mockResolvedValueOnce( |
| 96 | + createMockResponse({ |
| 97 | + ok: false, |
| 98 | + status: 401, |
| 99 | + text: JSON.stringify({ |
| 100 | + error: { |
| 101 | + code: 401, |
| 102 | + message: "Invalid Credentials", |
| 103 | + status: "UNAUTHENTICATED", |
| 104 | + }, |
| 105 | + }), |
| 106 | + }) |
| 107 | + ) |
| 108 | + .mockResolvedValueOnce({ |
| 109 | + json: vi.fn().mockResolvedValue({ |
| 110 | + code: 0, |
| 111 | + data: { |
| 112 | + token: { |
| 113 | + access_token: "fresh-token", |
| 114 | + refresh_token: "fresh-refresh-token", |
| 115 | + }, |
| 116 | + }, |
| 117 | + }), |
| 118 | + } as unknown as Response) |
| 119 | + .mockResolvedValueOnce( |
| 120 | + createMockResponse({ |
| 121 | + json: { |
| 122 | + files: [{ id: "ok" }], |
| 123 | + }, |
| 124 | + }) |
| 125 | + ); |
| 126 | + vi.stubGlobal("fetch", fetchMock); |
| 127 | + |
| 128 | + const data = await fs.request("https://www.googleapis.com/drive/v3/files"); |
| 129 | + |
| 130 | + expect(data.files).toHaveLength(1); |
| 131 | + expect(fetchMock).toHaveBeenCalledTimes(3); |
| 132 | + }); |
| 133 | + |
| 134 | + it("request should throw auth error when retry still gets 401", async () => { |
| 135 | + await localStorageDAO.saveValue("netdisk:token:googledrive", { |
| 136 | + accessToken: "expired-token", |
| 137 | + refreshToken: "refresh-token", |
| 138 | + createtime: Date.now(), |
| 139 | + }); |
| 140 | + |
| 141 | + const fs = new GoogleDriveFileSystem("/", "expired-token"); |
| 142 | + vi.stubGlobal( |
| 143 | + "fetch", |
| 144 | + vi |
| 145 | + .fn() |
| 146 | + .mockResolvedValueOnce(createMockResponse({ ok: false, status: 401, text: "expired" })) |
| 147 | + .mockResolvedValueOnce({ |
| 148 | + json: vi.fn().mockResolvedValue({ |
| 149 | + code: 0, |
| 150 | + data: { |
| 151 | + token: { |
| 152 | + access_token: "fresh-token", |
| 153 | + refresh_token: "fresh-refresh-token", |
| 154 | + }, |
| 155 | + }, |
| 156 | + }), |
| 157 | + } as unknown as Response) |
| 158 | + .mockResolvedValueOnce(createMockResponse({ ok: false, status: 401, text: "still expired" })) |
| 159 | + ); |
| 160 | + |
| 161 | + try { |
| 162 | + await fs.request("https://www.googleapis.com/drive/v3/files"); |
| 163 | + throw new Error("Expected request to fail"); |
| 164 | + } catch (error) { |
| 165 | + expect(error).toBeInstanceOf(FileSystemError); |
| 166 | + expect(isAuthError(error)).toBe(true); |
| 167 | + expect(error).toMatchObject({ |
| 168 | + provider: "googledrive", |
| 169 | + status: 401, |
| 170 | + auth: true, |
| 171 | + }); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + it("request should throw typed not found error", async () => { |
| 176 | + const fs = new GoogleDriveFileSystem("/", "token"); |
| 177 | + vi.stubGlobal( |
| 178 | + "fetch", |
| 179 | + vi.fn().mockResolvedValueOnce( |
| 180 | + createMockResponse({ |
| 181 | + ok: false, |
| 182 | + status: 404, |
| 183 | + text: JSON.stringify({ |
| 184 | + error: { |
| 185 | + code: 404, |
| 186 | + message: "File not found", |
| 187 | + status: "NOT_FOUND", |
| 188 | + }, |
| 189 | + }), |
| 190 | + }) |
| 191 | + ) |
| 192 | + ); |
| 193 | + |
| 194 | + try { |
| 195 | + await fs.request("https://www.googleapis.com/drive/v3/files/missing"); |
| 196 | + throw new Error("Expected request to fail"); |
| 197 | + } catch (error) { |
| 198 | + expect(error).toBeInstanceOf(FileSystemError); |
| 199 | + expect(isNotFoundError(error)).toBe(true); |
| 200 | + expect(error).toMatchObject({ |
| 201 | + provider: "googledrive", |
| 202 | + status: 404, |
| 203 | + code: "NOT_FOUND", |
| 204 | + notFound: true, |
| 205 | + }); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + it.each([409, 412])("request should throw typed conflict error for status %s", async (status) => { |
| 210 | + const fs = new GoogleDriveFileSystem("/", "token"); |
| 211 | + vi.stubGlobal( |
| 212 | + "fetch", |
| 213 | + vi.fn().mockResolvedValueOnce( |
| 214 | + createMockResponse({ |
| 215 | + ok: false, |
| 216 | + status, |
| 217 | + text: JSON.stringify({ |
| 218 | + error: { |
| 219 | + code: status, |
| 220 | + message: "Conflict", |
| 221 | + status: status === 409 ? "ABORTED" : "FAILED_PRECONDITION", |
| 222 | + }, |
| 223 | + }), |
| 224 | + }) |
| 225 | + ) |
| 226 | + ); |
| 227 | + |
| 228 | + try { |
| 229 | + await fs.request("https://www.googleapis.com/drive/v3/files/conflict"); |
| 230 | + throw new Error("Expected request to fail"); |
| 231 | + } catch (error) { |
| 232 | + expect(error).toBeInstanceOf(FileSystemError); |
| 233 | + expect(isConflictError(error)).toBe(true); |
| 234 | + expect(error).toMatchObject({ |
| 235 | + provider: "googledrive", |
| 236 | + status, |
| 237 | + conflict: true, |
| 238 | + }); |
| 239 | + } |
| 240 | + }); |
| 241 | + |
| 242 | + it("request should throw typed rate-limit error", async () => { |
| 243 | + const fs = new GoogleDriveFileSystem("/", "token"); |
| 244 | + vi.stubGlobal( |
| 245 | + "fetch", |
| 246 | + vi.fn().mockResolvedValueOnce( |
| 247 | + createMockResponse({ |
| 248 | + ok: false, |
| 249 | + status: 429, |
| 250 | + text: JSON.stringify({ |
| 251 | + error: { |
| 252 | + code: 429, |
| 253 | + message: "Quota exceeded", |
| 254 | + status: "RESOURCE_EXHAUSTED", |
| 255 | + }, |
| 256 | + }), |
| 257 | + }) |
| 258 | + ) |
| 259 | + ); |
| 260 | + |
| 261 | + try { |
| 262 | + await fs.request("https://www.googleapis.com/drive/v3/files"); |
| 263 | + throw new Error("Expected request to fail"); |
| 264 | + } catch (error) { |
| 265 | + expect(error).toBeInstanceOf(FileSystemError); |
| 266 | + expect(isRateLimitError(error)).toBe(true); |
| 267 | + expect(error).toMatchObject({ |
| 268 | + provider: "googledrive", |
| 269 | + status: 429, |
| 270 | + retryable: true, |
| 271 | + rateLimit: true, |
| 272 | + }); |
| 273 | + } |
| 274 | + }); |
62 | 275 | }); |
0 commit comments