llm-to-agent/apps/web-server/src/routes/conversations.test.ts
2026-08-13 16:30:13 +08:00

56 lines
1.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import pino from "pino";
import {
ConversationService,
type Conversation,
type ConversationRepository,
} from "@great-agent/agent-core";
import { createApp } from "../composition/create-app";
class MemoryRepository implements ConversationRepository {
private readonly values = new Map<string, Conversation>();
async listRecent() {
return [...this.values.values()];
}
async getById(id: string) {
return this.values.get(id) ?? null;
}
async create(value: Conversation) {
this.values.set(value.id, value);
}
async appendMessage(id: string, message: Conversation["messages"][number]) {
const current = this.values.get(id);
if (!current) throw new Error("not found");
const next = {
...current,
messages: [...current.messages, message],
updatedAt: message.createdAt,
};
this.values.set(id, next);
return next;
}
}
function createTestApp() {
let id = 0;
const service = new ConversationService({
conversations: new MemoryRepository(),
clock: { now: () => new Date("2026-08-12T00:00:00.000Z") },
ids: { create: () => `id_${++id}` },
});
return createApp(pino({ enabled: false }), service);
}
describe("普通会话路由", () => {
test("会话路由只提供查询,消息必须通过 Run 创建", async () => {
const app = createTestApp();
expect(await (await app.request("/api/conversations")).json()).toEqual([]);
const directWrite = await app.request("/api/conversations", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ content: "你好" }),
});
expect(directWrite.status).toBe(404);
});
});