mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-08 18:51:24 +03:00
75 lines
3.0 KiB
TypeScript
75 lines
3.0 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { OpenCode } from "../src/promise"
|
|
import { createPersistentPtyClient } from "../src/solid"
|
|
|
|
describe("createPersistentPtyClient", () => {
|
|
test("mints an authenticated ticket before opening the persistent terminal socket", async () => {
|
|
let request: Request | undefined
|
|
let socketURL: URL | undefined
|
|
const socket = { binaryType: "blob" } as unknown as WebSocket
|
|
const api = OpenCode.make({
|
|
baseUrl: "https://server.example",
|
|
headers: { Authorization: "Basic credential" },
|
|
fetch: async (input, init) => {
|
|
request = input instanceof Request ? input : new Request(input, init)
|
|
return Response.json({ data: { ticket: "persistent-ticket", expires_in: 60 } })
|
|
},
|
|
})
|
|
const pty = createPersistentPtyClient(api, {
|
|
url: "https://server.example",
|
|
openSocket(url) {
|
|
socketURL = url
|
|
return socket
|
|
},
|
|
})
|
|
|
|
expect(await pty.connect({ ptyID: "pty_1", cursor: 42, attachmentID: "attachment_1", takeover: true })).toBe(socket)
|
|
expect(request?.method).toBe("POST")
|
|
expect(request?.url).toBe("https://server.example/api/experimental/persistent-pty/pty_1/connect-token")
|
|
expect(request?.headers.get("authorization")).toBe("Basic credential")
|
|
expect(request?.headers.get("x-opencode-ticket")).toBe("1")
|
|
expect(socketURL?.toString()).toBe(
|
|
"wss://server.example/api/experimental/persistent-pty/pty_1/connect?ticket=persistent-ticket&cursor=42&attachment_id=attachment_1&takeover=true&input_protocol=1",
|
|
)
|
|
expect(socket.binaryType).toBe("arraybuffer")
|
|
})
|
|
|
|
test("does not open a socket when ticket minting fails", async () => {
|
|
let opened = false
|
|
const api = OpenCode.make({
|
|
baseUrl: "http://localhost:4096",
|
|
fetch: async () => new Response(null, { status: 401 }),
|
|
})
|
|
const pty = createPersistentPtyClient(api, {
|
|
url: "http://localhost:4096",
|
|
openSocket() {
|
|
opened = true
|
|
return { binaryType: "blob" } as unknown as WebSocket
|
|
},
|
|
})
|
|
|
|
await expect(pty.connect({ ptyID: "pty_1", cursor: 0, attachmentID: "attachment_1" })).rejects.toThrow()
|
|
expect(opened).toBe(false)
|
|
})
|
|
|
|
test("keeps the server path prefix when opening the persistent terminal socket", async () => {
|
|
let socketURL: URL | undefined
|
|
const api = OpenCode.make({
|
|
baseUrl: "https://server.example/base",
|
|
fetch: async () => Response.json({ data: { ticket: "ticket-1", expires_in: 60 } }),
|
|
})
|
|
const pty = createPersistentPtyClient(api, {
|
|
url: "https://server.example/base",
|
|
openSocket(url) {
|
|
socketURL = url
|
|
return { binaryType: "blob" } as unknown as WebSocket
|
|
},
|
|
})
|
|
|
|
await pty.connect({ ptyID: "pty_1", cursor: 0, attachmentID: "attachment_1" })
|
|
expect(socketURL?.toString()).toBe(
|
|
"wss://server.example/base/api/experimental/persistent-pty/pty_1/connect?ticket=ticket-1&cursor=0&attachment_id=attachment_1&takeover=false&input_protocol=1",
|
|
)
|
|
})
|
|
})
|