"use strict";
var import_vitest = require("vitest");
var import_blueprintCache = require("./blueprintCache");
function bp(url, pageHash) {
  return {
    schemaVersion: 1,
    url,
    capturedAt: (/* @__PURE__ */ new Date()).toISOString(),
    pageHash,
    sections: []
  };
}
(0, import_vitest.describe)("BlueprintCache", () => {
  (0, import_vitest.it)("returns undefined for unknown url", () => {
    const c = new import_blueprintCache.BlueprintCache(5);
    (0, import_vitest.expect)(c.get("http://a/")).toBeUndefined();
  });
  (0, import_vitest.it)("round-trips a single entry", () => {
    const c = new import_blueprintCache.BlueprintCache(5);
    const b = bp("http://a/", "h1");
    c.put("http://a/", b);
    (0, import_vitest.expect)(c.get("http://a/")).toBe(b);
  });
  (0, import_vitest.it)("overwrites on repeated put for the same url", () => {
    const c = new import_blueprintCache.BlueprintCache(5);
    const b1 = bp("http://a/", "h1");
    const b2 = bp("http://a/", "h2");
    c.put("http://a/", b1);
    c.put("http://a/", b2);
    (0, import_vitest.expect)(c.get("http://a/")).toBe(b2);
  });
  (0, import_vitest.it)("evicts least-recently-used entry once max is exceeded", () => {
    const c = new import_blueprintCache.BlueprintCache(2);
    c.put("http://a/", bp("http://a/", "ha"));
    c.put("http://b/", bp("http://b/", "hb"));
    c.put("http://c/", bp("http://c/", "hc"));
    (0, import_vitest.expect)(c.get("http://a/")).toBeUndefined();
    (0, import_vitest.expect)(c.get("http://b/")).toBeDefined();
    (0, import_vitest.expect)(c.get("http://c/")).toBeDefined();
  });
  (0, import_vitest.it)("counts get as a use for LRU ordering", () => {
    const c = new import_blueprintCache.BlueprintCache(2);
    c.put("http://a/", bp("http://a/", "ha"));
    c.put("http://b/", bp("http://b/", "hb"));
    c.get("http://a/");
    c.put("http://c/", bp("http://c/", "hc"));
    (0, import_vitest.expect)(c.get("http://a/")).toBeDefined();
    (0, import_vitest.expect)(c.get("http://b/")).toBeUndefined();
    (0, import_vitest.expect)(c.get("http://c/")).toBeDefined();
  });
  (0, import_vitest.it)("clear() empties the cache", () => {
    const c = new import_blueprintCache.BlueprintCache(2);
    c.put("http://a/", bp("http://a/", "ha"));
    c.clear();
    (0, import_vitest.expect)(c.get("http://a/")).toBeUndefined();
  });
});
(0, import_vitest.describe)("resolveBlueprintCacheSize", () => {
  let originalEnv;
  let warnSpy;
  (0, import_vitest.beforeEach)(() => {
    originalEnv = process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE;
    warnSpy = import_vitest.vi.spyOn(console, "warn").mockImplementation(() => {
    });
  });
  (0, import_vitest.afterEach)(() => {
    if (originalEnv === void 0)
      delete process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE;
    else
      process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = originalEnv;
    warnSpy.mockRestore();
  });
  (0, import_vitest.it)("returns the default when env var is unset", () => {
    delete process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE;
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(import_blueprintCache.DEFAULT_BLUEPRINT_CACHE_SIZE);
  });
  (0, import_vitest.it)("returns the default when env var is empty", () => {
    process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = "";
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(import_blueprintCache.DEFAULT_BLUEPRINT_CACHE_SIZE);
  });
  (0, import_vitest.it)("parses a valid positive integer override", () => {
    process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = "25";
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(25);
  });
  (0, import_vitest.it)("parses minimum value 1", () => {
    process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = "1";
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(1);
  });
  (0, import_vitest.it)("warns and falls back to default for non-numeric values", () => {
    process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = "not-a-number";
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(import_blueprintCache.DEFAULT_BLUEPRINT_CACHE_SIZE);
    (0, import_vitest.expect)(warnSpy).toHaveBeenCalledOnce();
  });
  (0, import_vitest.it)("warns and falls back to default for zero", () => {
    process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = "0";
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(import_blueprintCache.DEFAULT_BLUEPRINT_CACHE_SIZE);
    (0, import_vitest.expect)(warnSpy).toHaveBeenCalledOnce();
  });
  (0, import_vitest.it)("warns and falls back to default for negative values", () => {
    process.env.SKYRAMP_BLUEPRINT_CACHE_SIZE = "-5";
    (0, import_vitest.expect)((0, import_blueprintCache.resolveBlueprintCacheSize)()).toBe(import_blueprintCache.DEFAULT_BLUEPRINT_CACHE_SIZE);
    (0, import_vitest.expect)(warnSpy).toHaveBeenCalledOnce();
  });
});
