import { describe, expect, it } from "vitest";
import {
  importRegexTestSuite,
  serializeRegexTestSuite,
} from "./test-suite.serialization";
import type { RegexTestCase } from "./test-case.types";
import { chooseReplacementResultDraft } from "./current-result-draft";

function testWithExpectation(
  expectation: RegexTestCase["expectation"],
): RegexTestCase {
  return {
    id: "current-result",
    name: "Current result",
    enabled: true,
    flavour: "ecmascript",
    pattern: "x",
    flags: ["g"],
    options: {},
    subject: "x",
    replacement: "y",
    expectation,
  };
}

describe("current replacement result draft", () => {
  it("falls back to a round-trippable lightweight assertion above the exact-value limit", () => {
    const decision = chooseReplacementResultDraft({
      output: "oversized",
      resultTruncated: false,
      matchCount: 1,
      maximumExpectedUtf16: 4,
      canStore: () => true,
    });

    expect(decision.expectation).toEqual({ kind: "should-match" });
    expect(decision.notice).toContain("expected-value limit");
    const test = testWithExpectation(decision.expectation!);
    expect(
      importRegexTestSuite(serializeRegexTestSuite([test], true)).tests,
    ).toEqual([test]);
  });

  it("falls back when exact output would exceed the aggregate suite limit", () => {
    const decision = chooseReplacementResultDraft({
      output: "exact",
      resultTruncated: false,
      matchCount: 1,
      canStore: (expectation) => expectation.kind !== "replacement",
    });

    expect(decision.expectation).toEqual({ kind: "should-match" });
    expect(decision.notice).toContain("32 MiB");
  });
});
