-
Notifications
You must be signed in to change notification settings - Fork 324
(Codex) 修复 packages/filesystem 中认证恢复与删除幂等问题
#1375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { AuthVerify } from "./auth"; | ||
| import { LocalStorageDAO } from "@App/app/repo/localStorage"; | ||
|
|
||
| describe("AuthVerify", () => { | ||
| const localStorageDAO = new LocalStorageDAO(); | ||
| const key = "netdisk:token:onedrive"; | ||
| let originalFetch: typeof fetch; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
| await chrome.storage.local.clear(); | ||
| originalFetch = globalThis.fetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.stubGlobal("fetch", originalFetch); | ||
| }); | ||
|
|
||
| it("expired token refresh network failure should reject, not fallback old token", async () => { | ||
| await localStorageDAO.saveValue(key, { | ||
| accessToken: "old-access", | ||
| refreshToken: "old-refresh", | ||
| createtime: Date.now() - 3600000 - 1000, | ||
| }); | ||
|
|
||
| vi.stubGlobal("fetch", vi.fn().mockRejectedValueOnce(new Error("refresh network failed"))); | ||
|
|
||
| await expect(AuthVerify("onedrive")).rejects.toThrow("refresh network failed"); | ||
| }); | ||
|
|
||
| it("non-expired token should return cached token without refresh", async () => { | ||
| await localStorageDAO.saveValue(key, { | ||
| accessToken: "cached-access", | ||
| refreshToken: "cached-refresh", | ||
| createtime: Date.now(), | ||
| }); | ||
|
|
||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
|
|
||
| await expect(AuthVerify("onedrive")).resolves.toBe("cached-access"); | ||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import DropboxFileSystem from "./dropbox"; | ||
|
|
||
| describe("DropboxFileSystem", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("delete should be idempotent on path not found", async () => { | ||
| const fs = new DropboxFileSystem("/", "token"); | ||
| vi.spyOn(fs, "request").mockRejectedValue( | ||
| new Error('Dropbox API Error: 409 - {"error_summary":"path_lookup/not_found/..."}') | ||
| ); | ||
|
|
||
| await expect(fs.delete("missing.txt")).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import GoogleDriveFileSystem from "./googledrive"; | ||
|
|
||
| describe("GoogleDriveFileSystem", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("delete should be idempotent when file id is missing", async () => { | ||
| const fs = new GoogleDriveFileSystem("/", "token"); | ||
| vi.spyOn(fs, "getFileId").mockResolvedValue(null); | ||
| const requestSpy = vi.spyOn(fs, "request"); | ||
|
|
||
| await expect(fs.delete("missing.txt")).resolves.toBeUndefined(); | ||
| expect(requestSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("delete should be idempotent on 404 response", async () => { | ||
| const fs = new GoogleDriveFileSystem("/", "token"); | ||
| vi.spyOn(fs, "getFileId").mockResolvedValue("file-1"); | ||
| vi.spyOn(fs, "request").mockResolvedValue({ | ||
| status: 404, | ||
| text: vi.fn().mockResolvedValue("not found"), | ||
| } as unknown as Response); | ||
|
|
||
| await expect(fs.delete("missing.txt")).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import OneDriveFileSystem from "./onedrive"; | ||
| import { LocalStorageDAO } from "@App/app/repo/localStorage"; | ||
|
|
||
| function createMockResponse(options: { ok?: boolean; status?: number; text?: string; json?: any }): Response { | ||
| const { ok = true, status = 200, text = "", json = {} } = options; | ||
| return { | ||
| ok, | ||
| status, | ||
| text: vi.fn().mockResolvedValue(text), | ||
| json: vi.fn().mockResolvedValue(json), | ||
| headers: new Headers(), | ||
| } as unknown as Response; | ||
| } | ||
|
|
||
| describe("OneDriveFileSystem", () => { | ||
| const localStorageDAO = new LocalStorageDAO(); | ||
| let originalFetch: typeof fetch; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks(); | ||
| await chrome.storage.local.clear(); | ||
| originalFetch = globalThis.fetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.stubGlobal("fetch", originalFetch); | ||
| }); | ||
|
Comment on lines
+20
to
+28
|
||
|
|
||
| it("request should return retry result after token refresh", async () => { | ||
| await localStorageDAO.saveValue("netdisk:token:onedrive", { | ||
| accessToken: "expired-token", | ||
| refreshToken: "refresh-token", | ||
| createtime: Date.now(), | ||
| }); | ||
|
|
||
| const fs = new OneDriveFileSystem("/", "expired-token"); | ||
| const fetchMock = vi | ||
| .fn() | ||
| .mockResolvedValueOnce( | ||
| createMockResponse({ | ||
| ok: true, | ||
| status: 200, | ||
| json: { | ||
| error: { | ||
| code: "InvalidAuthenticationToken", | ||
| }, | ||
| }, | ||
| }) | ||
| ) | ||
| .mockResolvedValueOnce({ | ||
| json: vi.fn().mockResolvedValue({ | ||
| code: 0, | ||
| data: { | ||
| token: { | ||
| access_token: "fresh-token", | ||
| refresh_token: "fresh-refresh-token", | ||
| }, | ||
| }, | ||
| }), | ||
| } as unknown as Response) | ||
| .mockResolvedValueOnce( | ||
| createMockResponse({ | ||
| ok: true, | ||
| status: 200, | ||
| json: { | ||
| value: [ | ||
| { | ||
| name: "ok.txt", | ||
| size: 1, | ||
| eTag: "tag", | ||
| createdDateTime: new Date().toISOString(), | ||
| lastModifiedDateTime: new Date().toISOString(), | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
| ); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
|
|
||
| const data = await fs.request("https://graph.microsoft.com/v1.0/me/drive/special/approot/children"); | ||
|
|
||
| expect(data.value).toHaveLength(1); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it("delete should be idempotent on 404", async () => { | ||
| const fs = new OneDriveFileSystem("/", "token"); | ||
| vi.spyOn(fs, "request").mockResolvedValue({ | ||
| status: 404, | ||
| text: vi.fn().mockResolvedValue("not found"), | ||
| } as unknown as Response); | ||
|
|
||
| await expect(fs.delete("missing.txt")).resolves.toBeUndefined(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里用 vi.stubGlobal("fetch", ...) 覆盖了全局 fetch,但没有在用例结束后恢复,可能导致其它测试用例受到污染(同仓库里如 packages/filesystem/s3/client.test.ts 会在 afterEach 里 vi.unstubAllGlobals)。建议在本 describe 增加 afterEach 调用 vi.unstubAllGlobals()(或保存原 fetch 并恢复),确保测试隔离。