endpoint.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, expect, test } from "bun:test"
  2. import { LLM } from "../src"
  3. import * as OpenAIChat from "../src/protocols/openai-chat"
  4. import { Endpoint } from "../src/route"
  5. import { Model } from "../src/schema"
  6. const request = () =>
  7. LLM.request({
  8. model: Model.make({
  9. id: "model-1",
  10. provider: "test",
  11. route: OpenAIChat.route,
  12. }),
  13. prompt: "hello",
  14. })
  15. describe("Endpoint", () => {
  16. test("appends a static path to the model's baseURL", () => {
  17. const url = Endpoint.render(Endpoint.path("/chat", { baseURL: "https://api.example.test/v1/" }), {
  18. request: request(),
  19. body: {},
  20. })
  21. expect(url.toString()).toBe("https://api.example.test/v1/chat")
  22. })
  23. test("endpoint query params are appended to the rendered URL", () => {
  24. const url = Endpoint.render(
  25. Endpoint.path("/chat?alt=sse", {
  26. baseURL: "https://custom.example.test/root/",
  27. query: { "api-version": "2026-01-01", alt: "json" },
  28. }),
  29. {
  30. request: request(),
  31. body: {},
  32. },
  33. )
  34. expect(url.toString()).toBe("https://custom.example.test/root/chat?alt=json&api-version=2026-01-01")
  35. })
  36. test("path may be a function of the validated body", () => {
  37. const url = Endpoint.render(
  38. Endpoint.path<{ readonly modelId: string }>(
  39. ({ body }) => `/model/${encodeURIComponent(body.modelId)}/converse-stream`,
  40. { baseURL: "https://bedrock-runtime.us-east-1.amazonaws.com" },
  41. ),
  42. {
  43. request: request(),
  44. body: { modelId: "us.amazon.nova-micro-v1:0" },
  45. },
  46. )
  47. expect(url.toString()).toBe(
  48. "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.amazon.nova-micro-v1%3A0/converse-stream",
  49. )
  50. })
  51. })