import { describe, it, expect } from "vitest";
import {
  checkFractionAnswer,
  checkIntegerAnswer,
  checkDecimalAnswer,
  checkRatioAnswer,
  checkOrderingAnswer,
} from "./validation";

describe("checkIntegerAnswer", () => {
  it("solves an exact match", () => {
    expect(checkIntegerAnswer(6, 6).status).toBe("solved");
  });
  it("rejects a wrong value", () => {
    expect(checkIntegerAnswer(5, 6).status).toBe("wrong");
  });
});

describe("checkFractionAnswer", () => {
  it("solves a fully simplified answer", () => {
    expect(checkFractionAnswer(1, 2, 1, 2).status).toBe("solved");
  });

  // Regression: 5/10 is the right value but unsimplified — it must NOT be treated
  // as fully solved (this is what let the score double-count before the fix).
  it("flags a correct but unsimplified answer", () => {
    expect(checkFractionAnswer(5, 10, 1, 2).status).toBe("unsimplified");
  });

  it("accepts any non-zero denominator for zero", () => {
    expect(checkFractionAnswer(0, 5, 0, 1).status).toBe("solved");
  });

  it("rejects a wrong value", () => {
    expect(checkFractionAnswer(3, 4, 1, 8).status).toBe("wrong");
  });

  it("rejects a zero denominator", () => {
    const res = checkFractionAnswer(1, 0, 1, 2);
    expect(res.status).toBe("wrong");
    if (res.status === "wrong") expect(res.message).toMatch(/denominator/i);
  });
});

describe("checkDecimalAnswer", () => {
  it("solves within tolerance", () => {
    expect(checkDecimalAnswer(3.14, 3.14).status).toBe("solved");
  });
  it("rejects outside tolerance", () => {
    expect(checkDecimalAnswer(3.2, 3.14).status).toBe("wrong");
  });
});

describe("checkRatioAnswer", () => {
  it("solves an already-simplified ratio", () => {
    expect(checkRatioAnswer([1, 2], [1, 2]).status).toBe("solved");
  });
  it("flags an equivalent but unsimplified ratio", () => {
    expect(checkRatioAnswer([2, 4], [1, 2]).status).toBe("unsimplified");
  });
  it("rejects a non-equivalent ratio", () => {
    expect(checkRatioAnswer([1, 3], [1, 2]).status).toBe("wrong");
  });
});

describe("checkOrderingAnswer", () => {
  it("solves a correct ascending order", () => {
    expect(checkOrderingAnswer([-10, -8, -6, -4, -3, 5], [-10, -8, -6, -4, -3, 5]).status).toBe("solved");
  });
  it("rejects a wrong order", () => {
    expect(checkOrderingAnswer([5, -10], [-10, 5]).status).toBe("wrong");
  });
});
