import { afterEach, describe, expect, it, vi } from "vitest"; import { decodeQrImage } from "../../src/qr/decoder"; describe("QR image decoding", () => { afterEach(() => vi.unstubAllGlobals()); it("uses native QR detection when available and closes the bitmap", async () => { const close = vi.fn(); vi.stubGlobal( "createImageBitmap", vi.fn().mockResolvedValue({ width: 128, height: 128, close }), ); vi.stubGlobal( "BarcodeDetector", class { async detect() { return [ { rawValue: "otpauth://totp/Example?secret=JBSWY3DPEHPK3PXP" }, ]; } }, ); await expect( decodeQrImage(new File([new Uint8Array(10)], "qr.png")), ).resolves.toMatch(/^otpauth:/u); expect(close).toHaveBeenCalledOnce(); }); it("rejects oversized dimensions before pixel extraction", async () => { const close = vi.fn(); vi.stubGlobal( "createImageBitmap", vi.fn().mockResolvedValue({ width: 4097, height: 1, close }), ); await expect( decodeQrImage(new File([new Uint8Array(10)], "huge.png")), ).rejects.toThrow(/dimensions/iu); expect(close).toHaveBeenCalledOnce(); }); });