schema.test.ts 3.2 KB

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