import { describe, it } from "node:test";
import assert from "node:assert/strict";

import { getMessageHash, hasNewReply } from "../src/wangwang-rpa.js";

describe("wangwang-rpa 消息哈希", () => {
  it("getMessageHash 返回 32 位 hex", () => {
    const hash = getMessageHash("老板有现货");
    assert.equal(hash.length, 32);
    assert.ok(/^[0-9a-f]{32}$/.test(hash));
  });

  it("相同文本返回相同哈希", () => {
    const a = getMessageHash("有现货的");
    const b = getMessageHash("有现货的");
    assert.equal(a, b);
  });

  it("不同文本返回不同哈希", () => {
    const a = getMessageHash("有货");
    const b = getMessageHash("没货");
    assert.notEqual(a, b);
  });

  it("空字符串返回稳定哈希", () => {
    const hash = getMessageHash("");
    assert.equal(hash.length, 32);
  });

  it("hasNewReply: 不同哈希 → true", () => {
    assert.ok(hasNewReply("abc123", "def456"));
  });

  it("hasNewReply: 相同哈希 → false", () => {
    assert.ok(!hasNewReply("abc123", "abc123"));
  });

  it("hasNewReply: lastStoredHash 为空 → true", () => {
    assert.ok(hasNewReply("abc123", null));
    assert.ok(hasNewReply("abc123", ""));
  });

  it("hasNewReply: currentHash 为空且 lastStoredHash 也为空 → false", () => {
    assert.ok(!hasNewReply("", ""));
    assert.ok(!hasNewReply("", null));
  });
});
