model-config.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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("只接受执行基础设施配置,不接受 Provider Registry 投影", () => {
  9. expect(() => createModelAdapter({ env: {} } as CreateModelAdapterOptions)).toThrow(
  10. "requires executionConfig",
  11. );
  12. const adapter = createModelAdapter({
  13. env: {},
  14. executionConfig: createRuntimeAiSdkModelExecutionConfig({}),
  15. });
  16. expect(adapter).not.toHaveProperty("replaceRegistryConfig");
  17. expect(adapter).not.toHaveProperty("resolveConnection");
  18. });
  19. it("接受模型 IO 与流超时等 Adapter 自身选项", () => {
  20. expect(() =>
  21. createModelAdapter({
  22. env: {},
  23. executionConfig: createRuntimeAiSdkModelExecutionConfig({}),
  24. modelIoDir: "/tmp/zcode-model-io-test",
  25. modelIoFullRetentionEnabled: true,
  26. streamIdleTimeoutMs: 42_000,
  27. }),
  28. ).not.toThrow();
  29. });
  30. });
  31. describe("createRuntimeAiSdkModelExecutionConfig", () => {
  32. it("只装配来源 Header、网络、路由、环境和签名基础设施", () => {
  33. const endpointRoutingPort = {
  34. async resolve(url: string) {
  35. return { routed: false, url };
  36. },
  37. };
  38. const config = createRuntimeAiSdkModelExecutionConfig(
  39. { ZCODE_APP_VERSION: "3.9.0", ZCODE_ENV: "test" },
  40. {
  41. endpointRoutingPort,
  42. network: {
  43. caCertFile: "/tmp/test-ca.pem",
  44. httpProxy: "http://proxy.example.com",
  45. noProxy: "localhost",
  46. },
  47. sourceTitle: "electron",
  48. },
  49. );
  50. expect(config).toMatchObject({
  51. defaultHeaders: {
  52. "User-Agent": "ZCode/3.9.0",
  53. "X-ZCode-App-Version": "3.9.0",
  54. "X-Title": "Z Code@electron",
  55. },
  56. endpointRoutingPort,
  57. network: {
  58. caCertFile: "/tmp/test-ca.pem",
  59. httpProxy: "http://proxy.example.com",
  60. noProxy: "localhost",
  61. },
  62. });
  63. expect(config).not.toHaveProperty("providers");
  64. expect(config).not.toHaveProperty("defaultProviderId");
  65. });
  66. it("过滤空网络配置", () => {
  67. expect(createRuntimeAiSdkModelExecutionConfig({}, { network: {} })).not.toHaveProperty(
  68. "network",
  69. );
  70. });
  71. });
  72. describe("buildCliZCodeSourceHeaders", () => {
  73. it("生成运行时来源 Header,不根据模型或 Provider 推断", () => {
  74. expect(
  75. buildCliZCodeSourceHeaders(
  76. { ZCODE_APP_VERSION: "3.9.0", ZCODE_ENV: "production" },
  77. { sourceTitle: "cli" },
  78. ),
  79. ).toMatchObject({
  80. "User-Agent": "ZCode/3.9.0",
  81. "X-Title": "Z Code@cli",
  82. "X-ZCode-App-Version": "3.9.0",
  83. });
  84. });
  85. });