|
| 1 | +import { expect } from "bun:test" |
| 2 | +import fs from "fs/promises" |
| 3 | +import { Effect } from "effect" |
| 4 | +import { tmpdirScoped } from "../../core/test/fixture/tmpdir" |
| 5 | +import { it } from "../../core/test/lib/effect" |
| 6 | +import { ServerFetch } from "../src/fetch" |
| 7 | + |
| 8 | +it.live("lists no forms for a Session whose Location is not live, and pending forms once it is", () => |
| 9 | + Effect.gen(function* () { |
| 10 | + const handler = yield* ServerFetch.make({ |
| 11 | + app: { version: "test" }, |
| 12 | + database: { path: ":memory:" }, |
| 13 | + fs: { filewatcher: false }, |
| 14 | + models: { fetch: false }, |
| 15 | + }) |
| 16 | + const request = (path: string, body?: unknown) => |
| 17 | + Effect.promise(async () => { |
| 18 | + const response = await handler( |
| 19 | + new Request(`http://opencode.local${path}`, { |
| 20 | + method: body === undefined ? "GET" : "POST", |
| 21 | + headers: { "content-type": "application/json" }, |
| 22 | + body: body === undefined ? undefined : JSON.stringify(body), |
| 23 | + }), |
| 24 | + ) |
| 25 | + return { status: response.status, body: await response.json() } |
| 26 | + }) |
| 27 | + const create = (directory: string) => |
| 28 | + request("/api/session", { location: { directory } }).pipe( |
| 29 | + Effect.map((response) => (response.body as { data: { id: string } }).data.id), |
| 30 | + ) |
| 31 | + |
| 32 | + // A subagent whose worktree was deleted must not boot or 404. |
| 33 | + const deleted = yield* tmpdirScoped() |
| 34 | + const orphan = yield* create(deleted.path) |
| 35 | + yield* Effect.promise(() => fs.rm(deleted.path, { recursive: true, force: true })) |
| 36 | + expect(yield* request(`/api/session/${orphan}/form`)).toEqual({ status: 200, body: { data: [] } }) |
| 37 | + |
| 38 | + const directory = yield* tmpdirScoped() |
| 39 | + const live = yield* create(directory.path) |
| 40 | + const created = yield* request(`/api/session/${live}/form`, { |
| 41 | + title: "Pick one", |
| 42 | + fields: [{ key: "answer", type: "string" }], |
| 43 | + }) |
| 44 | + expect(created.status).toBe(200) |
| 45 | + expect(yield* request(`/api/session/${live}/form`)).toEqual({ |
| 46 | + status: 200, |
| 47 | + body: { data: [(created.body as { data: unknown }).data] }, |
| 48 | + }) |
| 49 | + }).pipe(Effect.scoped), |
| 50 | +) |
0 commit comments