codexConfigToml.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { describe, expect, it } from "vitest";
  2. import { parse as parseToml } from "smol-toml";
  3. import { stringifyCodexConfigToml } from "../src/providers/codexConfigToml.js";
  4. describe("codexConfigToml", () => {
  5. it("会把 provider 的 http_headers 序列化为 inline table", () => {
  6. const toml = stringifyCodexConfigToml({
  7. model_provider: "OpenAI",
  8. model_providers: {
  9. OpenAI: {
  10. base_url: "https://api.openai.com/v1",
  11. wire_api: "responses",
  12. requires_openai_auth: true,
  13. http_headers: {
  14. "User-Agent": "ZCode/unknown",
  15. "HTTP-Referer": "https://zcode.z.ai",
  16. "X-Title": "Z Code@electron",
  17. },
  18. },
  19. },
  20. });
  21. expect(toml).toContain("[model_providers.OpenAI]");
  22. expect(toml).toContain("http_headers = {");
  23. expect(toml).not.toContain("[model_providers.OpenAI.http_headers]");
  24. });
  25. it("provider 只有 http_headers 时也会写成 inline table", () => {
  26. const toml = stringifyCodexConfigToml({
  27. model_providers: {
  28. OpenAI: {
  29. http_headers: {
  30. "User-Agent": "ZCode/unknown",
  31. "HTTP-Referer": "https://zcode.z.ai",
  32. "X-Title": "Z Code@electron",
  33. },
  34. },
  35. },
  36. });
  37. expect(toml).toContain("[model_providers.OpenAI]");
  38. expect(toml).toContain("http_headers = {");
  39. expect(toml).not.toContain("[model_providers.OpenAI.http_headers]");
  40. });
  41. it("会兼容需要引号的 provider id", () => {
  42. const toml = stringifyCodexConfigToml({
  43. model_provider: "Provider Demo",
  44. model_providers: {
  45. "Provider Demo": {
  46. base_url: "https://example.com/v1",
  47. http_headers: {
  48. "X-Title": "Z Code@electron",
  49. },
  50. },
  51. },
  52. });
  53. expect(toml).toContain('[model_providers."Provider Demo"]');
  54. expect(toml).toContain("http_headers = {");
  55. expect(toml).not.toContain('[model_providers."Provider Demo".http_headers]');
  56. });
  57. it("会清理 query_params.http_headers 并把 http_headers 固定写在 provider 主表", () => {
  58. const toml = stringifyCodexConfigToml({
  59. model_provider: "custom",
  60. model_providers: {
  61. custom: {
  62. name: "custom",
  63. base_url: "http://192.168.100.166:8080/v1",
  64. wire_api: "responses",
  65. requires_openai_auth: true,
  66. query_params: {
  67. http_headers: {
  68. "User-Agent": "legacy/invalid",
  69. },
  70. },
  71. http_headers: {
  72. "User-Agent": "ZCode/unknown",
  73. "HTTP-Referer": "https://zcode.z.ai",
  74. "X-Title": "Z Code@electron",
  75. },
  76. },
  77. },
  78. });
  79. expect(toml).toContain("[model_providers.custom]");
  80. expect(toml).toContain("http_headers = {");
  81. expect(toml).not.toContain("[model_providers.custom.http_headers]");
  82. expect(toml).not.toContain("[model_providers.custom.query_params.http_headers]");
  83. expect(toml).not.toContain("[model_providers.custom.query_params.http_headers.http_headers]");
  84. const parsed = parseToml(toml) as {
  85. model_providers?: {
  86. custom?: {
  87. query_params?: Record<string, unknown>;
  88. http_headers?: Record<string, unknown>;
  89. };
  90. };
  91. };
  92. expect(parsed.model_providers?.custom?.query_params?.http_headers).toBeUndefined();
  93. expect(parsed.model_providers?.custom?.http_headers).toMatchObject({
  94. "User-Agent": "ZCode/unknown",
  95. "HTTP-Referer": "https://zcode.z.ai",
  96. "X-Title": "Z Code@electron",
  97. });
  98. });
  99. });