|
| 1 | +import { afterEach, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { uploadMedia } from "../../src/lib/api/media.js"; |
| 4 | + |
| 5 | +afterEach(() => { |
| 6 | + vi.restoreAllMocks(); |
| 7 | + vi.unstubAllGlobals(); |
| 8 | +}); |
| 9 | + |
| 10 | +it("deduplicates uploads using the file content hash", async () => { |
| 11 | + let uploadUrlBody: Record<string, unknown> | undefined; |
| 12 | + const fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { |
| 13 | + const url = |
| 14 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 15 | + if (url === "/_emdash/api/media/upload-url") { |
| 16 | + if (typeof init?.body !== "string") throw new TypeError("Expected a JSON request body"); |
| 17 | + uploadUrlBody = JSON.parse(init.body) as Record<string, unknown>; |
| 18 | + return Response.json({ |
| 19 | + success: true, |
| 20 | + data: { |
| 21 | + existing: true, |
| 22 | + mediaId: "existing-media", |
| 23 | + storageKey: "existing.pdf", |
| 24 | + url: "/_emdash/api/media/file/existing.pdf", |
| 25 | + }, |
| 26 | + }); |
| 27 | + } |
| 28 | + if (url === "/_emdash/api/media/existing-media") { |
| 29 | + return Response.json({ |
| 30 | + success: true, |
| 31 | + data: { |
| 32 | + item: { |
| 33 | + id: "existing-media", |
| 34 | + filename: "existing.pdf", |
| 35 | + mimeType: "application/pdf", |
| 36 | + url: "/_emdash/api/media/file/existing.pdf", |
| 37 | + storageKey: "existing.pdf", |
| 38 | + size: 3, |
| 39 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }); |
| 43 | + } |
| 44 | + return new Response(null, { status: 500 }); |
| 45 | + }); |
| 46 | + const file = new File([new Uint8Array([97, 98, 99])], "document.pdf", { |
| 47 | + type: "application/pdf", |
| 48 | + }); |
| 49 | + |
| 50 | + const item = await uploadMedia(file); |
| 51 | + |
| 52 | + expect(uploadUrlBody?.contentHash).toBe("sha1:a9993e364706816aba3e25717850c26c9cd0d89d"); |
| 53 | + expect(item.id).toBe("existing-media"); |
| 54 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 55 | +}); |
| 56 | + |
| 57 | +it("uploads without deduplication when Web Crypto is unavailable", async () => { |
| 58 | + vi.stubGlobal("crypto", {}); |
| 59 | + let uploadUrlBody: Record<string, unknown> | undefined; |
| 60 | + vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { |
| 61 | + const url = |
| 62 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 63 | + if (url === "/_emdash/api/media/upload-url") { |
| 64 | + if (typeof init?.body !== "string") throw new TypeError("Expected a JSON request body"); |
| 65 | + uploadUrlBody = JSON.parse(init.body) as Record<string, unknown>; |
| 66 | + return Response.json({ |
| 67 | + success: true, |
| 68 | + data: { |
| 69 | + uploadUrl: "/_emdash/api/media/new-media/upload", |
| 70 | + method: "PUT", |
| 71 | + headers: { "Content-Type": "application/pdf" }, |
| 72 | + mediaId: "new-media", |
| 73 | + storageKey: "new.pdf", |
| 74 | + expiresAt: "2026-01-01T01:00:00.000Z", |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + if (url === "/_emdash/api/media/new-media/upload") { |
| 79 | + return Response.json({ success: true, data: { uploaded: true, size: 3 } }); |
| 80 | + } |
| 81 | + if (url === "/_emdash/api/media/new-media/confirm") { |
| 82 | + return Response.json({ |
| 83 | + success: true, |
| 84 | + data: { |
| 85 | + item: { |
| 86 | + id: "new-media", |
| 87 | + filename: "new.pdf", |
| 88 | + mimeType: "application/pdf", |
| 89 | + url: "/_emdash/api/media/file/new.pdf", |
| 90 | + storageKey: "new.pdf", |
| 91 | + size: 3, |
| 92 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + } |
| 97 | + return new Response(null, { status: 500 }); |
| 98 | + }); |
| 99 | + const file = new File([new Uint8Array([1, 2, 3])], "new.pdf", { |
| 100 | + type: "application/pdf", |
| 101 | + }); |
| 102 | + |
| 103 | + const item = await uploadMedia(file); |
| 104 | + |
| 105 | + expect(uploadUrlBody).not.toHaveProperty("contentHash"); |
| 106 | + expect(item.id).toBe("new-media"); |
| 107 | +}); |
| 108 | + |
| 109 | +it("uploads without deduplication when content hashing fails", async () => { |
| 110 | + vi.stubGlobal("crypto", { |
| 111 | + subtle: { |
| 112 | + digest: vi.fn().mockRejectedValue(new Error("SHA-1 unavailable")), |
| 113 | + }, |
| 114 | + }); |
| 115 | + let uploadUrlBody: Record<string, unknown> | undefined; |
| 116 | + const fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { |
| 117 | + const url = |
| 118 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 119 | + if (url === "/_emdash/api/media/upload-url") { |
| 120 | + if (typeof init?.body !== "string") throw new TypeError("Expected a JSON request body"); |
| 121 | + uploadUrlBody = JSON.parse(init.body) as Record<string, unknown>; |
| 122 | + return new Response(null, { status: 501 }); |
| 123 | + } |
| 124 | + if (url === "/_emdash/api/media") { |
| 125 | + return Response.json({ |
| 126 | + success: true, |
| 127 | + data: { |
| 128 | + item: { |
| 129 | + id: "new-media", |
| 130 | + filename: "new.pdf", |
| 131 | + mimeType: "application/pdf", |
| 132 | + url: "/_emdash/api/media/file/new.pdf", |
| 133 | + storageKey: "new.pdf", |
| 134 | + size: 3, |
| 135 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 136 | + }, |
| 137 | + }, |
| 138 | + }); |
| 139 | + } |
| 140 | + return new Response(null, { status: 500 }); |
| 141 | + }); |
| 142 | + const file = new File([new Uint8Array([1, 2, 3])], "new.pdf", { |
| 143 | + type: "application/pdf", |
| 144 | + }); |
| 145 | + |
| 146 | + const item = await uploadMedia(file); |
| 147 | + |
| 148 | + expect(uploadUrlBody).not.toHaveProperty("contentHash"); |
| 149 | + expect(item.id).toBe("new-media"); |
| 150 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 151 | +}); |
| 152 | + |
| 153 | +it("does not deduplicate empty files by their shared hash", async () => { |
| 154 | + let uploadUrlBody: Record<string, unknown> | undefined; |
| 155 | + vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { |
| 156 | + const url = |
| 157 | + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; |
| 158 | + if (url === "/_emdash/api/media/upload-url") { |
| 159 | + if (typeof init?.body !== "string") throw new TypeError("Expected a JSON request body"); |
| 160 | + uploadUrlBody = JSON.parse(init.body) as Record<string, unknown>; |
| 161 | + return Response.json({ |
| 162 | + success: true, |
| 163 | + data: { |
| 164 | + uploadUrl: "/_emdash/api/media/empty-media/upload", |
| 165 | + method: "PUT", |
| 166 | + headers: { "Content-Type": "application/pdf" }, |
| 167 | + mediaId: "empty-media", |
| 168 | + storageKey: "empty.pdf", |
| 169 | + expiresAt: "2026-01-01T01:00:00.000Z", |
| 170 | + }, |
| 171 | + }); |
| 172 | + } |
| 173 | + if (url === "/_emdash/api/media/empty-media/upload") { |
| 174 | + return Response.json({ success: true, data: { uploaded: true, size: 0 } }); |
| 175 | + } |
| 176 | + if (url === "/_emdash/api/media/empty-media/confirm") { |
| 177 | + return Response.json({ |
| 178 | + success: true, |
| 179 | + data: { |
| 180 | + item: { |
| 181 | + id: "empty-media", |
| 182 | + filename: "empty.pdf", |
| 183 | + mimeType: "application/pdf", |
| 184 | + url: "/_emdash/api/media/file/empty.pdf", |
| 185 | + storageKey: "empty.pdf", |
| 186 | + size: 0, |
| 187 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 188 | + }, |
| 189 | + }, |
| 190 | + }); |
| 191 | + } |
| 192 | + return new Response(null, { status: 500 }); |
| 193 | + }); |
| 194 | + |
| 195 | + await uploadMedia(new File([], "empty.pdf", { type: "application/pdf" })); |
| 196 | + |
| 197 | + expect(uploadUrlBody).not.toHaveProperty("contentHash"); |
| 198 | +}); |
0 commit comments