Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/filesystem/baidu/baidu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it, vi, afterEach } from "vitest";
import BaiduFileSystem from "./baidu";

describe("BaiduFileSystem", () => {
afterEach(() => {
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里使用了 vi.stubGlobal("fetch", ...) 覆盖全局 fetch,但 afterEach 仅调用 vi.restoreAllMocks() 并不会还原 stubGlobal 的全局替换,可能导致后续测试用例继续使用本用例的 fetchMock(产生串扰/偶发失败)。建议在 afterEach 中补充 vi.unstubAllGlobals()(或至少 vi.unstubGlobal("fetch")),必要时再配合 vi.restoreAllMocks()/vi.clearAllMocks()。

Suggested change
afterEach(() => {
afterEach(() => {
vi.unstubAllGlobals();

Copilot uses AI. Check for mistakes.
vi.unstubAllGlobals();
vi.restoreAllMocks();
});

it("request should omit credentials without using global DNR rules", async () => {
const fetchMock = vi.fn().mockResolvedValue({
json: async () => ({ errno: 0 }),
});
vi.stubGlobal("fetch", fetchMock);
Comment on lines +10 to +14
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试用例标题里强调“without using global DNR rules”,但当前断言只覆盖了 fetch 的 credentials: "omit",并没有验证不再调用 chrome.declarativeNetRequest.updateDynamicRules(未来若有人回退/重新引入 DNR 逻辑,此测试仍会通过)。建议在测试中显式为 chrome.declarativeNetRequest 注入/spy updateDynamicRules 并断言其未被调用,以覆盖 PR 的关键目标。

Copilot uses AI. Check for mistakes.

// 监视 updateDynamicRules,确保不再依赖全局 DNR 规则
const updateDynamicRulesMock = vi.fn();
(chrome as any).declarativeNetRequest.updateDynamicRules = updateDynamicRulesMock;

const fs = new BaiduFileSystem("/apps", "token");

await expect(fs.request("https://pan.baidu.com/rest/2.0/xpan/file?method=list")).resolves.toEqual({
errno: 0,
});

expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(
"https://pan.baidu.com/rest/2.0/xpan/file?method=list",
expect.objectContaining({
credentials: "omit",
})
);
expect(updateDynamicRulesMock).not.toHaveBeenCalled();
});
});
29 changes: 2 additions & 27 deletions packages/filesystem/baidu/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,9 @@ export default class BaiduFileSystem implements FileSystem {
async request(url: string, config?: RequestInit) {
config = config || {};
const headers = <Headers>config.headers || new Headers();
// 处理请求匿名不发送cookie
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [100],
addRules: [
{
id: 100,
action: {
type: "modifyHeaders",
responseHeaders: [
{
operation: "remove",
header: "cookie",
},
],
},
condition: {
urlFilter: url,
resourceTypes: ["xmlhttprequest"],
},
},
],
});
config.headers = headers;
// 对百度网盘请求显式禁用 cookie,避免依赖全局 DNR 规则造成并发竞态
config.credentials = "omit";
return fetch(url, config)
.then((data) => data.json())
.then(async (data) => {
Expand All @@ -99,11 +79,6 @@ export default class BaiduFileSystem implements FileSystem {
});
}
return data;
})
.finally(() => {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [100],
});
});
}

Expand Down
Loading