openai-to-codex-request.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import { transformOpenAIRequestToCodex } from "@/app/v1/_lib/converters/openai-to-codex/request";
  3. describe("OpenAI → Codex 转换 - instructions 透传", () => {
  4. it("当输入包含 instructions 时应直接透传", () => {
  5. const originalInstructions = "透传:不要被转换器覆盖";
  6. const input: Record<string, unknown> = {
  7. instructions: originalInstructions,
  8. messages: [{ role: "user", content: "你好" }],
  9. };
  10. const output = transformOpenAIRequestToCodex("gpt-5-codex", input, true) as any;
  11. expect(output.instructions).toBe(originalInstructions);
  12. });
  13. it("当输入无 instructions 但有 system messages 时,应把 system 文本映射到 instructions", () => {
  14. const input: Record<string, unknown> = {
  15. messages: [
  16. { role: "system", content: "系统指令 1" },
  17. { role: "system", content: "系统指令 2" },
  18. { role: "user", content: "用户消息" },
  19. ],
  20. };
  21. const output = transformOpenAIRequestToCodex("gpt-5-codex", input, true) as any;
  22. expect(output.instructions).toBe("系统指令 1\n\n系统指令 2");
  23. expect(output.input?.[0]?.role).toBe("user");
  24. expect(output.input?.[0]?.content?.[0]?.text).toBe("用户消息");
  25. });
  26. it("当输入既无 instructions 也无 system messages 时,不应注入默认 instructions", () => {
  27. const input: Record<string, unknown> = {
  28. messages: [{ role: "user", content: "用户消息" }],
  29. };
  30. const output = transformOpenAIRequestToCodex("gpt-5-codex", input, true) as any;
  31. expect(output.instructions).toBeUndefined();
  32. });
  33. it("当输入显式设置 parallel_tool_calls=false 时,应透传到 Codex 请求", () => {
  34. const input: Record<string, unknown> = {
  35. messages: [{ role: "user", content: "你好" }],
  36. parallel_tool_calls: false,
  37. };
  38. const output = transformOpenAIRequestToCodex("gpt-5-codex", input, true) as any;
  39. expect(output.parallel_tool_calls).toBe(false);
  40. });
  41. });