dreame-action/test/signature.test.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-06-19 00:53:13 +08:00
import { createHash } from "crypto";
import { describe, expect, test } from "vitest";
import { buildSignedRequest, dreameSign, stringifyBody } from "../app/services/signature.server";
describe("dreame signature", () => {
test("generates stable 32-char lowercase md5", () => {
const bodyString = '{"activity_id":12}';
const timestamp = "1776901800000";
const secret = "abc123secret";
const expected = createHash("md5")
.update(Buffer.from(`${bodyString}${timestamp}${secret}`, "utf8"))
.digest("hex");
const sign = dreameSign(bodyString, timestamp, secret);
expect(sign).toBe(expected);
expect(sign).toMatch(/^[a-f0-9]{32}$/);
});
test("uses the same serialized body for signing and fetch", () => {
const payload = { activity_id: 12 };
const bodyString = stringifyBody(payload);
const signed = buildSignedRequest(payload, {
apiAppId: "app-id",
apiSecret: "secret",
timestamp: "1776901800000",
});
expect(signed.bodyString).toBe(bodyString);
expect(signed.bodyString).toBe('{"activity_id":12}');
expect(signed.headers["dreame-api-sign"]).toBe(
dreameSign(bodyString, "1776901800000", "secret"),
);
});
});