/**
 * `clean_error` のコード表とラベルの期待値表（指示書U 段階1-2）。
 *
 * ★このテストの目的は「後から読みやすく書き換えられるのを止めること」★ —
 *   ラベルは定義書
 *   `docs/handoff/analysis/table-definitions-02-clean-error.md:213` の**原文**であり、
 *   当方が作った文言ではない（D-077 の思想）。★原文が正であることをここで宣言しておく★
 *   （`CLAUDE.md` §6-2）。
 *
 * ★本指示書ではラベルをどの画面にも出していない★（出し方が未決。D-107 の申し送り）。
 *   それでもテストで固定する理由は上記のとおり。
 */
import { describe, expect, it } from "vitest";
import { CLEAN_ERROR, CLEAN_ERROR_LABELS, cleanErrorLabel } from "@/lib/config";

describe("CLEAN_ERROR", () => {
  // 定義書 :213 の原文: 0: 正常, 1: 異常(清掃未実施), 2: 異常(セキュリティ有効),
  //                     3: 異常(D-S制御盤異常), 4: 異常(清掃ロボット異常)
  it("コード値が定義書の原文と一致する", () => {
    expect(CLEAN_ERROR.normal).toBe(0);
    expect(CLEAN_ERROR.notExecuted).toBe(1);
    expect(CLEAN_ERROR.securityEnabled).toBe(2);
    expect(CLEAN_ERROR.controlBoardError).toBe(3);
    expect(CLEAN_ERROR.robotError).toBe(4);
  });

  it("★出発失敗（未実施）を表すのは 1 だけ★（ユーザー回答 2026-09-10）", () => {
    // 2/3/4 は「未実施とは違う扱い」なので、notExecuted と同値になってはいけない
    expect(CLEAN_ERROR.securityEnabled).not.toBe(CLEAN_ERROR.notExecuted);
    expect(CLEAN_ERROR.controlBoardError).not.toBe(CLEAN_ERROR.notExecuted);
    expect(CLEAN_ERROR.robotError).not.toBe(CLEAN_ERROR.notExecuted);
  });
});

describe("cleanErrorLabel", () => {
  // ★半角括弧のまま。全角に直したらこのテストが落ちる★
  it.each([
    [0, "正常"],
    [1, "異常(清掃未実施)"],
    [2, "異常(セキュリティ有効)"],
    [3, "異常(D-S制御盤異常)"],
    [4, "異常(清掃ロボット異常)"],
  ])("code=%i → %s（定義書の原文ママ）", (code, label) => {
    expect(cleanErrorLabel(code)).toBe(label);
  });

  it("★括弧が半角であること★（定義書の原文が半角。全角に直さない）", () => {
    for (const label of Object.values(CLEAN_ERROR_LABELS)) {
      expect(label).not.toContain("（");
      expect(label).not.toContain("）");
    }
  });

  // ★unsigned tinyint（0〜255）なのでコード表に無い値が入りうる（指示書U 落とし穴3）★
  it.each([5, 9, 255])("コード表に無い値 %i は String(code) を返す", (code) => {
    expect(cleanErrorLabel(code)).toBe(String(code));
  });

  // ★D-089 項3 / D-104 項2: 「その他」「不明」という語を使わない★
  it.each(["その他", "不明"])("ラベル表に「%s」という語が出てこない", (word) => {
    for (const label of Object.values(CLEAN_ERROR_LABELS)) {
      expect(label).not.toContain(word);
    }
    expect(cleanErrorLabel(255)).not.toContain(word);
  });

  it("ラベル表のキーは 0〜4 の5件だけ", () => {
    expect(Object.keys(CLEAN_ERROR_LABELS).map(Number).sort()).toEqual([0, 1, 2, 3, 4]);
  });
});
