endpoint.test.ts 1.7 KB

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