Skip to content

Commit 199f460

Browse files
committed
fix: refactor AI config handling in summary tests to use createAIConfigReader
1 parent 67d9587 commit 199f460

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

server/src/utils/__tests__/ai-summary.test.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import { afterEach, describe, expect, it, mock } from "bun:test";
2+
import type { AIConfig } from "@rin/api";
3+
import { AI_CONFIG_PREFIX } from "@rin/config";
24

35
const originalFetch = globalThis.fetch;
46

5-
const getAIConfigMock = mock();
6-
7-
mock.module("../db-config", () => ({
8-
getAIConfig: getAIConfigMock,
9-
}));
10-
117
afterEach(() => {
128
globalThis.fetch = originalFetch;
13-
getAIConfigMock.mockReset();
149
});
1510

11+
function createAIConfigReader(config: AIConfig) {
12+
const values = new Map<string, unknown>(
13+
Object.entries(config).map(([key, value]) => [`${AI_CONFIG_PREFIX}${key}`, value]),
14+
);
15+
16+
return {
17+
async get(key: string) {
18+
return values.get(key);
19+
},
20+
};
21+
}
22+
1623
describe("generateAISummaryResult", () => {
1724
it("returns a concrete error when AI responds with empty content", async () => {
18-
getAIConfigMock.mockResolvedValue({
25+
const serverConfig = createAIConfigReader({
1926
enabled: true,
2027
provider: "worker-ai",
2128
model: "llama-3-8b",
@@ -29,15 +36,15 @@ describe("generateAISummaryResult", () => {
2936
AI: {
3037
run: async () => ({ response: "" }),
3138
},
32-
} as unknown as Env, {} as any, "test content");
39+
} as unknown as Env, serverConfig, "test content");
3340

3441
expect(result.summary).toBeNull();
3542
expect(result.skipped).toBe(false);
3643
expect(result.error).toContain('Empty response from AI provider "worker-ai"');
3744
});
3845

3946
it("sends summary system prompt to Workers AI", async () => {
40-
getAIConfigMock.mockResolvedValue({
47+
const serverConfig = createAIConfigReader({
4148
enabled: true,
4249
provider: "worker-ai",
4350
model: "llama-3-8b",
@@ -55,7 +62,7 @@ describe("generateAISummaryResult", () => {
5562
return { response: "summary" };
5663
},
5764
},
58-
} as unknown as Env, {} as any, "test content");
65+
} as unknown as Env, serverConfig, "test content");
5966

6067
expect(result.summary).toBe("summary");
6168
expect(calls).toHaveLength(1);
@@ -70,7 +77,7 @@ describe("generateAISummaryResult", () => {
7077
});
7178

7279
it("sends summary system prompt to external AI providers", async () => {
73-
getAIConfigMock.mockResolvedValue({
80+
const serverConfig = createAIConfigReader({
7481
enabled: true,
7582
provider: "openai",
7683
model: "gpt-4o-mini",
@@ -91,7 +98,7 @@ describe("generateAISummaryResult", () => {
9198

9299
const { AI_SUMMARY_SYSTEM_PROMPT, generateAISummaryResult } = await import("../ai");
93100

94-
const result = await generateAISummaryResult({} as Env, {} as any, "external content");
101+
const result = await generateAISummaryResult({} as Env, serverConfig, "external content");
95102

96103
expect(result.summary).toBe("summary");
97104
expect(requests).toHaveLength(1);

0 commit comments

Comments
 (0)