|
1 | 1 | import { ToolDeniedError } from "@voltagent/core"; |
2 | 2 | import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { processAgentOptions } from "../utils/options"; |
3 | 4 | import { handleChatStream, handleGenerateText } from "./agent.handlers"; |
4 | 5 |
|
5 | 6 | describe("server-core: agent.handlers ClientHTTPError mapping", () => { |
@@ -56,6 +57,71 @@ describe("server-core: agent.handlers ClientHTTPError mapping", () => { |
56 | 57 | error: "Model timeout", |
57 | 58 | }); |
58 | 59 | }); |
| 60 | + |
| 61 | + it("handleGenerateText should pass request headers into agent options", async () => { |
| 62 | + const logger = { error: vi.fn() } as any; |
| 63 | + |
| 64 | + const mockAgent = { |
| 65 | + generateText: vi.fn(async () => ({ |
| 66 | + text: "ok", |
| 67 | + usage: undefined, |
| 68 | + finishReason: "stop", |
| 69 | + toolCalls: [], |
| 70 | + toolResults: [], |
| 71 | + feedback: null, |
| 72 | + })), |
| 73 | + } as any; |
| 74 | + |
| 75 | + const deps = { |
| 76 | + agentRegistry: { |
| 77 | + getAgent: vi.fn(() => mockAgent), |
| 78 | + }, |
| 79 | + } as any; |
| 80 | + |
| 81 | + const headers = new Headers({ |
| 82 | + Authorization: "Bearer test-token", |
| 83 | + "X-Tenant-ID": "tenant-1", |
| 84 | + }); |
| 85 | + |
| 86 | + const res = await handleGenerateText( |
| 87 | + "agent-1", |
| 88 | + { input: "hi" }, |
| 89 | + deps, |
| 90 | + logger, |
| 91 | + undefined, |
| 92 | + headers, |
| 93 | + ); |
| 94 | + |
| 95 | + expect(res.success).toBe(true); |
| 96 | + expect(mockAgent.generateText).toHaveBeenCalledWith( |
| 97 | + "hi", |
| 98 | + expect.objectContaining({ |
| 99 | + requestHeaders: { |
| 100 | + authorization: "Bearer test-token", |
| 101 | + "x-tenant-id": "tenant-1", |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe("server-core: processAgentOptions", () => { |
| 109 | + it("processAgentOptions should lowercase Headers entries", () => { |
| 110 | + const headers = new Headers(); |
| 111 | + vi.spyOn(headers, "entries").mockReturnValue( |
| 112 | + [ |
| 113 | + ["Authorization", "Bearer test-token"], |
| 114 | + ["X-Tenant-ID", "tenant-1"], |
| 115 | + ][Symbol.iterator]() as HeadersIterator<[string, string]>, |
| 116 | + ); |
| 117 | + |
| 118 | + const options = processAgentOptions({ options: {} }, undefined, headers); |
| 119 | + |
| 120 | + expect(options.requestHeaders).toEqual({ |
| 121 | + authorization: "Bearer test-token", |
| 122 | + "x-tenant-id": "tenant-1", |
| 123 | + }); |
| 124 | + }); |
59 | 125 | }); |
60 | 126 |
|
61 | 127 | describe("server-core: agent.handlers resumable memory envelope", () => { |
|
0 commit comments