pty-output-isolation.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { describe, expect } from "bun:test"
  2. import { Duration, Effect, Layer, Queue } from "effect"
  3. import { EventV2 } from "@opencode-ai/core/event"
  4. import { Location } from "@opencode-ai/core/location"
  5. import { Pty } from "@opencode-ai/core/pty"
  6. import { AbsolutePath } from "@opencode-ai/core/schema"
  7. import { location } from "../fixture/location"
  8. import { testEffect } from "../lib/effect"
  9. type Socket = Parameters<Pty.Interface["connect"]>[1]
  10. const locationLayer = Layer.succeed(
  11. Location.Service,
  12. Location.Service.of(location({ directory: AbsolutePath.make("/tmp") })),
  13. )
  14. const it = testEffect(Pty.layer.pipe(Layer.provideMerge(EventV2.defaultLayer), Layer.provideMerge(locationLayer)))
  15. const ptyTest = process.platform === "win32" ? it.live.skip : it.live
  16. const createPty = Effect.fn("PtyOutputIsolationTest.createPty")(function* (command: string) {
  17. const pty = yield* Pty.Service
  18. return yield* Effect.acquireRelease(
  19. pty.create({ command, args: [], cwd: "/tmp", env: { TERM: "xterm-256color", OPENCODE_TERMINAL: "1" } }),
  20. (info) => pty.remove(info.id).pipe(Effect.ignore),
  21. )
  22. })
  23. const decodeOutput = (data: string | Uint8Array | ArrayBuffer) =>
  24. typeof data === "string"
  25. ? data
  26. : Buffer.from(data instanceof Uint8Array ? data : new Uint8Array(data)).toString("utf8")
  27. const makeSocket = Effect.fn("PtyOutputIsolationTest.makeSocket")(function* (data: unknown) {
  28. const output = yield* Queue.unbounded<string>()
  29. const socket: Socket = {
  30. readyState: 1,
  31. data,
  32. send: (data) => Queue.offerUnsafe(output, decodeOutput(data)),
  33. close: () => {},
  34. }
  35. return { socket, output }
  36. })
  37. const waitForOutput = (output: Queue.Queue<string>, text: string, duration: Duration.Input = "5 seconds") =>
  38. Effect.gen(function* () {
  39. let received = ""
  40. while (!received.includes(text)) received += yield* Queue.take(output)
  41. return received
  42. }).pipe(
  43. Effect.timeoutOrElse({
  44. duration,
  45. orElse: () => Effect.fail(new Error(`timeout waiting for output containing ${JSON.stringify(text)}`)),
  46. }),
  47. )
  48. describe("pty output isolation", () => {
  49. ptyTest("does not leak output when websocket objects are reused", () =>
  50. Effect.gen(function* () {
  51. const pty = yield* Pty.Service
  52. const a = yield* createPty("cat")
  53. const b = yield* createPty("cat")
  54. const shared = yield* makeSocket({ events: { connection: "a" } })
  55. const outB = yield* Queue.unbounded<string>()
  56. yield* pty.connect(a.id, shared.socket)
  57. shared.socket.data = { events: { connection: "b" } }
  58. shared.socket.send = (data) => Queue.offerUnsafe(outB, decodeOutput(data))
  59. yield* pty.connect(b.id, shared.socket)
  60. yield* pty.write(a.id, "AAA\n")
  61. const verify = yield* makeSocket({ events: { connection: "verify-a" } })
  62. yield* pty.connect(a.id, verify.socket)
  63. expect(yield* waitForOutput(verify.output, "AAA")).toContain("AAA")
  64. expect(yield* waitForOutput(outB, "AAA", "100 millis").pipe(Effect.option)).toMatchObject({ _tag: "None" })
  65. }),
  66. )
  67. ptyTest("does not leak output when Bun recycles websocket objects before re-connect", () =>
  68. Effect.gen(function* () {
  69. const pty = yield* Pty.Service
  70. const info = yield* createPty("cat")
  71. const first = yield* makeSocket({ events: { connection: "a" } })
  72. const recycled = yield* Queue.unbounded<string>()
  73. yield* pty.connect(info.id, first.socket)
  74. first.socket.data = { events: { connection: "b" } }
  75. first.socket.send = (data) => Queue.offerUnsafe(recycled, decodeOutput(data))
  76. yield* pty.write(info.id, "AAA\n")
  77. const verify = yield* makeSocket({ events: { connection: "verify" } })
  78. yield* pty.connect(info.id, verify.socket)
  79. expect(yield* waitForOutput(verify.output, "AAA")).toContain("AAA")
  80. expect(yield* waitForOutput(recycled, "AAA", "100 millis").pipe(Effect.option)).toMatchObject({ _tag: "None" })
  81. }),
  82. )
  83. ptyTest("treats in-place socket data mutation as the same connection", () =>
  84. Effect.gen(function* () {
  85. const pty = yield* Pty.Service
  86. const info = yield* createPty("cat")
  87. const data = { connId: 1 }
  88. const socket = yield* makeSocket(data)
  89. yield* pty.connect(info.id, socket.socket)
  90. data.connId = 2
  91. yield* pty.write(info.id, "AAA\n")
  92. expect(yield* waitForOutput(socket.output, "AAA")).toContain("AAA")
  93. }),
  94. )
  95. })