session-error.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import { LLM, SessionError } from "../src/index.js"
  4. describe("SessionError", () => {
  5. test("exports one identified open envelope", () => {
  6. expect(SessionError.Error.ast.annotations?.identifier).toBe("Session.StructuredError")
  7. expect(Object.keys(SessionError).filter((key) => key !== "SessionError")).toEqual(["Error"])
  8. })
  9. test("round trips current and future error types through JSON", () => {
  10. const values: SessionError.Error[] = [
  11. { type: "provider.rate-limit", message: "Slow down" },
  12. { type: "provider.auth", message: "Authentication failed" },
  13. { type: "provider.future-condition", message: "A future provider failure" },
  14. { type: "unknown", message: "Unexpected" },
  15. ]
  16. const codec = Schema.fromJsonString(SessionError.Error)
  17. for (const value of values) {
  18. const encoded = Schema.encodeSync(codec)(value)
  19. expect(Schema.decodeUnknownSync(codec)(encoded)).toEqual(value)
  20. }
  21. })
  22. test("accepts future fields while exposing only the stable envelope", () => {
  23. expect(
  24. Schema.decodeUnknownSync(SessionError.Error)({
  25. type: "provider.timeout",
  26. message: "Timeout",
  27. retryAfterMs: 2_500,
  28. }),
  29. ).toEqual({ type: "provider.timeout", message: "Timeout" })
  30. })
  31. test("rejects missing envelope fields", () => {
  32. expect(() => Schema.decodeUnknownSync(SessionError.Error)({ type: "provider.auth" })).toThrow()
  33. expect(() => Schema.decodeUnknownSync(SessionError.Error)({ message: "Missing type" })).toThrow()
  34. })
  35. })
  36. test("FinishReason is the closed browser-safe provider set", () => {
  37. const reasons = ["stop", "length", "tool-calls", "content-filter", "error", "unknown"] as const
  38. expect(reasons.map((reason) => Schema.decodeUnknownSync(LLM.FinishReason)(reason))).toEqual([...reasons])
  39. expect(() => Schema.decodeUnknownSync(LLM.FinishReason)("other")).toThrow()
  40. })