import { describe, expect, it } from "vitest";
import { buildContract, parseContract } from "@/lib/format/contract";

/**
 * `facilities.contract`（10桁文字列。D-080）の期待値表テスト。
 * 指示書M 3-5 の表をそのままケースにしている。
 */
describe("parseContract", () => {
  // | # | 入力 | d_security_enabled | monitoring_enabled |
  const cases: [string, boolean, boolean][] = [
    ["0000000000", false, false],
    ["1000000000", true, false],
    ["0100000000", false, true],
    ["1100000000", true, true],
    ["11", true, true],
    ["1", true, false],
    ["", false, false],
    ["0000000000000", false, false],
    ["abcdefghij", false, false],
  ];

  it.each(cases)("%s → d_security=%s / monitoring=%s", (input, dSecurity, monitoring) => {
    expect(parseContract(input)).toEqual({
      d_security_enabled: dSecurity,
      monitoring_enabled: monitoring,
    });
  });
});

describe("buildContract（逆変換）", () => {
  it("両方 true → 1100000000", () => {
    expect(
      buildContract({ d_security_enabled: true, monitoring_enabled: true })
    ).toBe("1100000000");
  });

  it("両方 false → 0000000000", () => {
    expect(
      buildContract({ d_security_enabled: false, monitoring_enabled: false })
    ).toBe("0000000000");
  });

  it("D-Watchのみ → 0100000000", () => {
    expect(
      buildContract({ d_security_enabled: false, monitoring_enabled: true })
    ).toBe("0100000000");
  });

  it("D-Securityのみ → 1000000000", () => {
    expect(
      buildContract({ d_security_enabled: true, monitoring_enabled: false })
    ).toBe("1000000000");
  });

  it("parseContract と往復する", () => {
    const flags = { d_security_enabled: true, monitoring_enabled: false };
    expect(parseContract(buildContract(flags))).toEqual(flags);
  });
});
