"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var import_vitest = require("vitest");
var import_blueprint = require("../../../dom-analyzer/blueprint");
var import_blueprintCache = require("../../../dom-analyzer/blueprintCache");
var import_pageBlueprint = __toESM(require("./pageBlueprint"));
import_vitest.vi.mock("../../../dom-analyzer/blueprint", async () => {
  const actual = await import_vitest.vi.importActual("../../../dom-analyzer/blueprint");
  return {
    ...actual,
    buildPageBlueprint: import_vitest.vi.fn()
  };
});
const blueprintTool = import_pageBlueprint.default.find((t) => t.schema.name === "browser_blueprint");
function fakeBp(url, pageHash, sections = []) {
  return {
    schemaVersion: 1,
    url,
    capturedAt: "2026-05-16T00:00:00.000Z",
    pageHash,
    sections
  };
}
function mkTab(url) {
  return {
    page: { url: () => url },
    blueprintCache: new import_blueprintCache.BlueprintCache(5),
    modalStates: () => []
  };
}
function mkContext(tab) {
  return {
    ensureTab: async () => tab
  };
}
function mkResponse() {
  const calls = [];
  return {
    addTextResult: (s) => calls.push(s),
    addError: (s) => calls.push("ERR:" + s),
    setIncludeSnapshot: () => {
    },
    addCode: (_s) => {
    },
    _calls: calls
  };
}
(0, import_vitest.beforeEach)(() => {
  import_vitest.vi.clearAllMocks();
});
(0, import_vitest.describe)("browser_blueprint cache", () => {
  (0, import_vitest.it)("first call at a URL returns isFullCapture: true with the blueprint", async () => {
    const tab = mkTab("http://a/orders/1");
    const ctx = mkContext(tab);
    const bp = fakeBp("http://a/orders/1", "h1");
    import_blueprint.buildPageBlueprint.mockResolvedValueOnce(bp);
    const resp = mkResponse();
    await blueprintTool.handle(ctx, {}, resp);
    (0, import_vitest.expect)(resp._calls).toHaveLength(1);
    const out = JSON.parse(resp._calls[0]);
    (0, import_vitest.expect)(out.isFullCapture).toBe(true);
    (0, import_vitest.expect)(out.pageHash).toBe("h1");
    (0, import_vitest.expect)(out.blueprint).toEqual(bp);
    (0, import_vitest.expect)(out.possibleAssertions).toEqual(import_vitest.expect.any(Array));
  });
  (0, import_vitest.it)("second call at same URL returns isFullCapture: false with a delta", async () => {
    const tab = mkTab("http://a/orders/1");
    const ctx = mkContext(tab);
    const bp1 = fakeBp("http://a/orders/1", "h1");
    const bp2 = fakeBp("http://a/orders/1", "h2");
    import_blueprint.buildPageBlueprint.mockResolvedValueOnce(bp1).mockResolvedValueOnce(bp2);
    await blueprintTool.handle(ctx, {}, mkResponse());
    const r2 = mkResponse();
    await blueprintTool.handle(ctx, {}, r2);
    const out2 = JSON.parse(r2._calls[0]);
    (0, import_vitest.expect)(out2.isFullCapture).toBe(false);
    (0, import_vitest.expect)(out2.pageHash).toBe("h2");
    (0, import_vitest.expect)(out2.previousPageHash).toBe("h1");
    (0, import_vitest.expect)(out2.delta).toMatchObject({
      pageHashChange: { before: "h1", after: "h2" },
      hasStructuralChange: import_vitest.expect.any(Boolean),
      elementsAdded: import_vitest.expect.any(Array),
      elementsRemoved: import_vitest.expect.any(Array),
      textChanges: import_vitest.expect.any(Array)
    });
    (0, import_vitest.expect)(out2.possibleAssertions).toEqual(import_vitest.expect.any(Array));
  });
  (0, import_vitest.it)("updates stored entry so third call diffs against second", async () => {
    const tab = mkTab("http://a/orders/1");
    const ctx = mkContext(tab);
    const bp1 = fakeBp("http://a/orders/1", "h1");
    const bp2 = fakeBp("http://a/orders/1", "h2");
    const bp3 = fakeBp("http://a/orders/1", "h3");
    import_blueprint.buildPageBlueprint.mockResolvedValueOnce(bp1).mockResolvedValueOnce(bp2).mockResolvedValueOnce(bp3);
    await blueprintTool.handle(ctx, {}, mkResponse());
    await blueprintTool.handle(ctx, {}, mkResponse());
    const r3 = mkResponse();
    await blueprintTool.handle(ctx, {}, r3);
    const out3 = JSON.parse(r3._calls[0]);
    (0, import_vitest.expect)(out3.isFullCapture).toBe(false);
    (0, import_vitest.expect)(out3.previousPageHash).toBe("h2");
    (0, import_vitest.expect)(out3.pageHash).toBe("h3");
    (0, import_vitest.expect)(out3.possibleAssertions).toEqual(import_vitest.expect.any(Array));
  });
  (0, import_vitest.it)("different URL gets its own full capture", async () => {
    const tab = mkTab("http://a/orders/1");
    const ctx = mkContext(tab);
    const bp1 = fakeBp("http://a/orders/1", "h1");
    const bp2 = fakeBp("http://a/orders/2", "g1");
    import_blueprint.buildPageBlueprint.mockResolvedValueOnce(bp1).mockResolvedValueOnce(bp2);
    await blueprintTool.handle(ctx, {}, mkResponse());
    tab.page.url = () => "http://a/orders/2";
    const r2 = mkResponse();
    await blueprintTool.handle(ctx, {}, r2);
    const out2 = JSON.parse(r2._calls[0]);
    (0, import_vitest.expect)(out2.isFullCapture).toBe(true);
    (0, import_vitest.expect)(out2.blueprint).toEqual(bp2);
  });
});
