Convert question lifecycle tests to Effect
This commit is contained in:
parent
1557f31415
commit
c13699e2c6
1 changed files with 45 additions and 80 deletions
|
|
@ -1,12 +1,11 @@
|
||||||
import { afterEach, test, expect } from "bun:test"
|
import { afterEach, expect } from "bun:test"
|
||||||
import { Effect, Fiber, Layer } from "effect"
|
import { Cause, Effect, Exit, Fiber, Layer } from "effect"
|
||||||
import { Question } from "../../src/question"
|
import { Question } from "../../src/question"
|
||||||
import { Instance } from "../../src/project/instance"
|
import { Instance } from "../../src/project/instance"
|
||||||
import { QuestionID } from "../../src/question/schema"
|
import { QuestionID } from "../../src/question/schema"
|
||||||
import { disposeAllInstances, provideInstance, tmpdir, tmpdirScoped } from "../fixture/fixture"
|
import { disposeAllInstances, provideInstance, tmpdirScoped } from "../fixture/fixture"
|
||||||
import { SessionID } from "../../src/session/schema"
|
import { SessionID } from "../../src/session/schema"
|
||||||
import { testEffect } from "../lib/effect"
|
import { testEffect } from "../lib/effect"
|
||||||
import { AppRuntime } from "../../src/effect/app-runtime"
|
|
||||||
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
||||||
|
|
||||||
const it = testEffect(Layer.mergeAll(Question.defaultLayer, CrossSpawnSpawner.defaultLayer))
|
const it = testEffect(Layer.mergeAll(Question.defaultLayer, CrossSpawnSpawner.defaultLayer))
|
||||||
|
|
@ -35,16 +34,6 @@ const rejectEffect = Effect.fn("QuestionTest.reject")(function* (id: QuestionID)
|
||||||
yield* question.reject(id)
|
yield* question.reject(id)
|
||||||
})
|
})
|
||||||
|
|
||||||
const ask = (input: { sessionID: SessionID; questions: ReadonlyArray<Question.Info>; tool?: Question.Tool }) =>
|
|
||||||
AppRuntime.runPromise(Question.Service.use((svc) => svc.ask(input)))
|
|
||||||
|
|
||||||
const list = () => AppRuntime.runPromise(Question.Service.use((svc) => svc.list()))
|
|
||||||
|
|
||||||
const reply = (input: { requestID: QuestionID; answers: ReadonlyArray<Question.Answer> }) =>
|
|
||||||
AppRuntime.runPromise(Question.Service.use((svc) => svc.reply(input)))
|
|
||||||
|
|
||||||
const reject = (id: QuestionID) => AppRuntime.runPromise(Question.Service.use((svc) => svc.reject(id)))
|
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await disposeAllInstances()
|
await disposeAllInstances()
|
||||||
})
|
})
|
||||||
|
|
@ -370,72 +359,48 @@ it.live("questions stay isolated by directory", () =>
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
test("pending question rejects on instance dispose", async () => {
|
it.live("pending question rejects on instance dispose", () =>
|
||||||
await using tmp = await tmpdir({ git: true })
|
Effect.gen(function* () {
|
||||||
|
const dir = yield* tmpdirScoped({ git: true })
|
||||||
|
const fiber = yield* askEffect({
|
||||||
|
sessionID: SessionID.make("ses_dispose"),
|
||||||
|
questions: [
|
||||||
|
{
|
||||||
|
question: "Dispose me?",
|
||||||
|
header: "Dispose",
|
||||||
|
options: [{ label: "Yes", description: "Yes" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).pipe(provideInstance(dir), Effect.forkScoped)
|
||||||
|
|
||||||
const pending = Instance.provide({
|
expect(yield* waitForPending(1).pipe(provideInstance(dir))).toHaveLength(1)
|
||||||
directory: tmp.path,
|
yield* Effect.promise(() => Instance.provide({ directory: dir, fn: () => void Instance.dispose() }))
|
||||||
fn: () => {
|
|
||||||
return ask({
|
|
||||||
sessionID: SessionID.make("ses_dispose"),
|
|
||||||
questions: [
|
|
||||||
{
|
|
||||||
question: "Dispose me?",
|
|
||||||
header: "Dispose",
|
|
||||||
options: [{ label: "Yes", description: "Yes" }],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
|
||||||
},
|
|
||||||
})
|
|
||||||
const result = pending.then(
|
|
||||||
() => "resolved" as const,
|
|
||||||
(err) => err,
|
|
||||||
)
|
|
||||||
|
|
||||||
await Instance.provide({
|
const exit = yield* Fiber.await(fiber)
|
||||||
directory: tmp.path,
|
expect(Exit.isFailure(exit)).toBe(true)
|
||||||
fn: async () => {
|
if (Exit.isFailure(exit)) expect(Cause.squash(exit.cause)).toBeInstanceOf(Question.RejectedError)
|
||||||
const items = await list()
|
}),
|
||||||
expect(items).toHaveLength(1)
|
)
|
||||||
await Instance.dispose()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(await result).toBeInstanceOf(Question.RejectedError)
|
it.live("pending question rejects on instance reload", () =>
|
||||||
})
|
Effect.gen(function* () {
|
||||||
|
const dir = yield* tmpdirScoped({ git: true })
|
||||||
|
const fiber = yield* askEffect({
|
||||||
|
sessionID: SessionID.make("ses_reload"),
|
||||||
|
questions: [
|
||||||
|
{
|
||||||
|
question: "Reload me?",
|
||||||
|
header: "Reload",
|
||||||
|
options: [{ label: "Yes", description: "Yes" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).pipe(provideInstance(dir), Effect.forkScoped)
|
||||||
|
|
||||||
test("pending question rejects on instance reload", async () => {
|
expect(yield* waitForPending(1).pipe(provideInstance(dir))).toHaveLength(1)
|
||||||
await using tmp = await tmpdir({ git: true })
|
yield* Effect.promise(() => Instance.reload({ directory: dir }))
|
||||||
|
|
||||||
const pending = Instance.provide({
|
const exit = yield* Fiber.await(fiber)
|
||||||
directory: tmp.path,
|
expect(Exit.isFailure(exit)).toBe(true)
|
||||||
fn: () => {
|
if (Exit.isFailure(exit)) expect(Cause.squash(exit.cause)).toBeInstanceOf(Question.RejectedError)
|
||||||
return ask({
|
}),
|
||||||
sessionID: SessionID.make("ses_reload"),
|
)
|
||||||
questions: [
|
|
||||||
{
|
|
||||||
question: "Reload me?",
|
|
||||||
header: "Reload",
|
|
||||||
options: [{ label: "Yes", description: "Yes" }],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
|
||||||
},
|
|
||||||
})
|
|
||||||
const result = pending.then(
|
|
||||||
() => "resolved" as const,
|
|
||||||
(err) => err,
|
|
||||||
)
|
|
||||||
|
|
||||||
await Instance.provide({
|
|
||||||
directory: tmp.path,
|
|
||||||
fn: async () => {
|
|
||||||
const items = await list()
|
|
||||||
expect(items).toHaveLength(1)
|
|
||||||
await Instance.reload({ directory: tmp.path })
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(await result).toBeInstanceOf(Question.RejectedError)
|
|
||||||
})
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue