connectivity-probe-wire.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. requestSecurityState: {
  37. createExecution: () => ({
  38. protectTransport: ({ transport }) => transport,
  39. take: () => [],
  40. }),
  41. },
  42. // 离线拦截所有请求,包含意外的鉴权/控制面请求;绝不回落真实账号网络。
  43. transport: async (input, init) => {
  44. const request = new Request(input, init);
  45. requests.push(request);
  46. bodies.push((await request.json()) as Record<string, unknown>);
  47. return new Response(
  48. JSON.stringify({
  49. error: { type: "invalid_request_error", message: "probe captured" },
  50. }),
  51. {
  52. status: 400,
  53. headers: { "content-type": "application/json" },
  54. },
  55. );
  56. },
  57. },
  58. );
  59. const modelId = "probe-model-unchanged";
  60. const resolved = fixture.resolve(
  61. { providerId, modelId },
  62. { maxOutputTokens: 512, reasoningLevel: "disabled" },
  63. );
  64. const result = streamText(
  65. createStreamTextOptions({
  66. includeModelIO: false,
  67. resolved: { ...resolved, properties: createTestModelProperties() },
  68. request: {
  69. messages: [
  70. { role: "system", content: "You are ZCode connectivity probe." },
  71. { role: "user", content: "hi" },
  72. ],
  73. },
  74. statusContext: {
  75. providerId,
  76. modelId,
  77. providerKind: resolved.providerKind,
  78. requestId: "probe-wire",
  79. } as never,
  80. }),
  81. );
  82. const events = [];
  83. for await (const event of result.fullStream) events.push(event);
  84. expect(events.some((event) => event.type === "error")).toBe(true);
  85. expect(requests).toHaveLength(1);
  86. // 配置名变为api.baseUrl,但SDK仍要求baseURL;不能丢失endpoint回落到默认域名。
  87. expect(requests[0]?.url).toBe(
  88. `https://probe.invalid/v1/${kind === "anthropic" ? "messages" : kind === "openai-compatible" ? "chat/completions" : "responses"}`,
  89. );
  90. expect(requests[0]?.headers.get("authorization")).toBe(`Bearer ${apiKey}`);
  91. const body = bodies[0]!;
  92. expect(body.model).toBe(modelId);
  93. expect(body.stream).toBe(true);
  94. expect(body.tools).toBeUndefined();
  95. const budgetField =
  96. kind === "openai"
  97. ? "max_output_tokens"
  98. : kind === "anthropic"
  99. ? "max_tokens"
  100. : "max_completion_tokens";
  101. expect(body[budgetField]).toBe(512);
  102. if (kind === "anthropic") {
  103. expect(body.system).toEqual([{ type: "text", text: "You are ZCode connectivity probe." }]);
  104. expect(body.messages).toEqual([{ role: "user", content: [{ type: "text", text: "hi" }] }]);
  105. } else if (kind === "openai-compatible") {
  106. expect(body.messages).toEqual([
  107. { role: "system", content: "You are ZCode connectivity probe." },
  108. { role: "user", content: "hi" },
  109. ]);
  110. } else {
  111. expect(body.input).toEqual([
  112. { role: "system", content: "You are ZCode connectivity probe." },
  113. { role: "user", content: [{ type: "input_text", text: "hi" }] },
  114. ]);
  115. }
  116. },
  117. );
  118. });