import{getIconLibrary,registerIconLibrary,unregisterIconLibrary,watchIcon,unwatchIcon}from"../library";const REGISTRY_KEY="scIconLibraries",noopResolver=e=>`${e}.svg`;describe("icon library registry",(()=>{const e=[];beforeEach((()=>{delete window[REGISTRY_KEY]})),afterEach((()=>{for(;e.length;)unwatchIcon(e.pop());delete window[REGISTRY_KEY]})),describe("register / unregister",(()=>{it("registers a library and getIconLibrary returns it by name",(()=>{registerIconLibrary("default",{resolver:noopResolver});const e=getIconLibrary("default");expect(null==e?void 0:e.name).toBe("default"),expect(null==e?void 0:e.resolver("plus")).toBe("plus.svg")})),it("re-registering the same name replaces instead of duplicating",(()=>{var e;registerIconLibrary("default",{resolver:()=>"first"}),registerIconLibrary("default",{resolver:()=>"second"}),expect(window[REGISTRY_KEY]).toHaveLength(1),expect(null===(e=getIconLibrary("default"))||void 0===e?void 0:e.resolver("x")).toBe("second")})),it("unregisterIconLibrary removes only the named entry",(()=>{registerIconLibrary("default",{resolver:noopResolver}),registerIconLibrary("admin",{resolver:noopResolver}),unregisterIconLibrary("default"),expect(getIconLibrary("default")).toBeUndefined(),expect(getIconLibrary("admin")).toBeDefined()}))})),describe("watcher redraw lifecycle",(()=>{it("does not fire redraw for watchers of a different library",(()=>{const r=jest.fn();!function(r,o){const t={library:"admin",redraw:o};watchIcon(t),e.push(t)}(0,r),registerIconLibrary("default",{resolver:noopResolver}),expect(r).not.toHaveBeenCalled()})),it("does not fire redraw after unwatchIcon",(()=>{const e=jest.fn(),r={library:"default",redraw:e};watchIcon(r),unwatchIcon(r),registerIconLibrary("default",{resolver:noopResolver}),expect(e).not.toHaveBeenCalled()}))})),describe("collision safety",(()=>{it.each([["non-array object",{stolen:!0}],["null",null],["number",42],["string","hijacked"]])("getIconLibrary returns undefined when scIconLibraries is %s",((e,r)=>{window[REGISTRY_KEY]=r,expect((()=>getIconLibrary("default"))).not.toThrow(),expect(getIconLibrary("default")).toBeUndefined()})),it("getIconLibrary survives a hostile proxy that throws on access",(()=>{window[REGISTRY_KEY]=new Proxy([],{get(e,r){if("find"===r||"filter"===r)throw new Error("hostile proxy");return Reflect.get(e,r)}}),expect((()=>getIconLibrary("default"))).not.toThrow(),expect(getIconLibrary("default")).toBeUndefined()})),it("registerIconLibrary recovers from a non-array collision",(()=>{var e;window[REGISTRY_KEY]={stolen:!0},registerIconLibrary("default",{resolver:noopResolver}),expect(Array.isArray(window[REGISTRY_KEY])).toBe(!0),expect(null===(e=getIconLibrary("default"))||void 0===e?void 0:e.name).toBe("default")}))}))}));