test2123213
132 行
4.2 KiB
JavaScript
132 行
4.2 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const test = require("node:test");
|
|
|
|
const {handler} = require("./index.js");
|
|
|
|
test("event draft uses the injected model router and preserves known context", async (t) => {
|
|
const previousEnv = {...process.env};
|
|
const previousFetch = global.fetch;
|
|
t.after(() => {
|
|
process.env = previousEnv;
|
|
global.fetch = previousFetch;
|
|
});
|
|
|
|
Object.assign(process.env, {
|
|
OPENAI_BASE_URL: "https://router.example.test/v1/",
|
|
OPENAI_API_KEY: "test-token",
|
|
OPENAI_MODEL: "test-model"
|
|
});
|
|
|
|
global.fetch = async (url, init) => {
|
|
assert.equal(url, "https://router.example.test/v1/chat/completions");
|
|
assert.equal(init.headers.authorization, "Bearer test-token");
|
|
const body = JSON.parse(init.body);
|
|
assert.equal(body.model, "test-model");
|
|
assert.deepEqual(body.messages.map((message) => message.role), ["system", "user"]);
|
|
const request = JSON.parse(body.messages[1].content);
|
|
assert.equal(request.idea, "想办一场独立开发者互相体验产品的周末活动");
|
|
assert.deepEqual(request.known_context, {
|
|
category: "产品交流",
|
|
district: "武昌区"
|
|
});
|
|
return new Response(JSON.stringify({
|
|
choices: [{
|
|
message: {
|
|
role: "assistant",
|
|
content: "```json\n"
|
|
+ JSON.stringify({
|
|
title: "周末产品互诊室",
|
|
category: "产品交流",
|
|
district: "武昌区",
|
|
venue: "武昌区交通便利、可分组交流的共享空间",
|
|
description: "邀请武汉独立开发者带上正在打磨的产品,通过分组体验、问题反馈和行动计划三个环节,交换真实用户视角并认识同频伙伴。"
|
|
})
|
|
+ "\n```"
|
|
}
|
|
}]
|
|
}), {
|
|
status: 200,
|
|
headers: {"content-type": "application/json"}
|
|
});
|
|
};
|
|
|
|
const response = await handler({
|
|
path: "/api/cloud/event-draft",
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
idea: "想办一场独立开发者互相体验产品的周末活动",
|
|
category: "产品交流",
|
|
district: "武昌区"
|
|
})
|
|
});
|
|
|
|
assert.equal(response.statusCode, 200);
|
|
assert.deepEqual(JSON.parse(response.body), {
|
|
ok: true,
|
|
draft: {
|
|
title: "周末产品互诊室",
|
|
category: "产品交流",
|
|
district: "武昌区",
|
|
venue: "武昌区交通便利、可分组交流的共享空间",
|
|
description: "邀请武汉独立开发者带上正在打磨的产品,通过分组体验、问题反馈和行动计划三个环节,交换真实用户视角并认识同频伙伴。"
|
|
}
|
|
});
|
|
});
|
|
|
|
test("event submission validates required fields before database access", async () => {
|
|
const response = await handler({
|
|
path: "/api/cloud/events",
|
|
method: "POST",
|
|
body: JSON.stringify({title: "周末共学"})
|
|
});
|
|
|
|
assert.equal(response.statusCode, 400);
|
|
const payload = JSON.parse(response.body);
|
|
assert.equal(payload.ok, false);
|
|
assert.match(payload.error, /发起人/);
|
|
});
|
|
|
|
test("event draft rejects an empty idea", async () => {
|
|
const response = await handler({
|
|
path: "/api/cloud/event-draft",
|
|
method: "POST",
|
|
body: JSON.stringify({idea: " "})
|
|
});
|
|
|
|
assert.equal(response.statusCode, 400);
|
|
assert.deepEqual(JSON.parse(response.body), {
|
|
ok: false,
|
|
error: "请先写下一句活动想法"
|
|
});
|
|
});
|
|
|
|
test("event draft rejects invalid structured model output", async (t) => {
|
|
const previousEnv = {...process.env};
|
|
const previousFetch = global.fetch;
|
|
t.after(() => {
|
|
process.env = previousEnv;
|
|
global.fetch = previousFetch;
|
|
});
|
|
|
|
Object.assign(process.env, {
|
|
OPENAI_BASE_URL: "https://router.example.test/v1",
|
|
OPENAI_API_KEY: "test-token",
|
|
OPENAI_MODEL: "test-model"
|
|
});
|
|
global.fetch = async () => new Response(JSON.stringify({
|
|
choices: [{message: {role: "assistant", content: "这不是 JSON"}}]
|
|
}), {
|
|
status: 200,
|
|
headers: {"content-type": "application/json"}
|
|
});
|
|
|
|
const response = await handler({
|
|
path: "/api/cloud/event-draft",
|
|
method: "POST",
|
|
body: JSON.stringify({idea: "想在东湖边组织一场周末城市漫游活动"})
|
|
});
|
|
|
|
assert.equal(response.statusCode, 502);
|
|
assert.match(JSON.parse(response.body).error, /不是有效 JSON/);
|
|
});
|