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