import { describe, expect, it } from "vitest";
import {
  decryptServiceToken,
  encryptServiceToken,
  generateServiceToken,
  hashServiceToken,
  tokenKeyPrefix,
} from "@/modules/customer/token-crypto";

describe("token-crypto", () => {
  it("可加密并解密 token 原文", () => {
    process.env.JWT_SECRET = "unit-test-secret";
    const token = generateServiceToken();
    const ciphertext = encryptServiceToken(token);
    expect(ciphertext).not.toBe(token);
    expect(decryptServiceToken(ciphertext)).toBe(token);
  });

  it("生成 chj_ 前缀 token", () => {
    const token = generateServiceToken();
    expect(token.startsWith("chj_")).toBe(true);
    expect(token.length).toBeGreaterThan(20);
  });

  it("hash 稳定", () => {
    const token = "chj_test_token";
    expect(hashServiceToken(token)).toHaveLength(64);
    expect(hashServiceToken(token)).toBe(hashServiceToken(token));
  });

  it("key prefix 用于列表展示", () => {
    const token = generateServiceToken();
    expect(tokenKeyPrefix(token).length).toBeGreaterThan(0);
  });
});
