Просмотр исходного кода

test(server): migrate session select to effect runner (#27203)

Kit Langton 3 месяцев назад
Родитель
Сommit
2447f42294
1 измененных файлов с 70 добавлено и 84 удалено
  1. 70 84
      packages/opencode/test/server/session-select.test.ts

+ 70 - 84
packages/opencode/test/server/session-select.test.ts

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