endpoint.test.ts 1.7 KB

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