1
0

protocol.test.ts 1.2 KB

123456789101112131415161718192021222324252627
  1. import { describe, expect, test } from "bun:test"
  2. import { PtyProtocol } from "@opencode-ai/core/pty/protocol"
  3. describe("pty protocol", () => {
  4. test("drops invalid binary input frames and decodes valid ones", () => {
  5. expect(PtyProtocol.decodeInput("ready")).toBe("ready")
  6. expect(PtyProtocol.decodeInput(new Uint8Array([0xff, 0xfe, 0xfd]))).toBeUndefined()
  7. expect(PtyProtocol.decodeInput(new TextEncoder().encode("hello"))).toBe("hello")
  8. expect(PtyProtocol.decodeInput(new TextEncoder().encode("hello").buffer)).toBe("hello")
  9. })
  10. test("encodes the cursor as a 0x00-prefixed JSON control frame", () => {
  11. const frame = PtyProtocol.metaFrame(42)
  12. expect(frame[0]).toBe(0)
  13. expect(JSON.parse(new TextDecoder().decode(frame.subarray(1)))).toEqual({ cursor: 42 })
  14. })
  15. test("splits replay into bounded frames", () => {
  16. expect(PtyProtocol.chunks("")).toEqual([])
  17. expect(PtyProtocol.chunks("abc")).toEqual(["abc"])
  18. const big = "x".repeat(PtyProtocol.REPLAY_CHUNK + 1)
  19. const frames = PtyProtocol.chunks(big)
  20. expect(frames.length).toBe(2)
  21. expect(frames[0].length).toBe(PtyProtocol.REPLAY_CHUNK)
  22. expect(frames.join("")).toBe(big)
  23. })
  24. })