import { describe, expect, test, vi } from "vitest";
import { z } from "zod";
import { ZEmbeddedData, ZLinkedEmbeddedField } from "./embedded-data";
import { type TLegacyEmbeddedFields, embeddedFieldsFromLegacyInput } from "./embedded-data-mapping";
import {
  RESERVED_FIELD_CATALOG,
  type TClientEmbeddedValueResponse,
  type TEmbeddedFieldsSurvey,
  type TEmbeddedValueRef,
  type TEmbeddedValueResponse,
  type TLinkedEmbeddedField,
  type TReservedFieldCatalogEntry,
  buildEmbeddedLookup,
  coerceToEmbeddedDataType,
  dropShadowedReservedEntries,
  findComputedEmbeddedField,
  getComputedEmbeddedFields,
  getComputedFieldDataType,
  getIngestedEmbeddedFields,
  getIngestedStorageKeys,
  getLogicVariableValue,
  getSurveyEmbeddedFields,
  listDisplayableReservedFields,
  listMidSurveyReservedEntries,
  listReadableFields,
  listShadowingNames,
  mergeReservedValues,
  projectClientReservedValues,
  projectIngestedDefaults,
  projectReservedValues,
  redactUrlQuery,
  resolveEmbeddedValue,
} from "./embedded-data-resolver";
import type { TI18nString } from "./i18n";
import { RESERVED_FIELD_NAMES } from "./reserved-field-names";
import type { TResponseData, TResponseVariables } from "./responses";
import type { TSurveyBlocks } from "./surveys/blocks";
import { TSurveyElementTypeEnum } from "./surveys/elements";

const SCORE_VARIABLE_ID = "clx0000000000000000000v1";
const DUAL_KEY = "clx0000000000000000000v2";

/** A response with every location the read seam can point at populated. */
const response: TEmbeddedValueResponse = {
  id: "clx0000000000000000000r1",
  surveyId: "clx0000000000000000000s1",
  createdAt: new Date("2026-08-01T09:00:00.000Z"),
  updatedAt: new Date("2026-08-01T09:05:00.000Z"),
  finished: true,
  language: "de",
  data: {
    plan: "premium",
    seats: "25",
    rating: 4,
    zero: "0",
    empty: "",
    opted_in: "TRUE",
    signup_date: "2026-08-06",
    bad_date: "banana",
    bad_number: "abc",
    multi: ["a", "b"],
    matrix: { row1: "col1" },
    [DUAL_KEY]: "from-data",
  },
  variables: {
    [SCORE_VARIABLE_ID]: 42,
    [DUAL_KEY]: "from-variables",
  },
  ttc: { q1: 10_000, _total: 30_000 },
  meta: {
    source: "web",
    url: "https://example.com?plan=premium",
    userAgent: { browser: "Chrome", os: "macOS" },
    country: "DE",
  },
};

/** A response where everything optional is absent — the "nothing was captured" case. */
const sparseResponse: TEmbeddedValueResponse = {
  id: "clx0000000000000000000r2",
  surveyId: "clx0000000000000000000s1",
  createdAt: new Date("2026-08-02T12:00:00.000Z"),
  updatedAt: new Date("2026-08-02T12:00:00.000Z"),
  finished: false,
  language: null,
  data: {},
  variables: {},
  meta: {},
};

const makeField = (
  overrides: Partial<TLinkedEmbeddedField["field"]> = {}
): TLinkedEmbeddedField["field"] => ({
  key: null,
  name: "Field",
  source: "ingested",
  dataType: "string",
  defaultValue: null,
  locked: false,
  ...overrides,
});

const resolve = (
  field: TLinkedEmbeddedField["field"],
  storageKey: string,
  onResponse: TEmbeddedValueResponse = response
) => resolveEmbeddedValue({ field, link: { storageKey } }, onResponse);

/**
 * Sample catalog entries — synthetic names on purpose, so the mechanism tests keep exercising the
 * seam rather than accidentally re-asserting the production catalog's contents (which have their own
 * describe block below).
 */
const countryEntry: TReservedFieldCatalogEntry = {
  name: "country",
  dataType: "string",
  availability: "server",
  privacy: "drop",
  display: "none",
  read: (r) => r.meta.country,
};
const browserEntry: TReservedFieldCatalogEntry = {
  name: "browser",
  dataType: "string",
  availability: "server",
  privacy: "drop",
  display: "none",
  read: (r) => r.meta.userAgent?.browser,
};
const totalTimeEntry: TReservedFieldCatalogEntry = {
  name: "total_time",
  dataType: "number",
  availability: "server",
  privacy: "keep",
  display: "none",
  read: (r) => r.ttc?._total,
};
const languageEntry: TReservedFieldCatalogEntry = {
  name: "response_language",
  dataType: "string",
  availability: "both",
  privacy: "keep",
  display: "none",
  read: (r) => r.language,
};
const completedEntry: TReservedFieldCatalogEntry = {
  name: "completed",
  dataType: "boolean",
  availability: "server",
  privacy: "keep",
  display: "none",
  read: (r) => r.finished,
};
const startedAtEntry: TReservedFieldCatalogEntry = {
  name: "started_at",
  dataType: "date",
  availability: "server",
  privacy: "keep",
  display: "none",
  read: (r) => r.createdAt,
};
const actionEntry: TReservedFieldCatalogEntry = {
  name: "action",
  dataType: "string",
  availability: "client",
  privacy: "keep",
  display: "none",
  read: (r) => r.meta.action,
};

describe("resolveEmbeddedValue", () => {
  describe("computed fields read response.variables", () => {
    test("returns the value stored under the link's storageKey", () => {
      const field = makeField({ source: "computed", dataType: "number", defaultValue: 0 });
      expect(resolve(field, SCORE_VARIABLE_ID)).toBe(42);
    });

    test("reads variables, never data, when the same key exists in both", () => {
      const field = makeField({ source: "computed" });
      expect(resolve(field, DUAL_KEY)).toBe("from-variables");
    });

    test("coerces a string stored in variables to the field's number dataType", () => {
      const field = makeField({ source: "computed", dataType: "number" });
      const withStringScore: TEmbeddedValueResponse = {
        ...response,
        variables: { [SCORE_VARIABLE_ID]: "42" },
      };
      expect(resolve(field, SCORE_VARIABLE_ID, withStringScore)).toBe(42);
    });
  });

  describe("ingested fields read response.data", () => {
    test("returns the value stored under the link's storageKey", () => {
      expect(resolve(makeField(), "plan")).toBe("premium");
    });

    test("reads data, never variables, when the same key exists in both", () => {
      expect(resolve(makeField(), DUAL_KEY)).toBe("from-data");
    });

    test("coerces a string answer to the field's number dataType", () => {
      const field = makeField({ dataType: "number" });
      expect(resolve(field, "seats")).toBe(25);
    });

    test('treats "0" as a present number value, not as missing', () => {
      const field = makeField({ dataType: "number", defaultValue: 9 });
      expect(resolve(field, "zero")).toBe(0);
    });

    test("treats an empty string as a present string value, not as missing", () => {
      const field = makeField({ defaultValue: "fallback" });
      expect(resolve(field, "empty")).toBe("");
    });
  });

  describe("reserved catalog entries read through their typed accessor", () => {
    test("resolves a meta field", () => {
      expect(resolveEmbeddedValue({ entry: countryEntry }, response)).toBe("DE");
    });

    test("resolves a nested meta.userAgent field", () => {
      expect(resolveEmbeddedValue({ entry: browserEntry }, response)).toBe("Chrome");
    });

    test("resolves ttc._total", () => {
      expect(resolveEmbeddedValue({ entry: totalTimeEntry }, response)).toBe(30_000);
    });

    test("resolves a top-level field", () => {
      expect(resolveEmbeddedValue({ entry: languageEntry }, response)).toBe("de");
    });

    test("resolves a boolean field, where false is a value and not a gap", () => {
      expect(resolveEmbeddedValue({ entry: completedEntry }, response)).toBe(true);
      expect(resolveEmbeddedValue({ entry: completedEntry }, sparseResponse)).toBe(false);
    });

    test("resolves a Date-backed date field to its ISO string", () => {
      expect(resolveEmbeddedValue({ entry: startedAtEntry }, response)).toBe("2026-08-01T09:00:00.000Z");
    });

    test("returns undefined when the accessor finds nothing (no default tier for reserved)", () => {
      expect(resolveEmbeddedValue({ entry: countryEntry }, sparseResponse)).toBeUndefined();
      expect(resolveEmbeddedValue({ entry: totalTimeEntry }, sparseResponse)).toBeUndefined();
      expect(resolveEmbeddedValue({ entry: languageEntry }, sparseResponse)).toBeUndefined();
    });

    test("returns undefined for a stored row that claims source reserved", () => {
      // ZEmbeddedData rejects such rows; this arm only defends against data that skipped the schema.
      const field = makeField({ source: "reserved", defaultValue: "should-not-surface" });
      expect(resolve(field, "plan")).toBeUndefined();
    });

    test("a throwing accessor reads as missing instead of crashing the resolve", () => {
      const hostnameEntry: TReservedFieldCatalogEntry = {
        name: "hostname",
        dataType: "string",
        availability: "client",
        privacy: "redactQuery",
        display: "none",
        read: (r) => new URL(r.meta.url ?? "").hostname,
      };
      // sparseResponse has no meta.url, so the accessor throws on `new URL("")` — the resolver must
      // treat that like every other dirty input: one field unset, no exception escaping.
      expect(resolveEmbeddedValue({ entry: hostnameEntry }, sparseResponse)).toBeUndefined();
      expect(resolveEmbeddedValue({ entry: hostnameEntry }, response)).toBe("example.com");
    });
  });

  describe("locked ingested fields", () => {
    test("resolve to the default even when data holds a value", () => {
      const field = makeField({ locked: true, defaultValue: "enterprise" });
      expect(resolve(field, "plan")).toBe("enterprise");
    });

    test("resolve to undefined when locked with no default, ignoring the stored value", () => {
      const field = makeField({ locked: true, defaultValue: null });
      expect(resolve(field, "plan")).toBeUndefined();
    });

    test("locked is ignored for computed fields, which never receive outside writes", () => {
      const field = makeField({ source: "computed", dataType: "number", locked: true, defaultValue: 0 });
      expect(resolve(field, SCORE_VARIABLE_ID)).toBe(42);
    });
  });

  describe("missing values fall back to defaultValue, then to undefined", () => {
    test("missing ingested value returns the default", () => {
      const field = makeField({ defaultValue: "fallback" });
      expect(resolve(field, "not-captured")).toBe("fallback");
    });

    test("missing ingested value with a null default returns undefined", () => {
      expect(resolve(makeField(), "not-captured")).toBeUndefined();
    });

    test("missing computed value returns the default", () => {
      const field = makeField({ source: "computed", dataType: "number", defaultValue: 0 });
      expect(resolve(field, "clx0000000000000000000v9")).toBe(0);
    });

    test("an uncoercible stored value is treated as missing and falls to the default", () => {
      const field = makeField({ dataType: "number", defaultValue: 7 });
      expect(resolve(field, "bad_number")).toBe(7);
    });

    test("an uncoercible stored value with a null default returns undefined", () => {
      const field = makeField({ dataType: "number", defaultValue: null });
      expect(resolve(field, "bad_number")).toBeUndefined();
    });

    test("a default that cannot be coerced to the dataType resolves to undefined", () => {
      // The schema forbids a mismatched default, so this only occurs for unparsed input — the
      // resolver still keeps its contract: the result agrees with dataType or is undefined.
      const field = makeField({ dataType: "number", defaultValue: "not-a-number" });
      expect(resolve(field, "not-captured")).toBeUndefined();
    });
  });

  describe("non-scalar response data (shared map with question answers)", () => {
    test("a string[] answer under the storage key is treated as missing", () => {
      const field = makeField({ defaultValue: "fallback" });
      expect(resolve(field, "multi")).toBe("fallback");
    });

    test("a record answer under the storage key is treated as missing", () => {
      expect(resolve(makeField(), "matrix")).toBeUndefined();
    });
  });

  test("a mixed field/link/entry shape is unrepresentable at compile time", () => {
    // This pin is enforced by `tsc` (this package's tsconfig includes *.test.ts, and an unused
    // expect-error directive fails the build), NOT by vitest — no CI workflow currently runs
    // typecheck, so the pin bites on local and agent `pnpm typecheck` runs only.
    const acceptsRef = (ref: TEmbeddedValueRef): TEmbeddedValueRef => ref;
    const mixed = { field: makeField(), link: { storageKey: "plan" }, entry: countryEntry };

    // @ts-expect-error — the `?: never` exclusion props reject a ref that is both shapes at once,
    // so the resolver never has to pick a winner silently. Fails typecheck if the props are removed.
    acceptsRef(mixed);

    // The exclusion props must not tax the two legitimate shapes.
    expect(acceptsRef({ field: makeField(), link: { storageKey: "plan" } })).toBeTruthy();
    expect(acceptsRef({ entry: countryEntry })).toBeTruthy();
  });

  test("a degenerate ref carrying entry: undefined resolves through its field, not the entry branch", () => {
    // The discriminator is definedness, not key presence — a `"entry" in ref` check would route
    // this object into the entry branch and crash on `undefined.read`.
    const ref: TEmbeddedValueRef = { field: makeField(), link: { storageKey: "plan" }, entry: undefined };
    expect(resolveEmbeddedValue(ref, response)).toBe("premium");
  });
});

describe("coerceToEmbeddedDataType", () => {
  // The old switch had no arm for a type outside the enum and returned undefined; it must not throw.
  test.each(["unknown", "constructor", "toString", "__proto__", "hasOwnProperty"])(
    "coerces nothing for an unknown dataType %s",
    (dataType) => {
      expect(coerceToEmbeddedDataType("42", dataType as never)).toBeUndefined();
    }
  );

  describe("string", () => {
    test.each([
      ["hello", "hello"],
      ["", ""],
      [42, "42"],
      [true, "true"],
      [false, "false"],
    ])("coerces %p to %p", (input, expected) => {
      expect(coerceToEmbeddedDataType(input, "string")).toBe(expected);
    });

    test("coerces a Date to its ISO string", () => {
      expect(coerceToEmbeddedDataType(new Date("2026-08-01T09:00:00.000Z"), "string")).toBe(
        "2026-08-01T09:00:00.000Z"
      );
    });

    test.each([[["a", "b"]], [{ row1: "col1" }], [null], [undefined], [Number.NaN]])(
      "rejects %p",
      (input) => {
        expect(coerceToEmbeddedDataType(input, "string")).toBeUndefined();
      }
    );

    test("rejects an invalid Date instance instead of throwing on toISOString", () => {
      expect(coerceToEmbeddedDataType(new Date("banana"), "string")).toBeUndefined();
    });
  });

  describe("number", () => {
    test.each([
      [42, 42],
      [3.14, 3.14],
      ["42", 42],
      [" 7 ", 7],
      ["-1.5", -1.5],
      ["0", 0],
    ])("coerces %p to %p", (input, expected) => {
      expect(coerceToEmbeddedDataType(input, "number")).toBe(expected);
    });

    test.each<[unknown, string]>([
      ["", 'empty string — Number("") === 0 is an artifact, not data'],
      ["   ", "blank string"],
      ["abc", "non-numeric string"],
      ["25 seats", "partially numeric string — parseFloat would invent 25 where Number rejects"],
      [true, "boolean"],
      [Number.NaN, "NaN"],
      [Number.POSITIVE_INFINITY, "Infinity"],
      [new Date("2026-08-01"), "Date"],
      [["1"], "array"],
    ])("rejects %p (%s)", (input) => {
      expect(coerceToEmbeddedDataType(input, "number")).toBeUndefined();
    });
  });

  describe("boolean", () => {
    test.each([
      [true, true],
      [false, false],
      ["true", true],
      ["TRUE", true],
      [" False ", false],
    ])("coerces %p to %p", (input, expected) => {
      expect(coerceToEmbeddedDataType(input, "boolean")).toBe(expected);
    });

    test.each<[unknown, string]>([
      ["1", "numeric string — truthiness guessing is write-side ingest policy"],
      ["yes", "colloquial truthy string"],
      ["", "empty string"],
      [1, "number"],
      [0, "number"],
    ])("rejects %p (%s)", (input) => {
      expect(coerceToEmbeddedDataType(input, "boolean")).toBeUndefined();
    });
  });

  describe("date", () => {
    test("keeps an ISO date string as-is, not promoted to a midnight datetime", () => {
      expect(coerceToEmbeddedDataType("2026-08-06", "date")).toBe("2026-08-06");
    });

    test("keeps an ISO datetime string as-is", () => {
      expect(coerceToEmbeddedDataType("2026-08-06T10:30:00Z", "date")).toBe("2026-08-06T10:30:00Z");
    });

    test("coerces a Date instance to its ISO string", () => {
      expect(coerceToEmbeddedDataType(new Date("2026-08-06T10:30:00.000Z"), "date")).toBe(
        "2026-08-06T10:30:00.000Z"
      );
    });

    test("rejects an invalid Date instance", () => {
      expect(coerceToEmbeddedDataType(new Date("banana"), "date")).toBeUndefined();
    });

    test.each<[unknown, string]>([
      ["banana", "non-date string"],
      ["42", "would year-parse via new Date, which is not a date the user stored"],
      ["2026-13-45", "impossible calendar date"],
      ["06.08.2026", "non-ISO format — write-side coercion (ENG-1845) normalizes formats"],
      [1_723_000_000_000, "epoch number"],
      [true, "boolean"],
    ])("rejects %p (%s)", (input) => {
      expect(coerceToEmbeddedDataType(input, "date")).toBeUndefined();
    });

    test("accepts exactly the strings ZEmbeddedData accepts as date defaults — the two rules must not drift", () => {
      // Both files declare the ISO rule privately on purpose (each owns its side); this corpus is
      // the pin that keeps them the same rule. If either side alone gains offset/local acceptance,
      // a stored default becomes unresolvable (or vice versa) and this goes red.
      const dateDefaultRow = (defaultValue: string) => ({
        id: "clx0000000000000000000e1",
        createdAt: new Date("2026-08-01T09:00:00.000Z"),
        updatedAt: new Date("2026-08-01T09:00:00.000Z"),
        key: null,
        name: "Signup date",
        description: null,
        source: "ingested",
        dataType: "date",
        defaultValue,
        locked: false,
        surveyId: "clx0000000000000000000s1",
        workspaceId: "clx0000000000000000000w1",
      });

      const corpus = [
        "2026-08-06",
        "2026-08-06T10:30:00Z",
        "2026-08-06T10:30:00.123Z",
        "2026-08-06T10:30:00+02:00",
        "2026-08-06T10:30:00",
        " 2026-08-06",
      ];

      for (const candidate of corpus) {
        const rowAccepts = ZEmbeddedData.safeParse(dateDefaultRow(candidate)).success;
        const readAccepts = coerceToEmbeddedDataType(candidate, "date") !== undefined;
        expect({ candidate, readAccepts }).toStrictEqual({ candidate, readAccepts: rowAccepts });
      }
    });
  });
});

/*
 * The shared redaction, tested directly. Both surfaces that apply `privacy: "redactQuery"` call this
 * one function — the projections below, and the "Anonymize responses" toggle at ingest
 * (`apps/web/lib/response/anonymize.ts`) — so the rule is pinned here rather than once per caller.
 */
describe("redactUrlQuery", () => {
  test("strips the query string and keeps origin + path", () => {
    expect(redactUrlQuery("https://example.com/pricing?token=secret&email=a@b.c")).toBe(
      "https://example.com/pricing"
    );
  });

  test("keeps a URL that has no query string byte-for-byte", () => {
    expect(redactUrlQuery("https://example.com/pricing")).toBe("https://example.com/pricing");
  });

  test("keeps the port and a non-default scheme, which are part of the origin", () => {
    expect(redactUrlQuery("http://localhost:3000/s/abc?x=1")).toBe("http://localhost:3000/s/abc");
  });

  // Pinned deliberately: the fragment is dropped as well. The OAuth implicit flow puts `access_token`
  // in the fragment, so keeping it under "Anonymize responses" would be the exact leak the toggle is
  // for. Changing this to preserve `#…` is a product decision, not a refactor.
  test("drops the fragment as well as the query", () => {
    expect(redactUrlQuery("https://example.com/app#access_token=secret")).toBe("https://example.com/app");
    expect(redactUrlQuery("https://example.com/app?a=1#/route")).toBe("https://example.com/app");
  });

  test("returns a malformed URL rather than throwing, and still cuts its query", () => {
    expect(() => redactUrlQuery("not a url at all")).not.toThrow();
    expect(redactUrlQuery("not a url at all")).toBe("not a url at all");
    // A relative path never parses as an absolute URL, but must not smuggle a token through.
    expect(redactUrlQuery("/checkout?session=secret")).toBe("/checkout");
  });

  test("does not concatenate the literal 'null' origin of a schemeless-host URL", () => {
    expect(redactUrlQuery("mailto:someone@example.com?subject=hi")).toBe("mailto:someone@example.com");
  });

  test("leaves an empty string alone", () => {
    expect(redactUrlQuery("")).toBe("");
  });
});

describe("projectReservedValues", () => {
  test("projects catalog entries into a name-keyed map, omitting absent values", () => {
    const projected = projectReservedValues(
      [browserEntry, totalTimeEntry, languageEntry, completedEntry, actionEntry],
      response
    );

    expect(projected).toStrictEqual({
      browser: "Chrome",
      total_time: 30_000,
      response_language: "de",
      // Booleans are stringified: the recall/logic maps have no boolean slot.
      completed: "true",
    });
    // meta.action was never captured, so the key must be absent — not null, not "".
    expect("action" in projected).toBe(false);
  });

  test("the projected map merges into the maps recall and logic already read", () => {
    const projected = projectReservedValues([browserEntry, totalTimeEntry], response);

    // Compile-time fit: assignable to both existing lookup-map types.
    const asVariables: TResponseVariables = projected;
    const asData: TResponseData = projected;

    // Runtime fit: a merged map serves reserved values through plain key lookup, the only access
    // pattern replaceRecallInfo / getLeftOperandValue use.
    const mergedVariables = { ...response.variables, ...asVariables };
    expect(mergedVariables.browser).toBe("Chrome");
    expect(mergedVariables[SCORE_VARIABLE_ID]).toBe(42);
    expect(asData.total_time).toBe(30_000);
  });

  test("projects an empty map when nothing was captured", () => {
    expect(projectReservedValues([countryEntry, totalTimeEntry], sparseResponse)).toStrictEqual({});
  });

  test("projects through the resolver's coercion — a Date-backed entry lands as its ISO string", () => {
    // The one entry kind where resolving differs from the raw read: a raw `Date` must never reach
    // the recall/logic maps.
    expect(projectReservedValues([startedAtEntry], response)).toStrictEqual({
      started_at: "2026-08-01T09:00:00.000Z",
    });
  });

  test("omits an entry whose raw value cannot be coerced to its dataType", () => {
    const miscastEntry: TReservedFieldCatalogEntry = {
      name: "total_time",
      dataType: "number",
      availability: "server",
      privacy: "keep",
      display: "none",
      read: (r) => r.meta.source, // "web" — present, but not a number; projecting it would invent data
    };
    expect(projectReservedValues([miscastEntry], response)).toStrictEqual({});
  });

  test("keeps falsy-but-present values: false stringifies, empty string and zero stay in the map", () => {
    const emptySourceEntry: TReservedFieldCatalogEntry = {
      name: "empty_source",
      dataType: "string",
      availability: "client",
      privacy: "keep",
      display: "none",
      read: () => "",
    };
    const zeroTimeEntry: TReservedFieldCatalogEntry = {
      name: "zero_time",
      dataType: "number",
      availability: "server",
      privacy: "keep",
      display: "none",
      read: () => 0,
    };

    // sparseResponse has finished: false — a value, not a gap, exactly like "" and 0.
    expect(
      projectReservedValues([completedEntry, emptySourceEntry, zeroTimeEntry], sparseResponse)
    ).toStrictEqual({
      completed: "false",
      empty_source: "",
      zero_time: 0,
    });
  });

  test("a throwing accessor is skipped without aborting the rest of the catalog", () => {
    const hostnameEntry: TReservedFieldCatalogEntry = {
      name: "hostname",
      dataType: "string",
      availability: "client",
      privacy: "redactQuery",
      display: "none",
      read: (r) => new URL(r.meta.url ?? "").hostname,
    };

    // meta emptied: the hostname accessor throws on `new URL("")`, and the entries after it must
    // still be evaluated.
    expect(projectReservedValues([hostnameEntry, languageEntry], { ...response, meta: {} })).toStrictEqual({
      response_language: "de",
    });
  });
});

describe("RESERVED_FIELD_CATALOG", () => {
  /** Every Tier-1 location captured — today's ingest shape, with IP capture enabled. */
  const capturedResponse: TEmbeddedValueResponse = {
    id: "clx0000000000000000000r3",
    surveyId: "clx0000000000000000000s2",
    createdAt: new Date("2026-08-01T09:00:00.000Z"),
    updatedAt: new Date("2026-08-01T09:02:30.000Z"),
    finished: true,
    language: "de",
    data: { plan: "premium" },
    variables: {},
    // Milliseconds, and deliberately not a whole number of seconds.
    ttc: { q1: 40_000, q2: 50_400, _total: 90_400 },
    meta: {
      source: "link",
      url: "https://example.com/pricing?utm_source=news&email=a@b.co",
      userAgent: { browser: "Chrome", os: "macOS", device: "desktop" },
      country: "DE",
      action: "Clicked Upgrade",
      ipAddress: "203.0.113.7",
      // Browser-runtime context, snapshotted by the renderer at display time (ENG-1841).
      pagePath: "/pricing",
      pageReferrer: "https://news.example.org/weekly?issue=42",
      utmSource: "news",
      utmMedium: "email",
      utmCampaign: "august-launch",
      utmTerm: "pricing",
      utmContent: "hero-cta",
      screenWidth: 2560,
      screenHeight: 1440,
      viewportWidth: 1280,
      viewportHeight: 800,
      timezone: "Europe/Berlin",
      locale: "de-AT",
    },
  };

  /**
   * A response stored years before this catalog existed: `meta` holds only what was captured then
   * (source + url), there is no `ttc` at all, and no language was selected. The catalog must resolve
   * what is there and report the rest as unset — never throw, and never invent a value.
   */
  const historicalResponse: TEmbeddedValueResponse = {
    id: "clx0000000000000000000r4",
    surveyId: "clx0000000000000000000s2",
    createdAt: new Date("2024-03-04T08:00:00.000Z"),
    updatedAt: new Date("2024-03-04T08:01:00.000Z"),
    finished: true,
    language: null,
    data: { plan: "free" },
    variables: {},
    meta: { source: "link", url: "https://example.com/old" },
  };

  const projectCatalog = (onResponse: TEmbeddedValueResponse) =>
    projectReservedValues(RESERVED_FIELD_CATALOG, onResponse);

  /** The named entry, failing loudly instead of resolving `undefined.read` if it is ever removed. */
  const catalogEntry = (name: string): TReservedFieldCatalogEntry => {
    const entry = RESERVED_FIELD_CATALOG.find((candidate) => candidate.name === name);
    if (!entry) throw new Error(`No reserved catalog entry named ${name}`);
    return entry;
  };

  test("declares exactly the Tier-1 entries, with their dataType, availability, privacy and display", () => {
    // The catalog is a product decision, so it is pinned as a table rather than probed field by
    // field: adding, removing or reclassifying an entry has to be a deliberate edit here too.
    // `status` is absent on purpose — `Response` has no status column and `finished` already carries
    // the complete/partial distinction.
    //
    // `display` (ENG-2540) is in the table for that reason: a new entry cannot reach the response
    // card and the response table without someone deciding how prominent it is.
    expect(
      RESERVED_FIELD_CATALOG.map(({ name, dataType, availability, privacy, display }) => ({
        name,
        dataType,
        availability,
        privacy,
        display,
      }))
    ).toStrictEqual([
      { name: "source", dataType: "string", availability: "client", privacy: "keep", display: "primary" },
      { name: "url", dataType: "string", availability: "client", privacy: "redactQuery", display: "primary" },
      { name: "country", dataType: "string", availability: "server", privacy: "drop", display: "primary" },
      { name: "action", dataType: "string", availability: "client", privacy: "keep", display: "primary" },
      { name: "browser", dataType: "string", availability: "server", privacy: "drop", display: "primary" },
      { name: "os", dataType: "string", availability: "server", privacy: "drop", display: "primary" },
      { name: "deviceType", dataType: "string", availability: "server", privacy: "drop", display: "primary" },
      {
        name: "ipAddress",
        dataType: "string",
        availability: "server",
        privacy: "drop",
        display: "secondary",
      },
      { name: "finished", dataType: "boolean", availability: "server", privacy: "keep", display: "none" },
      { name: "language", dataType: "string", availability: "both", privacy: "keep", display: "none" },
      { name: "responseId", dataType: "string", availability: "server", privacy: "keep", display: "none" },
      { name: "surveyId", dataType: "string", availability: "server", privacy: "keep", display: "none" },
      {
        name: "durationSeconds",
        dataType: "number",
        availability: "server",
        privacy: "keep",
        display: "none",
      },
      { name: "startedAt", dataType: "date", availability: "server", privacy: "keep", display: "none" },
      { name: "finishedAt", dataType: "date", availability: "server", privacy: "keep", display: "none" },
      // Browser-runtime context (ENG-1841). All `client`: the renderer captures them itself, so a
      // mid-survey picker may offer them. `pageReferrer` is `redactQuery` because a URL's query
      // string is where an identifier hides; the rest carry nothing identifying on their own. There
      // is no `pageUrl` - it read the same `location.href` as `url`, so the two were identical.
      { name: "pagePath", dataType: "string", availability: "client", privacy: "keep", display: "secondary" },
      {
        name: "pageReferrer",
        dataType: "string",
        availability: "client",
        privacy: "redactQuery",
        display: "secondary",
      },
      {
        name: "utmSource",
        dataType: "string",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "utmMedium",
        dataType: "string",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "utmCampaign",
        dataType: "string",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      { name: "utmTerm", dataType: "string", availability: "client", privacy: "keep", display: "secondary" },
      {
        name: "utmContent",
        dataType: "string",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "screenWidth",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "screenHeight",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "viewportWidth",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "viewportHeight",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      { name: "timezone", dataType: "string", availability: "client", privacy: "keep", display: "secondary" },
      { name: "locale", dataType: "string", availability: "client", privacy: "keep", display: "secondary" },
      {
        name: "visitPagesViewed",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "visitUniquePagesViewed",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "visitTimeOnSite",
        dataType: "number",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "visitEntryReferrer",
        dataType: "string",
        availability: "client",
        privacy: "redactQuery",
        display: "secondary",
      },
      {
        name: "visitUrls",
        dataType: "string",
        availability: "client",
        privacy: "keep",
        display: "secondary",
      },
      // Opened by creative (ENG-4001). `server`: the ingest routes check the creative and
      // stamp both, so no mid-survey picker offers them. `keep`: a creative's id and name are the
      // workspace's own copy, nothing the respondent disclosed.
      {
        name: "openedByCreativeId",
        dataType: "string",
        availability: "server",
        privacy: "keep",
        display: "secondary",
      },
      {
        name: "openedByCreativeName",
        dataType: "string",
        availability: "server",
        privacy: "keep",
        display: "secondary",
      },
    ]);
  });

  test("reads the creative that opened the survey off its meta, and nothing when none did", () => {
    const opened: TEmbeddedValueResponse = {
      ...historicalResponse,
      meta: { openedByCreativeId: "clh7m1qkx0000qwer1234abcd", openedByCreativeName: "Feedback tab" },
    };

    expect(projectCatalog(opened)).toEqual(
      expect.objectContaining({
        openedByCreativeId: "clh7m1qkx0000qwer1234abcd",
        openedByCreativeName: "Feedback tab",
      })
    );
    expect(projectCatalog(historicalResponse)).not.toHaveProperty("openedByCreativeName");
  });

  test("every entry resolves against a fully captured response", () => {
    // One exact-map assertion rather than fifteen lookups: a missing key is a failed entry, and an
    // extra key is an entry nobody declared in the table above.
    expect(projectCatalog(capturedResponse)).toStrictEqual({
      source: "link",
      // `privacy: "redactQuery"` applied by the projection: the stored meta still holds
      // `?utm_source=news&email=a@b.co`, but a projected value can be recalled into a redirect URL.
      url: "https://example.com/pricing",
      country: "DE",
      action: "Clicked Upgrade",
      browser: "Chrome",
      os: "macOS",
      deviceType: "desktop",
      ipAddress: "203.0.113.7",
      finished: "true",
      language: "de",
      responseId: "clx0000000000000000000r3",
      surveyId: "clx0000000000000000000s2",
      durationSeconds: 90,
      startedAt: "2026-08-01T09:00:00.000Z",
      finishedAt: "2026-08-01T09:02:30.000Z",
      pagePath: "/pricing",
      // `redactQuery` too, for the same reason as `url`: a projected value can be recalled into a
      // redirect URL or an email. The fixture above keeps `?issue=42`, so this asserts the read-side
      // narrowing rather than a capture change.
      pageReferrer: "https://news.example.org/weekly",
      utmSource: "news",
      utmMedium: "email",
      utmCampaign: "august-launch",
      utmTerm: "pricing",
      utmContent: "hero-cta",
      // Numbers stay numbers through the projection — a logic condition compares them arithmetically.
      screenWidth: 2560,
      screenHeight: 1440,
      viewportWidth: 1280,
      viewportHeight: 800,
      timezone: "Europe/Berlin",
      locale: "de-AT",
    });
  });

  test("a redactQuery entry projects without its query string, while storage keeps it", () => {
    // The finding this pins: reserved `url` is interpolable into an ending card's redirect URL and a
    // follow-up email, so projecting the href verbatim forwards whatever the survey link carried —
    // for a link survey that includes the single-use `suToken`, which is a credential.
    const tokenised = {
      ...capturedResponse,
      meta: { ...capturedResponse.meta, url: "https://app.test/s/abc?suToken=secret&lang=de#top" },
    };

    expect(projectCatalog(tokenised).url).toBe("https://app.test/s/abc");
    // Read-side narrowing only — capture is the Anonymize toggle's job, and it is off here.
    expect(tokenised.meta.url).toBe("https://app.test/s/abc?suToken=secret&lang=de#top");
  });

  test("a historical response resolves none of the browser-runtime fields, and does not throw", () => {
    // The migration story for ENG-1841 in one assertion: nothing was backfilled, because the values
    // only ever existed in a browser that has long since closed. Every new entry must therefore
    // report unset on a response collected before the renderer captured them — not "", not 0.
    const projected = projectCatalog(historicalResponse);

    for (const name of [
      "pagePath",
      "pageReferrer",
      "utmSource",
      "utmMedium",
      "utmCampaign",
      "utmTerm",
      "utmContent",
      "screenWidth",
      "screenHeight",
      "viewportWidth",
      "viewportHeight",
      "timezone",
    ]) {
      expect(projected).not.toHaveProperty(name);
    }
  });

  test("resolves what a historical response captured and reports the rest as unset", () => {
    expect(projectCatalog(historicalResponse)).toStrictEqual({
      source: "link",
      url: "https://example.com/old",
      finished: "true",
      responseId: "clx0000000000000000000r4",
      surveyId: "clx0000000000000000000s2",
      startedAt: "2024-03-04T08:00:00.000Z",
      finishedAt: "2024-03-04T08:01:00.000Z",
    });
  });

  test("durationSeconds converts the stored milliseconds to whole seconds", () => {
    // ttc is milliseconds (MAX_RESPONSE_TTC in responses.ts), so a raw read would publish 90400
    // under a field named — and typed — in seconds.
    expect(projectCatalog(capturedResponse).durationSeconds).toBe(90);
  });

  test("durationSeconds is unset on a partial response rather than a duration-so-far", () => {
    // calculateTtcTotal is the only writer of `_total`, and every call site guards it behind
    // `finished ?` — so a partial response has per-element timings and no total. Summing them here
    // would report a duration no other surface agrees with.
    const partialResponse: TEmbeddedValueResponse = {
      ...capturedResponse,
      finished: false,
      ttc: { q1: 40_000, q2: 50_400 },
    };
    const projected = projectCatalog(partialResponse);

    expect("durationSeconds" in projected).toBe(false);
    expect(projected.finished).toBe("false");
  });

  test("startedAt and finishedAt resolve to ISO strings, never Date objects", () => {
    const startedAt = resolveEmbeddedValue({ entry: catalogEntry("startedAt") }, capturedResponse);
    const finishedAt = resolveEmbeddedValue({ entry: catalogEntry("finishedAt") }, capturedResponse);

    expect(startedAt).toBe("2026-08-01T09:00:00.000Z");
    expect(finishedAt).toBe("2026-08-01T09:02:30.000Z");
    // A raw Date reaching the recall/logic maps would render as a locale-dependent string.
    expect(startedAt).not.toBeInstanceOf(Date);
    expect(finishedAt).not.toBeInstanceOf(Date);
  });

  test("finished resolves as a boolean and projects as the string the maps can hold", () => {
    const entry = catalogEntry("finished");

    expect(resolveEmbeddedValue({ entry }, capturedResponse)).toBe(true);
    expect(resolveEmbeddedValue({ entry }, { ...capturedResponse, finished: false })).toBe(false);
    // false is a value, not a gap: it must survive the projection as "false", not vanish.
    expect(projectCatalog({ ...capturedResponse, finished: false }).finished).toBe("false");
  });

  test("no accessor throws on a response where every optional location is absent", () => {
    // The catalog runs over responses nobody curated; an accessor that assumed today's meta shape
    // would take a whole export loop down. sparseResponse is that worst case.
    expect(() => projectCatalog(sparseResponse)).not.toThrow();
    expect(projectCatalog(sparseResponse)).toStrictEqual({
      finished: "false",
      responseId: sparseResponse.id,
      surveyId: sparseResponse.surveyId,
      startedAt: "2026-08-02T12:00:00.000Z",
      finishedAt: "2026-08-02T12:00:00.000Z",
    });
  });

  test("RESERVED_FIELD_NAMES is exactly the catalog's names, lowercased", () => {
    // The blocklist is a hand-written leaf module (it cannot import the catalog without closing an
    // import cycle — see reserved-field-names.ts), so this is the only thing keeping the two in
    // step. A name added to one side and not the other fails here.
    expect([...RESERVED_FIELD_NAMES].sort()).toStrictEqual(
      RESERVED_FIELD_CATALOG.map((entry) => entry.name.toLowerCase()).sort()
    );
  });
});

describe("projectClientReservedValues", () => {
  /** What a running survey holds: no `id`, no `createdAt`/`updatedAt`, no final `finished`. */
  const midSurvey: TClientEmbeddedValueResponse = {
    surveyId: "clx0000000000000000000s2",
    language: "de",
    data: { plan: "premium" },
    variables: {},
    ttc: { q1: 40_000 },
    meta: { source: "link", url: "https://example.com/pricing", action: "Clicked Upgrade" },
  };

  test("redacts the query from a mid-survey url, which is the interpolable one", () => {
    // The leak path this closes: an author can put `#recall:url#` in an ending card's redirect URL, so
    // the mid-survey projection is what a third-party host would receive. On a single-use link the
    // href carries `suToken`, a credential, plus whatever prefill params the link was built with.
    const projected = projectClientReservedValues(RESERVED_FIELD_CATALOG, {
      ...midSurvey,
      meta: { ...midSurvey.meta, url: "https://app.test/s/abc?suToken=secret&country=DE" },
    });

    expect(projected.url).toBe("https://app.test/s/abc");
  });

  test("projects only the entries a client can read mid-survey", () => {
    expect(projectClientReservedValues(RESERVED_FIELD_CATALOG, midSurvey)).toStrictEqual({
      source: "link",
      url: "https://example.com/pricing",
      action: "Clicked Upgrade",
      language: "de",
    });
  });

  test("never invokes a server-only accessor", () => {
    // Filtering by availability is not enough on its own: a server accessor invoked against the
    // client slice would read `undefined` and be dropped by coercion anyway, so the output would look
    // identical while the accessor had in fact run. Spies are what distinguish the two.
    const serverRead = vi.fn(() => "should never be read");
    const clientRead = vi.fn(() => "link");
    const bothRead = vi.fn(() => "de");

    const projected = projectClientReservedValues(
      [
        {
          name: "server_only",
          dataType: "string",
          availability: "server",
          privacy: "keep",
          display: "none",
          read: serverRead,
        },
        {
          name: "client_only",
          dataType: "string",
          availability: "client",
          privacy: "keep",
          display: "none",
          read: clientRead,
        },
        {
          name: "either_side",
          dataType: "string",
          availability: "both",
          privacy: "keep",
          display: "none",
          read: bothRead,
        },
      ],
      midSurvey
    );

    expect(projected).toStrictEqual({ client_only: "link", either_side: "de" });
    expect(serverRead).not.toHaveBeenCalled();
    expect(clientRead).toHaveBeenCalledTimes(1);
    expect(bothRead).toHaveBeenCalledTimes(1);
  });

  test("omits a client entry whose value has not been captured yet", () => {
    expect(
      projectClientReservedValues(RESERVED_FIELD_CATALOG, { ...midSurvey, language: null, meta: {} })
    ).toStrictEqual({});
  });
});

describe("listReadableFields", () => {
  const blocks: TSurveyBlocks = [
    {
      id: "clx0000000000000000000b1",
      name: "Block 1",
      elements: [
        {
          id: "q1",
          type: TSurveyElementTypeEnum.OpenText,
          headline: { default: "What is your name?" },
          required: false,
          inputType: "text",
          charLimit: { enabled: false },
        },
        {
          id: "q2",
          type: TSurveyElementTypeEnum.OpenText,
          headline: { default: "<p>Rate <b>us</b></p>" },
          required: false,
          inputType: "text",
          charLimit: { enabled: false },
        },
      ],
    },
    {
      id: "clx0000000000000000000b2",
      name: "Block 2",
      elements: [
        {
          id: "q3",
          type: TSurveyElementTypeEnum.OpenText,
          headline: { default: "#recall:q1/fallback:there# how are you?" },
          required: false,
          inputType: "text",
          charLimit: { enabled: false },
        },
        {
          id: "q4",
          type: TSurveyElementTypeEnum.OpenText,
          headline: { default: "" },
          required: false,
          inputType: "text",
          charLimit: { enabled: false },
        },
        {
          id: "q5",
          type: TSurveyElementTypeEnum.OpenText,
          headline: { default: "How likely?", de: "Wie wahrscheinlich?" },
          required: false,
          inputType: "text",
          charLimit: { enabled: false },
        },
      ],
    },
  ];

  const embeddedDataPairs: TLinkedEmbeddedField[] = [
    {
      field: makeField({ name: "Score", source: "computed", dataType: "number", defaultValue: 0 }),
      link: { storageKey: SCORE_VARIABLE_ID },
    },
    { field: makeField({ name: "Plan" }), link: { storageKey: "plan" } },
  ];

  test("returns all four groups, each entry keyed by its reference key", () => {
    const fields = listReadableFields({
      blocks,
      embeddedData: embeddedDataPairs,
      reservedEntries: [countryEntry, totalTimeEntry],
      contactAttributeKeys: [
        { key: "email", name: "Email address" },
        { key: "user_id", name: null },
      ],
    });

    expect(fields.question.map((field) => field.key)).toEqual(["q1", "q2", "q3", "q4", "q5"]);
    expect(fields.embeddedData).toEqual([
      { key: SCORE_VARIABLE_ID, label: "Score" },
      { key: "plan", label: "Plan" },
    ]);
    expect(fields.reserved).toEqual([
      { key: "country", label: "Country" },
      { key: "total_time", label: "Total Time" },
    ]);
    expect(fields.contactAttribute).toEqual([
      { key: "email", label: "Email address" },
      { key: "user_id", label: "user_id" },
    ]);
  });

  // A draft or migrated block can be stored without `elements` (ENG-2497).
  test("a block with no elements array adds no question fields", () => {
    const fields = listReadableFields({
      blocks: [
        { id: "clx0000000000000000000b0", name: "Legacy" } as unknown as TSurveyBlocks[number],
        ...blocks,
      ],
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.question.map((field) => field.key)).toEqual(["q1", "q2", "q3", "q4", "q5"]);
  });

  test("one Embedded Data group carries every source and dataType in the definitions' order", () => {
    // ENG-1853: the pickers draw one group where they used to draw Variables and Hidden Fields, so
    // this list is the group. A row's source and type decide its icon and its operators at the call
    // site, but never which group it lands in, and the order is the definitions' own.
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [
        { field: makeField({ name: "Plan tier" }), link: { storageKey: "plan" } },
        {
          field: makeField({ name: "Seats", source: "computed", dataType: "number", defaultValue: 0 }),
          link: { storageKey: "seats" },
        },
        { field: makeField({ name: "Is trial", dataType: "boolean" }), link: { storageKey: "is_trial" } },
        { field: makeField({ name: "Signed up", dataType: "date" }), link: { storageKey: "signed_up" } },
      ],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.embeddedData).toEqual([
      { key: "plan", label: "Plan tier" },
      { key: "seats", label: "Seats" },
      { key: "is_trial", label: "Is trial" },
      { key: "signed_up", label: "Signed up" },
    ]);
  });

  test("a shared field carries its library key as the secondary label, a survey-owned one does not", () => {
    // The name is workspace-wide prose and the key is the identifier a URL parameter actually
    // spells; the two are edited independently, so someone who knows only the key needs the key on
    // the row to find the field at all. A local field has no library key and shows one string.
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [
        { field: makeField({ name: "Plan tier", key: "plan_tier" }), link: { storageKey: "plan" } },
        { field: makeField({ name: "Referrer", key: null }), link: { storageKey: "ref" } },
      ],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.embeddedData).toEqual([
      { key: "plan", label: "Plan tier", secondaryLabel: "plan_tier" },
      { key: "ref", label: "Referrer" },
    ]);
    // Absent rather than null, so a consumer can test it with a plain truthiness check.
    expect(fields.embeddedData[1].secondaryLabel).toBeUndefined();
  });

  test("the storage key labels a blank-named field and is never the secondary label", () => {
    // Repeating the storage key under every local field's name would be noise on the common case,
    // so the fallback is the label itself — one row, one string.
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [{ field: makeField({ name: "   " }), link: { storageKey: "plan" } }],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.embeddedData).toEqual([{ key: "plan", label: "plan" }]);
  });

  test("question labels come from headlines: HTML stripped, recall flattened, empty falls back to id", () => {
    const fields = listReadableFields({
      blocks,
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.question).toEqual([
      { key: "q1", label: "What is your name?" },
      { key: "q2", label: "Rate us" },
      { key: "q3", label: "___ how are you?" },
      { key: "q4", label: "q4" },
      { key: "q5", label: "How likely?" },
    ]);
  });

  test("resolves headlines for the requested language, falling back to default", () => {
    const fields = listReadableFields({
      blocks,
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
      languageCode: "de",
    });

    const q5 = fields.question.find((field) => field.key === "q5");
    const q1 = fields.question.find((field) => field.key === "q1");
    expect(q5?.label).toBe("Wie wahrscheinlich?");
    expect(q1?.label).toBe("What is your name?");
  });

  const singleElementBlocks = (elementId: string, headline: TI18nString): TSurveyBlocks => [
    {
      id: "clx0000000000000000000b3",
      name: "Block",
      elements: [
        {
          id: elementId,
          type: TSurveyElementTypeEnum.OpenText,
          headline,
          required: false,
          inputType: "text",
          charLimit: { enabled: false },
        },
      ],
    },
  ];

  test("a blank locale entry falls back to the default headline, not to a blank label", () => {
    // Blank locale entries are a real stored shape; today's apps/web picker labels this case with
    // the bare element id (its getLocalizedValue has no default fallback) — this pins the improved,
    // packages/surveys-style semantics the module documents.
    const fields = listReadableFields({
      blocks: singleElementBlocks("q6", { default: "How likely?", de: "" }),
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
      languageCode: "de",
    });

    expect(fields.question).toEqual([{ key: "q6", label: "How likely?" }]);
  });

  test("flattens every recall token in a headline, not only the first", () => {
    const fields = listReadableFields({
      blocks: singleElementBlocks("q7", {
        default: "#recall:q1/fallback:a# and #recall:q2/fallback:b#",
      }),
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.question).toEqual([{ key: "q7", label: "___ and ___" }]);
  });

  test("a non-string default in unparsed survey JSON degrades to the id fallback instead of throwing", () => {
    // prisma-json-types reads skip zod, so dirty i18n objects are representable at runtime.
    const dirtyHeadline = { default: 42 } as unknown as TI18nString;
    const fields = listReadableFields({
      blocks: singleElementBlocks("q8", dirtyHeadline),
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.question).toEqual([{ key: "q8", label: "q8" }]);
  });

  test("reserved labels fall back to the entry name when title-casing yields nothing", () => {
    const underscoreEntry: TReservedFieldCatalogEntry = {
      name: "_",
      dataType: "string",
      availability: "server",
      privacy: "keep",
      display: "none",
      read: () => undefined,
    };
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [],
      reservedEntries: [underscoreEntry],
      contactAttributeKeys: [],
    });

    expect(fields.reserved).toEqual([{ key: "_", label: "_" }]);
  });

  test("camelCase reserved names get a spaced label, not a run-together one", () => {
    // The catalog names mirror the `meta` keys they read, so they are camelCase rather than
    // snake_case. Labelling them with the snake_case formatter alone rendered "DurationSeconds" in
    // the pickers, and made the Playwright assertions that a server-only field is ABSENT pass for the
    // wrong reason: they searched for "Duration Seconds", a string nothing ever rendered.
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [],
      reservedEntries: RESERVED_FIELD_CATALOG.filter((entry) =>
        ["durationSeconds", "ipAddress", "responseId"].includes(entry.name)
      ),
      contactAttributeKeys: [],
    });

    expect(fields.reserved).toEqual([
      { key: "ipAddress", label: "Ip Address" },
      { key: "responseId", label: "Response Id" },
      { key: "durationSeconds", label: "Duration Seconds" },
    ]);
  });

  test("embedded data labels fall back to the storageKey when the definition name is blank", () => {
    // ZEmbeddedData forbids blank names, but derived legacy pairs carry plain variable names.
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [{ field: makeField({ name: "   " }), link: { storageKey: "plan" } }],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.embeddedData).toEqual([{ key: "plan", label: "plan" }]);
  });

  test("contact attribute labels fall back to the key when the name is blank", () => {
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [{ key: "user_id", name: "" }],
    });

    expect(fields.contactAttribute).toEqual([{ key: "user_id", label: "user_id" }]);
  });

  test("returns four empty groups for empty inputs", () => {
    const fields = listReadableFields({
      blocks: [],
      embeddedData: [],
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields).toStrictEqual({ question: [], embeddedData: [], reserved: [], contactAttribute: [] });
  });
});

describe("embeddedFieldsFromLegacyInput", () => {
  const legacySurvey = {
    variables: [
      { id: "clx0000000000000000000v1", name: "score", type: "number" as const, value: 10 },
      { id: "clx0000000000000000000v3", name: "plan_name", type: "text" as const, value: "basic" },
    ],
    hiddenFields: { enabled: true, fieldIds: ["source_page", "Coupon-Code"] },
  };

  test("maps a variable to a computed field addressed by its existing cuid", () => {
    const [scorePair] = embeddedFieldsFromLegacyInput(legacySurvey);
    expect(scorePair).toStrictEqual({
      field: {
        key: null,
        name: "score",
        source: "computed",
        dataType: "number",
        defaultValue: 10,
        locked: false,
      },
      link: { storageKey: "clx0000000000000000000v1" },
    });
  });

  test("maps a text variable to a string field with its value as default", () => {
    const pairs = embeddedFieldsFromLegacyInput(legacySurvey);
    expect(pairs[1]).toStrictEqual({
      field: {
        key: null,
        name: "plan_name",
        source: "computed",
        dataType: "string",
        defaultValue: "basic",
        locked: false,
      },
      link: { storageKey: "clx0000000000000000000v3" },
    });
  });

  test("maps hidden fields to ingested string fields addressed by name, keeping legacy spellings", () => {
    const pairs = embeddedFieldsFromLegacyInput(legacySurvey);
    expect(pairs.slice(2)).toStrictEqual([
      {
        field: {
          key: null,
          name: "source_page",
          source: "ingested",
          dataType: "string",
          defaultValue: null,
          locked: false,
        },
        link: { storageKey: "source_page" },
      },
      {
        field: {
          key: null,
          name: "Coupon-Code",
          source: "ingested",
          dataType: "string",
          defaultValue: null,
          locked: false,
        },
        link: { storageKey: "Coupon-Code" },
      },
    ]);
  });

  test("derives declared hidden fields even when hiddenFields is disabled", () => {
    // Recall and logic consult fieldIds alone today — and the link-survey URL path ingests values
    // even when disabled — so deriving must not hide fields that may hold data.
    const pairs = embeddedFieldsFromLegacyInput({
      variables: [],
      hiddenFields: { enabled: false, fieldIds: ["plan"] },
    });
    expect(pairs).toHaveLength(1);
    expect(pairs[0].link.storageKey).toBe("plan");
  });

  test("handles absent fieldIds and fully empty declarations", () => {
    expect(embeddedFieldsFromLegacyInput({ variables: [], hiddenFields: { enabled: true } })).toEqual([]);
    expect(
      embeddedFieldsFromLegacyInput({ variables: [], hiddenFields: { enabled: false, fieldIds: [] } })
    ).toEqual([]);
  });

  test("derived pairs resolve end-to-end through resolveEmbeddedValue", () => {
    const pairs = embeddedFieldsFromLegacyInput(legacySurvey);

    // The variable's value arrives under its cuid in response.variables.
    expect(resolveEmbeddedValue(pairs[0], response)).toBe(42);
    // No stored value → the variable's declared value serves as the default.
    expect(resolveEmbeddedValue(pairs[0], sparseResponse)).toBe(10);
    // Hidden fields read response.data under their name; absent ones stay unset (null default).
    expect(resolveEmbeddedValue(pairs[2], sparseResponse)).toBeUndefined();
  });

  test("derived pairs enumerate through listReadableFields keyed by storageKey", () => {
    const fields = listReadableFields({
      blocks: [],
      embeddedData: embeddedFieldsFromLegacyInput(legacySurvey),
      reservedEntries: [],
      contactAttributeKeys: [],
    });

    expect(fields.embeddedData).toEqual([
      { key: "clx0000000000000000000v1", label: "score" },
      { key: "clx0000000000000000000v3", label: "plan_name" },
      { key: "source_page", label: "source_page" },
      { key: "Coupon-Code", label: "Coupon-Code" },
    ]);
  });
});

describe("getSurveyEmbeddedFields", () => {
  // A survey still carrying the legacy keys beside (or instead of) its rows: the accessor reads only
  // the rows, since the legacy keys are derived from them at the read seam (ENG-2404).
  const legacySurvey: TEmbeddedFieldsSurvey & TLegacyEmbeddedFields = {
    variables: [{ id: "clx0000000000000000000v1", name: "score", type: "number" as const, value: 10 }],
    hiddenFields: { enabled: true, fieldIds: ["source_page"] },
  };

  const rows: TLinkedEmbeddedField[] = [
    {
      field: {
        key: null,
        name: "renamed_score",
        source: "computed",
        dataType: "number",
        defaultValue: 7,
        locked: false,
      },
      link: { storageKey: "clx0000000000000000000v1" },
    },
  ];

  test("uses the joined rows when the survey carries them", () => {
    expect(getSurveyEmbeddedFields({ ...legacySurvey, embeddedFields: rows })).toStrictEqual(rows);
  });

  test("reports nothing when the select omitted the join, rather than reading the legacy keys", () => {
    // ENG-2412 removed the legacy fallback: the rows are the whole answer. The consequence is that
    // every survey select reaching a reader has to carry `selectSurveyEmbeddedDataLinks` — one that
    // does not makes the survey read as having no Embedded Data at all.
    expect(getSurveyEmbeddedFields(legacySurvey)).toStrictEqual([]);
    expect(getSurveyEmbeddedFields({ ...legacySurvey, embeddedFields: null })).toStrictEqual([]);
    expect(embeddedFieldsFromLegacyInput(legacySurvey)).not.toStrictEqual([]);
  });

  test("an empty row list means no fields, so deleting a survey's rows removes them", () => {
    // Previously this fell back to the legacy columns, which is why deleting a survey's rows made its
    // fields reappear. Now they disappear, which is what the tables being the source of truth means.
    expect(getSurveyEmbeddedFields({ ...legacySurvey, embeddedFields: [] })).toStrictEqual([]);
  });

  test("a survey with no fields at all answers [] through either path", () => {
    const empty: TEmbeddedFieldsSurvey & TLegacyEmbeddedFields = {
      variables: [],
      hiddenFields: { enabled: false, fieldIds: [] },
    };
    expect(getSurveyEmbeddedFields(empty)).toEqual([]);
    expect(getSurveyEmbeddedFields({ ...empty, embeddedFields: [] })).toEqual([]);
  });

  test("partitions by source, preserving the inlined order within each group", () => {
    const survey = {
      ...legacySurvey,
      embeddedFields: [
        ...rows,
        {
          field: {
            key: null,
            name: "utm_source",
            source: "ingested" as const,
            dataType: "string" as const,
            defaultValue: null,
            locked: false,
          },
          link: { storageKey: "utm_source" },
        },
        {
          field: {
            key: null,
            name: "plan",
            source: "ingested" as const,
            dataType: "string" as const,
            defaultValue: null,
            locked: false,
          },
          link: { storageKey: "plan" },
        },
      ],
    };

    expect(getComputedEmbeddedFields(survey)).toStrictEqual([rows[0]]);
    expect(getIngestedStorageKeys(survey)).toStrictEqual(["utm_source", "plan"]);
    expect(getIngestedEmbeddedFields(survey).map(({ field }) => field.name)).toStrictEqual([
      "utm_source",
      "plan",
    ]);
  });

  test("the partitions report nothing too, rather than falling back independently", () => {
    expect(getComputedEmbeddedFields(legacySurvey)).toStrictEqual([]);
    expect(getIngestedStorageKeys(legacySurvey)).toStrictEqual([]);
  });
});

describe("ZLinkedEmbeddedField mirrors TLinkedEmbeddedField", () => {
  const pair: TLinkedEmbeddedField = {
    field: {
      key: null,
      name: "score",
      source: "computed",
      dataType: "number",
      defaultValue: 10,
      locked: false,
    },
    link: { storageKey: "clx0000000000000000000v1" },
  };

  test("a TLinkedEmbeddedField parses, and the parsed value is assignable back", () => {
    const parsed = ZLinkedEmbeddedField.parse(pair);
    // Compile-time half of the check: the inferred type must satisfy the interface, and vice versa.
    const roundTripped: TLinkedEmbeddedField = parsed;
    const asSchemaType: z.infer<typeof ZLinkedEmbeddedField> = pair;
    expect(roundTripped).toStrictEqual(pair);
    expect(asSchemaType).toStrictEqual(pair);
  });

  test("every field embeddedFieldsFromLegacyInput produces round-trips through the schema", () => {
    const derived = embeddedFieldsFromLegacyInput({
      variables: [
        { id: "clx0000000000000000000v1", name: "score", type: "number", value: 10 },
        { id: "clx0000000000000000000v3", name: "plan_name", type: "text", value: "basic" },
      ],
      hiddenFields: { enabled: true, fieldIds: ["source_page", "Coupon-Code"] },
    });

    expect(z.array(ZLinkedEmbeddedField).parse(derived)).toStrictEqual(derived);
  });

  test("strips nothing the readers need, and rejects a blank name or storage key", () => {
    // The valid pair parses, so the rejections below are the rules firing and not the whole shape
    // being refused.
    expect(ZLinkedEmbeddedField.safeParse(pair).success).toBe(true);

    expect(ZLinkedEmbeddedField.safeParse({ ...pair, field: { ...pair.field, name: "  " } }).success).toBe(
      false
    );
    expect(ZLinkedEmbeddedField.safeParse({ ...pair, link: { storageKey: "" } }).success).toBe(false);
    // Whitespace-only, not just empty: an ingested field's storage key is its URL param name, so a
    // padded `" plan "` would never match `?plan=` while still counting as a distinct field under
    // `@@unique([surveyId, storageKey])`. Rejected twice over — by the blank check and by the
    // legacy-charset rule — so removing either one alone keeps this passing.
    expect(ZLinkedEmbeddedField.safeParse({ ...pair, link: { storageKey: "  " } }).success).toBe(false);
  });
});

/**
 * The logic engines' read of a computed field. Lives here because
 * `packages/surveys/src/lib/logic.ts` and `apps/web/lib/surveyLogic/utils.ts` are near-copies and
 * both consume it — one definition of the value-preservation rule instead of two that can drift.
 */
describe("getLogicVariableValue", () => {
  const computedField = (storageKey: string, dataType: "number" | "string", defaultValue: number | string) =>
    ({
      field: {
        key: null,
        name: storageKey,
        source: "computed" as const,
        dataType,
        defaultValue,
        locked: false,
      },
      link: { storageKey },
    }) satisfies TLinkedEmbeddedField;

  const numberField = computedField("score", "number", 5);
  const textField = computedField("tier", "string", "gold");
  const fields = [numberField, textField];

  test("reads a true/false field as the spelling conditions compare it with (ENG-6973)", () => {
    const flag = {
      field: {
        ...computedField("qualified", "string", "").field,
        dataType: "boolean" as const,
        defaultValue: null,
      },
      link: { storageKey: "qualified" },
    } satisfies TLinkedEmbeddedField;

    expect(getLogicVariableValue([flag], "qualified", { qualified: true })).toBe("true");
    // A stored false is a value, not "unset".
    expect(getLogicVariableValue([flag], "qualified", { qualified: false })).toBe("false");
    expect(getLogicVariableValue([flag], "qualified", { qualified: "FALSE" })).toBe("false");
    expect(getLogicVariableValue([flag], "qualified", {})).toBe("");
  });

  test("reads a stored value under the field's declared type", () => {
    expect(getLogicVariableValue(fields, "score", { score: 42 })).toBe(42);
    expect(getLogicVariableValue(fields, "tier", { tier: "silver" })).toBe("silver");
  });

  test("a number field coerces its stored value, numeric strings included", () => {
    expect(getLogicVariableValue(fields, "score", { score: "42" })).toBe(42);
  });

  test("a number field reads a stored true/false as 0, never as 1 (ENG-6973)", () => {
    expect(getLogicVariableValue(fields, "score", { score: true })).toBe(0);
    expect(getLogicVariableValue(fields, "score", { score: false })).toBe(0);
  });

  // The four cases below are the deltas `resolveEmbeddedValue` deliberately does not reproduce.
  // Swapping this helper onto it would change what already-stored responses evaluate to, and
  // responses are never migrated — so each one is asserted against the declared default it must NOT
  // fall back to.
  test("delta (a): a non-numeric stored value is 0, never the declared default", () => {
    expect(getLogicVariableValue(fields, "score", { score: "abc" })).toBe(0);
  });

  test('delta (a): a string field holding 0 is "", never "0"', () => {
    expect(getLogicVariableValue(fields, "tier", { tier: 0 })).toBe("");
  });

  test('delta (d): a response missing the key is 0 / "", never the declared default', () => {
    expect(getLogicVariableValue(fields, "score", {})).toBe(0);
    expect(getLogicVariableValue(fields, "tier", {})).toBe("");
  });

  test("a key the survey does not declare resolves to undefined, not to a coerced blank", () => {
    // Distinct from the "missing value" cases above: there is no field, so there is no type to
    // evaluate under, and the caller must be able to tell the two apart.
    expect(getLogicVariableValue(fields, "deleted_variable", { deleted_variable: 7 })).toBeUndefined();
  });
});

describe("getComputedFieldDataType", () => {
  const fields: TLinkedEmbeddedField[] = [
    {
      field: {
        key: null,
        name: "score",
        source: "computed",
        dataType: "number",
        defaultValue: 5,
        locked: false,
      },
      link: { storageKey: "score" },
    },
  ];

  test("answers the declared type", () => {
    expect(getComputedFieldDataType(fields, "score")).toBe("number");
  });

  test("answers undefined for a field the survey no longer declares, instead of throwing", () => {
    // The guard both engines depend on: this runs before the operator switch, and a throw here is
    // swallowed by `evaluateSingleCondition`'s try/catch into a silent `false`.
    expect(getComputedFieldDataType(fields, "deleted_variable")).toBeUndefined();
    expect(findComputedEmbeddedField(fields, "deleted_variable")).toBeUndefined();
  });
});

describe("listDisplayableReservedFields", () => {
  /** Every Tier-1 location captured, with IP capture on. Local rather than shared, so the display
   * assertions below cannot be broken by an edit made for a projection test. */
  const captured: TEmbeddedValueResponse = {
    id: "clx0000000000000000000r9",
    surveyId: "clx0000000000000000000s9",
    createdAt: new Date("2026-08-01T09:00:00.000Z"),
    updatedAt: new Date("2026-08-01T09:02:30.000Z"),
    finished: true,
    language: "de",
    data: { plan: "premium" },
    variables: {},
    ttc: { _total: 90_400 },
    meta: {
      source: "link",
      url: "https://example.com/pricing?utm_source=news&email=a@b.co",
      userAgent: { browser: "Chrome", os: "macOS", device: "desktop" },
      country: "DE",
      action: "Clicked Upgrade",
      ipAddress: "203.0.113.7",
      pagePath: "/pricing",
      pageReferrer: "https://news.example.org/weekly?issue=42",
      utmSource: "news",
      utmMedium: "email",
      utmCampaign: "august-launch",
      utmTerm: "pricing",
      utmContent: "hero-cta",
      screenWidth: 2560,
      screenHeight: 1440,
      viewportWidth: 1280,
      viewportHeight: 800,
      timezone: "Europe/Berlin",
      locale: "de-AT",
      visitPagesViewed: 4,
      visitUniquePagesViewed: 3,
      visitTimeOnSite: 125,
      visitEntryReferrer: "https://www.google.com/",
      visitUrls: "/ · /pricing · /plans · /pricing",
      openedByCreativeId: "clh7m1qkx0000qwer1234abcd",
      openedByCreativeName: "Feedback tab",
    },
  };

  const names = (display: "primary" | "secondary" | "none", response = captured): string[] =>
    listDisplayableReservedFields(RESERVED_FIELD_CATALOG, response, display).map(({ entry }) => entry.name);

  test("primary is exactly the seven both surfaces already showed", () => {
    // Pinned as a literal list, unlike the availability-derived assertions elsewhere: this set is a
    // product decision about what an existing response card and table look like, so a catalog entry
    // that silently joined it would be a visible regression rather than a new feature.
    expect(names("primary")).toStrictEqual([
      "source",
      "url",
      "country",
      "action",
      "browser",
      "os",
      "deviceType",
    ]);
  });

  test("secondary carries the browser-runtime context and the visit, plus ipAddress and the opening creative", () => {
    expect(names("secondary")).toStrictEqual([
      "ipAddress",
      "pagePath",
      "pageReferrer",
      "utmSource",
      "utmMedium",
      "utmCampaign",
      "utmTerm",
      "utmContent",
      "screenWidth",
      "screenHeight",
      "viewportWidth",
      "viewportHeight",
      "timezone",
      "locale",
      "visitPagesViewed",
      "visitUniquePagesViewed",
      "visitTimeOnSite",
      "visitEntryReferrer",
      "visitUrls",
      "openedByCreativeId",
      "openedByCreativeName",
    ]);
  });

  test("the three groups partition the catalog — no entry is listed twice or not at all", () => {
    expect([...names("primary"), ...names("secondary"), ...names("none")].sort()).toStrictEqual(
      RESERVED_FIELD_CATALOG.map((entry) => entry.name).sort()
    );
  });

  test("a response that carries nothing yields nothing — no empty rows, no placeholders", () => {
    // The pre-ENG-1841 case, and the acceptance criterion: such a response must render exactly as it
    // did before. Values are omitted, not blanked, so the surfaces need no rule of their own.
    const sparse = { ...captured, meta: {} };

    expect(names("secondary", sparse)).toStrictEqual([]);
    // The primary group survives on whatever is not in `meta`: nothing here, since all seven read it.
    expect(names("primary", sparse)).toStrictEqual([]);
  });

  test("with the drop fields anonymized away, they are absent rather than blank", () => {
    // `anonymize.ts` removes the keys at ingest, so this needs no read-time policy of its own —
    // duplicating that decision is how the two would drift.
    const anonymized = {
      ...captured,
      meta: { source: "link", url: "https://example.com/pricing", pagePath: "/pricing" },
    };

    expect(names("primary", anonymized)).toStrictEqual(["source", "url"]);
    expect(names("secondary", anonymized)).toStrictEqual(["pagePath"]);
  });

  test("values are rendered strings, and inherit the projection's redaction", () => {
    const fields = listDisplayableReservedFields(RESERVED_FIELD_CATALOG, captured, "primary");

    // `url` is `privacy: "redactQuery"`, so a display surface shows origin + path — the same value
    // recall gets, from the same projection, rather than a second reading of `meta.url`.
    expect(fields.find(({ entry }) => entry.name === "url")?.value).toBe("https://example.com/pricing");
    for (const { value } of fields) {
      expect(typeof value).toBe("string");
    }
  });

  test("numbers and booleans arrive as strings a surface can render directly", () => {
    const secondary = listDisplayableReservedFields(RESERVED_FIELD_CATALOG, captured, "secondary");

    expect(secondary.find(({ entry }) => entry.name === "viewportWidth")?.value).toBe("1280");
  });

  test("it does NOT drop an entry the survey declares — a display surface shows both", () => {
    // Unlike the value maps (ENG-2538), which must pick one meaning for a name. Here the author needs
    // to see their own field in its own section AND what was auto-captured, so this takes no survey.
    expect(names("primary")).toContain("country");
  });

  test("order follows the catalog, not the response", () => {
    // So a surface rendering these gets a stable order across responses without sorting.
    expect(names("primary")).toStrictEqual(
      RESERVED_FIELD_CATALOG.filter((entry) => entry.display === "primary").map((entry) => entry.name)
    );
  });
});

describe("mergeReservedValues", () => {
  test("reserved values fill keys the response does not have", () => {
    expect(mergeReservedValues({ country: "DE", url: "https://x.test" }, { q1: "answer" })).toStrictEqual({
      country: "DE",
      url: "https://x.test",
      q1: "answer",
    });
  });

  test("THE GRANDFATHER RULE: response data wins on a colliding key", () => {
    // Flip the two spreads in `mergeReservedValues` and this goes red. A survey stored today may
    // legitimately declare a hidden field named `country`; its value lives in `response.data` and
    // must keep resolving from there forever.
    expect(mergeReservedValues({ country: "DE" }, { country: "Declared answer" })).toStrictEqual({
      country: "Declared answer",
    });
  });

  test("a declared key wins even when its value is empty or zero", () => {
    // Falsy is still an answer — a `??`-style merge would wrongly fall through to the reserved value.
    expect(mergeReservedValues({ country: "DE" }, { country: "" })).toStrictEqual({ country: "" });
    expect(mergeReservedValues({ durationSeconds: 42 }, { durationSeconds: 0 })).toStrictEqual({
      durationSeconds: 0,
    });
  });

  test("neither input is mutated", () => {
    const reserved = { country: "DE" };
    const responseData = { q1: "answer" };

    mergeReservedValues(reserved, responseData);

    expect(reserved).toStrictEqual({ country: "DE" });
    expect(responseData).toStrictEqual({ q1: "answer" });
  });
});

describe("dropShadowedReservedEntries", () => {
  const names = (declared: string[]): string[] =>
    dropShadowedReservedEntries(RESERVED_FIELD_CATALOG, declared).map((entry) => entry.name);

  test("THE GRANDFATHER RULE, first half: a declared name removes the entry outright", () => {
    // The point of ENG-2538. `mergeReservedValues` only demotes a reserved value behind a key that
    // *exists*, so the entry has to be gone from the projection for a declared-but-empty field to
    // win. Asserting absence here, and the consequence — no key in the projected map — below.
    expect(names(["url"])).not.toContain("url");
    expect(names(["url"])).toContain("source");
  });

  test("unlike the picker, it keeps server-derived entries", () => {
    // The only difference between this and `listMidSurveyReservedEntries`, and the reason they are
    // two functions: the server value map needs `country` and `durationSeconds`, the picker must not
    // offer them.
    expect(names([])).toContain("country");
    expect(names([])).toContain("durationSeconds");
    expect(names([])).toStrictEqual(RESERVED_FIELD_CATALOG.map((entry) => entry.name));
  });

  test("shadowing is per name, not a blanket opt-out", () => {
    const offered = names(["url", "country"]);

    expect(offered).not.toContain("url");
    expect(offered).not.toContain("country");
    expect(offered).toContain("timezone");
    expect(offered).toHaveLength(RESERVED_FIELD_CATALOG.length - 2);
  });

  test("matching is case-sensitive, because the read namespace is", () => {
    // `response.data["Country"]` and a reserved `country` are different keys, so a survey holding a
    // legacy `Country` field still resolves the reserved `country` — dropping the entry would lose a
    // value that resolves today. New declarations are refused under any casing by
    // RESERVED_FIELD_NAMES, so this only ever concerns names that predate that guard.
    expect(names(["Country"])).toContain("country");
  });

  test("a name no catalog entry uses changes nothing", () => {
    expect(names(["plan", "some_element_id"])).toStrictEqual(names([]));
  });
});

describe("listShadowingNames", () => {
  const link = (storageKey: string): TLinkedEmbeddedField =>
    ({ field: { name: storageKey }, link: { storageKey } }) as unknown as TLinkedEmbeddedField;

  test("counts declared field storage keys and element ids alike", () => {
    expect(listShadowingNames([link("plan"), link("cm_var_1")], ["q1", "url"])).toStrictEqual([
      "plan",
      "cm_var_1",
      "q1",
      "url",
    ]);
  });

  test("element ids are in the list, which is the easy half to forget", () => {
    // A question with id `url` is absent from `response.data` until it is answered, so without this
    // the reserved read would win for exactly as long as the respondent had not reached it.
    expect(
      dropShadowedReservedEntries(RESERVED_FIELD_CATALOG, listShadowingNames([], ["url"])).map(
        (entry) => entry.name
      )
    ).not.toContain("url");
  });

  test("empty inputs yield an empty list rather than undefined", () => {
    expect(listShadowingNames([], [])).toStrictEqual([]);
  });
});

describe("listMidSurveyReservedEntries", () => {
  const names = (declared: string[]): string[] =>
    listMidSurveyReservedEntries(RESERVED_FIELD_CATALOG, declared).map((entry) => entry.name);

  test("offers only what a browser can resolve mid-survey", () => {
    // Derived from the catalog's own `availability` rather than a literal list, because the catalog
    // grows: ENG-1841 added twelve client entries at once, and a pinned list turns every such
    // addition into a failing test that says nothing about this function. Re-deriving from the data
    // still checks the contract — that `availability` is the only thing consulted, and that no
    // server-derived entry leaks through. Which specific names must never appear is pinned below.
    const clientResolvable = RESERVED_FIELD_CATALOG.filter((entry) => entry.availability !== "server").map(
      (entry) => entry.name
    );

    expect(names([]).sort()).toStrictEqual([...clientResolvable].sort());
  });

  test("every server-derived field named by the ticket is absent", () => {
    // The ticket's own description lists browser/os/deviceType as available during the survey. They
    // are not on this branch: UAParser derives all three from the `user-agent` header in the ingest
    // routes, so the renderer cannot know them. The catalog's `availability` is the authority.
    const offered = names([]);

    for (const serverOnly of [
      "country",
      "ipAddress",
      "browser",
      "os",
      "deviceType",
      "durationSeconds",
      "finished",
      "responseId",
      "startedAt",
      "finishedAt",
    ]) {
      expect(offered).not.toContain(serverOnly);
    }
  });

  test("a declared field of the same name removes the reserved entry from the picker", () => {
    // Otherwise the author sees two identically-named rows with no way to tell them apart, and the
    // reserved one would be a lie: `mergeReservedValues` already resolves that name to the declared
    // field's value.
    expect(names(["url"])).not.toContain("url");
    expect(names(["url"])).toContain("source");
  });

  test("shadowing a server-only name changes nothing, since it was never offered", () => {
    // Stated as "same as declaring nothing", which is the claim, and which no catalog addition breaks.
    expect(names(["country"])).toStrictEqual(names([]));
  });

  test("entries are returned as catalog entries, carrying their dataType", () => {
    const entry = listMidSurveyReservedEntries(RESERVED_FIELD_CATALOG, []).find((e) => e.name === "url");

    expect(entry?.dataType).toBe("string");
    expect(entry?.availability).not.toBe("server");
  });
});

describe("projectIngestedDefaults", () => {
  const surveyWith = (...fields: TLinkedEmbeddedField["field"][]) => ({
    embeddedFields: fields.map((field, index) => ({ field, link: { storageKey: `k${index}` } })),
  });

  test("a field nothing arrived for answers with its default", () => {
    // The regression (ENG-3266 follow-up): ingest never writes `defaultValue`, and nothing read it
    // back, so a recall token rendered its own `fallback:` text and a logic condition read unset.
    const survey = surveyWith(makeField({ dataType: "number", defaultValue: 20 }));

    expect(projectIngestedDefaults(survey, {})).toStrictEqual({ k0: 20 });
  });

  test("a supplied value wins, including the ones that look empty", () => {
    const survey = surveyWith(
      makeField({ dataType: "number", defaultValue: 20 }),
      makeField({ dataType: "string", defaultValue: "fallback" })
    );

    // `0` and `""` are values the respondent supplied; only the caller's merge order can protect
    // them, and it only can if this function leaves their keys out.
    expect(projectIngestedDefaults(survey, { k0: 0, k1: "" })).toStrictEqual({});
  });

  test("a stored value that does not fit the type resolves to the default", () => {
    // What `?pts=name` leaves behind: ingest keeps it verbatim and flags `coercion_failed`, and the
    // survey should show the declared fallback rather than the text that failed to be a number.
    const survey = surveyWith(makeField({ dataType: "number", defaultValue: 20 }));

    expect(projectIngestedDefaults(survey, { k0: "name" })).toStrictEqual({ k0: 20 });
  });

  test("a field with no default contributes no key", () => {
    const survey = surveyWith(makeField({ dataType: "number" }));

    expect(projectIngestedDefaults(survey, {})).toStrictEqual({});
  });

  test("a boolean default is stringified the way ingest stores one", () => {
    // `TResponseData` has no boolean member, and the read seams downstream compare on `"true"`.
    const survey = surveyWith(makeField({ dataType: "boolean", defaultValue: true }));

    expect(projectIngestedDefaults(survey, {})).toStrictEqual({ k0: "true" });
  });

  test("a locked field answers with its default even when storage holds a value", () => {
    // Locking is retroactive: a field locked after its survey collected responses has values that
    // were legitimate when they arrived, and `resolveEmbeddedValue` answers with the default for
    // them. Leaving `locked` to `applyIngestContract` alone would disagree on exactly that survey.
    const survey = surveyWith(makeField({ dataType: "number", defaultValue: 20, locked: true }));

    expect(projectIngestedDefaults(survey, { k0: 99 })).toStrictEqual({ k0: 20 });
  });

  test("computed fields are not this function's business", () => {
    // They are seeded from their own definitions into `variables`, which is a different map.
    const survey = surveyWith(makeField({ source: "computed", dataType: "number", defaultValue: 7 }));

    expect(projectIngestedDefaults(survey, {})).toStrictEqual({});
  });
});

describe("buildEmbeddedLookup", () => {
  const survey = {
    embeddedFields: [
      { field: makeField({ dataType: "number", defaultValue: 20 }), link: { storageKey: "pts" } },
    ],
  };

  test("the response beats the default", () => {
    expect(buildEmbeddedLookup(survey, {}, { pts: 5 })).toStrictEqual({ pts: 5 });
  });

  test("the default answers when the response has nothing", () => {
    expect(buildEmbeddedLookup(survey, {}, {})).toStrictEqual({ pts: 20 });
  });

  test("the response beats a reserved entry of the same name", () => {
    // The layer order this function exists to pin: flipping any pair here is a regression that used
    // to live in two identical spreads inside a component, where nothing could fail on it.
    expect(buildEmbeddedLookup(survey, { url: "reserved" }, { url: "answered", pts: 5 })).toStrictEqual({
      url: "answered",
      pts: 5,
    });
  });

  test("a stored value that does not fit the type loses to the default", () => {
    // What `?pts=name` leaves behind. `projectIngestedDefaults` gets this right on its own, so the
    // only thing that can break it is the merge: the response layer carries the uncoercible value
    // too, and spreading it last puts `"name"` back over the 20 the default just supplied.
    expect(buildEmbeddedLookup(survey, {}, { pts: "name" })).toStrictEqual({ pts: 20 });
  });

  test("a supplied value its own type accepts is an answer, falsy ones included", () => {
    // The falsy cases the last spread must not overwrite — and they are per type, not universal:
    // `0` is a number, `""` is a string, and each coerces under its own field, so neither emits a
    // default. An `""` under a *number* field is not this case: it does not coerce, and the resolver
    // answers with the default for it, which is what the row above pins.
    const text = {
      embeddedFields: [{ field: makeField({ defaultValue: "fallback" }), link: { storageKey: "who" } }],
    };

    expect(buildEmbeddedLookup(survey, { pts: 7 }, { pts: 0 })).toStrictEqual({ pts: 0 });
    expect(buildEmbeddedLookup(text, {}, { who: "" })).toStrictEqual({ who: "" });
  });
});
