Newer
Older
client_dwatch / lib / db / __tests__ / ds-status.test.ts
import { describe, expect, it } from "vitest";
import {
  connectionStatusFromControlStatus,
  controlStatusFromConnectionStatus,
  isListedControlStatus,
} from "@/lib/db/ds-status";

// ★一覧に出す制御盤の状態か★(指示書BB A1・D-182)— 0/1 以外は一覧に出さない(例外にしない)
describe("isListedControlStatus", () => {
  it.each([
    [0, true],
    [1, true],
    [2, false],
    [-1, false],
    [255, false],
  ] as const)("controls.status=%i → %s", (input, expected) => {
    expect(isListedControlStatus(input)).toBe(expected);
  });
});

// 期待値表(D-065 Q1-2。docs/decisions.md:483-484)
// ★`control_error_histories.status` の変換(旧 Q1-3)は削除した★(指示書AX D14。D-Sec アラートは robot_error_reports から読む)
describe("connectionStatusFromControlStatus", () => {
  it.each([
    [0, "ok"],
    [1, "error"],
  ] as const)("controls.status=%i → %s", (input, expected) => {
    expect(connectionStatusFromControlStatus(input)).toBe(expected);
  });

  it("0/1以外は例外を投げる(黙って握りつぶさない)", () => {
    expect(() => connectionStatusFromControlStatus(2)).toThrow();
    expect(() => connectionStatusFromControlStatus(-1)).toThrow();
  });
});

describe("controlStatusFromConnectionStatus", () => {
  it.each([
    ["ok", 0],
    ["error", 1],
  ] as const)("connection_status=%s → %i(seedの逆変換)", (input, expected) => {
    expect(controlStatusFromConnectionStatus(input)).toBe(expected);
  });
});

describe("往復(正変換 → 逆変換が元に戻ること)", () => {
  it.each([0, 1] as const)("controls.status=%i", (status) => {
    expect(
      controlStatusFromConnectionStatus(connectionStatusFromControlStatus(status))
    ).toBe(status);
  });
});