Files
anomalyco_opencode/packages/opencode/test/server/session-select.test.ts
T
Dax Raad 2807c17345 core: eliminate async overhead in server initialization by making Server.Default() synchronous
Removes unnecessary await calls on Server.Default() throughout the codebase, simplifying the server initialization flow and removing async overhead for faster startup.
2026-04-09 22:47:16 -04:00

85 lines
2.4 KiB
TypeScript

import { afterEach, describe, expect, test } from "bun:test"
import { Session } from "../../src/session"
import { Log } from "../../src/util/log"
import { Instance } from "../../src/project/instance"
import { Server } from "../../src/server/server"
import { tmpdir } from "../fixture/fixture"
Log.init({ print: false })
afterEach(async () => {
await Instance.disposeAll()
})
describe("tui.selectSession endpoint", () => {
test("should return 200 when called with valid session", async () => {
await using tmp = await tmpdir({ git: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
// #given
const session = await Session.create({})
// #when
const app = Server.Default().app
const response = await app.request("/tui/select-session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sessionID: session.id }),
})
// #then
expect(response.status).toBe(200)
const body = await response.json()
expect(body).toBe(true)
await Session.remove(session.id)
},
})
})
test("should return 404 when session does not exist", async () => {
await using tmp = await tmpdir({ git: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
// #given
const nonExistentSessionID = "ses_nonexistent123"
// #when
const app = Server.Default().app
const response = await app.request("/tui/select-session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sessionID: nonExistentSessionID }),
})
// #then
expect(response.status).toBe(404)
},
})
})
test("should return 400 when session ID format is invalid", async () => {
await using tmp = await tmpdir({ git: true })
await Instance.provide({
directory: tmp.path,
fn: async () => {
// #given
const invalidSessionID = "invalid_session_id"
// #when
const app = Server.Default().app
const response = await app.request("/tui/select-session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sessionID: invalidSessionID }),
})
// #then
expect(response.status).toBe(400)
},
})
})
})