67 lines
3.1 KiB
TypeScript
67 lines
3.1 KiB
TypeScript
import { test, expect, describe } from "bun:test";
|
|
import {
|
|
describeLarkError, isTokenInvalidCode, normalizeDomain, parseLarkReply, replyUuid, sentSignature, validateAppId, validateAppSecret,
|
|
} from "../src/openplatform_lark_bots_core";
|
|
|
|
describe("larkReply (the shape integrators send on task creation)", () => {
|
|
test("absent / null means no reply", () => {
|
|
expect(parseLarkReply(undefined)).toEqual({ ok: true, value: null });
|
|
expect(parseLarkReply(null)).toEqual({ ok: true, value: null });
|
|
});
|
|
test("accepts lkb_ + om_, trimmed", () => {
|
|
expect(parseLarkReply({ botId: " lkb_0123456789ab ", messageId: "om_x100b4f2a_c-9" }))
|
|
.toEqual({ ok: true, value: { botId: "lkb_0123456789ab", messageId: "om_x100b4f2a_c-9" } });
|
|
});
|
|
test("rejects wrong shapes and ids", () => {
|
|
expect(parseLarkReply("lkb_0123456789ab").ok).toBe(false);
|
|
expect(parseLarkReply([]).ok).toBe(false);
|
|
expect(parseLarkReply({ botId: "opw_0123456789ab", messageId: "om_abcd" }).ok).toBe(false);
|
|
// thread id (omt_) is not a message id — the reply API needs a message
|
|
expect(parseLarkReply({ botId: "lkb_0123456789ab", messageId: "omt_abcd" }).ok).toBe(false);
|
|
expect(parseLarkReply({ botId: "lkb_0123456789ab" }).ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("credentials", () => {
|
|
test("app id shape", () => {
|
|
expect(validateAppId(" cli_a1b2c3d4e5f6a7b8 ")).toEqual({ ok: true, value: "cli_a1b2c3d4e5f6a7b8" });
|
|
expect(validateAppId("a1b2c3d4").ok).toBe(false);
|
|
expect(validateAppId("").ok).toBe(false);
|
|
});
|
|
test("app secret shape", () => {
|
|
expect(validateAppSecret("AbCdEfGhIjKlMnOpQrStUvWxYz012345").ok).toBe(true);
|
|
expect(validateAppSecret("short").ok).toBe(false);
|
|
expect(validateAppSecret("has space in it 1234567890").ok).toBe(false);
|
|
});
|
|
test("domain defaults to feishu", () => {
|
|
expect(normalizeDomain(undefined)).toEqual({ ok: true, value: "feishu" });
|
|
expect(normalizeDomain("lark")).toEqual({ ok: true, value: "lark" });
|
|
expect(normalizeDomain("larksuite").ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("dedupe", () => {
|
|
test("same kind + same entry time = same card; anything else differs", () => {
|
|
expect(sentSignature("manual_review", 100)).toBe(sentSignature("manual_review", 100));
|
|
expect(sentSignature("manual_review", 100)).not.toBe(sentSignature("manual_review", 200));
|
|
expect(sentSignature("fix_succeeded", 100)).not.toBe(sentSignature("awaiting_merge", 100));
|
|
});
|
|
test("reply uuid is deterministic and within Lark's 50-char limit", () => {
|
|
const u = replyUuid("task-1", sentSignature("dispatched", 1));
|
|
expect(u).toBe(replyUuid("task-1", sentSignature("dispatched", 1)));
|
|
expect(u).not.toBe(replyUuid("task-2", sentSignature("dispatched", 1)));
|
|
expect(u.length).toBeLessThanOrEqual(50);
|
|
});
|
|
});
|
|
|
|
describe("lark errors", () => {
|
|
test("token-invalid codes trigger a refresh", () => {
|
|
expect(isTokenInvalidCode(99991663)).toBe(true);
|
|
expect(isTokenInvalidCode(230002)).toBe(false);
|
|
});
|
|
test("unknown codes keep Lark's code and msg", () => {
|
|
expect(describeLarkError(12345, "boom")).toContain("12345");
|
|
expect(describeLarkError(12345, "boom")).toContain("boom");
|
|
});
|
|
});
|