model-config.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildCliZCodeSourceHeaders,
  4. createRuntimeAiSdkModelExecutionConfig,
  5. } from "../src/model-config.js";
  6. import { createModelAdapter, type CreateModelAdapterOptions } from "../src/model-factory.js";
  7. describe("createModelAdapter", () => {
  8. it("缺少执行基础设施配置时拒绝创建 Adapter", () => {
  9. expect(() => createModelAdapter({ env: {} } as CreateModelAdapterOptions)).toThrow(
  10. "requires executionConfig",
  11. );
  12. });
  13. });
  14. describe("createRuntimeAiSdkModelExecutionConfig", () => {
  15. it("装配来源 Header、网络和路由基础设施", () => {
  16. const endpointRoutingPort = {
  17. async resolve(url: string) {
  18. return { routed: false, url };
  19. },
  20. };
  21. const config = createRuntimeAiSdkModelExecutionConfig(
  22. { ZCODE_APP_VERSION: "3.9.0", ZCODE_ENV: "test" },
  23. {
  24. endpointRoutingPort,
  25. network: {
  26. caCertFile: "/tmp/test-ca.pem",
  27. httpProxy: "http://proxy.example.com",
  28. noProxy: "localhost",
  29. },
  30. sourceTitle: "electron",
  31. },
  32. );
  33. expect(config).toMatchObject({
  34. defaultHeaders: {
  35. "User-Agent": "ZCode/3.9.0",
  36. "X-ZCode-App-Version": "3.9.0",
  37. "X-Title": "Z Code@electron",
  38. },
  39. endpointRoutingPort,
  40. network: {
  41. caCertFile: "/tmp/test-ca.pem",
  42. httpProxy: "http://proxy.example.com",
  43. noProxy: "localhost",
  44. },
  45. });
  46. });
  47. it("过滤空网络配置", () => {
  48. expect(createRuntimeAiSdkModelExecutionConfig({}, { network: {} })).not.toHaveProperty(
  49. "network",
  50. );
  51. });
  52. });
  53. describe("buildCliZCodeSourceHeaders", () => {
  54. it("生成运行时来源 Header,不根据模型或 Provider 推断", () => {
  55. expect(
  56. buildCliZCodeSourceHeaders(
  57. { ZCODE_APP_VERSION: "3.9.0", ZCODE_ENV: "production" },
  58. { sourceTitle: "cli" },
  59. ),
  60. ).toMatchObject({
  61. "User-Agent": "ZCode/3.9.0",
  62. "X-Title": "Z Code@cli",
  63. "X-ZCode-App-Version": "3.9.0",
  64. });
  65. });
  66. it("X-Client-Language 优先使用 Desktop 下发的界面语言", () => {
  67. expect(
  68. buildCliZCodeSourceHeaders({ ZCODE_CLIENT_LANGUAGE: " ja-JP " }, { sourceTitle: "electron" }),
  69. ).toMatchObject({ "X-Client-Language": "ja-JP" });
  70. });
  71. it("界面语言为空或含非打印字符时回落进程 Intl", () => {
  72. const intlLocale = Intl.DateTimeFormat().resolvedOptions().locale;
  73. for (const value of ["", " ", "ko\u0001KR"]) {
  74. expect(
  75. buildCliZCodeSourceHeaders({ ZCODE_CLIENT_LANGUAGE: value }, { sourceTitle: "cli" }),
  76. ).toMatchObject({ "X-Client-Language": intlLocale });
  77. }
  78. });
  79. });