"use strict";
var import_vitest = require("vitest");
var import_concurrency_utils = require("./concurrency-utils");
const TIMEOUT_MS = 100;
(0, import_vitest.describe)("debounceAsync", () => {
  (0, import_vitest.beforeEach)(() => {
    import_vitest.vi.useFakeTimers();
  });
  (0, import_vitest.afterEach)(async () => {
    import_vitest.vi.useRealTimers();
    import_vitest.vi.restoreAllMocks();
  });
  (0, import_vitest.it)("should not execute function before delay", async () => {
    const fn = import_vitest.vi.fn().mockResolvedValue("result");
    const debouncedFn = (0, import_concurrency_utils.debounceAsync)(fn, TIMEOUT_MS);
    debouncedFn("a");
    await import_vitest.vi.advanceTimersByTimeAsync(TIMEOUT_MS - 1);
    (0, import_vitest.expect)(fn).not.toHaveBeenCalled();
  });
  (0, import_vitest.it)("should execute function only once after delay", async () => {
    const fn = import_vitest.vi.fn().mockResolvedValue("result");
    const debouncedFn = (0, import_concurrency_utils.debounceAsync)(fn, TIMEOUT_MS);
    debouncedFn("a");
    debouncedFn("b");
    await import_vitest.vi.advanceTimersByTimeAsync(TIMEOUT_MS - 1);
    debouncedFn("c");
    await import_vitest.vi.advanceTimersByTimeAsync(TIMEOUT_MS);
    (0, import_vitest.expect)(fn).toHaveBeenCalledTimes(1);
    (0, import_vitest.expect)(fn).toHaveBeenCalledWith("c");
  });
  (0, import_vitest.it)("should preserve `this` context", async () => {
    const context = { value: 42 };
    const fn = import_vitest.vi.fn(function() {
      return Promise.resolve(this.value);
    });
    const debouncedFn = (0, import_concurrency_utils.debounceAsync)(fn, TIMEOUT_MS);
    const promise = debouncedFn.call(context);
    await import_vitest.vi.advanceTimersByTimeAsync(TIMEOUT_MS);
    await (0, import_vitest.expect)(promise).resolves.toBe(42);
  });
  (0, import_vitest.it)("should handle rejections", async () => {
    const error = new Error("test rejection");
    const fn = import_vitest.vi.fn().mockRejectedValue(error);
    const debouncedFn = (0, import_concurrency_utils.debounceAsync)(fn, 1);
    import_vitest.vi.useRealTimers();
    await (0, import_vitest.expect)(debouncedFn).rejects.toThrow(error);
  });
});
//# sourceMappingURL=concurrency-utils.test.js.map
