fix: type llm moderation test fixtures

This commit is contained in:
MythEclipse
2026-05-14 19:28:56 +07:00
parent 65ab5ecb32
commit 54cd4e0386

View File

@@ -1,4 +1,5 @@
import { describe, expect, it, vi, beforeEach } from "vitest"; import { describe, expect, it, vi, beforeEach } from "vitest";
import type { MessageRecord } from "../../src/moderation/types";
import { import {
parseModerationResponse, parseModerationResponse,
runModerationAnalysis, runModerationAnalysis,
@@ -8,6 +9,33 @@ vi.mock("../../src/retry", () => ({
retryWithBackoff: vi.fn((fn) => fn()), retryWithBackoff: vi.fn((fn) => fn()),
})); }));
/**
* Helper to create a full MessageRecord fixture with sensible defaults.
* Only override fields that differ from defaults.
*/
function createMessageRecord(
overrides: Partial<MessageRecord> = {},
): MessageRecord {
const now = Date.now();
return {
id: "m1",
guild_id: "guild123",
channel_id: "channel123",
thread_id: null,
user_id: "user123",
username: "user1",
avatar_url: null,
content: "hello",
edited_content: null,
created_at: now,
edited_at: null,
deleted_at: null,
type: "text",
metadata: null,
...overrides,
};
}
describe("parseModerationResponse", () => { describe("parseModerationResponse", () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
@@ -255,7 +283,7 @@ describe("runModerationAnalysis", () => {
}); });
const result = await runModerationAnalysis({ const result = await runModerationAnalysis({
targets: [{ id: "m1", username: "user1", content: "hello" }], targets: [createMessageRecord()],
contextText: "test context", contextText: "test context",
}); });
@@ -273,7 +301,7 @@ describe("runModerationAnalysis", () => {
await expect( await expect(
runModerationAnalysis({ runModerationAnalysis({
targets: [{ id: "m1", username: "user1", content: "hello" }], targets: [createMessageRecord()],
contextText: "test context", contextText: "test context",
}), }),
).rejects.toThrow(/LLM API error 500/); ).rejects.toThrow(/LLM API error 500/);
@@ -287,7 +315,7 @@ describe("runModerationAnalysis", () => {
await expect( await expect(
runModerationAnalysis({ runModerationAnalysis({
targets: [{ id: "m1", username: "user1", content: "hello" }], targets: [createMessageRecord()],
contextText: "test context", contextText: "test context",
}), }),
).rejects.toThrow(/Invalid LLM response structure/); ).rejects.toThrow(/Invalid LLM response structure/);
@@ -303,7 +331,7 @@ describe("runModerationAnalysis", () => {
await expect( await expect(
runModerationAnalysis({ runModerationAnalysis({
targets: [{ id: "m1", username: "user1", content: "hello" }], targets: [createMessageRecord()],
contextText: "test context", contextText: "test context",
}), }),
).rejects.toThrow(/No content in LLM response/); ).rejects.toThrow(/No content in LLM response/);