/**
 * HealthLevel 类型和配置的统一测试
 * 验证 dashboard-health.ts 作为唯一真实来源
 */

import { describe, it, expect } from "vitest";
import {
  HealthLevel,
  HEALTH_LEVEL_CONFIG,
} from "@/types/dashboard-health";
import { getHealthLevel } from "@/lib/dashboard-health";

describe("HealthLevel 类型统一", () => {
  it("HealthLevel 应包含正确的4个值：excellent, good, pass, danger", () => {
    const expectedLevels: HealthLevel[] = [
      "excellent",
      "good",
      "pass",
      "danger",
    ];
    // 验证类型推断正确 - 如果 HealthLevel 包含 "fair" 则编译失败
    const actualLevels = Object.keys(HEALTH_LEVEL_CONFIG) as HealthLevel[];
    expect(actualLevels.sort()).toEqual(expectedLevels.sort());
  });

  it("HEALTH_LEVEL_CONFIG 应包含所有4个等级的配置", () => {
    const keys = Object.keys(HEALTH_LEVEL_CONFIG);
    expect(keys).toContain("excellent");
    expect(keys).toContain("good");
    expect(keys).toContain("pass");
    expect(keys).toContain("danger");
    expect(keys).toHaveLength(4);
  });

  it("HEALTH_LEVEL_CONFIG 不应包含 'fair' 键", () => {
    const keys = Object.keys(HEALTH_LEVEL_CONFIG);
    expect(keys).not.toContain("fair");
  });

  it("每个 HEALTH_LEVEL_CONFIG 条目应包含完整的配置字段", () => {
    for (const [key, config] of Object.entries(HEALTH_LEVEL_CONFIG)) {
      expect(config).toHaveProperty("level", key);
      expect(config).toHaveProperty("label");
      expect(config).toHaveProperty("icon");
      expect(config).toHaveProperty("color");
      expect(config).toHaveProperty("minScore");
      expect(config).toHaveProperty("maxScore");
      expect(config.color).toHaveProperty("bg");
      expect(config.color).toHaveProperty("text");
      expect(config.color).toHaveProperty("border");
      expect(config.label.length).toBeGreaterThan(0);
    }
  });
});

describe("getHealthLevel 函数", () => {
  it("评分 85 应返回 'excellent'", () => {
    expect(getHealthLevel(85)).toBe("excellent");
  });

  it("评分 65 应返回 'good'", () => {
    expect(getHealthLevel(65)).toBe("good");
  });

  it("评分 45 应返回 'pass'", () => {
    expect(getHealthLevel(45)).toBe("pass");
  });

  it("评分 20 应返回 'danger'", () => {
    expect(getHealthLevel(20)).toBe("danger");
  });

  it("getHealthLevel 不应返回 'fair'", () => {
    // 遍历所有可能的分数，确保永远不会返回 "fair"
    for (let score = 0; score <= 100; score++) {
      const result = getHealthLevel(score);
      expect(result).not.toBe("fair");
    }
  });

  it("边界值测试：80 为 excellent 下界", () => {
    expect(getHealthLevel(80)).toBe("excellent");
  });

  it("边界值测试：79 为 good 上界", () => {
    expect(getHealthLevel(79)).toBe("good");
  });

  it("边界值测试：60 为 good 下界", () => {
    expect(getHealthLevel(60)).toBe("good");
  });

  it("边界值测试：59 为 pass 上界", () => {
    expect(getHealthLevel(59)).toBe("pass");
  });

  it("边界值测试：40 为 pass 下界", () => {
    expect(getHealthLevel(40)).toBe("pass");
  });

  it("边界值测试：39 为 danger 上界", () => {
    expect(getHealthLevel(39)).toBe("danger");
  });

  it("边界值测试：0 为 danger", () => {
    expect(getHealthLevel(0)).toBe("danger");
  });

  it("边界值测试：100 为 excellent", () => {
    expect(getHealthLevel(100)).toBe("excellent");
  });
});

describe("onboarding.ts 不再定义自己的 HealthLevel", () => {
  it("onboarding.ts 应从 dashboard-health 导入 HealthLevel 而非自己定义", async () => {
    // 读取 onboarding.ts 源码，验证不再有独立的 HealthLevel 定义
    const onboardingModule = await import("@/types/onboarding");
    // onboarding 导出的 HealthLevel 应该与 dashboard-health 的完全一致
    const dashboardModule = await import("@/types/dashboard-health");

    // 验证 onboarding 导出的 getHealthLevel 返回 "pass" 而非 "fair"
    if ("getHealthLevel" in onboardingModule) {
      const onboardingGetHealthLevel = onboardingModule.getHealthLevel as (
        score: number,
      ) => string;
      expect(onboardingGetHealthLevel(45)).toBe("pass");
      expect(onboardingGetHealthLevel(45)).not.toBe("fair");
    }

    // 验证 onboarding 导出的 HEALTH_LEVELS 是 dashboard-health 的 HEALTH_LEVEL_CONFIG 的别名
    expect(onboardingModule.HEALTH_LEVELS).toBe(dashboardModule.HEALTH_LEVEL_CONFIG);
  });
});
