|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { discoverLottieResponse, LottieDiscovery } from "./lottieDiscovery.js"; |
| 3 | + |
| 4 | +describe("bounded Lottie discovery", () => { |
| 5 | + it("awaits delayed candidates and ignores response events after its save boundary", async () => { |
| 6 | + const discovery = new LottieDiscovery(); |
| 7 | + const response = { url: () => "https://public.example/a.json", headers: () => ({}) }; |
| 8 | + discovery.collect(response); |
| 9 | + discovery.collect(response); |
| 10 | + let finish!: (response: Response) => void; |
| 11 | + const fetchMock = vi.fn( |
| 12 | + () => |
| 13 | + new Promise<Response>((resolve) => { |
| 14 | + finish = resolve; |
| 15 | + }), |
| 16 | + ); |
| 17 | + vi.stubGlobal("fetch", fetchMock); |
| 18 | + const budget = { remainingBytes: 1000 }; |
| 19 | + let settled = false; |
| 20 | + const pending = discovery |
| 21 | + .run(budget, () => 10000) |
| 22 | + .then((result) => { |
| 23 | + settled = true; |
| 24 | + return result; |
| 25 | + }); |
| 26 | + await Promise.resolve(); |
| 27 | + expect(settled).toBe(false); |
| 28 | + finish( |
| 29 | + new Response(JSON.stringify({ v: "5", w: 100, h: 100, fr: 30, ip: 0, op: 30, layers: [] })), |
| 30 | + ); |
| 31 | + expect(await pending).toHaveLength(1); |
| 32 | + const remaining = budget.remainingBytes; |
| 33 | + discovery.collect({ url: () => "https://public.example/late.json", headers: () => ({}) }); |
| 34 | + expect(await discovery.run(budget, () => 10000)).toEqual([]); |
| 35 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 36 | + expect(budget.remainingBytes).toBe(remaining); |
| 37 | + }); |
| 38 | + |
| 39 | + it("caps ordinary JSON candidates and their aggregate byte consumption", async () => { |
| 40 | + const discovery = new LottieDiscovery(); |
| 41 | + for (let n = 0; n < 100; n++) |
| 42 | + discovery.collect({ |
| 43 | + url: () => `https://public.example/api/${n}`, |
| 44 | + headers: () => ({ "content-type": "application/json" }), |
| 45 | + }); |
| 46 | + const fetchMock = vi.fn(async (_url: string | URL | Request) => new Response("{}")); |
| 47 | + vi.stubGlobal("fetch", fetchMock); |
| 48 | + const budget = { remainingBytes: 1000 }; |
| 49 | + expect(await discovery.run(budget, () => 10000)).toEqual([]); |
| 50 | + expect(fetchMock).toHaveBeenCalledTimes(32); |
| 51 | + expect(budget.remainingBytes).toBe(936); |
| 52 | + }); |
| 53 | + it("retains explicit JSON when a later archive displaces a generic candidate", async () => { |
| 54 | + const discovery = new LottieDiscovery(); |
| 55 | + discovery.collect({ url: () => "https://public.example/real.json", headers: () => ({}) }); |
| 56 | + for (let n = 0; n < 31; n++) |
| 57 | + discovery.collect({ |
| 58 | + url: () => `https://public.example/api/${n}`, |
| 59 | + headers: () => ({ "content-type": "application/json" }), |
| 60 | + }); |
| 61 | + discovery.collect({ url: () => "https://public.example/anim.lottie", headers: () => ({}) }); |
| 62 | + const fetchMock = vi.fn(async (_url: string | URL | Request) => new Response("{}")); |
| 63 | + vi.stubGlobal("fetch", fetchMock); |
| 64 | + await discovery.run({ remainingBytes: 1000 }, () => 10000); |
| 65 | + const urls = fetchMock.mock.calls.map((call) => call[0]); |
| 66 | + expect(urls).toContain("https://public.example/real.json"); |
| 67 | + expect(urls).not.toContain("https://public.example/api/0"); |
| 68 | + expect(urls).toHaveLength(31); |
| 69 | + }); |
| 70 | + afterEach(() => vi.unstubAllGlobals()); |
| 71 | + it.each([undefined, "1"])( |
| 72 | + "does not trust intercepted Content-Length %s or materialize Puppeteer bodies", |
| 73 | + async (length) => { |
| 74 | + const buffer = vi.fn(() => { |
| 75 | + throw new Error("unbounded body read"); |
| 76 | + }); |
| 77 | + const response = { |
| 78 | + url: () => "https://public.example/animation.json", |
| 79 | + headers: () => ({ |
| 80 | + "content-type": "application/json", |
| 81 | + ...(length ? { "content-length": length } : {}), |
| 82 | + }), |
| 83 | + buffer, |
| 84 | + }; |
| 85 | + const cancel = vi.fn(); |
| 86 | + vi.stubGlobal( |
| 87 | + "fetch", |
| 88 | + vi.fn( |
| 89 | + async () => |
| 90 | + new Response( |
| 91 | + new ReadableStream({ |
| 92 | + pull(controller) { |
| 93 | + controller.enqueue(new Uint8Array(4)); |
| 94 | + }, |
| 95 | + cancel, |
| 96 | + }), |
| 97 | + { headers: length ? { "content-length": length } : {} }, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ); |
| 101 | + expect(await discoverLottieResponse(response, { remainingBytes: 5 })).toBeNull(); |
| 102 | + expect(buffer).not.toHaveBeenCalled(); |
| 103 | + expect(cancel).toHaveBeenCalledOnce(); |
| 104 | + }, |
| 105 | + ); |
| 106 | + it("retains valid discovered JSON with its existing byte accounting", async () => { |
| 107 | + const data = { v: "5.12.2", w: 100, h: 100, layers: [], fr: 30, ip: 0, op: 30 }; |
| 108 | + const body = JSON.stringify(data); |
| 109 | + vi.stubGlobal( |
| 110 | + "fetch", |
| 111 | + vi.fn(async () => new Response(body)), |
| 112 | + ); |
| 113 | + const budget = { remainingBytes: body.length }; |
| 114 | + const found = await discoverLottieResponse( |
| 115 | + { url: () => "https://public.example/animation.json?version=1", headers: () => ({}) }, |
| 116 | + budget, |
| 117 | + ); |
| 118 | + expect(found?.data).toEqual(data); |
| 119 | + expect(found?.dataBudget).toBe(budget); |
| 120 | + expect(budget.remainingBytes).toBe(0); |
| 121 | + }); |
| 122 | +}); |
0 commit comments