/**
 * exam-types 工具函数测试
 *
 * 测试 buildDraftKey / getStudentId / cn 等工具函数。
 */

import { describe, it, expect, beforeEach } from "vitest";
import { buildDraftKey, getStudentId, cn } from "./exam-types";

describe("buildDraftKey", () => {
  it("应返回 exam-draft:examId:studentId 格式", () => {
    const key = buildDraftKey("exam-001", "student-001");
    expect(key).toBe("exam-draft:exam-001:student-001");
  });

  it("应包含 examId 和 studentId", () => {
    const examId = "midterm-math";
    const studentId = "user-abc";
    const key = buildDraftKey(examId, studentId);
    expect(key).toContain(examId);
    expect(key).toContain(studentId);
  });

  it("不同参数应生成不同 key", () => {
    const key1 = buildDraftKey("exam-001", "student-001");
    const key2 = buildDraftKey("exam-002", "student-001");
    const key3 = buildDraftKey("exam-001", "student-002");
    expect(key1).not.toBe(key2);
    expect(key1).not.toBe(key3);
    expect(key2).not.toBe(key3);
  });

  it("应以 exam-draft: 前缀开头", () => {
    const key = buildDraftKey("exam-001", "student-001");
    expect(key.startsWith("exam-draft:")).toBe(true);
  });
});

describe("getStudentId", () => {
  beforeEach(() => {
    localStorage.clear();
  });

  it("localStorage 为空时应返回默认值 current-student", () => {
    const id = getStudentId();
    expect(id).toBe("current-student");
  });

  it("localStorage 有 edu-user 且含 id 时应返回该 id", () => {
    localStorage.setItem(
      "edu-user",
      JSON.stringify({ id: "student-xyz", name: "张三" }),
    );
    const id = getStudentId();
    expect(id).toBe("student-xyz");
  });

  it("localStorage 有 edu-user 但无 id 时应返回默认值", () => {
    localStorage.setItem(
      "edu-user",
      JSON.stringify({ name: "张三" }),
    );
    const id = getStudentId();
    expect(id).toBe("current-student");
  });

  it("localStorage edu-user 为无效 JSON 时应返回默认值", () => {
    localStorage.setItem("edu-user", "not-valid-json{");
    const id = getStudentId();
    expect(id).toBe("current-student");
  });

  it("localStorage edu-user 为 null JSON 时应返回默认值", () => {
    localStorage.setItem("edu-user", "null");
    const id = getStudentId();
    expect(id).toBe("current-student");
  });
});

describe("cn", () => {
  it("应拼接多个字符串类名", () => {
    expect(cn("a", "b", "c")).toBe("a b c");
  });

  it("应过滤 false 值", () => {
    expect(cn("a", false, "b")).toBe("a b");
  });

  it("应过滤 null 值", () => {
    expect(cn("a", null, "b")).toBe("a b");
  });

  it("应过滤 undefined 值", () => {
    expect(cn("a", undefined, "b")).toBe("a b");
  });

  it("应过滤空字符串", () => {
    expect(cn("a", "", "b")).toBe("a b");
  });

  it("全部为 falsy 时应返回空字符串", () => {
    expect(cn(false, null, undefined, "")).toBe("");
  });

  it("无参数时应返回空字符串", () => {
    expect(cn()).toBe("");
  });

  it("单个类名应原样返回", () => {
    expect(cn("only-class")).toBe("only-class");
  });

  it("条件类名场景", () => {
    const isActive = true;
    const isDisabled = false;
    const result = cn("btn", isActive && "btn-active", isDisabled && "btn-disabled");
    expect(result).toBe("btn btn-active");
  });
});
