import { mockValidateFileUploads } from "./__mocks__/storage-utils";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { problemForbidden } from "@/app/api/v3/lib/response";
import { can } from "@/lib/authorization";
import { createV3Response, updateV3Response } from "./operations";

// Exercise response writes with an authorized actor; denial behavior is covered in visibility.test.ts.
vi.mock("@/lib/authzed/scope-readiness", () => ({ isSurveyVisibilityReady: vi.fn(async () => false) }));
vi.mock("@/lib/authorization", () => ({ can: vi.fn(async () => true) }));
vi.mock("server-only", () => ({}));
vi.mock("node:crypto", async (importOriginal) => await importOriginal<typeof import("node:crypto")>());
vi.mock("crypto", async (importOriginal) => await importOriginal<typeof import("crypto")>());

const {
  mockRequireAccess,
  mockGetSurveyForWrite,
  mockCreate,
  mockUpdate,
  mockReadback,
  mockDispatch,
  mockGetWorkspaceId,
  mockGetScoped,
  mockValidateResponseData,
  mockToResource,
} = vi.hoisted(() => ({
  mockRequireAccess: vi.fn(),
  mockGetSurveyForWrite: vi.fn(),
  mockCreate: vi.fn(),
  mockUpdate: vi.fn(),
  mockReadback: vi.fn(),
  mockDispatch: vi.fn(),
  mockGetWorkspaceId: vi.fn(),
  mockGetScoped: vi.fn(),
  mockValidateResponseData: vi.fn(),
  mockToResource: vi.fn(),
}));

vi.mock("@/app/api/v3/lib/auth", () => ({
  requireV3WorkspaceAccess: mockRequireAccess,
  getV3AuthorizationActor: () => ({ type: "apiKey", id: "key" }),
}));
vi.mock("@/modules/api/lib/validation", () => ({ validateResponseData: mockValidateResponseData }));
vi.mock("@/lib/workspace/service", () => ({ getWorkspaceLegacyStoragePrefixes: async () => [] }));
vi.mock("./serializers", () => ({
  createV3ResponseSerializer: () => ({ toResource: mockToResource, toListItem: vi.fn() }),
}));
vi.mock("./service", () => ({
  getResponseWorkspaceId: mockGetWorkspaceId,
  getScopedV3Response: mockGetScoped,
  deleteScopedResponse: vi.fn(),
  deleteScopedResponses: vi.fn(),
  listV3ResponseKeysetPage: vi.fn(),
  hydrateV3Responses: vi.fn(),
  countV3Responses: vi.fn(),
  getV3ResponseSurveys: vi.fn(),
}));
vi.mock("./write-service", () => ({
  getSurveyForV3Write: mockGetSurveyForWrite,
  createScopedResponse: mockCreate,
  updateScopedResponse: mockUpdate,
  readbackV3Response: mockReadback,
  dispatchV3ResponsePipeline: mockDispatch,
}));
vi.mock("@formbricks/logger", () => ({
  logger: { withContext: () => ({ warn: vi.fn(), error: vi.fn(), info: vi.fn() }) },
}));

const WORKSPACE_ID = "clws000000000000000000001";
const SURVEY_ID = "clsv000000000000000000001";
const RESPONSE_ID = "clrs000000000000000000001";

const survey = (over: Record<string, unknown> = {}) => ({
  id: SURVEY_ID,
  name: "NPS",
  workspaceId: WORKSPACE_ID,
  updatedAt: new Date("2026-09-01T10:00:00.000Z"),
  blocks: [
    { id: "blk", name: "Block", elements: [{ id: "q1", type: "openText", headline: { default: "Q1" } }] },
  ],
  questions: [],
  endings: [{ id: "cmp1" }],
  languages: [{ default: true, enabled: true, language: { code: "en" } }],
  embeddedDataLinks: [],
  embeddedFields: [],
  isAnonymizeResponsesEnabled: false,
  ...over,
});

const row = (over: Record<string, unknown> = {}) => ({
  id: RESPONSE_ID,
  surveyId: SURVEY_ID,
  finished: false,
  data: {},
  variables: {},
  language: null,
  ...over,
});

const params = { authentication: { apiKeyId: "key", workspacePermissions: [] } as never, requestId: "req-1" };

const bodyOf = async (response: Response) => JSON.parse(await response.text());

beforeEach(() => {
  vi.clearAllMocks();
  mockRequireAccess.mockResolvedValue({ workspaceId: WORKSPACE_ID, organizationId: "org-1" });
  mockGetSurveyForWrite.mockResolvedValue(survey());
  mockValidateResponseData.mockReturnValue(null);
  mockValidateFileUploads.mockReturnValue(true);
  mockToResource.mockReturnValue({ id: RESPONSE_ID });
  mockCreate.mockResolvedValue({ ok: true, responseId: RESPONSE_ID });
  mockUpdate.mockResolvedValue({ ok: true, responseId: RESPONSE_ID });
  mockReadback.mockResolvedValue(row());
  mockGetWorkspaceId.mockResolvedValue(WORKSPACE_ID);
  mockGetScoped.mockResolvedValue(row());
});

describe("createV3Response — authorization", () => {
  /**
   * The correction to the contract's own 422 list. `surveyId` is the scope anchor and is resolved
   * *before* authorization, so a distinct answer for "no such survey" is readable by anyone holding
   * any valid key — an existence oracle over every other tenant's ids.
   */
  test("a survey that does not exist and one in another workspace are byte-identical 403s", async () => {
    mockGetSurveyForWrite.mockResolvedValueOnce(null);
    const missing = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: false, data: {} },
    });

    // The foreign case must build its 403 through the real rejection path, not through the same
    // object the missing branch constructs — otherwise this asserts the mock rather than the code.
    mockGetSurveyForWrite.mockResolvedValueOnce(survey({ workspaceId: "clws000000000000000000002" }));
    mockRequireAccess.mockImplementationOnce(async (_auth, _ws, _perm, requestId, instance) =>
      problemForbidden(requestId, "You are not authorized to access this resource", instance)
    );
    const foreign = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: false, data: {} },
    });

    expect(missing.status).toBe(403);
    expect(foreign.status).toBe(403);
    expect(await missing.text()).toBe(await foreign.text());
  });

  test("creating requires readWrite, not read", async () => {
    await createV3Response({ ...params, body: { surveyId: SURVEY_ID, finished: false, data: {} } });

    expect(mockRequireAccess).toHaveBeenCalledWith(
      expect.anything(),
      WORKSPACE_ID,
      "readWrite",
      "req-1",
      undefined
    );
  });

  test("a workspace the caller cannot write to is never queried for a survey write", async () => {
    mockRequireAccess.mockResolvedValueOnce(problemForbidden("req-1", undefined, undefined));

    await createV3Response({ ...params, body: { surveyId: SURVEY_ID, finished: false, data: {} } });

    expect(mockCreate).not.toHaveBeenCalled();
  });
});

describe("createV3Response — the happy path and its side effects", () => {
  test("201 carries a Location header pointing at the new response", async () => {
    const response = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: false, data: { q1: "hi" } },
    });

    expect(response.status).toBe(201);
    expect(response.headers.get("Location")).toBe(`/api/v3/responses/${RESPONSE_ID}`);
    expect(await bodyOf(response)).toEqual({ data: { id: RESPONSE_ID } });
  });

  /**
   * Quota evaluation can finish a response the caller submitted as partial, and that response has
   * genuinely finished. Reading `finished` off the request body instead would skip the finish side of
   * the pipeline for exactly the responses a quota just closed.
   */
  test("responseFinished follows the persisted row, not the request body", async () => {
    mockReadback.mockResolvedValueOnce(row({ finished: true }));

    await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: false, data: {} },
    });

    expect(mockDispatch).toHaveBeenCalledWith(expect.objectContaining({ alsoFinished: true }));
  });

  test("a partial response fires no finish event", async () => {
    await createV3Response({ ...params, body: { surveyId: SURVEY_ID, finished: false, data: {} } });

    expect(mockDispatch).toHaveBeenCalledWith(
      expect.objectContaining({ event: "responseCreated", alsoFinished: false })
    );
  });

  test("the pipeline is dispatched after the write, never before", async () => {
    const order: string[] = [];
    mockCreate.mockImplementationOnce(async () => {
      order.push("write");
      return { ok: true, responseId: RESPONSE_ID };
    });
    mockDispatch.mockImplementationOnce(async () => {
      order.push("dispatch");
    });

    await createV3Response({ ...params, body: { surveyId: SURVEY_ID, finished: true, data: {} } });

    expect(order).toEqual(["write", "dispatch"]);
  });
});

describe("createV3Response — 422s", () => {
  test("a hidden-field key in data is a 422 naming the key", async () => {
    mockGetSurveyForWrite.mockResolvedValueOnce(
      survey({
        embeddedFields: [
          {
            field: { name: "plan", source: "ingested", dataType: "text", locked: false, defaultValue: null },
            link: { storageKey: "plan" },
          },
        ],
      })
    );

    const response = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: false, data: { plan: "free" } },
    });

    expect(response.status).toBe(422);
    expect((await bodyOf(response)).invalid_params).toEqual([
      expect.objectContaining({ name: "plan", code: "unsupported_field", referenceType: "hiddenField" }),
    ]);
    expect(mockCreate).not.toHaveBeenCalled();
  });

  test("an ending from another survey is a 422", async () => {
    const response = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: true, data: {}, endingId: "cmp-other" },
    });

    expect(response.status).toBe(422);
    expect((await bodyOf(response)).invalid_params[0]).toMatchObject({ name: "endingId" });
  });

  /** Reference failures are raised by the transaction, and they reach the caller the same way. */
  test("a reference conflict from the write is reported as a 422, not a 409", async () => {
    mockCreate.mockResolvedValueOnce({
      ok: false,
      issues: [{ name: "singleUseId", reason: "already used", code: "duplicate_identifier" }],
    });

    const response = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: true, data: {}, singleUseId: "su-1" },
    });

    expect(response.status).toBe(422);
    expect((await bodyOf(response)).invalid_params[0].name).toBe("singleUseId");
  });

  test("every failing rule is reported at once rather than one per round trip", async () => {
    mockValidateResponseData.mockReturnValueOnce({
      q1: [{ ruleId: "minLength", ruleType: "minLength", message: "Too short" }],
    });

    const response = await createV3Response({
      ...params,
      body: {
        surveyId: SURVEY_ID,
        finished: true,
        data: { q1: "x" },
        endingId: "nope",
        language: "es",
      },
    });

    const names = (await bodyOf(response)).invalid_params.map((param: { name: string }) => param.name);
    expect(names).toEqual(expect.arrayContaining(["language", "endingId", "q1"]));
  });
});

describe("updateV3Response", () => {
  test("an unknown response id is the same 403 as a foreign one", async () => {
    mockGetWorkspaceId.mockResolvedValueOnce(null);

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { finished: true },
    });

    expect(response.status).toBe(403);
    expect(await response.text()).toBe(await problemForbidden("req-1", undefined, undefined).text());
  });

  /**
   * The mutation this closes: lowering the third argument to `"read"` left the whole suite green, so
   * nothing stopped a read-only key rewriting `data`, `finished` and `tags` across the workspace.
   * Create and validate both pinned their permission; patch — the one that mutates existing rows —
   * did not.
   */
  test("patching requires readWrite, not read", async () => {
    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { finished: true } });

    expect(mockRequireAccess).toHaveBeenCalledWith(
      expect.anything(),
      WORKSPACE_ID,
      "readWrite",
      "req-1",
      undefined
    );
  });

  /**
   * The deliberate divergence from v1 and v2, which both re-emit on every patch. Re-running every
   * webhook, integration and follow-up email because someone fixed a typo in a finished response is
   * not what "finished" means.
   */
  test("patching an already-finished response does not re-fire responseFinished", async () => {
    mockGetScoped.mockResolvedValueOnce(row({ finished: true }));
    mockReadback.mockResolvedValueOnce(row({ finished: true }));

    await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { data: { q1: "typo fixed" } },
    });

    expect(mockDispatch).toHaveBeenCalledWith(
      expect.objectContaining({ event: "responseUpdated", alsoFinished: false })
    );
  });

  /**
   * A legacy value the read withheld (here a JSON `null` under a second element) is kept by the
   * patch and left out of validation: the caller never saw it, so it can neither lose it by omission
   * nor be refused over it while correcting a different answer.
   */
  test("a withheld legacy answer is kept and not validated when another answer is patched", async () => {
    mockGetSurveyForWrite.mockResolvedValueOnce(
      survey({
        blocks: [
          {
            id: "blk",
            name: "Block",
            elements: [
              { id: "q1", type: "openText", headline: { default: "Q1" } },
              { id: "q2", type: "openText", headline: { default: "Q2" }, required: true },
            ],
          },
        ],
      })
    );
    mockGetScoped.mockResolvedValueOnce(row({ data: { q1: "old", q2: null } }));

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { data: { q1: "edited" } },
    });

    expect(response.status).toBe(200);
    expect(mockValidateResponseData.mock.calls[0][1]).toEqual({ q1: "edited" });
    expect(mockUpdate.mock.calls[0][0].patch.data).toEqual({ q1: "edited", q2: null });
  });

  test("the transition to finished does fire it", async () => {
    mockGetScoped.mockResolvedValueOnce(row({ finished: false }));
    mockReadback.mockResolvedValueOnce(row({ finished: true }));

    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { finished: true } });

    expect(mockDispatch).toHaveBeenCalledWith(expect.objectContaining({ alsoFinished: true }));
  });

  /**
   * Marking and restoring go through the patch the response already has: the same workspace gate
   * (`readWrite`) and the same per-response `response.write` check, so a read-only seat is refused.
   */
  test.each(["lowQuality", null] as const)("outcome %s is handed to the write", async (outcome) => {
    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { outcome } });

    expect(mockUpdate.mock.calls[0][0].patch.outcome).toBe(outcome);
  });

  test("an outcome-only patch leaves every other key out of the write", async () => {
    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { outcome: "lowQuality" } as never });

    const { patch } = mockUpdate.mock.calls[0][0];
    expect(patch.finished).toBeUndefined();
    expect(patch.tagIds).toBeUndefined();
  });

  test("a caller without response.write is refused before anything is written", async () => {
    vi.mocked(can).mockResolvedValueOnce(false);

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { outcome: "lowQuality" } as never,
    });

    expect(response.status).toBe(403);
    expect(mockUpdate).not.toHaveBeenCalled();
    expect(mockDispatch).not.toHaveBeenCalled();
    expect(can).toHaveBeenCalledWith(expect.anything(), "response.write", {
      type: "response",
      id: RESPONSE_ID,
    });
  });

  test("the audit record carries the old and the new outcome", async () => {
    const auditLog = {} as never;
    mockGetScoped.mockResolvedValueOnce(row({ outcome: null }));
    mockReadback.mockResolvedValueOnce(row({ outcome: "lowQuality" }));

    await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { outcome: "lowQuality" } as never,
      auditLog,
    });

    expect(auditLog).toMatchObject({
      targetId: RESPONSE_ID,
      oldObject: { id: RESPONSE_ID, outcome: null },
      newObject: { id: RESPONSE_ID, outcome: "lowQuality" },
    });
  });

  test.each([
    ["marking", null, "lowQuality"],
    ["restoring", "lowQuality", null],
  ] as const)("%s sends responseUpdated with the read-back row", async (_label, before, after) => {
    mockGetScoped.mockResolvedValueOnce(row({ outcome: before }));
    mockReadback.mockResolvedValueOnce(row({ outcome: after }));

    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { outcome: after } });

    expect(mockDispatch).toHaveBeenCalledWith(
      expect.objectContaining({
        event: "responseUpdated",
        response: expect.objectContaining({ outcome: after }),
        alsoFinished: false,
      })
    );
  });

  /** An absent key must not reach Prisma as `undefined` — for a nullable column that is a null-out. */
  test("only the keys the payload carried are handed to the write", async () => {
    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { finished: true } });

    const { patch } = mockUpdate.mock.calls[0][0];
    expect(patch.finished).toBe(true);
    expect(patch.endingId).toBeUndefined();
    expect(patch.language).toBeUndefined();
    expect(patch.data).toBeUndefined();
    expect(patch.tagIds).toBeUndefined();
  });

  /**
   * A patch that changes language and answers together must be validated against the labels it is
   * about to have, not the ones it is leaving behind.
   */
  test("answers are validated against the language the patch is applying", async () => {
    mockGetScoped.mockResolvedValueOnce(row({ language: "en" }));
    mockGetSurveyForWrite.mockResolvedValueOnce(
      survey({
        languages: [
          { default: true, enabled: true, language: { code: "en" } },
          { default: false, enabled: true, language: { code: "de" } },
        ],
      })
    );

    await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { language: "de", data: { q1: "ja" } },
    });

    expect(mockValidateResponseData).toHaveBeenCalledWith(
      expect.anything(),
      expect.anything(),
      "de",
      expect.anything()
    );
  });
});

describe("the read-back race", () => {
  /**
   * The row was committed and then removed before it could be read back. There is nothing truthful
   * left to return, and the two operations answer differently on purpose: a create has no resource
   * to name, while a patch falls back to the same 403 every other missing response gets.
   */
  test("a create whose row vanishes answers 500 rather than inventing a resource", async () => {
    mockReadback.mockResolvedValueOnce(null);

    const response = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: false, data: {} },
    });

    expect(response.status).toBe(500);
    expect(mockDispatch).not.toHaveBeenCalled();
  });

  test("a patch whose row vanishes answers the same 403 as any missing response", async () => {
    mockReadback.mockResolvedValueOnce(null);

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { finished: true },
    });

    expect(response.status).toBe(403);
    expect(mockDispatch).not.toHaveBeenCalled();
  });

  test("a survey deleted under an in-flight patch answers 403 rather than throwing", async () => {
    mockGetSurveyForWrite.mockResolvedValueOnce(null);

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { finished: true },
    });

    expect(response.status).toBe(403);
    expect(mockUpdate).not.toHaveBeenCalled();
  });
});

describe("what the operation hands to the write", () => {
  /** Nothing asserted the marshalling of the request body into the persist input. */
  test("every create field reaches createScopedResponse", async () => {
    await createV3Response({
      ...params,
      body: {
        surveyId: SURVEY_ID,
        finished: true,
        data: { q1: "hi" },
        ttc: { q1: 1200 },
        meta: { source: "zendesk" },
        tags: ["cltg000000000000000000001"],
        endingId: "cmp1",
        language: "en",
        contactId: "clct000000000000000000001",
        displayId: "cldp000000000000000000001",
        singleUseId: "su-1",
      },
    });

    expect(mockCreate).toHaveBeenCalledWith(
      expect.objectContaining({
        workspaceId: WORKSPACE_ID,
        finished: true,
        data: { q1: "hi" },
        ttc: { q1: 1200, _total: 1200 },
        meta: { source: "zendesk" },
        tagIds: ["cltg000000000000000000001"],
        endingId: "cmp1",
        language: "en",
        contactId: "clct000000000000000000001",
        displayId: "cldp000000000000000000001",
        singleUseId: "su-1",
      })
    );
  });

  /** An omitted ttc must not become {_total: 0} — see normalizeV3Ttc. */
  test("an omitted ttc reaches the write as an empty map", async () => {
    await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: true, data: {} },
    });

    expect(mockCreate.mock.calls[0][0].ttc).toEqual({});
  });

  /**
   * `ttc` is create-only for a caller, but a patch that finishes the response must still derive
   * `_total`, which is what every other write path does.
   */
  test("a patch that finishes derives _total from the stored timings", async () => {
    mockGetScoped.mockResolvedValueOnce(row({ finished: false, ttc: { q1: 1000, nps: 500 } }));

    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { finished: true } });

    expect(mockUpdate.mock.calls[0][0].patch.ttc).toEqual({ q1: 1000, nps: 500, _total: 1500 });
  });

  test("a patch that does not finish leaves ttc alone", async () => {
    mockGetScoped.mockResolvedValueOnce(row({ finished: false, ttc: { q1: 1000 } }));

    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { data: { q1: "x" } } });

    expect(mockUpdate.mock.calls[0][0].patch.ttc).toBeUndefined();
  });

  test("patching an already-finished response does not re-total", async () => {
    mockGetScoped.mockResolvedValueOnce(row({ finished: true, ttc: { q1: 1000, _total: 1000 } }));

    await updateV3Response({ ...params, responseId: RESPONSE_ID, body: { finished: true } });

    expect(mockUpdate.mock.calls[0][0].patch.ttc).toBeUndefined();
  });

  /**
   * `embeddedData` merges while `data` replaces, and nothing exercised that fold through an
   * operation — only the planner underneath it.
   */
  test("embeddedData merges onto the stored map while data replaces it", async () => {
    mockGetSurveyForWrite.mockResolvedValue(
      survey({
        embeddedFields: [
          {
            field: { name: "Plan", source: "ingested", dataType: "text", locked: false, defaultValue: null },
            link: { storageKey: "plan" },
          },
        ],
      })
    );
    mockGetScoped.mockResolvedValueOnce(row({ data: { q1: "old", nps: 7, plan: "free" } }));

    await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { data: { q1: "new" }, embeddedData: { Plan: "enterprise" } },
    });

    // q1 replaced, nps dropped by the wholesale replace, plan merged from embeddedData.
    expect(mockUpdate.mock.calls[0][0].patch.data).toEqual({ q1: "new", plan: "enterprise" });
  });

  test("null in embeddedData clears the stored key through the operation", async () => {
    mockGetSurveyForWrite.mockResolvedValue(
      survey({
        embeddedFields: [
          {
            field: { name: "Plan", source: "ingested", dataType: "text", locked: false, defaultValue: null },
            link: { storageKey: "plan" },
          },
        ],
      })
    );
    mockGetScoped.mockResolvedValueOnce(row({ data: { q1: "a", plan: "free" } }));

    await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { embeddedData: { Plan: null } },
    });

    expect(mockUpdate.mock.calls[0][0].patch.data).toEqual({ q1: "a" });
  });
});

describe("file-upload answers", () => {
  /**
   * The survey has to DECLARE the file-upload element, or these tests prove nothing.
   *
   * With the default fixture — one `openText` called `q1` — the key `upload` is not publishable, so
   * `planAnswerDataWrite` raises `unsupported_field` and the 422 arrives before `uploadIssues`
   * is ever consulted. Both refusal tests then pass with the storage check deleted, which is the one
   * check here that carries a security property.
   */
  const withUploadElement = () =>
    survey({
      blocks: [
        {
          id: "blk",
          name: "Block",
          elements: [
            { id: "q1", type: "openText", headline: { default: "Q1" } },
            { id: "upload", type: "fileUpload", headline: { default: "Upload" } },
          ],
        },
      ],
    });

  beforeEach(() => {
    mockGetSurveyForWrite.mockResolvedValue(withUploadElement());
  });

  /**
   * A stored answer is a storage path that the dashboard, the export and the read endpoints later
   * resolve into a signed URL. Without this check a caller with write access to one workspace could
   * store another workspace's path under a file-upload element and have it resolved on their own
   * response — the cross-tenant storage reference ENG-1981 closed on v1 and v2.
   */
  test("a file URL outside this survey's element is refused", async () => {
    mockValidateFileUploads.mockReturnValueOnce(false);

    const response = await createV3Response({
      ...params,
      body: {
        surveyId: SURVEY_ID,
        finished: true,
        data: { upload: ["http://x.test/storage/other-ws/private/surveys/other/elements/up/f.pdf"] },
      },
    });

    expect(response.status).toBe(422);
    // The reason a file-upload failure has always carried: it is part of the API's responses.
    expect((await bodyOf(response)).invalid_params).toEqual([
      expect.objectContaining({
        name: "data",
        code: "invalid_reference",
        reason:
          "A file-upload answer references a file that does not belong to this survey's upload element.",
      }),
    ]);
    expect(mockCreate).not.toHaveBeenCalled();
  });

  test("the check is scoped to the survey's own workspace and id", async () => {
    await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: true, data: { upload: ["ok"] } },
    });

    expect(mockValidateFileUploads).toHaveBeenCalledWith(
      expect.objectContaining({ workspaceId: WORKSPACE_ID, surveyId: SURVEY_ID })
    );
  });

  test("wiring: a signature answer is a single string and reaches the storage check", async () => {
    mockGetSurveyForWrite.mockResolvedValue(
      survey({
        blocks: [
          {
            id: "blk",
            name: "Block",
            elements: [
              { id: "q1", type: "openText", headline: { default: "Q1" } },
              { id: "sig", type: "signature", headline: { default: "Sign" }, required: false },
            ],
          },
        ],
      })
    );
    const signatureUrl = `/storage/${WORKSPACE_ID}/private/surveys/${SURVEY_ID}/elements/sig/s--fid--abc.png`;

    const response = await createV3Response({
      ...params,
      body: { surveyId: SURVEY_ID, finished: true, data: { sig: signatureUrl } },
    });

    expect(response.status).toBe(201);
    expect(mockValidateFileUploads).toHaveBeenCalledWith(
      expect.objectContaining({ data: { sig: signatureUrl }, workspaceId: WORKSPACE_ID, surveyId: SURVEY_ID })
    );
  });

  test("wiring: a signature URL the storage check refuses is a 422 and nothing is stored", async () => {
    mockGetSurveyForWrite.mockResolvedValue(
      survey({
        blocks: [
          {
            id: "blk",
            name: "Block",
            elements: [{ id: "sig", type: "signature", headline: { default: "Sign" }, required: false }],
          },
        ],
      })
    );
    mockValidateFileUploads.mockReturnValueOnce(false);

    const response = await createV3Response({
      ...params,
      body: {
        surveyId: SURVEY_ID,
        finished: true,
        data: { sig: "/storage/other-ws/private/surveys/other/elements/sig/s.png" },
      },
    });

    expect(response.status).toBe(422);
    expect((await bodyOf(response)).invalid_params).toEqual([
      expect.objectContaining({
        name: "data",
        code: "invalid_reference",
        reason:
          "A signature answer is malformed or references a file that does not belong to this survey's signature element.",
      }),
    ]);
    expect(mockCreate).not.toHaveBeenCalled();
  });

  test("a bad file-upload answer next to a Signature keeps the file-upload reason, and the Signature is checked apart", async () => {
    mockGetSurveyForWrite.mockResolvedValue(
      survey({
        blocks: [
          {
            id: "blk",
            name: "Block",
            elements: [
              { id: "upload", type: "fileUpload", headline: { default: "Upload" } },
              { id: "sig", type: "signature", headline: { default: "Sign" }, required: false },
            ],
          },
        ],
      })
    );
    mockValidateFileUploads.mockReturnValueOnce(false);

    const response = await createV3Response({
      ...params,
      body: {
        surveyId: SURVEY_ID,
        finished: true,
        data: { upload: ["bad"], sig: "/storage/ws/private/surveys/s/elements/sig/s--fid--abc.png" },
      },
    });

    expect(response.status).toBe(422);
    expect((await bodyOf(response)).invalid_params[0].reason).toMatch(/^A file-upload answer/);
    expect(mockValidateFileUploads).toHaveBeenCalledTimes(1);
    expect(mockValidateFileUploads).toHaveBeenCalledWith(
      expect.objectContaining({ data: { upload: ["bad"] } })
    );
  });

  test("patch is checked too, not only create", async () => {
    mockValidateFileUploads.mockReturnValueOnce(false);

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { data: { upload: ["http://x.test/storage/other/private/surveys/o/elements/u/f.pdf"] } },
    });

    expect(response.status).toBe(422);
    expect(mockUpdate).not.toHaveBeenCalled();
  });

  /**
   * The check runs on the caller's `data`, never on the merged map — the same gate answer validation
   * uses, and for the same reason. `composed.data` carries the stored answers too, so a patch that
   * does not send `data` would re-run URLs collected months ago through today's
   * `allowedFileExtensions`: narrow those afterwards and every later patch 422s naming `data`, a field
   * the request never carried and the caller cannot fix.
   */
  test("a patch that does not send data leaves stored uploads unchecked", async () => {
    mockGetScoped.mockResolvedValueOnce(
      row({ data: { upload: ["http://x.test/storage/ws/private/surveys/s/elements/upload/old.pdf"] } })
    );
    // Would refuse if it ran at all — the point is that it does not.
    mockValidateFileUploads.mockReturnValue(false);

    const response = await updateV3Response({
      ...params,
      responseId: RESPONSE_ID,
      body: { finished: true },
    });

    expect(mockValidateFileUploads).not.toHaveBeenCalled();
    expect(response.status).toBe(200);
  });
});
