test(server): migrate session select to effect runner (#27203)
This commit is contained in:
@@ -1,101 +1,87 @@
|
|||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { describe, expect } from "bun:test"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
import { Session as SessionNs } from "@/session/session"
|
import { Session } from "@/session/session"
|
||||||
import type { SessionID } from "../../src/session/schema"
|
|
||||||
import * as Log from "@opencode-ai/core/util/log"
|
import * as Log from "@opencode-ai/core/util/log"
|
||||||
import { Instance } from "../../src/project/instance"
|
|
||||||
import { WithInstance } from "../../src/project/with-instance"
|
|
||||||
import { Server } from "../../src/server/server"
|
import { Server } from "../../src/server/server"
|
||||||
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
|
import { TestInstance } from "../fixture/fixture"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
|
||||||
void Log.init({ print: false })
|
void Log.init({ print: false })
|
||||||
|
|
||||||
function run<A, E>(fx: Effect.Effect<A, E, SessionNs.Service>) {
|
const it = testEffect(Session.defaultLayer)
|
||||||
return Effect.runPromise(fx.pipe(Effect.provide(SessionNs.defaultLayer)))
|
|
||||||
}
|
|
||||||
|
|
||||||
const svc = {
|
|
||||||
...SessionNs,
|
|
||||||
create(input?: SessionNs.CreateInput) {
|
|
||||||
return run(SessionNs.Service.use((svc) => svc.create(input)))
|
|
||||||
},
|
|
||||||
remove(id: SessionID) {
|
|
||||||
return run(SessionNs.Service.use((svc) => svc.remove(id)))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await disposeAllInstances()
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("tui.selectSession endpoint", () => {
|
describe("tui.selectSession endpoint", () => {
|
||||||
test("should return 200 when called with valid session", async () => {
|
it.instance("should return 200 when called with valid session", () =>
|
||||||
await using tmp = await tmpdir({ git: true })
|
Effect.gen(function* () {
|
||||||
await WithInstance.provide({
|
const tmp = yield* TestInstance
|
||||||
directory: tmp.path,
|
const session = yield* Session.Service.use((svc) => svc.create({}))
|
||||||
fn: async () => {
|
|
||||||
// #given
|
|
||||||
const session = await svc.create({})
|
|
||||||
|
|
||||||
// #when
|
const app = Server.Default().app
|
||||||
const app = Server.Default().app
|
const response = yield* Effect.promise(() =>
|
||||||
const response = await app.request("/tui/select-session", {
|
Promise.resolve(
|
||||||
method: "POST",
|
app.request("/tui/select-session", {
|
||||||
headers: { "Content-Type": "application/json" },
|
method: "POST",
|
||||||
body: JSON.stringify({ sessionID: session.id }),
|
headers: {
|
||||||
})
|
"Content-Type": "application/json",
|
||||||
|
"x-opencode-directory": tmp.directory,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ sessionID: session.id }),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
// #then
|
expect(response.status).toBe(200)
|
||||||
expect(response.status).toBe(200)
|
const body = yield* Effect.promise(() => response.json())
|
||||||
const body = await response.json()
|
expect(body).toBe(true)
|
||||||
expect(body).toBe(true)
|
}),
|
||||||
|
{ git: true },
|
||||||
|
)
|
||||||
|
|
||||||
await svc.remove(session.id)
|
it.instance("should return 404 when session does not exist", () =>
|
||||||
},
|
Effect.gen(function* () {
|
||||||
})
|
const tmp = yield* TestInstance
|
||||||
})
|
const nonExistentSessionID = "ses_nonexistent123"
|
||||||
|
|
||||||
test("should return 404 when session does not exist", async () => {
|
const app = Server.Default().app
|
||||||
await using tmp = await tmpdir({ git: true })
|
const response = yield* Effect.promise(() =>
|
||||||
await WithInstance.provide({
|
Promise.resolve(
|
||||||
directory: tmp.path,
|
app.request("/tui/select-session", {
|
||||||
fn: async () => {
|
method: "POST",
|
||||||
// #given
|
headers: {
|
||||||
const nonExistentSessionID = "ses_nonexistent123"
|
"Content-Type": "application/json",
|
||||||
|
"x-opencode-directory": tmp.directory,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ sessionID: nonExistentSessionID }),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
// #when
|
expect(response.status).toBe(404)
|
||||||
const app = Server.Default().app
|
}),
|
||||||
const response = await app.request("/tui/select-session", {
|
{ git: true },
|
||||||
method: "POST",
|
)
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ sessionID: nonExistentSessionID }),
|
|
||||||
})
|
|
||||||
|
|
||||||
// #then
|
it.instance("should return 400 when session ID format is invalid", () =>
|
||||||
expect(response.status).toBe(404)
|
Effect.gen(function* () {
|
||||||
},
|
const tmp = yield* TestInstance
|
||||||
})
|
const invalidSessionID = "invalid_session_id"
|
||||||
})
|
|
||||||
|
|
||||||
test("should return 400 when session ID format is invalid", async () => {
|
const app = Server.Default().app
|
||||||
await using tmp = await tmpdir({ git: true })
|
const response = yield* Effect.promise(() =>
|
||||||
await WithInstance.provide({
|
Promise.resolve(
|
||||||
directory: tmp.path,
|
app.request("/tui/select-session", {
|
||||||
fn: async () => {
|
method: "POST",
|
||||||
// #given
|
headers: {
|
||||||
const invalidSessionID = "invalid_session_id"
|
"Content-Type": "application/json",
|
||||||
|
"x-opencode-directory": tmp.directory,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ sessionID: invalidSessionID }),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
// #when
|
expect(response.status).toBe(400)
|
||||||
const app = Server.Default().app
|
}),
|
||||||
const response = await app.request("/tui/select-session", {
|
{ git: true },
|
||||||
method: "POST",
|
)
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ sessionID: invalidSessionID }),
|
|
||||||
})
|
|
||||||
|
|
||||||
// #then
|
|
||||||
expect(response.status).toBe(400)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user