import { describe, expect, test } from "vitest";
import type { TLinkedEmbeddedField } from "../embedded-data-resolver";
import {
  type TDeclaredFieldSource,
  collectDeclaredFieldNames,
  describeDeclaredFieldNameError,
  describeDeclaredFieldNameErrors,
  validateNewDeclaredFieldClashes,
  validateNewDeclaredFieldNames,
  validateNewDeclaredFields,
  validateNewElementIdClashes,
  validateNewQuestionCodeClashes,
} from "./declared-field-guard";
import { TValidateIdErrorCode } from "./validation";

const refusedNames = (params: { existing: string[]; incoming: string[] }): string[] =>
  validateNewDeclaredFieldNames(params).map((error) => error.field);

/** A survey's declared fields, spelled the way `TSurvey` carries them. */
const declared = (fields: { variables?: string[]; hiddenFields?: string[] }): TDeclaredFieldSource => ({
  variables: fields.variables?.map((name) => ({ id: `var_${name}`, name, type: "text", value: "" })),
  hiddenFields: fields.hiddenFields === undefined ? undefined : { fieldIds: fields.hiddenFields },
});

const clashes = (params: { existing: TDeclaredFieldSource; incoming: TDeclaredFieldSource }): string[] =>
  validateNewDeclaredFieldClashes(params).map((error) => error.field);

describe("validateNewDeclaredFieldNames", () => {
  describe("grandfathering", () => {
    test("a reserved name already in `existing` passes", () => {
      // The whole point of the ticket: surveys in production already declare these, their values
      // live at response.data["country"], and nothing may be renamed.
      expect(refusedNames({ existing: ["country"], incoming: ["country"] })).toEqual([]);
      expect(
        refusedNames({
          existing: ["country", "url", "source", "browser"],
          incoming: ["country", "url", "source", "browser"],
        })
      ).toEqual([]);
    });

    test("the same name absent from `existing` is refused", () => {
      expect(refusedNames({ existing: [], incoming: ["country"] })).toEqual(["country"]);
      expect(refusedNames({ existing: ["other_field"], incoming: ["country"] })).toEqual(["country"]);
    });

    test("a grandfathered survey may still not add a *different* reserved name", () => {
      expect(refusedNames({ existing: ["country"], incoming: ["country", "browser"] })).toEqual(["browser"]);
    });

    test("grandfathering is per name, not a blanket exemption for the payload", () => {
      expect(refusedNames({ existing: ["country"], incoming: ["country", "team_size", "url"] })).toEqual([
        "url",
      ]);
    });

    test("the reprieve is spent by deleting the field: re-adding it afterwards is refused", () => {
      // `existing` is the survey's CURRENT names, so grandfathering lasts as long as the field does.
      // Pinned because it is a decision, not an accident: remembering every name a survey ever had
      // would need storage this layer has no access to, and a survey that gives up its declared
      // `country` gains the auto-captured one in exchange.
      const survey = { existing: ["country", "team_size"] };

      // Still there, still fine — the save that keeps it, and the save that drops it, both pass.
      expect(refusedNames({ ...survey, incoming: ["country", "team_size"] })).toEqual([]);
      expect(refusedNames({ ...survey, incoming: ["team_size"] })).toEqual([]);

      // After that save the survey no longer declares it, so the name is new again.
      expect(refusedNames({ existing: ["team_size"], incoming: ["team_size", "country"] })).toEqual([
        "country",
      ]);
    });
  });

  describe("case-insensitivity", () => {
    test("a reserved name is refused under any casing", () => {
      expect(refusedNames({ existing: [], incoming: ["Country"] })).toEqual(["Country"]);
      expect(refusedNames({ existing: [], incoming: ["COUNTRY"] })).toEqual(["COUNTRY"]);
      // camelCase catalog entries are stored lowercased in the reserved set.
      expect(refusedNames({ existing: [], incoming: ["deviceType"] })).toEqual(["deviceType"]);
    });

    test("`existing` grandfathers across casing too", () => {
      expect(refusedNames({ existing: ["Country"], incoming: ["country"] })).toEqual([]);
      expect(refusedNames({ existing: ["country"], incoming: ["Country"] })).toEqual([]);
    });

    test("a duplicated incoming name yields at most one error", () => {
      expect(refusedNames({ existing: [], incoming: ["country", "Country"] })).toEqual(["country"]);
    });
  });

  describe("which names are refused", () => {
    test("refuses Tier-1 reserved field names (RESERVED_FIELD_NAMES)", () => {
      for (const name of ["source", "url", "country", "browser", "os", "finished", "language"]) {
        expect(refusedNames({ existing: [], incoming: [name] })).toEqual([name]);
      }
    });

    test("refuses link-survey system params and forbidden ids (RESERVED_DECLARED_FIELD_NAMES)", () => {
      for (const name of ["userid", "lang", "suid"]) {
        expect(refusedNames({ existing: [], incoming: [name] })).toEqual([name]);
      }
    });

    test("refuses names that are not safe identifiers", () => {
      const errors = validateNewDeclaredFieldNames({ existing: [], incoming: ["Team Size"] });
      expect(errors).toHaveLength(1);
      expect(errors[0].code).not.toBe(TValidateIdErrorCode.Reserved);
    });

    test("allows an ordinary new name", () => {
      expect(refusedNames({ existing: [], incoming: ["team_size", "plan", "signup_source"] })).toEqual([]);
    });

    test("reports a reserved name with the Reserved code", () => {
      const errors = validateNewDeclaredFieldNames({ existing: [], incoming: ["country"] });
      expect(errors).toEqual([{ code: TValidateIdErrorCode.Reserved, field: "country" }]);
    });

    test("a name colliding with an existing one is not reported as a duplicate", () => {
      // Duplicate detection belongs to the reconcile and the v3 reference validation; reporting it
      // here would turn every grandfathered name into an error.
      expect(refusedNames({ existing: ["team_size"], incoming: ["team_size"] })).toEqual([]);
    });
  });
});

describe("collectDeclaredFieldNames", () => {
  test("collects variable names and hidden field ids", () => {
    expect(
      collectDeclaredFieldNames({
        variables: [{ id: "v1", name: "score", type: "number", value: 0 }],
        hiddenFields: { fieldIds: ["team_size", "plan"] },
      })
    ).toEqual(["score", "team_size", "plan"]);
  });

  test("a payload omitting `hiddenFields` declares nothing from it", () => {
    // Presence is `!== undefined`, not `in` — mirroring `resolveDesiredEmbeddedFields`. A patch that
    // never mentioned hiddenFields must not read as declaring the empty set, and must not be
    // validated as if it had re-declared the survey's stored fields either.
    expect(collectDeclaredFieldNames({ variables: [], hiddenFields: undefined })).toEqual([]);
    expect(collectDeclaredFieldNames({})).toEqual([]);
  });

  test("an object literal with both keys spelled out but undefined declares nothing", () => {
    // The exact shape every write seam builds.
    const payload = { variables: undefined, hiddenFields: undefined };
    expect("hiddenFields" in payload).toBe(true);
    expect(collectDeclaredFieldNames(payload)).toEqual([]);
  });

  test("a null carrier declares nothing", () => {
    expect(collectDeclaredFieldNames({ variables: null, hiddenFields: null })).toEqual([]);
  });

  test("hiddenFields present with no fieldIds declares nothing", () => {
    expect(collectDeclaredFieldNames({ hiddenFields: {} })).toEqual([]);
  });

  test("a payload that omits hiddenFields validates no hidden field name", () => {
    expect(
      validateNewDeclaredFieldNames({
        existing: [],
        incoming: collectDeclaredFieldNames({ variables: [], hiddenFields: undefined }),
      })
    ).toEqual([]);
  });
});

describe("describeDeclaredFieldNameError", () => {
  const reasonFor = (name: string): string =>
    describeDeclaredFieldNameError(validateNewDeclaredFieldNames({ existing: [], incoming: [name] })[0]);

  test("a Tier-1 catalog name is not described as unfillable", () => {
    // `RESERVED_FIELD_NAMES` is deliberately absent from the capture-refusal list, so `?country=DE`
    // DOES fill a survey's declared `country`. Telling an integrator otherwise could send them off to
    // remove URL params that work.
    const reason = reasonFor("country");

    expect(reason).toContain("auto-captured system field");
    expect(reason).not.toContain("URL contract");
  });

  test("a link-survey system param is described as the URL contract's own, because it is", () => {
    // `getHiddenFieldsFromSearchParams` never fills a field declared under one of these spellings.
    const reason = reasonFor("lang");

    expect(reason).toContain('"lang"');
    expect(reason).toContain("never filled from the URL");
  });

  test("a case variant is refused by naming the reserved spelling it collides with, not the one sent", () => {
    // A grandfathered `userid` field IS filled by `?userid=` (ENG-2892), so the true statement is about
    // `userId`, the spelling the URL contract reserves: a field of THAT name is never filled.
    const reason = reasonFor("userid");

    expect(reason).toContain('"userId"');
    expect(reason).toContain("never filled from the URL");
  });

  test("a name in both lists takes the URL-contract reason", () => {
    // `source` is the one Tier-1 field that is also a link-survey system param. Both statements are
    // true of it; the stronger one wins.
    const reason = reasonFor("source");

    expect(reason).toContain("URL contract");
    expect(reason).not.toContain("auto-captured system field");
  });

  test("a name that is merely not a safe identifier keeps the naming-rule reason", () => {
    // `UserRegion`, not `Team Size`: a space is refused by the shared `HasSpaces` check BEFORE
    // `isSafeIdentifier` is consulted, so the old spelling never exercised this reason — it passed
    // only while every non-reserved code returned the same sentence (the drift ENG-2539's review
    // caught). `UserRegion` genuinely produces `NotSafeIdentifier`.
    const reason = reasonFor("UserRegion");

    expect(reason).toContain("lowercase letter");
    expect(reason).not.toContain("reserved");
  });

  // ENG-2539: the reason follows the check that fired. One sentence for everything non-reserved sent
  // a caller in circles — told about lowercase letters when the space was the whole problem, and ""
  // read identically to a charset violation.
  describe("the reason names the check that actually fired", () => {
    test("a space names the space, not the charset", () => {
      const reason = reasonFor("team size");

      expect(reason).toContain("spaces");
      expect(reason).not.toContain("lowercase letter");
    });

    test("an illegal character names the legacy charset, not the strict one", () => {
      const reason = reasonFor("user:name");

      expect(reason).toContain("letters, numbers, underscores and hyphens");
      expect(reason).not.toContain("lowercase letter");
    });

    test("an empty name says so", () => {
      const reason = reasonFor("");

      expect(reason).toContain("must not be empty");
      expect(reason).not.toContain("lowercase letter");
    });
  });
});

describe("describeDeclaredFieldNameErrors", () => {
  test("names every refused field and says existing fields keep working", () => {
    const message = describeDeclaredFieldNameErrors(
      validateNewDeclaredFieldNames({ existing: [], incoming: ["country", "url"] })
    );

    expect(message).toContain('"country"');
    expect(message).toContain('"url"');
    expect(message).toContain("newly added names only");
  });
});

// ENG-2933: `PUT /api/v1/management/surveys/{id}` accepted a variable named after one of the survey's
// hidden fields. The editor and v3 both refuse it, and the reconcile cannot catch it (a variable is
// stored under its id, a hidden field under its name), so the name guard is where it belongs.
describe("validateNewDeclaredFieldClashes", () => {
  describe("a new clash is refused", () => {
    test("a variable may not take an existing hidden field's name", () => {
      expect(
        clashes({
          existing: declared({ hiddenFields: ["plan"] }),
          incoming: declared({ hiddenFields: ["plan"], variables: ["plan"] }),
        })
      ).toEqual(["plan"]);
    });

    test("a hidden field may not take an existing variable's name", () => {
      expect(
        clashes({
          existing: declared({ variables: ["score"] }),
          incoming: declared({ variables: ["score"], hiddenFields: ["score"] }),
        })
      ).toEqual(["score"]);
    });

    test("a create may not declare both sides under one name", () => {
      expect(
        clashes({
          existing: {},
          incoming: declared({ variables: ["plan"], hiddenFields: ["plan"] }),
        })
      ).toEqual(["plan"]);
    });

    test("matching is case-insensitive, as in the editor and v3", () => {
      expect(
        clashes({
          existing: declared({ hiddenFields: ["Plan"] }),
          incoming: declared({ hiddenFields: ["Plan"], variables: ["plan"] }),
        })
      ).toEqual(["plan"]);
    });

    test("reports the Duplicate code the editor's Embedded Data card reports for the same clash", () => {
      expect(
        validateNewDeclaredFieldClashes({
          existing: declared({ hiddenFields: ["plan"] }),
          incoming: declared({ hiddenFields: ["plan"], variables: ["plan"] }),
        })
      ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
    });

    test("the error names the side the write introduces", () => {
      // The variable already existed, so the hidden field is the newcomer — and the message should
      // echo the spelling the caller just sent, not the one already stored.
      expect(
        clashes({
          existing: declared({ variables: ["plan"] }),
          incoming: declared({ variables: ["plan"], hiddenFields: ["Plan"] }),
        })
      ).toEqual(["Plan"]);
      expect(
        clashes({
          existing: declared({ hiddenFields: ["plan"] }),
          incoming: declared({ hiddenFields: ["plan"], variables: ["PLAN"] }),
        })
      ).toEqual(["PLAN"]);
    });
  });

  describe("grandfathering", () => {
    // 46 production surveys hold this state (`first_name`, `brand`, `score`, ...). Their plain
    // read-modify-write PUT resends both sides, and a blanket refusal would break every one of them.
    const holdsClash = declared({ variables: ["first_name", "score"], hiddenFields: ["first_name", "plan"] });

    test("a survey that already holds the clash may resend it", () => {
      expect(clashes({ existing: holdsClash, incoming: holdsClash })).toEqual([]);
    });

    test("grandfathering survives a case change on either side", () => {
      expect(
        clashes({
          existing: declared({ variables: ["First_Name"], hiddenFields: ["first_name"] }),
          incoming: declared({ variables: ["first_name"], hiddenFields: ["FIRST_NAME"] }),
        })
      ).toEqual([]);
    });

    test("a grandfathered clash does not license a new one", () => {
      expect(
        clashes({
          existing: holdsClash,
          incoming: declared({
            variables: ["first_name", "score", "plan"],
            hiddenFields: ["first_name", "plan"],
          }),
        })
      ).toEqual(["plan"]);
    });

    test("the reprieve is per pair: a name that is only a variable today is not grandfathered as a clash", () => {
      // `score` is declared (as a variable), so the reserved-name guard would grandfather it — but
      // no hidden field shares it yet, so adding one is a NEW clash.
      expect(
        clashes({
          existing: holdsClash,
          incoming: declared({
            variables: ["first_name", "score"],
            hiddenFields: ["first_name", "plan", "score"],
          }),
        })
      ).toEqual(["score"]);
    });

    test("dropping one side spends the reprieve", () => {
      // The save that drops the variable passes; after it the survey no longer holds the clash, so
      // adding the variable back is refused like any new clash.
      expect(
        clashes({
          existing: holdsClash,
          incoming: declared({ variables: ["score"], hiddenFields: ["first_name", "plan"] }),
        })
      ).toEqual([]);
      expect(
        clashes({
          existing: declared({ variables: ["score"], hiddenFields: ["first_name", "plan"] }),
          incoming: declared({ variables: ["score", "first_name"], hiddenFields: ["first_name", "plan"] }),
        })
      ).toEqual(["first_name"]);
    });
  });

  describe("what the write leaves in place", () => {
    test("a carrier the payload never mentions is the survey's current one", () => {
      // The reconcile carries an unmentioned carrier's rows over unchanged, so a payload that sends
      // only `variables` still ends up beside the hidden fields it did not mention.
      expect(
        clashes({
          existing: declared({ hiddenFields: ["plan"] }),
          incoming: declared({ variables: ["plan"] }),
        })
      ).toEqual(["plan"]);
      expect(
        clashes({
          existing: declared({ variables: ["plan"] }),
          incoming: declared({ hiddenFields: ["plan"] }),
        })
      ).toEqual(["plan"]);
    });

    test("a null carrier is treated as unmentioned, not as empty", () => {
      expect(
        clashes({
          existing: declared({ hiddenFields: ["plan"] }),
          incoming: { variables: declared({ variables: ["plan"] }).variables, hiddenFields: null },
        })
      ).toEqual(["plan"]);
    });

    test("moving a name across namespaces in one write is not a clash", () => {
      // Hidden field `plan` becomes variable `plan`: after the write only one field carries the name.
      expect(
        clashes({
          existing: declared({ hiddenFields: ["plan"] }),
          incoming: declared({ hiddenFields: [], variables: ["plan"] }),
        })
      ).toEqual([]);
    });

    test("distinct names never clash", () => {
      expect(
        clashes({
          existing: {},
          incoming: declared({ variables: ["score", "tier"], hiddenFields: ["plan", "user_region"] }),
        })
      ).toEqual([]);
    });
  });
});

describe("validateNewElementIdClashes", () => {
  /** Blocks holding one element per id, as `TSurvey.blocks` carries them. */
  const blocks = (...elementIds: string[]) => [{ elements: elementIds.map((id) => ({ id })) }];

  const elementClashes = (params: { existing: TDeclaredFieldSource; incoming: TDeclaredFieldSource }) =>
    validateNewElementIdClashes(params).map((error) => error.field);

  describe("a new clash is refused", () => {
    test("a hidden field may not take an existing element's id", () => {
      expect(
        elementClashes({
          existing: { blocks: blocks("plan") },
          incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: blocks("plan") },
        })
      ).toEqual(["plan"]);
    });

    test("an element may not take an existing hidden field's name", () => {
      expect(
        elementClashes({
          existing: { ...declared({ hiddenFields: ["plan"] }), blocks: blocks("q1") },
          incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: blocks("plan") },
        })
      ).toEqual(["plan"]);
    });

    test("a create may not declare both under one name", () => {
      expect(
        validateNewElementIdClashes({
          existing: {},
          incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: blocks("plan") },
        })
      ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
    });

    test("matching is case-insensitive, and the error names the side the write introduces", () => {
      // Only an exact match is dead at storage, but recall and logic address both by name — so the
      // editor and v3 refuse a case variant, and so does this.
      expect(
        elementClashes({
          existing: { blocks: blocks("Plan") },
          incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: blocks("Plan") },
        })
      ).toEqual(["plan"]);
      expect(
        elementClashes({
          existing: { ...declared({ hiddenFields: ["plan"] }), blocks: [] },
          incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: blocks("PLAN") },
        })
      ).toEqual(["PLAN"]);
    });

    test("an ingested row is checked by its storage key", () => {
      expect(
        elementClashes({
          existing: { blocks: blocks("plan") },
          incoming: {
            embeddedFields: [
              {
                field: {
                  key: null,
                  name: "Plan tier",
                  source: "ingested",
                  dataType: "string",
                  defaultValue: null,
                  locked: false,
                },
                link: { storageKey: "plan" },
              },
            ],
            blocks: blocks("plan"),
          },
        })
      ).toEqual(["plan"]);
    });
  });

  describe("grandfathering", () => {
    const holdsClash = { ...declared({ hiddenFields: ["plan", "tier"] }), blocks: blocks("plan") };

    test("a survey that already holds the clash may resend it", () => {
      expect(elementClashes({ existing: holdsClash, incoming: holdsClash })).toEqual([]);
    });

    test("a grandfathered clash does not license a new one", () => {
      expect(
        elementClashes({
          existing: holdsClash,
          incoming: { ...declared({ hiddenFields: ["plan", "tier"] }), blocks: blocks("plan", "tier") },
        })
      ).toEqual(["tier"]);
    });
  });

  describe("what the write leaves in place", () => {
    test("blocks the payload never mentions are the survey's current ones", () => {
      expect(
        elementClashes({
          existing: { blocks: blocks("plan") },
          incoming: declared({ hiddenFields: ["plan"] }),
        })
      ).toEqual(["plan"]);
    });

    test("empty blocks are the survey's current ones too, because the update keeps them", () => {
      expect(
        elementClashes({
          existing: { blocks: blocks("plan") },
          incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: [] },
        })
      ).toEqual(["plan"]);
    });

    test("a variable under an element's id is out of scope here", () => {
      // A variable is stored under its cuid, not its name, so it never loses its value to an answer.
      expect(
        elementClashes({
          existing: {},
          incoming: { ...declared({ variables: ["plan"] }), blocks: blocks("plan") },
        })
      ).toEqual([]);
    });

    test("malformed draft blocks are skipped rather than thrown on", () => {
      // The draft save runs the guard over blocks `ZSurveyDraft` only checks as records.
      expect(
        elementClashes({
          existing: {},
          incoming: {
            ...declared({ hiddenFields: ["plan"] }),
            blocks: [null, {}, { elements: "plan" }, { elements: [null, { id: 7 }, { id: "plan" }] }],
          },
        })
      ).toEqual(["plan"]);
    });
  });
});

describe("validateNewDeclaredFields", () => {
  test("reports reserved names and clashes together", () => {
    expect(
      validateNewDeclaredFields({
        existing: {},
        incoming: declared({ variables: ["country", "plan"], hiddenFields: ["plan"] }),
      })
    ).toEqual([
      { code: TValidateIdErrorCode.Reserved, field: "country" },
      { code: TValidateIdErrorCode.Duplicate, field: "plan" },
    ]);
  });

  test("a name refused as reserved is not reported a second time as a clash", () => {
    const errors = validateNewDeclaredFields({
      existing: {},
      incoming: declared({ variables: ["country"], hiddenFields: ["country"] }),
    });

    expect(errors).toEqual([{ code: TValidateIdErrorCode.Reserved, field: "country" }]);
  });

  test("a name in both clashes is reported once", () => {
    expect(
      validateNewDeclaredFields({
        existing: {},
        incoming: {
          ...declared({ variables: ["plan"], hiddenFields: ["plan"] }),
          blocks: [{ elements: [{ id: "plan" }] }],
        },
      })
    ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
  });

  test("grandfathers a reserved name and a clash the survey already holds", () => {
    const survey = declared({ variables: ["country", "first_name"], hiddenFields: ["first_name"] });

    expect(validateNewDeclaredFields({ existing: survey, incoming: survey })).toEqual([]);
  });

  test("the clash sentence says what is wrong without claiming the other side came first", () => {
    const [error] = validateNewDeclaredFields({
      existing: {},
      incoming: declared({ variables: ["plan"], hiddenFields: ["plan"] }),
    });

    expect(describeDeclaredFieldNameError(error)).toBe(
      'Field name "plan" cannot be used: a second field in this survey would carry that name, and recall and logic address fields by name.'
    );
  });
});

/**
 * ENG-3228: the V2 carrier. A payload that declares its fields as rows says everything the two
 * legacy keys said and more, so the guard has to read it instead of them — otherwise a panel edit
 * would pass the reserved-name and clash checks by declaring nothing the guard can see.
 */
describe("the embeddedFields carrier", () => {
  const entry = (
    name: string,
    source: "computed" | "ingested",
    overrides: { key?: string; storageKey?: string } = {}
  ): TLinkedEmbeddedField => ({
    field: {
      key: overrides.key ?? null,
      name,
      source,
      dataType: "string",
      defaultValue: null,
      locked: false,
    },
    link: { storageKey: overrides.storageKey ?? name },
  });

  test("names a computed field by its name and an ingested one by its storage key", () => {
    // Not the same column for both: recall addresses a variable by name and a hidden field by the
    // key its value arrives under, which is exactly what the legacy columns carried.
    expect(
      collectDeclaredFieldNames({
        embeddedFields: [entry("score", "computed"), entry("Plan tier", "ingested", { storageKey: "plan" })],
      })
    ).toEqual(["score", "plan"]);
  });

  test("takes precedence over the legacy keys in the same payload", () => {
    expect(
      collectDeclaredFieldNames({
        embeddedFields: [entry("score", "computed")],
        ...declared({ variables: ["ignored_variable"], hiddenFields: ["ignored_hidden_field"] }),
      })
    ).toEqual(["score"]);
  });

  test("refuses a newly declared reserved name, like any other carrier", () => {
    expect(
      validateNewDeclaredFields({
        existing: {},
        incoming: { embeddedFields: [entry("country", "ingested")] },
      })
    ).toEqual([{ code: TValidateIdErrorCode.Reserved, field: "country" }]);
  });

  test("grandfathers a reserved name the survey's rows already hold", () => {
    const survey = { embeddedFields: [entry("country", "ingested")] };

    expect(validateNewDeclaredFields({ existing: survey, incoming: survey })).toEqual([]);
  });

  test("refuses a computed and an ingested field that would share a name", () => {
    expect(
      validateNewDeclaredFields({
        existing: {},
        incoming: { embeddedFields: [entry("plan", "computed"), entry("plan", "ingested")] },
      })
    ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
  });

  describe("a newly linked shared field", () => {
    test("is checked by its library key against the survey's local names", () => {
      // The link is authored here even though the definition is not, so its key enters this survey's
      // recall namespace for the first time and has to be checked like any other new name.
      expect(
        validateNewDeclaredFields({
          existing: { embeddedFields: [entry("plan", "computed")] },
          incoming: {
            embeddedFields: [
              entry("plan", "computed"),
              entry("Plan tier", "ingested", { key: "plan", storageKey: "plan" }),
            ],
          },
        })
      ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
    });

    test("is named by its key rather than its label, which is never an identifier", () => {
      // A library field displayed as `Plan tier` is perfectly legal; checking the label would refuse
      // every link to one, because `validateId` demands a safe identifier from a new name.
      expect(
        validateNewDeclaredFields({
          existing: {},
          incoming: {
            embeddedFields: [entry("Plan tier", "computed", { key: "plan_tier", storageKey: "cuid_1" })],
          },
        })
      ).toEqual([]);
    });

    test("clashes case-insensitively, like every other name here", () => {
      expect(
        validateNewDeclaredFields({
          existing: { embeddedFields: [entry("Plan", "computed")] },
          incoming: {
            embeddedFields: [
              entry("Plan", "computed"),
              entry("Plan tier", "ingested", { key: "plan", storageKey: "plan" }),
            ],
          },
        })
      ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
    });
  });
});

describe("validateNewQuestionCodeClashes (ENG-6778)", () => {
  /** Blocks holding one coded element per code, as `TSurvey.blocks` carries them. */
  const coded = (...codes: string[]) => [{ elements: codes.map((code, i) => ({ id: `e${i}`, code })) }];

  const codeClashes = (params: { existing: TDeclaredFieldSource; incoming: TDeclaredFieldSource }) =>
    validateNewQuestionCodeClashes(params).map((error) => error.field);

  test("a new hidden field may not take a question code, ignoring case", () => {
    expect(
      codeClashes({
        existing: { blocks: coded("q1") },
        incoming: { ...declared({ hiddenFields: ["Q1"] }), blocks: coded("q1") },
      })
    ).toEqual(["Q1"]);
  });

  test("a new variable may not take a question code", () => {
    expect(
      codeClashes({
        existing: { blocks: coded("score") },
        incoming: { ...declared({ variables: ["score"] }), blocks: coded("score") },
      })
    ).toEqual(["score"]);
  });

  test("a create declaring both under one name is refused", () => {
    expect(
      validateNewQuestionCodeClashes({
        existing: {},
        incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: coded("plan") },
      })
    ).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
  });

  test("a clash the survey already holds is grandfathered", () => {
    const both = { ...declared({ hiddenFields: ["plan"] }), blocks: coded("plan") };
    expect(codeClashes({ existing: both, incoming: both })).toEqual([]);
  });

  test("blocks the payload does not carry are the survey's current ones", () => {
    expect(
      codeClashes({
        existing: { blocks: coded("plan") },
        incoming: declared({ hiddenFields: ["plan"] }),
      })
    ).toEqual(["plan"]);
  });

  test("distinct names, uncoded elements and malformed blocks never clash", () => {
    expect(
      codeClashes({
        existing: {},
        incoming: {
          ...declared({ hiddenFields: ["plan"] }),
          blocks: [{ elements: [{ id: "plan" }, null] }, "nope", { elements: [{ id: "x", code: "q1" }] }],
        },
      })
    ).toEqual([]);
  });

  test("validateNewDeclaredFields reports it once, with the other clashes", () => {
    const errors = validateNewDeclaredFields({
      existing: { blocks: coded("plan") },
      incoming: { ...declared({ hiddenFields: ["plan"] }), blocks: coded("plan") },
    });
    expect(errors).toEqual([{ code: TValidateIdErrorCode.Duplicate, field: "plan" }]);
  });
});
