schema.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import { ContentPart, LLMEvent, LLMRequest, ModelID, ModelLimits, ModelRef, ProviderID } from "../src/schema"
  4. const model = new ModelRef({
  5. id: ModelID.make("fake-model"),
  6. provider: ProviderID.make("fake-provider"),
  7. route: "openai-chat",
  8. baseURL: "https://fake.local",
  9. limits: new ModelLimits({}),
  10. })
  11. describe("llm schema", () => {
  12. test("decodes a minimal request", () => {
  13. const input: unknown = {
  14. id: "req_1",
  15. model,
  16. system: [{ type: "text", text: "You are terse." }],
  17. messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
  18. tools: [],
  19. generation: {},
  20. }
  21. const decoded = Schema.decodeUnknownSync(LLMRequest)(input)
  22. expect(decoded.id).toBe("req_1")
  23. expect(decoded.messages[0]?.content[0]?.type).toBe("text")
  24. })
  25. test("accepts custom route ids", () => {
  26. const decoded = Schema.decodeUnknownSync(LLMRequest)({
  27. model: { ...model, route: "custom-route" },
  28. system: [],
  29. messages: [],
  30. tools: [],
  31. generation: {},
  32. })
  33. expect(decoded.model.route).toBe("custom-route")
  34. })
  35. test("rejects invalid event type", () => {
  36. expect(() => Schema.decodeUnknownSync(LLMEvent)({ type: "bogus" })).toThrow()
  37. })
  38. test("content part tagged union exposes guards", () => {
  39. expect(ContentPart.guards.text({ type: "text", text: "hi" })).toBe(true)
  40. expect(ContentPart.guards.media({ type: "text", text: "hi" })).toBe(false)
  41. })
  42. })