event.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. import { expect, test } from "bun:test"
  2. import { isOpenCodeEvent, type OpenCodeEvent, type OpenCodeEventEncoded } from "../src/groups/event.js"
  3. type JsonShape<Value> = Value extends string | number | boolean | null
  4. ? Value
  5. : Value extends undefined
  6. ? never
  7. : Value extends ReadonlyArray<infer Item>
  8. ? ReadonlyArray<JsonShape<Item>>
  9. : Value extends object
  10. ? {
  11. readonly [Key in keyof Value as undefined extends Value[Key] ? never : Key]: JsonShape<Value[Key]>
  12. } & {
  13. readonly [Key in keyof Value as undefined extends Value[Key] ? Key : never]?: JsonShape<
  14. Exclude<Value[Key], undefined>
  15. >
  16. }
  17. : Value
  18. // JSON.stringify omits undefined object properties, so normalize them before
  19. // requiring every runtime event shape to fit its encoded wire contract.
  20. const wireReady: [JsonShape<OpenCodeEvent>] extends [JsonShape<OpenCodeEventEncoded>] ? true : false = true
  21. test("classifies public events by type", () => {
  22. expect(isOpenCodeEvent({ type: "server.connected" })).toBe(true)
  23. expect(isOpenCodeEvent({ type: "mcp.status.changed" })).toBe(true)
  24. expect(isOpenCodeEvent({ type: "mcp.resources.changed" })).toBe(true)
  25. expect(isOpenCodeEvent({ type: "mcp.tools.changed" })).toBe(false)
  26. })
  27. test("keeps public event runtime values within the encoded contract", () => {
  28. expect(wireReady).toBe(true)
  29. })