"use strict";
var import_vitest = require("vitest");
var import_utils = require("./utils.cjs");
class Entry {
  createdAt;
  totalDurationInS = void 0;
  predictionDurationInS = void 0;
  note = void 0;
  constructor(createdAt, note) {
    this.createdAt = createdAt;
    this.note = note;
  }
}
(0, import_vitest.describe)("BoundedCache", () => {
  (0, import_vitest.it)("evicts oldest entry when maxLen is exceeded", () => {
    const cache = new import_utils.BoundedCache(2);
    cache.set(1, new Entry(1));
    cache.set(2, new Entry(2));
    cache.set(3, new Entry(3));
    (0, import_vitest.expect)(cache.size).toBe(2);
    (0, import_vitest.expect)([...cache.keys()]).toEqual([2, 3]);
    (0, import_vitest.expect)(cache.get(1)).toBeUndefined();
    (0, import_vitest.expect)(cache.get(2).createdAt).toBe(2);
    (0, import_vitest.expect)(cache.get(3).createdAt).toBe(3);
  });
  (0, import_vitest.it)("setOrUpdate creates a value via factory when key is missing", () => {
    var _a;
    const cache = new import_utils.BoundedCache(10);
    const factory = import_vitest.vi.fn(() => new Entry(100));
    const value = cache.setOrUpdate(1, factory, { predictionDurationInS: 0.42 });
    (0, import_vitest.expect)(factory).toHaveBeenCalledTimes(1);
    (0, import_vitest.expect)(value.createdAt).toBe(100);
    (0, import_vitest.expect)(value.predictionDurationInS).toBe(0.42);
    (0, import_vitest.expect)((_a = cache.get(1)) == null ? void 0 : _a.predictionDurationInS).toBe(0.42);
  });
  (0, import_vitest.it)("setOrUpdate updates existing value and does not call factory", () => {
    const cache = new import_utils.BoundedCache(10);
    cache.set(1, new Entry(1, "before"));
    const factory = import_vitest.vi.fn(() => new Entry(999));
    const value = cache.setOrUpdate(1, factory, { note: "after", totalDurationInS: 1.5 });
    (0, import_vitest.expect)(factory).not.toHaveBeenCalled();
    (0, import_vitest.expect)(value.createdAt).toBe(1);
    (0, import_vitest.expect)(value.note).toBe("after");
    (0, import_vitest.expect)(value.totalDurationInS).toBe(1.5);
  });
  (0, import_vitest.it)("updateValue returns undefined for missing key", () => {
    const cache = new import_utils.BoundedCache(10);
    const result = cache.updateValue(404, { note: "missing" });
    (0, import_vitest.expect)(result).toBeUndefined();
  });
  (0, import_vitest.it)("updateValue ignores undefined fields", () => {
    const cache = new import_utils.BoundedCache(10);
    cache.set(1, new Entry(1, "keep"));
    const result = cache.updateValue(1, {
      note: void 0,
      predictionDurationInS: 0.1
    });
    (0, import_vitest.expect)(result == null ? void 0 : result.createdAt).toBe(1);
    (0, import_vitest.expect)(result == null ? void 0 : result.note).toBe("keep");
    (0, import_vitest.expect)(result == null ? void 0 : result.predictionDurationInS).toBe(0.1);
  });
  (0, import_vitest.it)("pop without predicate removes the oldest entry", () => {
    const cache = new import_utils.BoundedCache(10);
    cache.set(1, new Entry(1));
    cache.set(2, new Entry(2));
    cache.set(3, new Entry(3));
    const popped = cache.pop();
    (0, import_vitest.expect)(popped == null ? void 0 : popped.createdAt).toBe(1);
    (0, import_vitest.expect)([...cache.keys()]).toEqual([2, 3]);
  });
  (0, import_vitest.it)("pop with predicate removes the most recent matching entry", () => {
    const cache = new import_utils.BoundedCache(10);
    const e1 = new Entry(1);
    e1.totalDurationInS = 0;
    const e2 = new Entry(2);
    e2.totalDurationInS = 1;
    const e3 = new Entry(3);
    e3.totalDurationInS = 2;
    cache.set(1, e1);
    cache.set(2, e2);
    cache.set(3, e3);
    const popped = cache.pop((entry) => (entry.totalDurationInS ?? 0) > 0);
    (0, import_vitest.expect)(popped == null ? void 0 : popped.createdAt).toBe(3);
    (0, import_vitest.expect)(popped == null ? void 0 : popped.totalDurationInS).toBe(2);
    (0, import_vitest.expect)([...cache.keys()]).toEqual([1, 2]);
  });
  (0, import_vitest.it)("pop with predicate returns undefined when no match exists", () => {
    const cache = new import_utils.BoundedCache(10);
    const e1 = new Entry(1);
    e1.totalDurationInS = 0;
    cache.set(1, e1);
    const popped = cache.pop((entry) => (entry.totalDurationInS ?? 0) > 10);
    (0, import_vitest.expect)(popped).toBeUndefined();
    (0, import_vitest.expect)(cache.size).toBe(1);
  });
  (0, import_vitest.it)("clear removes all entries", () => {
    const cache = new import_utils.BoundedCache(10);
    cache.set(1, new Entry(1));
    cache.set(2, new Entry(2));
    cache.clear();
    (0, import_vitest.expect)(cache.size).toBe(0);
    (0, import_vitest.expect)([...cache.keys()]).toEqual([]);
  });
});
//# sourceMappingURL=utils.test.cjs.map