Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

2.1 KB
Raw Blame
import { describe, expect, it } from "vitest";

import {
  chunkWecomMarkdown,
  decodeWecomLane,
  encodeWecomGroupLane,
  escapeWecomMarkdown,
} from "../codec.js";

describe("WeCom lane codec", () => {
  it("round-trips group chat ids without exposing path separators", () => {
    const lane = encodeWecomGroupLane("chat/with spaces/中文");
    expect(lane).toMatch(/^group\/[A-Za-z0-9_-]+$/);
    expect(decodeWecomLane(lane)).toEqual({
      kind: "group",
      targetId: "chat/with spaces/中文",
    });
  });

  it("treats ordinary ids as single-chat targets", () => {
    expect(decodeWecomLane("zhangsan")).toEqual({
      kind: "single",
      targetId: "zhangsan",
    });
  });

  it.each(["group/", "group/abc=", "group/abc+", "group/abc/", "group/_w"])(
    "rejects malformed group lane %s",
    (lane) => {
      expect(() => decodeWecomLane(lane)).toThrow("WECOM_GROUP_LANE_INVALID");
    },
  );
});

describe("WeCom markdown chunking", () => {
  it("keeps every chunk under the transport byte limit", () => {
    const chunks = chunkWecomMarkdown("中".repeat(20_000));
    expect(chunks.length).toBeGreaterThan(1);
    expect(
      chunks.every((chunk) => Buffer.byteLength(chunk, "utf8") <= 18 * 1024),
    ).toBe(true);
    expect(chunks.join("")).toBe("中".repeat(20_000));
  });

  it("normalizes an empty final response", () => {
    expect(chunkWecomMarkdown(" \r\n ")).toEqual([]);
  });

  it("preserves code fences and line boundaries in long Markdown replies", () => {
    const text = "```ts\n" + "const 中文 = '🙂';\n".repeat(1500) + "```";
    const chunks = chunkWecomMarkdown(text);
    expect(chunks.length).toBeGreaterThan(1);
    expect(chunks.every((chunk) => Buffer.byteLength(chunk, "utf8") <= 18 * 1024)).toBe(true);
    expect(chunks.every((chunk) => chunk.startsWith("```ts\n") && chunk.endsWith("```"))).toBe(true);
    expect(chunks.join("").match(/const 中文 = '🙂';/g)).toHaveLength(1500);
  });

  it("escapes plain text before sending it through markdown", () => {
    expect(escapeWecomMarkdown("**not bold** [link](x)")).toBe(
      "\\*\\*not bold\\*\\* \\[link\\]\\(x\\)",
    );
  });
});