connectivity-probe-wire.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { streamText } from "ai";
  2. import { describe, expect, it } from "vitest";
  3. import { createStreamTextOptions } from "../src/model/runner-options.js";
  4. import { TestProviderConfigFixture } from "./test-provider-config.js";
  5. import { createTestModelProperties } from "./test-model-format.js";
  6. describe("connectivity probe wire contract", () => {
  7. it.each([
  8. { providerId: "api-anthropic", kind: "anthropic" },
  9. { providerId: "api-chat", kind: "openai-compatible" },
  10. { providerId: "api-responses", kind: "openai" },
  11. { providerId: "account:bigmodel-start-plan", kind: "anthropic", accountMode: "start-plan" },
  12. {
  13. providerId: "account:bigmodel-individual-coding-plan",
  14. kind: "anthropic",
  15. accountMode: "individual-coding-plan",
  16. },
  17. {
  18. providerId: "account:zai-team-coding-plan",
  19. kind: "anthropic",
  20. accountMode: "team-coding-plan",
  21. },
  22. ] as const)(
  23. "preserves roles, text and identity for $providerId",
  24. async ({ providerId, kind, ...scope }) => {
  25. const requests: Request[] = [];
  26. const bodies: Record<string, unknown>[] = [];
  27. const apiKey = `fake-${providerId}`;
  28. const fixture = new TestProviderConfigFixture(
  29. {
  30. env: {},
  31. providers: {
  32. [providerId]: { kind, apiKey, baseURL: "https://probe.invalid/v1", ...scope },
  33. },
  34. },
  35. {
  36. // 离线拦截所有请求,包含意外的鉴权/控制面请求;绝不回落真实账号网络。
  37. transport: async (input, init) => {
  38. const request = new Request(input, init);
  39. requests.push(request);
  40. bodies.push((await request.json()) as Record<string, unknown>);
  41. return new Response(
  42. JSON.stringify({
  43. error: { type: "invalid_request_error", message: "probe captured" },
  44. }),
  45. {
  46. status: 400,
  47. headers: { "content-type": "application/json" },
  48. },
  49. );
  50. },
  51. },
  52. );
  53. const modelId = "probe-model-unchanged";
  54. const resolved = fixture.resolve(
  55. { providerId, modelId },
  56. { maxOutputTokens: 1, reasoningLevel: "disabled" },
  57. );
  58. const result = streamText(
  59. createStreamTextOptions({
  60. includeModelIO: false,
  61. resolved: { ...resolved, properties: createTestModelProperties() },
  62. request: {
  63. messages: [
  64. { role: "system", content: "You are ZCode connectivity probe." },
  65. { role: "user", content: "hi" },
  66. ],
  67. },
  68. statusContext: {
  69. providerId,
  70. modelId,
  71. providerKind: resolved.providerKind,
  72. requestId: "probe-wire",
  73. } as never,
  74. }),
  75. );
  76. const events = [];
  77. for await (const event of result.fullStream) events.push(event);
  78. expect(events.some((event) => event.type === "error")).toBe(true);
  79. expect(requests).toHaveLength(1);
  80. // 配置名变为api.baseUrl,但SDK仍要求baseURL;不能丢失endpoint回落到默认域名。
  81. expect(requests[0]?.url).toBe(
  82. `https://probe.invalid/v1/${kind === "anthropic" ? "messages" : kind === "openai-compatible" ? "chat/completions" : "responses"}`,
  83. );
  84. expect(requests[0]?.headers.get("authorization")).toBe(`Bearer ${apiKey}`);
  85. const body = bodies[0]!;
  86. expect(body.model).toBe(modelId);
  87. expect(body.stream).toBe(true);
  88. expect(body.tools).toBeUndefined();
  89. const budgetField =
  90. kind === "openai"
  91. ? "max_output_tokens"
  92. : kind === "anthropic"
  93. ? "max_tokens"
  94. : "max_completion_tokens";
  95. expect(body[budgetField]).toBe(1);
  96. if (kind === "anthropic") {
  97. expect(body.system).toEqual([{ type: "text", text: "You are ZCode connectivity probe." }]);
  98. expect(body.messages).toEqual([{ role: "user", content: [{ type: "text", text: "hi" }] }]);
  99. } else if (kind === "openai-compatible") {
  100. expect(body.messages).toEqual([
  101. { role: "system", content: "You are ZCode connectivity probe." },
  102. { role: "user", content: "hi" },
  103. ]);
  104. } else {
  105. expect(body.input).toEqual([
  106. { role: "system", content: "You are ZCode connectivity probe." },
  107. { role: "user", content: [{ type: "input_text", text: "hi" }] },
  108. ]);
  109. }
  110. },
  111. );
  112. });