schema.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import * as OpenAIChat from "../src/protocols/openai-chat"
  4. import * as OpenAIResponses from "../src/protocols/openai-responses"
  5. import { ContentPart, LLMEvent, LLMRequest, Model, ModelID, ProviderID, Usage } from "../src/schema"
  6. import { ProviderShared } from "../src/protocols/shared"
  7. const model = new Model({
  8. id: ModelID.make("fake-model"),
  9. provider: ProviderID.make("fake-provider"),
  10. route: OpenAIChat.route,
  11. })
  12. const decodeLLMRequest = Schema.decodeUnknownSync(LLMRequest as unknown as Schema.Decoder<LLMRequest>)
  13. const decodeLLMEvent = Schema.decodeUnknownSync(LLMEvent as unknown as Schema.Decoder<LLMEvent>)
  14. describe("llm schema", () => {
  15. test("decodes a minimal request", () => {
  16. const input: unknown = {
  17. id: "req_1",
  18. model,
  19. system: [{ type: "text", text: "You are terse." }],
  20. messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
  21. tools: [],
  22. generation: {},
  23. }
  24. const decoded = decodeLLMRequest(input)
  25. expect(decoded.id).toBe("req_1")
  26. expect(decoded.messages[0]?.content[0]?.type).toBe("text")
  27. })
  28. test("accepts custom route ids", () => {
  29. const decoded = decodeLLMRequest({
  30. model: Model.update(model, { route: OpenAIResponses.route }),
  31. system: [],
  32. messages: [],
  33. tools: [],
  34. generation: {},
  35. })
  36. expect(decoded.model.route.id).toBe("openai-responses")
  37. })
  38. test("rejects invalid event type", () => {
  39. expect(() => decodeLLMEvent({ type: "bogus" })).toThrow()
  40. })
  41. test("finish constructors accept usage input", () => {
  42. expect(LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 1 } }).usage).toBeInstanceOf(Usage)
  43. expect(LLMEvent.finish({ reason: "stop", usage: { outputTokens: 2 } }).usage).toBeInstanceOf(Usage)
  44. })
  45. test("content part tagged union exposes guards", () => {
  46. expect(ContentPart.guards.text({ type: "text", text: "hi" })).toBe(true)
  47. expect(ContentPart.guards.media({ type: "text", text: "hi" })).toBe(false)
  48. })
  49. })
  50. describe("LLM.Usage", () => {
  51. test("subtractTokens clamps non-sensical breakdowns to zero", () => {
  52. // Defense against a provider reporting cached_tokens > prompt_tokens or
  53. // reasoning_tokens > completion_tokens — the negative would otherwise
  54. // round-trip through the pipeline and crash strict downstream schemas.
  55. expect(ProviderShared.subtractTokens(5, 3)).toBe(2)
  56. expect(ProviderShared.subtractTokens(5, 10)).toBe(0)
  57. expect(ProviderShared.subtractTokens(5, undefined)).toBe(5)
  58. expect(ProviderShared.subtractTokens(undefined, 3)).toBeUndefined()
  59. expect(ProviderShared.subtractTokens(undefined, undefined)).toBeUndefined()
  60. })
  61. test("sumTokens returns undefined only when every input is undefined", () => {
  62. expect(ProviderShared.sumTokens(1, 2, 3)).toBe(6)
  63. expect(ProviderShared.sumTokens(1, undefined, 3)).toBe(4)
  64. expect(ProviderShared.sumTokens(undefined, undefined, undefined)).toBeUndefined()
  65. expect(ProviderShared.sumTokens()).toBeUndefined()
  66. })
  67. test("visibleOutputTokens clamps reasoning > output to zero", () => {
  68. expect(new Usage({ outputTokens: 10, reasoningTokens: 4 }).visibleOutputTokens).toBe(6)
  69. expect(new Usage({ outputTokens: 10 }).visibleOutputTokens).toBe(10)
  70. expect(new Usage({ outputTokens: 4, reasoningTokens: 10 }).visibleOutputTokens).toBe(0)
  71. expect(new Usage({}).visibleOutputTokens).toBe(0)
  72. })
  73. })