import { expect, test, vi } from "vitest" import { CoordinationAbortError, KeyedLock } from "#Source/orchestration/index.ts" test("KeyedLock isolates different keys and cleans state after release", () => { const keyedLock = new KeyedLock() expect(keyedLock.getKeyCount()).toBe(0) expect(keyedLock.hasKey("alpha")).toBe(false) const alphaPermit = keyedLock.tryAcquire("alpha") const betaPermit = keyedLock.tryAcquire("beta") expect(alphaPermit?.details).toEqual({ coordination: "keyed-lock", key: "alpha" }) expect(betaPermit?.details).toEqual({ coordination: "keyed-lock", key: "beta" }) expect(keyedLock.tryAcquire("alpha")).toBeUndefined() expect(keyedLock.hasKey("alpha")).toBe(true) expect(keyedLock.hasKey("beta")).toBe(true) expect(keyedLock.getKeyCount()).toBe(2) alphaPermit?.release() betaPermit?.release() expect(keyedLock.hasKey("alpha")).toBe(false) expect(keyedLock.hasKey("beta")).toBe(false) expect(keyedLock.getKeyCount()).toBe(0) }) test("KeyedLock acquire cleans aborted waiters and releases state when idle", async () => { const keyedLock = new KeyedLock() const blockingPermit = keyedLock.tryAcquire("alpha") const abortController = new AbortController() expect(blockingPermit).toBeDefined() const waitingPermitPromise = keyedLock.acquire("alpha", { abortSignal: abortController.signal }) expect(keyedLock.hasKey("alpha")).toBe(true) expect(keyedLock.getKeyCount()).toBe(1) abortController.abort("cancelled") await expect(waitingPermitPromise).rejects.toBeInstanceOf(CoordinationAbortError) expect(keyedLock.hasKey("alpha")).toBe(true) blockingPermit?.release() expect(keyedLock.hasKey("alpha")).toBe(false) expect(keyedLock.getKeyCount()).toBe(0) }) test("KeyedLock runExclusive serializes callbacks for the same key", async () => { const keyedLock = new KeyedLock() let activeCount = 0 let peakCount = 0 await Promise.all([ keyedLock.runExclusive("alpha", async () => { activeCount = activeCount + 1 peakCount = Math.max(peakCount, activeCount) await Promise.resolve() activeCount = activeCount - 1 }), keyedLock.runExclusive("alpha", async () => { activeCount = activeCount + 1 peakCount = Math.max(peakCount, activeCount) await Promise.resolve() activeCount = activeCount - 1 }), ]) expect(peakCount).toBe(1) expect(keyedLock.hasKey("alpha")).toBe(false) }) test("KeyedLock duplicate release stays silent by default and uses custom handler when provided", () => { const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => { // no-op to silence warnings during test }) const silentKeyedLock = new KeyedLock() const silentPermit = silentKeyedLock.tryAcquire("alpha") expect(silentPermit).toBeDefined() silentPermit?.release() silentPermit?.release() silentPermit?.release() expect(silentKeyedLock.hasKey("alpha")).toBe(false) expect(warnSpy).not.toHaveBeenCalled() const onDuplicateRelease = vi.fn() const customKeyedLock = new KeyedLock({ onDuplicateRelease }) const customPermit = customKeyedLock.tryAcquire("alpha") expect(customPermit).toBeDefined() customPermit?.release() customPermit?.release() customPermit?.release() expect(customKeyedLock.hasKey("alpha")).toBe(false) expect(onDuplicateRelease).toHaveBeenCalledTimes(2) expect(onDuplicateRelease).toHaveBeenCalledWith( "KeyedLock permit release was called more than once.", ) expect(warnSpy).not.toHaveBeenCalled() warnSpy.mockRestore() })