import { prisma } from "@/lib/__mocks__/database";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { AIOutputTokenLimitError } from "@formbricks/ai";
import { prepareV3SurveyCreateInput } from "@/app/api/v3/surveys/prepare";
import type { TSurveyImportDraft } from "../core/draft";
import { SurveyImportFailedError, SurveyImportInputError, SurveyImportTimeoutError } from "../core/errors";
import { checkImportDraft } from "../core/final-gate";
import { isObjectMemberName } from "../core/id-registry";
import { SURVEY_IMPORT_AI_MAX_OUTPUT_TOKENS_PER_CALL } from "../limits";
import type { TSurveyImportReport } from "../types";
import {
  IMPORTABLE_QSF_FIXTURES,
  type TImportableQsfFixture,
  loadQsfFixture,
} from "./__fixtures__/load-fixture";
import { loadRecordedPlan, recordedGenerate, timedGenerate } from "./__fixtures__/recorded-plans";
import type { TQsfPlanGenerate } from "./ai-plan";
import { type TRunQsfImportParams, runQsfImport } from "./pipeline";
import { prepareQsfImport } from "./prepare";

const mocks = vi.hoisted(() => ({
  generateOrganizationAIObject: vi.fn(),
  getExternalUrlsPermission: vi.fn(),
  checkImportDraft: vi.fn(),
  realCheckImportDraft: undefined as unknown as typeof import("../core/final-gate").checkImportDraft,
}));

const aiTimeoutEnv = vi.hoisted(() => ({ multiplier: 1 }));

vi.mock("server-only", () => ({}));
// The draft's name is looked up in the workspace and suffixed in the user's language (English here).
vi.mock("@/lib/user/locale", () => ({ getUserLocaleOrDefault: vi.fn(async () => "en-US") }));
vi.mock("@/lingodotdev/server", () => import("../core/__fixtures__/lingodotdev-server-en-us"));
vi.mock("@/lib/env", async (importOriginal) => {
  const original = await importOriginal<typeof import("@/lib/env")>();
  return {
    ...original,
    env: {
      ...original.env,
      get AI_TIMEOUT_MULTIPLIER() {
        return aiTimeoutEnv.multiplier;
      },
    },
  };
});
// Only the AI call is replaced; the reader, sanitizer, checks, assembly and the create's own checks run.
vi.mock("@/lib/ai/service", () => ({ generateOrganizationAIObject: mocks.generateOrganizationAIObject }));
vi.mock("@/modules/survey/lib/permission", () => ({
  getExternalUrlsPermission: mocks.getExternalUrlsPermission,
}));
vi.mock("../core/final-gate", async (importOriginal) => {
  const original = await importOriginal<typeof import("../core/final-gate")>();
  mocks.realCheckImportDraft = original.checkImportDraft;
  return {
    ...original,
    checkImportDraft: mocks.checkImportDraft,
    // The pipeline's own way in: through the same mock, so a test can make the gate fail.
    checkImportDraftInSlices: async (document: Parameters<typeof original.checkImportDraft>[0]) =>
      mocks.checkImportDraft(document),
  };
});

const WORKSPACE_ID = "clxx1234567890123456789012";

const run = (fixture: string, overrides: Partial<TRunQsfImportParams> = {}) =>
  runQsfImport({
    prepared: prepareQsfImport(loadQsfFixture(fixture), fixture),
    workspaceId: WORKSPACE_ID,
    organizationId: "org_1",
    userId: "user_1",
    signal: new AbortController().signal,
    deadlineMs: 120_000,
    onProgress: vi.fn(),
    ...overrides,
  });

/** The organization's model answers from the fixture's recorded plan. */
const answerFrom = (plan: string) => {
  const generate = recordedGenerate(loadRecordedPlan(plan));
  mocks.generateOrganizationAIObject.mockImplementation(generate);
};

const elementsOf = (document: TSurveyImportDraft) => document.blocks.flatMap((block) => block.elements);

type TExpectedReport = [TSurveyImportReport["summary"], string[]];

/** What each fixture's import must report: the summary, and the issue codes (with causes), sorted. */
const EXPECTED_REPORTS: Record<TImportableQsfFixture, TExpectedReport> = {
  "simple.qsf": [
    { blocks: 1, questions: 5, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 0 },
    [],
  ],
  "multilang-en-de.qsf": [
    {
      blocks: 1,
      questions: 3,
      languages: ["en-US", "de-DE"],
      logicRules: 0,
      importedLogicRules: 0,
      hiddenFields: 0,
    },
    ["ending_added"],
  ],
  "logic-skip-display-branch.qsf": [
    { blocks: 4, questions: 6, languages: ["en-US"], logicRules: 6, importedLogicRules: 0, hiddenFields: 1 },
    ["ending_added", ...Array(6).fill("logic_not_imported")],
  ],
  "matrix-slider-ranking.qsf": [
    { blocks: 3, questions: 8, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 0 },
    [
      "matrix_single_answer",
      "question_skipped:ai_skipped",
      "question_skipped:ai_skipped",
      "question_skipped:unsupported_type",
      "question_skipped:unsupported_type",
      "question_skipped:unsupported_type",
    ],
  ],
  "pages-and-blocks.qsf": [
    { blocks: 3, questions: 3, languages: ["en-US"], logicRules: 1, importedLogicRules: 0, hiddenFields: 0 },
    ["ending_added", "logic_not_imported", "question_skipped:not_in_flow"],
  ],
  "signature.qsf": [
    { blocks: 1, questions: 2, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 0 },
    // No `question_skipped:unsupported_type`: Signature used to be skipped for its type.
    ["ending_added"],
  ],
  "embedded-data.qsf": [
    { blocks: 2, questions: 2, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 4 },
    ["ending_added", ...Array(4).fill("field_renamed"), "piped_text_removed"],
  ],
  "large-150.qsf": [
    {
      blocks: 30,
      questions: 150,
      languages: ["en-US"],
      logicRules: 0,
      importedLogicRules: 0,
      hiddenFields: 0,
    },
    // Fifteen 0–7 sliders: 8 points, which a rating has not, so each becomes a 10-point rating.
    ["ending_added", ...Array(15).fill("scale_changed")],
  ],
  "legacy-object-payload.qsf": [
    { blocks: 1, questions: 2, languages: ["de-DE"], logicRules: 0, importedLogicRules: 0, hiddenFields: 0 },
    ["ending_added"],
  ],
  "nps-and-numeric-scales.qsf": [
    { blocks: 1, questions: 6, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 0 },
    ["ending_added"],
  ],
  "labels-and-languages.qsf": [
    {
      blocks: 2,
      questions: 3,
      languages: ["en-US", "de-DE", "zh-Hans-CN", "zh-Hant-TW"],
      logicRules: 0,
      importedLogicRules: 0,
      hiddenFields: 0,
    },
    [
      "choice_label_renamed",
      "choice_label_renamed",
      "ending_added",
      "language_skipped",
      "translation_missing",
      "translation_missing",
    ],
  ],
  "rich-text.qsf": [
    { blocks: 2, questions: 6, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 1 },
    [
      "field_renamed",
      "formatting_dropped",
      "headline_fallback",
      "headline_fallback",
      "image_dropped",
      "image_dropped",
      "markup_escaped",
      "piped_text_removed",
      "script_dropped",
      "script_dropped",
      "text_too_long",
    ],
  ],
  "prompt-injection.qsf": [
    { blocks: 1, questions: 2, languages: ["en-US"], logicRules: 0, importedLogicRules: 0, hiddenFields: 0 },
    ["ending_added"],
  ],
  "pollution.qsf": [
    {
      blocks: 2,
      questions: 4,
      languages: ["en-US", "de-DE"],
      logicRules: 0,
      importedLogicRules: 0,
      hiddenFields: 6,
    },
    [
      "choice_dropped:invalid_id",
      "choice_dropped:invalid_id",
      "ending_added",
      ...Array(6).fill("field_renamed"),
      "language_skipped",
      "question_skipped:invalid_id",
      "question_skipped:invalid_id",
      "translation_missing",
    ],
  ],
};

beforeEach(() => {
  aiTimeoutEnv.multiplier = 1;
  vi.resetAllMocks();
  vi.mocked(prisma.survey.findMany).mockResolvedValue([]);
  mocks.getExternalUrlsPermission.mockResolvedValue(true);
  mocks.checkImportDraft.mockImplementation(mocks.realCheckImportDraft);
});

describe("prepareQsfImport", () => {
  test("reads the file into the import's model", () => {
    const prepared = prepareQsfImport(loadQsfFixture("simple.qsf"), "simple.qsf");

    expect(prepared).toMatchObject({ fileName: "simple.qsf", surveyName: "Customer feedback" });
    expect(prepared.survey.questions.size).toBe(5);
  });

  test("never reads top-level keys it does not use, so a large file is not copied", () => {
    // A key that cannot be read stands in for the rest of a 15 MB file: copying it is what costs.
    const qsf = loadQsfFixture("simple.qsf");
    Object.defineProperty(qsf, "Unused", {
      enumerable: true,
      get: () => {
        throw new Error("an unchecked key was read");
      },
    });

    expect(prepareQsfImport(qsf, "a.qsf").surveyName).toBe("Customer feedback");
  });

  test.each([
    ["any other JSON file", "not-a-qsf.json"],
    ["a flow nested past the cap", "deep-flow.qsf"],
    ["a survey past the question limit", "over-limit.qsf"],
  ])("refuses %s with SurveyImportInputError, before any AI", (_case, fixture) => {
    expect(() => prepareQsfImport(loadQsfFixture(fixture), fixture)).toThrow(SurveyImportInputError);
  });
});

describe("runQsfImport on recorded plans", () => {
  test.each([1, 3])("passes the scaled per-call budget to the model (%i)", async (multiplier) => {
    aiTimeoutEnv.multiplier = multiplier;
    answerFrom("simple.qsf");
    await run("simple.qsf", { deadlineMs: 120_000 * multiplier });
    expect(mocks.generateOrganizationAIObject).toHaveBeenCalledWith(
      expect.objectContaining({ timeout: 45_000 * multiplier })
    );
  });

  test("names the draft as every import does, in the user's language and numbered past names taken", async () => {
    answerFrom("simple.qsf");
    const first = (await run("simple.qsf")).payload.name;
    expect(first).toMatch(/ \(imported\)$/);

    answerFrom("simple.qsf");
    vi.mocked(prisma.survey.findMany).mockResolvedValue([{ name: first }] as never);
    expect((await run("simple.qsf")).payload.name).toBe(first.replace(/ \(imported\)$/, " (imported 2)"));
  });

  test.each(IMPORTABLE_QSF_FIXTURES)(
    "%s becomes a draft the create accepts, with its report",
    async (fixture) => {
      answerFrom(fixture);
      const onProgress = vi.fn();

      const result = await run(fixture, { onProgress });

      // The shared create check: v3 validation (recall order enforced) and the survey service's schema.
      expect(checkImportDraft(result.payload)).toEqual([]);
      // And exactly what POST /api/v3/surveys runs first, on the body the dialog sends.
      expect(prepareV3SurveyCreateInput(JSON.parse(JSON.stringify(result.payload))).ok).toBe(true);
      // A survey with no ending can be published, and respondents would finish on nothing.
      expect(result.payload.endings.length).toBeGreaterThan(0);

      const [summary, codes] = EXPECTED_REPORTS[fixture];
      expect(result.report.source).toEqual({ kind: "qsf", fileName: fixture });
      expect(result.report.summary).toEqual(summary);
      expect(
        result.report.issues
          .map((issue) => `${issue.code}${issue.params?.cause ? `:${String(issue.params.cause)}` : ""}`)
          .sort()
      ).toEqual(codes);
      expect(onProgress.mock.calls.map(([stage]) => stage)).toEqual(["ai", "assembling"]);
      expect(result.usage?.inputTokens).toBeGreaterThan(0);
      expect(result.usage?.outputTokens).toBeGreaterThan(0);
    },
    30_000
  );

  test("imports a Qualtrics Signature question as a Signature element, required as Qualtrics forced it", async () => {
    answerFrom("signature.qsf");

    const result = await run("signature.qsf");

    expect(elementsOf(result.payload).map((element) => [element.id, element.type, element.required])).toEqual(
      [
        ["Q1", "signature", true],
        ["Q2", "signature", false],
      ]
    );
    expect(elementsOf(result.payload)).toEqual(
      expect.arrayContaining([expect.objectContaining({ type: "signature", allowTypedSignature: true })])
    );
    expect(result.report.issues.map((issue) => issue.params?.cause)).not.toContain("unsupported_type");
    // The create accepts the element and keeps the setting.
    const parsed = prepareV3SurveyCreateInput(JSON.parse(JSON.stringify(result.payload)));
    expect(parsed.ok).toBe(true);
  });

  test("turns on the language switch and browser-language selection for a draft in several enabled languages", async () => {
    answerFrom("labels-and-languages.qsf");
    const multi = await run("labels-and-languages.qsf");
    // German misses some texts, so it is imported turned off, leaving one enabled language.
    answerFrom("pollution.qsf");
    const oneEnabled = await run("pollution.qsf");
    answerFrom("simple.qsf");
    const single = await run("simple.qsf");

    expect(multi.payload).toMatchObject({ showLanguageSwitch: true, autoSelectLanguage: true });
    for (const payload of [oneEnabled.payload, single.payload]) {
      expect("showLanguageSwitch" in payload).toBe(false);
      expect("autoSelectLanguage" in payload).toBe(false);
    }
    // The create takes them as they are, and stores both.
    const parsed = prepareV3SurveyCreateInput(JSON.parse(JSON.stringify(multi.payload)));
    expect(parsed.ok && parsed.document).toMatchObject({
      showLanguageSwitch: true,
      autoSelectLanguage: true,
    });
    expect(mocks.realCheckImportDraft(multi.payload)).toEqual([]);
  });

  test("reports what was removed from the end message only when the survey shows it", async () => {
    const endingLines = (result: Awaited<ReturnType<typeof run>>) =>
      result.report.issues.filter((issue) => issue.params?.subject === "ending").map((issue) => issue.code);
    answerFrom("rich-text.qsf");
    const redirected = await run("rich-text.qsf");
    // Without external URLs the redirect is refused, and the end message is what the survey shows.
    mocks.getExternalUrlsPermission.mockResolvedValue(false);
    answerFrom("rich-text.qsf");
    const shown = await run("rich-text.qsf");

    expect(endingLines(redirected)).toEqual([]);
    expect(endingLines(shown)).toEqual(["image_dropped"]);
  });

  test("calls the organization's AI with the import's tracing feature", async () => {
    answerFrom("simple.qsf");

    await run("simple.qsf");

    expect(mocks.generateOrganizationAIObject).toHaveBeenCalledWith(
      expect.objectContaining({
        organizationId: "org_1",
        aiTracing: { distinctId: "user_1", feature: "ai_survey_import", workspaceId: WORKSPACE_ID },
        maxOutputTokens: SURVEY_IMPORT_AI_MAX_OUTPUT_TOKENS_PER_CALL,
        abortSignal: expect.any(AbortSignal),
      })
    );
  });

  test("the prompt-injection draft carries only text the file wrote, no link and no disallowed type", async () => {
    answerFrom("prompt-injection.qsf");
    const qsf = JSON.stringify(loadQsfFixture("prompt-injection.qsf"));

    const result = await run("prompt-injection.qsf");

    const draft = JSON.stringify(result.payload);
    for (const field of ["buttonUrl", "buttonLink", "imageUrl", "videoUrl", "href", "calUserName", "url"]) {
      expect(draft).not.toContain(`"${field}"`);
    }
    expect(elementsOf(result.payload).map((element) => element.type)).toEqual([
      "multipleChoiceSingle",
      "cta",
    ]);
    for (const element of elementsOf(result.payload)) {
      const texts = [
        ...Object.values(element.headline),
        ...("choices" in element ? element.choices.flatMap((choice) => Object.values(choice.label)) : []),
      ];
      for (const text of texts) expect(qsf).toContain(text);
    }
    // The notes the model wrote, links included, belong to no rule: none reaches the report.
    expect(JSON.stringify(result.report)).not.toContain("evil");
  });

  test("a polluting file leaves Object.prototype alone and names nothing after its members", async () => {
    answerFrom("pollution.qsf");
    const before = Object.getOwnPropertyNames(Object.prototype).sort();

    const result = await run("pollution.qsf");

    expect(Object.getOwnPropertyNames(Object.prototype).sort()).toEqual(before);
    expect(({} as Record<string, unknown>).polluted).toBeUndefined();
    const names = [
      ...elementsOf(result.payload).map((element) => element.id),
      ...result.payload.hiddenFields.fieldIds,
      ...result.payload.languages.map((language) => language.code),
      ...elementsOf(result.payload).flatMap((element) => Object.keys(element.headline)),
    ];
    expect(names.filter(isObjectMemberName)).toEqual([]);
  });

  test("retries what a plan got wrong, once, and drops what is still wrong", async () => {
    const hostile = loadRecordedPlan("hostile");
    const recorded = recordedGenerate(loadRecordedPlan("simple.qsf"));
    let calls = 0;
    mocks.generateOrganizationAIObject.mockImplementation(
      async (request: Parameters<TQsfPlanGenerate>[0]) => {
        calls += 1;
        if (calls === 1) return { object: hostile };
        // The retry gets QID1 to QID3 right; QID4 and QID5 stay wrong.
        const fixed = await recorded(request);
        const object = fixed.object as { questions: { ref: string }[] };
        return {
          ...fixed,
          object: {
            ...object,
            questions: object.questions.filter((question) => !["QID4", "QID5"].includes(question.ref)),
          },
        };
      }
    );

    const result = await run("simple.qsf");

    expect(calls).toBe(2);
    expect(elementsOf(result.payload).map((element) => element.id)).toEqual(["Q1", "Q2", "Q3"]);
    expect(result.report.issues).toEqual([
      { code: "question_skipped", severity: "warning", questionTag: "Q4", params: { cause: "plan_invalid" } },
      { code: "question_skipped", severity: "warning", questionTag: "Q5", params: { cause: "plan_invalid" } },
    ]);
  });

  test("a page split across AI calls, or a question only the retry placed, still makes one block", async () => {
    const recorded = recordedGenerate(loadRecordedPlan("simple.qsf"));
    let calls = 0;
    mocks.generateOrganizationAIObject.mockImplementation(
      async (request: Parameters<TQsfPlanGenerate>[0]) => {
        calls += 1;
        // Read before the await: the halves run at the same time.
        const call = calls;
        // The whole page is too long for one call, so it is split in two; the first half then leaves
        // QID2 out, which only the retry places.
        if (call === 1) throw new AIOutputTokenLimitError({ maxOutputTokens: 8192 });
        const result = await recorded(request);
        if (call !== 2) return result;
        const object = result.object as { questions: { ref: string }[] };
        return {
          ...result,
          object: { ...object, questions: object.questions.filter((entry) => entry.ref !== "QID2") },
        };
      }
    );

    const result = await run("simple.qsf");

    expect(calls).toBe(4);
    expect(result.payload.blocks.map((block) => block.elements.map((element) => element.id))).toEqual([
      ["Q1", "Q2", "Q3", "Q4", "Q5"],
    ]);
  });

  test("times out, not fails, when the deadline leaves no time for any AI call", async () => {
    answerFrom("simple.qsf");

    // 9.9 s for the whole import: 4.9 s for a call once assembly is reserved, under the 5 s minimum.
    const error = await run("simple.qsf", { deadlineMs: 9_900 }).catch((caught: unknown) => caught);

    expect(error).toBeInstanceOf(SurveyImportTimeoutError);
    expect(mocks.generateOrganizationAIObject).not.toHaveBeenCalled();
  });

  test("fails as too large for the AI, not times out, when every call runs out of output tokens", async () => {
    mocks.generateOrganizationAIObject.mockRejectedValue(
      new AIOutputTokenLimitError({ maxOutputTokens: 8192 })
    );

    const error = await run("simple.qsf").catch((caught: unknown) => caught);

    expect(error).toBeInstanceOf(SurveyImportFailedError);
    expect((error as SurveyImportFailedError).reason).toBe("too_large_for_ai");
  });

  test("has nothing to import when the AI skips every question", async () => {
    mocks.generateOrganizationAIObject.mockResolvedValue({
      object: {
        blocks: [],
        questions: [],
        skipped: ["QID1", "QID2", "QID3", "QID4", "QID5"].map((ref) => ({ ref, reason: "No" })),
      },
    });

    const error = await run("simple.qsf").catch((caught: unknown) => caught);

    expect(error).toBeInstanceOf(SurveyImportFailedError);
    expect((error as SurveyImportFailedError).reason).toBe("nothing_importable");
  });

  test("drops an element the create would refuse, reports it and assembles again", async () => {
    answerFrom("simple.qsf");
    mocks.checkImportDraft.mockReturnValueOnce([
      { name: "blocks.0.elements.1.headline", reason: "The question is missing" },
    ]);

    const result = await run("simple.qsf");

    expect(elementsOf(result.payload).map((element) => element.id)).toEqual(["Q1", "Q3", "Q4", "Q5"]);
    expect(result.report.issues).toEqual([
      {
        code: "question_skipped",
        severity: "warning",
        questionTag: "Q2",
        params: { cause: "validation_failed" },
      },
    ]);
  });

  test.each([
    ["outside an element", [[{ name: "endings.0.label", reason: "r" }]]],
    [
      "twice",
      [
        [{ name: "blocks.0.elements.1.headline", reason: "r" }],
        [{ name: "blocks.0.elements.0.headline", reason: "r" }],
      ],
    ],
  ])("fails the import when the create would refuse the draft %s", async (_case, failures) => {
    answerFrom("simple.qsf");
    for (const failure of failures) mocks.checkImportDraft.mockReturnValueOnce(failure);

    const error = await run("simple.qsf").catch((caught: unknown) => caught);

    expect(error).toBeInstanceOf(SurveyImportFailedError);
    expect((error as SurveyImportFailedError).reason).toBe("draft_invalid");
  });

  test("asks about external URLs only for a survey with a redirect, and drops the redirect without them", async () => {
    answerFrom("simple.qsf");
    await run("simple.qsf");
    expect(mocks.getExternalUrlsPermission).not.toHaveBeenCalled();

    answerFrom("rich-text.qsf");
    mocks.getExternalUrlsPermission.mockResolvedValue(false);
    const result = await run("rich-text.qsf");

    expect(mocks.getExternalUrlsPermission).toHaveBeenCalledWith("org_1");
    expect(result.payload.endings.map((ending) => ending.type)).toEqual(["endScreen"]);
    expect(result.report.issues).toContainEqual({ code: "external_url_removed", severity: "warning" });
  });

  test("stops at once when the import was aborted before it started", async () => {
    const controller = new AbortController();
    controller.abort();
    const onProgress = vi.fn();

    await expect(run("simple.qsf", { signal: controller.signal, onProgress })).rejects.toThrow();
    expect(onProgress).not.toHaveBeenCalled();
    expect(mocks.generateOrganizationAIObject).not.toHaveBeenCalled();
  });

  test("passes Stop to the AI call and lets its abort through", async () => {
    const controller = new AbortController();
    mocks.generateOrganizationAIObject.mockImplementation(
      (request: Parameters<TQsfPlanGenerate>[0]) =>
        new Promise((_resolve, reject) => {
          request.abortSignal.addEventListener("abort", () => reject(request.abortSignal.reason), {
            once: true,
          });
          controller.abort(new DOMException("Stopped", "AbortError"));
        })
    );

    const error = await run("simple.qsf", { signal: controller.signal }).catch((caught: unknown) => caught);

    expect((error as DOMException).name).toBe("AbortError");
  });

  describe("with a model that takes time", () => {
    afterEach(() => {
      vi.useRealTimers();
    });

    test("finishes with a draft when one AI call runs past its own timeout, splitting that chunk", async () => {
      const prepared = prepareQsfImport(loadQsfFixture("large-150.qsf"), "large-150.qsf");
      vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date", "performance"] });
      // The first chunk stalls past the 45 s call timeout; its halves and every other call take 20 s.
      mocks.generateOrganizationAIObject.mockImplementation(
        timedGenerate(loadRecordedPlan("large-150.qsf"), (refs) =>
          refs.includes("QID1") && refs.length === 20 ? Infinity : 20_000
        )
      );

      const settled = runQsfImport({
        prepared,
        workspaceId: WORKSPACE_ID,
        organizationId: "org_1",
        userId: "user_1",
        signal: new AbortController().signal,
        deadlineMs: 120_000,
        onProgress: vi.fn(),
      });
      await vi.advanceTimersByTimeAsync(130_000);
      const result = await settled;

      expect(result.report.summary.questions).toBe(150);
      expect(mocks.realCheckImportDraft(result.payload)).toEqual([]);
      expect(mocks.generateOrganizationAIObject).toHaveBeenCalledTimes(10);
    });
  });

  test("leaves an ordinary survey's draft as it was: it fits the create's request body", async () => {
    answerFrom("multilang-en-de.qsf");

    const result = await run("multilang-en-de.qsf");

    expect(result.report.issues.some((issue) => issue.params?.cause === "draft_too_large")).toBe(false);
    expect(result.report.summary.languages).toEqual(["en-US", "de-DE"]);
  });
});
