-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathwebdav.test.ts
More file actions
231 lines (188 loc) · 7.37 KB
/
webdav.test.ts
File metadata and controls
231 lines (188 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { describe, it, expect, vi, beforeEach } from "vitest";
import type { WebDAVClient } from "webdav";
import { getPatcher } from "webdav";
import WebDAVFileSystem from "./webdav";
import { WarpTokenError } from "../error";
/** 创建 mock WebDAVClient */
function createMockClient(overrides?: Partial<WebDAVClient>): WebDAVClient {
return {
getQuota: vi.fn().mockResolvedValue({}),
getDirectoryContents: vi.fn().mockResolvedValue([]),
getFileContents: vi.fn().mockResolvedValue("content"),
putFileContents: vi.fn().mockResolvedValue(true),
createDirectory: vi.fn().mockResolvedValue(undefined),
deleteFile: vi.fn().mockResolvedValue(undefined),
...overrides,
} as unknown as WebDAVClient;
}
/** 创建可测试的 WebDAVFileSystem 实例(替换 client 为 mock) */
function createTestFS(mockClient: WebDAVClient, url = "https://dav.example.com"): WebDAVFileSystem {
const fs = WebDAVFileSystem.fromCredentials(url, {});
fs.client = mockClient;
return fs;
}
describe("WebDAVFileSystem", () => {
let mockClient: WebDAVClient;
beforeEach(() => {
vi.clearAllMocks();
mockClient = createMockClient();
});
describe("initWebDAVPatch", () => {
it("应当通过 getPatcher 注册 fetch patch,设置 credentials 为 omit", () => {
// fromCredentials 内部调用 initWebDAVPatch,验证 patcher 已注册 fetch
WebDAVFileSystem.fromCredentials("https://dav.example.com", {});
const patcher = getPatcher();
// 验证 fetch 已被 patch(patcher 内部有 fetch 注册)
expect(patcher.isPatched("fetch")).toBe(true);
});
});
describe("fromCredentials", () => {
it("应当创建 WebDAVFileSystem 实例并设置 url 和 basePath", () => {
const fs = WebDAVFileSystem.fromCredentials("https://dav.example.com", {
authType: "password" as any,
username: "user",
password: "pass",
});
expect(fs).toBeInstanceOf(WebDAVFileSystem);
expect(fs.url).toBe("https://dav.example.com");
expect(fs.basePath).toBe("/");
});
});
describe("fromSameClient", () => {
it("应当复用已有 client 并设置新 basePath", () => {
const fs = createTestFS(mockClient);
const subFs = WebDAVFileSystem.fromSameClient(fs, "/subdir");
expect(subFs).toBeInstanceOf(WebDAVFileSystem);
expect(subFs.url).toBe("https://dav.example.com");
expect(subFs.basePath).toBe("/subdir");
expect(subFs.client).toBe(mockClient);
});
});
describe("verify", () => {
it("应当成功验证", async () => {
const fs = createTestFS(mockClient);
await expect(fs.verify()).resolves.toBeUndefined();
expect(mockClient.getQuota).toHaveBeenCalled();
});
it("应当在 401 时抛出 WarpTokenError", async () => {
(mockClient.getQuota as ReturnType<typeof vi.fn>).mockRejectedValue({
response: { status: 401 },
message: "Unauthorized",
});
const fs = createTestFS(mockClient);
await expect(fs.verify()).rejects.toBeInstanceOf(WarpTokenError);
});
it("应当在其他错误时抛出包含原始信息的 Error", async () => {
(mockClient.getQuota as ReturnType<typeof vi.fn>).mockRejectedValue({
message: "Network error",
});
const fs = createTestFS(mockClient);
await expect(fs.verify()).rejects.toThrow("WebDAV verify failed: Network error");
});
});
describe("openDir", () => {
it("应当返回新实例并拼接路径", async () => {
const fs = createTestFS(mockClient);
const subFs = (await fs.openDir("docs")) as WebDAVFileSystem;
expect(subFs).toBeInstanceOf(WebDAVFileSystem);
expect(subFs.basePath).toBe("/docs");
expect(subFs.client).toBe(mockClient);
});
it("应当支持嵌套 openDir", async () => {
const fs = createTestFS(mockClient);
const sub1 = (await fs.openDir("a")) as WebDAVFileSystem;
const sub2 = (await sub1.openDir("b")) as WebDAVFileSystem;
expect(sub2.basePath).toBe("/a/b");
});
});
describe("createDir", () => {
it("应当调用 createDirectory", async () => {
const fs = createTestFS(mockClient);
await fs.createDir("new-folder");
expect(mockClient.createDirectory).toHaveBeenCalledWith("/new-folder");
});
it("应当在 405 错误时静默成功(目录已存在)", async () => {
(mockClient.createDirectory as ReturnType<typeof vi.fn>).mockRejectedValue({
response: { status: 405 },
message: "405 Method Not Allowed",
});
const fs = createTestFS(mockClient);
await expect(fs.createDir("existing")).resolves.toBeUndefined();
});
it("应当在 message 包含 405 时也静默成功", async () => {
(mockClient.createDirectory as ReturnType<typeof vi.fn>).mockRejectedValue({
message: "Request failed with status code 405",
});
const fs = createTestFS(mockClient);
await expect(fs.createDir("existing")).resolves.toBeUndefined();
});
it("应当在其他错误时抛出异常", async () => {
const err = new Error("Forbidden");
(mockClient.createDirectory as ReturnType<typeof vi.fn>).mockRejectedValue(err);
const fs = createTestFS(mockClient);
await expect(fs.createDir("denied")).rejects.toThrow("Forbidden");
});
});
describe("delete", () => {
it("应当调用 deleteFile", async () => {
const fs = createTestFS(mockClient);
await fs.delete("test.txt");
expect(mockClient.deleteFile).toHaveBeenCalledWith("/test.txt");
});
});
describe("list", () => {
it("应当列出文件并过滤目录", async () => {
(mockClient.getDirectoryContents as ReturnType<typeof vi.fn>).mockResolvedValue([
{
type: "file",
basename: "test.txt",
lastmod: "2024-01-01T00:00:00Z",
etag: '"abc"',
size: 1024,
},
{
type: "directory",
basename: "subdir",
lastmod: "2024-01-01T00:00:00Z",
etag: "",
size: 0,
},
]);
const fs = createTestFS(mockClient);
const files = await fs.list();
expect(files).toHaveLength(1);
expect(files[0]).toMatchObject({
name: "test.txt",
path: "/",
digest: '"abc"',
size: 1024,
});
});
it("应当在 404 时返回空数组", async () => {
(mockClient.getDirectoryContents as ReturnType<typeof vi.fn>).mockRejectedValue({
response: { status: 404 },
});
const fs = createTestFS(mockClient);
const files = await fs.list();
expect(files).toHaveLength(0);
});
it("应当在其他错误时抛出异常", async () => {
const err = new Error("Server Error");
(err as any).response = { status: 500 };
(mockClient.getDirectoryContents as ReturnType<typeof vi.fn>).mockRejectedValue(err);
const fs = createTestFS(mockClient);
await expect(fs.list()).rejects.toThrow("Server Error");
});
});
describe("getDirUrl", () => {
it("应当返回 url + basePath", async () => {
const fs = createTestFS(mockClient);
const subFs = (await fs.openDir("docs")) as WebDAVFileSystem;
expect(await subFs.getDirUrl()).toBe("https://dav.example.com/docs");
});
it("根路径应返回 url + /", async () => {
const fs = createTestFS(mockClient);
expect(await fs.getDirUrl()).toBe("https://dav.example.com/");
});
});
});