test: tighten server method helper coverage

This commit is contained in:
Peter Steinberger 2026-03-13 17:57:05 +00:00
parent 981062a94e
commit 91f1894372
1 changed files with 55 additions and 44 deletions

View File

@ -221,59 +221,70 @@ describe("injectTimestamp", () => {
});
describe("timestampOptsFromConfig", () => {
it("extracts timezone from config", () => {
const opts = timestampOptsFromConfig({
agents: {
defaults: {
userTimezone: "America/Chicago",
},
},
it.each([
{
name: "extracts timezone from config",
// oxlint-disable-next-line typescript/no-explicit-any
} as any);
expect(opts.timezone).toBe("America/Chicago");
});
it("falls back gracefully with empty config", () => {
// oxlint-disable-next-line typescript/no-explicit-any
const opts = timestampOptsFromConfig({} as any);
expect(opts.timezone).toBeDefined();
cfg: { agents: { defaults: { userTimezone: "America/Chicago" } } } as any,
expected: "America/Chicago",
},
{
name: "falls back gracefully with empty config",
// oxlint-disable-next-line typescript/no-explicit-any
cfg: {} as any,
expected: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
])("$name", ({ cfg, expected }) => {
expect(timestampOptsFromConfig(cfg).timezone).toBe(expected);
});
});
describe("normalizeRpcAttachmentsToChatAttachments", () => {
it("passes through string content", () => {
const res = normalizeRpcAttachmentsToChatAttachments([
{ type: "file", mimeType: "image/png", fileName: "a.png", content: "Zm9v" },
]);
expect(res).toEqual([
{ type: "file", mimeType: "image/png", fileName: "a.png", content: "Zm9v" },
]);
});
it("converts Uint8Array content to base64", () => {
const bytes = new TextEncoder().encode("foo");
const res = normalizeRpcAttachmentsToChatAttachments([{ content: bytes }]);
expect(res[0]?.content).toBe("Zm9v");
it.each([
{
name: "passes through string content",
attachments: [{ type: "file", mimeType: "image/png", fileName: "a.png", content: "Zm9v" }],
expected: [{ type: "file", mimeType: "image/png", fileName: "a.png", content: "Zm9v" }],
},
{
name: "converts Uint8Array content to base64",
attachments: [{ content: new TextEncoder().encode("foo") }],
expected: [{ type: undefined, mimeType: undefined, fileName: undefined, content: "Zm9v" }],
},
{
name: "converts ArrayBuffer content to base64",
attachments: [{ content: new TextEncoder().encode("bar").buffer }],
expected: [{ type: undefined, mimeType: undefined, fileName: undefined, content: "YmFy" }],
},
{
name: "drops attachments without usable content",
attachments: [{ content: undefined }, { mimeType: "image/png" }],
expected: [],
},
])("$name", ({ attachments, expected }) => {
expect(normalizeRpcAttachmentsToChatAttachments(attachments)).toEqual(expected);
});
});
describe("sanitizeChatSendMessageInput", () => {
it("rejects null bytes", () => {
expect(sanitizeChatSendMessageInput("before\u0000after")).toEqual({
ok: false,
error: "message must not contain null bytes",
});
});
it("strips unsafe control characters while preserving tab/newline/carriage return", () => {
const result = sanitizeChatSendMessageInput("a\u0001b\tc\nd\re\u0007f\u007f");
expect(result).toEqual({ ok: true, message: "ab\tc\nd\ref" });
});
it("normalizes unicode to NFC", () => {
expect(sanitizeChatSendMessageInput("Cafe\u0301")).toEqual({ ok: true, message: "Café" });
it.each([
{
name: "rejects null bytes",
input: "before\u0000after",
expected: { ok: false as const, error: "message must not contain null bytes" },
},
{
name: "strips unsafe control characters while preserving tab/newline/carriage return",
input: "a\u0001b\tc\nd\re\u0007f\u007f",
expected: { ok: true as const, message: "ab\tc\nd\ref" },
},
{
name: "normalizes unicode to NFC",
input: "Cafe\u0301",
expected: { ok: true as const, message: "Café" },
},
])("$name", ({ input, expected }) => {
expect(sanitizeChatSendMessageInput(input)).toEqual(expected);
});
});