|
| 1 | +/** |
| 2 | + * Unit tests for createHttpAdapter. |
| 3 | + * |
| 4 | + * Mocks global `fetch` to verify URL construction, method/headers, error routing, |
| 5 | + * and flush semantics without a real server. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 9 | +import { createHttpAdapter } from "./http.js"; |
| 10 | + |
| 11 | +const BASE = "/api/projects/proj-abc"; |
| 12 | + |
| 13 | +// ── fetch mock helpers ──────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +function stubFetch( |
| 16 | + handler: (url: string, init?: RequestInit) => { ok: boolean; status?: number; body?: unknown }, |
| 17 | +): ReturnType<typeof vi.fn> { |
| 18 | + const mock = vi.fn(async (url: string, init?: RequestInit) => { |
| 19 | + const r = handler(url, init); |
| 20 | + return { |
| 21 | + ok: r.ok, |
| 22 | + status: r.status ?? (r.ok ? 200 : 500), |
| 23 | + json: async () => r.body ?? {}, |
| 24 | + }; |
| 25 | + }); |
| 26 | + vi.stubGlobal("fetch", mock); |
| 27 | + return mock; |
| 28 | +} |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + stubFetch(() => ({ ok: true, body: { content: "" } })); |
| 32 | +}); |
| 33 | + |
| 34 | +afterEach(() => { |
| 35 | + vi.unstubAllGlobals(); |
| 36 | +}); |
| 37 | + |
| 38 | +// ── read() ──────────────────────────────────────────────────────────────────── |
| 39 | + |
| 40 | +describe("read()", () => { |
| 41 | + it("fetches the correct URL with ?optional=1", async () => { |
| 42 | + const mock = stubFetch(() => ({ ok: true, body: { content: "<html/>" } })); |
| 43 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 44 | + await adapter.read("comp.html"); |
| 45 | + expect(mock).toHaveBeenCalledWith( |
| 46 | + `${BASE}/files/${encodeURIComponent("comp.html")}?optional=1`, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns content on success", async () => { |
| 51 | + stubFetch(() => ({ ok: true, body: { content: "<html>hello</html>" } })); |
| 52 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 53 | + expect(await adapter.read("comp.html")).toBe("<html>hello</html>"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns undefined when response body lacks content field", async () => { |
| 57 | + stubFetch(() => ({ ok: true, body: {} })); |
| 58 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 59 | + expect(await adapter.read("missing.html")).toBeUndefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("returns undefined on non-ok response", async () => { |
| 63 | + stubFetch(() => ({ ok: false, status: 404 })); |
| 64 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 65 | + expect(await adapter.read("gone.html")).toBeUndefined(); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +// ── write() ─────────────────────────────────────────────────────────────────── |
| 70 | + |
| 71 | +describe("write()", () => { |
| 72 | + it("PUTs to the correct URL with text/plain body", async () => { |
| 73 | + const mock = stubFetch(() => ({ ok: true })); |
| 74 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 75 | + await adapter.write("comp.html", "<html>new</html>"); |
| 76 | + expect(mock).toHaveBeenCalledWith( |
| 77 | + `${BASE}/files/${encodeURIComponent("comp.html")}`, |
| 78 | + expect.objectContaining({ |
| 79 | + method: "PUT", |
| 80 | + headers: expect.objectContaining({ "Content-Type": "text/plain" }), |
| 81 | + body: "<html>new</html>", |
| 82 | + }), |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("fires persist:error on non-ok response without throwing", async () => { |
| 87 | + stubFetch(() => ({ ok: false, status: 503 })); |
| 88 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 89 | + const onError = vi.fn(); |
| 90 | + adapter.on("persist:error", onError); |
| 91 | + await expect(adapter.write("comp.html", "x")).resolves.toBeUndefined(); |
| 92 | + expect(onError).toHaveBeenCalledWith( |
| 93 | + expect.objectContaining({ error: expect.objectContaining({ message: "HTTP 503" }) }), |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("fires persist:error on network error without throwing", async () => { |
| 98 | + vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new TypeError("network down"))); |
| 99 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 100 | + const onError = vi.fn(); |
| 101 | + adapter.on("persist:error", onError); |
| 102 | + await expect(adapter.write("comp.html", "x")).resolves.toBeUndefined(); |
| 103 | + expect(onError).toHaveBeenCalledWith( |
| 104 | + expect.objectContaining({ |
| 105 | + error: expect.objectContaining({ message: expect.stringContaining("network down") }), |
| 106 | + }), |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it("does not fire persist:error on success", async () => { |
| 111 | + stubFetch(() => ({ ok: true })); |
| 112 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 113 | + const onError = vi.fn(); |
| 114 | + adapter.on("persist:error", onError); |
| 115 | + await adapter.write("comp.html", "x"); |
| 116 | + expect(onError).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +// ── flush() ─────────────────────────────────────────────────────────────────── |
| 121 | + |
| 122 | +describe("flush()", () => { |
| 123 | + it("resolves immediately when no writes are in flight", async () => { |
| 124 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 125 | + await expect(adapter.flush()).resolves.toBeUndefined(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("waits for an in-flight write before resolving", async () => { |
| 129 | + let resolveFetch!: () => void; |
| 130 | + vi.stubGlobal( |
| 131 | + "fetch", |
| 132 | + vi.fn().mockImplementation(async (_url: string, init?: RequestInit) => { |
| 133 | + if (init?.method === "PUT") { |
| 134 | + await new Promise<void>((r) => { |
| 135 | + resolveFetch = r; |
| 136 | + }); |
| 137 | + } |
| 138 | + return { ok: true, status: 200, json: async () => ({}) }; |
| 139 | + }), |
| 140 | + ); |
| 141 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 142 | + void adapter.write("comp.html", "x"); // intentionally not awaited |
| 143 | + let flushed = false; |
| 144 | + const flushDone = adapter.flush().then(() => { |
| 145 | + flushed = true; |
| 146 | + }); |
| 147 | + expect(flushed).toBe(false); |
| 148 | + resolveFetch(); |
| 149 | + await flushDone; |
| 150 | + expect(flushed).toBe(true); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +// ── listVersions() / loadFrom() ─────────────────────────────────────────────── |
| 155 | + |
| 156 | +describe("listVersions()", () => { |
| 157 | + it("returns empty array (server versioning not exposed by this adapter)", async () => { |
| 158 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 159 | + expect(await adapter.listVersions("comp.html")).toEqual([]); |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +describe("loadFrom()", () => { |
| 164 | + it("returns undefined (server versioning not exposed by this adapter)", async () => { |
| 165 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 166 | + expect(await adapter.loadFrom("comp.html", "v1")).toBeUndefined(); |
| 167 | + }); |
| 168 | +}); |
| 169 | + |
| 170 | +// ── on() / unsubscribe ──────────────────────────────────────────────────────── |
| 171 | + |
| 172 | +describe("on() / unsubscribe", () => { |
| 173 | + it("unsubscribe removes the listener", async () => { |
| 174 | + stubFetch(() => ({ ok: false, status: 500 })); |
| 175 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 176 | + const onError = vi.fn(); |
| 177 | + const unsub = adapter.on("persist:error", onError); |
| 178 | + unsub(); |
| 179 | + await adapter.write("comp.html", "x"); |
| 180 | + expect(onError).not.toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + |
| 183 | + it("multiple listeners all fire", async () => { |
| 184 | + stubFetch(() => ({ ok: false, status: 500 })); |
| 185 | + const adapter = createHttpAdapter({ projectFilesUrl: BASE }); |
| 186 | + const a = vi.fn(); |
| 187 | + const b = vi.fn(); |
| 188 | + adapter.on("persist:error", a); |
| 189 | + adapter.on("persist:error", b); |
| 190 | + await adapter.write("comp.html", "x"); |
| 191 | + expect(a).toHaveBeenCalledOnce(); |
| 192 | + expect(b).toHaveBeenCalledOnce(); |
| 193 | + }); |
| 194 | +}); |
0 commit comments