mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-04 01:06:16 -04:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5eef02d8bf | |||
| b33579c2ec | |||
| c3d10af784 |
@@ -37,7 +37,7 @@ import { Permission } from "@/permission"
|
|||||||
import { Global } from "@opencode-ai/core/global"
|
import { Global } from "@opencode-ai/core/global"
|
||||||
import { Effect, Layer, Option, Context, Schema, Types } from "effect"
|
import { Effect, Layer, Option, Context, Schema, Types } from "effect"
|
||||||
import { zod } from "@/util/effect-zod"
|
import { zod } from "@/util/effect-zod"
|
||||||
import { withStatics } from "@/util/schema"
|
import { optionalOmitUndefined, withStatics } from "@/util/schema"
|
||||||
|
|
||||||
const log = Log.create({ service: "session" })
|
const log = Log.create({ service: "session" })
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ const Summary = Schema.Struct({
|
|||||||
additions: Schema.Number,
|
additions: Schema.Number,
|
||||||
deletions: Schema.Number,
|
deletions: Schema.Number,
|
||||||
files: Schema.Number,
|
files: Schema.Number,
|
||||||
diffs: Schema.optional(Schema.Array(Snapshot.FileDiff)),
|
diffs: optionalOmitUndefined(Schema.Array(Snapshot.FileDiff)),
|
||||||
})
|
})
|
||||||
|
|
||||||
const Share = Schema.Struct({
|
const Share = Schema.Struct({
|
||||||
@@ -138,31 +138,31 @@ const Share = Schema.Struct({
|
|||||||
const Time = Schema.Struct({
|
const Time = Schema.Struct({
|
||||||
created: Schema.Number,
|
created: Schema.Number,
|
||||||
updated: Schema.Number,
|
updated: Schema.Number,
|
||||||
compacting: Schema.optional(Schema.Number),
|
compacting: optionalOmitUndefined(Schema.Number),
|
||||||
archived: Schema.optional(Schema.Number),
|
archived: optionalOmitUndefined(Schema.Number),
|
||||||
})
|
})
|
||||||
|
|
||||||
const Revert = Schema.Struct({
|
const Revert = Schema.Struct({
|
||||||
messageID: MessageID,
|
messageID: MessageID,
|
||||||
partID: Schema.optional(PartID),
|
partID: optionalOmitUndefined(PartID),
|
||||||
snapshot: Schema.optional(Schema.String),
|
snapshot: optionalOmitUndefined(Schema.String),
|
||||||
diff: Schema.optional(Schema.String),
|
diff: optionalOmitUndefined(Schema.String),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const Info = Schema.Struct({
|
export const Info = Schema.Struct({
|
||||||
id: SessionID,
|
id: SessionID,
|
||||||
slug: Schema.String,
|
slug: Schema.String,
|
||||||
projectID: ProjectID,
|
projectID: ProjectID,
|
||||||
workspaceID: Schema.optional(WorkspaceID),
|
workspaceID: optionalOmitUndefined(WorkspaceID),
|
||||||
directory: Schema.String,
|
directory: Schema.String,
|
||||||
parentID: Schema.optional(SessionID),
|
parentID: optionalOmitUndefined(SessionID),
|
||||||
summary: Schema.optional(Summary),
|
summary: optionalOmitUndefined(Summary),
|
||||||
share: Schema.optional(Share),
|
share: optionalOmitUndefined(Share),
|
||||||
title: Schema.String,
|
title: Schema.String,
|
||||||
version: Schema.String,
|
version: Schema.String,
|
||||||
time: Time,
|
time: Time,
|
||||||
permission: Schema.optional(Permission.Ruleset),
|
permission: optionalOmitUndefined(Permission.Ruleset),
|
||||||
revert: Schema.optional(Revert),
|
revert: optionalOmitUndefined(Revert),
|
||||||
})
|
})
|
||||||
.annotate({ identifier: "Session" })
|
.annotate({ identifier: "Session" })
|
||||||
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
||||||
@@ -170,7 +170,7 @@ export type Info = Types.DeepMutable<Schema.Schema.Type<typeof Info>>
|
|||||||
|
|
||||||
export const ProjectInfo = Schema.Struct({
|
export const ProjectInfo = Schema.Struct({
|
||||||
id: ProjectID,
|
id: ProjectID,
|
||||||
name: Schema.optional(Schema.String),
|
name: optionalOmitUndefined(Schema.String),
|
||||||
worktree: Schema.String,
|
worktree: Schema.String,
|
||||||
})
|
})
|
||||||
.annotate({ identifier: "ProjectSummary" })
|
.annotate({ identifier: "ProjectSummary" })
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Schema } from "effect"
|
import { Option, Schema, SchemaGetter } from "effect"
|
||||||
|
import { zod, ZodOverride } from "./effect-zod"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integer greater than zero.
|
* Integer greater than zero.
|
||||||
@@ -10,6 +11,19 @@ export const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0))
|
|||||||
*/
|
*/
|
||||||
export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
|
export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional public JSON field that accepts explicit `undefined` internally but
|
||||||
|
* encodes it as an omitted key, matching `JSON.stringify` legacy responses.
|
||||||
|
*/
|
||||||
|
export const optionalOmitUndefined = <S extends Schema.Top>(schema: S) =>
|
||||||
|
Schema.optionalKey(schema).pipe(
|
||||||
|
Schema.decodeTo(Schema.optional(schema), {
|
||||||
|
decode: SchemaGetter.passthrough({ strict: false }),
|
||||||
|
encode: SchemaGetter.transformOptional(Option.filter((value) => value !== undefined)),
|
||||||
|
}),
|
||||||
|
Schema.annotate({ [ZodOverride]: zod(schema).optional() }),
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strip `readonly` from a nested type. Stand-in for `effect`'s `Types.DeepMutable`
|
* Strip `readonly` from a nested type. Stand-in for `effect`'s `Types.DeepMutable`
|
||||||
* until `effect:core/x228my` ("Types.DeepMutable widens unknown to `{}`") lands.
|
* until `effect:core/x228my` ("Types.DeepMutable widens unknown to `{}`") lands.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { afterEach, describe, expect } from "bun:test"
|
||||||
import type { UpgradeWebSocket } from "hono/ws"
|
import type { UpgradeWebSocket } from "hono/ws"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
import { Flag } from "@opencode-ai/core/flag/flag"
|
import { Flag } from "@opencode-ai/core/flag/flag"
|
||||||
@@ -13,6 +13,7 @@ import { MessageV2 } from "../../src/session/message-v2"
|
|||||||
import * as Log from "@opencode-ai/core/util/log"
|
import * as Log from "@opencode-ai/core/util/log"
|
||||||
import { resetDatabase } from "../fixture/db"
|
import { resetDatabase } from "../fixture/db"
|
||||||
import { tmpdir } from "../fixture/fixture"
|
import { tmpdir } from "../fixture/fixture"
|
||||||
|
import { it } from "../lib/effect"
|
||||||
|
|
||||||
void Log.init({ print: false })
|
void Log.init({ print: false })
|
||||||
|
|
||||||
@@ -32,44 +33,70 @@ function pathFor(path: string, params: Record<string, string>) {
|
|||||||
return Object.entries(params).reduce((result, [key, value]) => result.replace(`:${key}`, value), path)
|
return Object.entries(params).reduce((result, [key, value]) => result.replace(`:${key}`, value), path)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createSession(directory: string, input?: Session.CreateInput) {
|
function createSession(directory: string, input?: Session.CreateInput) {
|
||||||
return Instance.provide({
|
return Effect.promise(
|
||||||
directory,
|
async () =>
|
||||||
fn: async () => runSession(Session.Service.use((svc) => svc.create(input))),
|
await Instance.provide({
|
||||||
|
directory,
|
||||||
|
fn: () => runSession(Session.Service.use((svc) => svc.create(input))),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTextMessage(directory: string, sessionID: SessionID, text: string) {
|
||||||
|
return Effect.promise(
|
||||||
|
async () =>
|
||||||
|
await Instance.provide({
|
||||||
|
directory,
|
||||||
|
fn: () =>
|
||||||
|
runSession(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const svc = yield* Session.Service
|
||||||
|
const info = yield* svc.updateMessage({
|
||||||
|
id: MessageID.ascending(),
|
||||||
|
role: "user",
|
||||||
|
sessionID,
|
||||||
|
agent: "build",
|
||||||
|
model: { providerID: ProviderID.make("test"), modelID: ModelID.make("test") },
|
||||||
|
time: { created: Date.now() },
|
||||||
|
})
|
||||||
|
const part = yield* svc.updatePart({
|
||||||
|
id: PartID.ascending(),
|
||||||
|
sessionID,
|
||||||
|
messageID: info.id,
|
||||||
|
type: "text",
|
||||||
|
text,
|
||||||
|
})
|
||||||
|
return { info, part }
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function request(path: string, init?: RequestInit) {
|
||||||
|
return Effect.promise(async () => app().request(path, init))
|
||||||
|
}
|
||||||
|
|
||||||
|
function json<T>(response: Response) {
|
||||||
|
return Effect.promise(async () => {
|
||||||
|
if (response.status !== 200) throw new Error(await response.text())
|
||||||
|
return (await response.json()) as T
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createTextMessage(directory: string, sessionID: SessionID, text: string) {
|
function requestJson<T>(path: string, init?: RequestInit) {
|
||||||
return Instance.provide({
|
return request(path, init).pipe(Effect.flatMap(json<T>))
|
||||||
directory,
|
|
||||||
fn: async () =>
|
|
||||||
runSession(
|
|
||||||
Effect.gen(function* () {
|
|
||||||
const svc = yield* Session.Service
|
|
||||||
const info = yield* svc.updateMessage({
|
|
||||||
id: MessageID.ascending(),
|
|
||||||
role: "user",
|
|
||||||
sessionID,
|
|
||||||
agent: "build",
|
|
||||||
model: { providerID: ProviderID.make("test"), modelID: ModelID.make("test") },
|
|
||||||
time: { created: Date.now() },
|
|
||||||
})
|
|
||||||
const part = yield* svc.updatePart({
|
|
||||||
id: PartID.ascending(),
|
|
||||||
sessionID,
|
|
||||||
messageID: info.id,
|
|
||||||
type: "text",
|
|
||||||
text,
|
|
||||||
})
|
|
||||||
return { info, part }
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function json<T>(response: Response) {
|
function withTmp<A, E, R>(
|
||||||
if (response.status !== 200) throw new Error(await response.text())
|
options: Parameters<typeof tmpdir>[0],
|
||||||
return (await response.json()) as T
|
fn: (tmp: Awaited<ReturnType<typeof tmpdir>>) => Effect.Effect<A, E, R>,
|
||||||
|
) {
|
||||||
|
return Effect.acquireRelease(
|
||||||
|
Effect.promise(() => tmpdir(options)),
|
||||||
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
||||||
|
).pipe(Effect.flatMap(fn))
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@@ -79,210 +106,199 @@ afterEach(async () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("session HttpApi", () => {
|
describe("session HttpApi", () => {
|
||||||
test("serves read routes through Hono bridge", async () => {
|
it.live(
|
||||||
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false } })
|
"serves read routes through Hono bridge",
|
||||||
const headers = { "x-opencode-directory": tmp.path }
|
withTmp({ git: true, config: { formatter: false, lsp: false } }, (tmp) =>
|
||||||
const parent = await createSession(tmp.path, { title: "parent" })
|
Effect.gen(function* () {
|
||||||
const child = await createSession(tmp.path, { title: "child", parentID: parent.id })
|
const headers = { "x-opencode-directory": tmp.path }
|
||||||
const message = await createTextMessage(tmp.path, parent.id, "hello")
|
const parent = yield* createSession(tmp.path, { title: "parent" })
|
||||||
await createTextMessage(tmp.path, parent.id, "world")
|
const child = yield* createSession(tmp.path, { title: "child", parentID: parent.id })
|
||||||
|
const message = yield* createTextMessage(tmp.path, parent.id, "hello")
|
||||||
|
yield* createTextMessage(tmp.path, parent.id, "world")
|
||||||
|
|
||||||
expect(
|
const listed = yield* requestJson<Session.Info[]>(`${SessionPaths.list}?roots=true`, { headers })
|
||||||
(await json<Session.Info[]>(await app().request(`${SessionPaths.list}?roots=true`, { headers }))).map(
|
expect(listed.map((item) => item.id)).toContain(parent.id)
|
||||||
(item) => item.id,
|
expect(Object.hasOwn(listed[0]!, "parentID")).toBe(false)
|
||||||
),
|
|
||||||
).toContain(parent.id)
|
|
||||||
|
|
||||||
expect(await json<Record<string, unknown>>(await app().request(SessionPaths.status, { headers }))).toEqual({})
|
expect(yield* requestJson<Record<string, unknown>>(SessionPaths.status, { headers })).toEqual({})
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await json<Session.Info>(await app().request(pathFor(SessionPaths.get, { sessionID: parent.id }), { headers })),
|
yield* requestJson<Session.Info>(pathFor(SessionPaths.get, { sessionID: parent.id }), { headers }),
|
||||||
).toMatchObject({ id: parent.id, title: "parent" })
|
).toMatchObject({ id: parent.id, title: "parent" })
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
(
|
(yield* requestJson<Session.Info[]>(pathFor(SessionPaths.children, { sessionID: parent.id }), {
|
||||||
await json<Session.Info[]>(
|
headers,
|
||||||
await app().request(pathFor(SessionPaths.children, { sessionID: parent.id }), { headers }),
|
})).map((item) => item.id),
|
||||||
)
|
).toEqual([child.id])
|
||||||
).map((item) => item.id),
|
|
||||||
).toEqual([child.id])
|
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await json<unknown[]>(await app().request(pathFor(SessionPaths.todo, { sessionID: parent.id }), { headers })),
|
yield* requestJson<unknown[]>(pathFor(SessionPaths.todo, { sessionID: parent.id }), { headers }),
|
||||||
).toEqual([])
|
).toEqual([])
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await json<unknown[]>(await app().request(pathFor(SessionPaths.diff, { sessionID: parent.id }), { headers })),
|
yield* requestJson<unknown[]>(pathFor(SessionPaths.diff, { sessionID: parent.id }), { headers }),
|
||||||
).toEqual([])
|
).toEqual([])
|
||||||
|
|
||||||
const messages = await app().request(`${pathFor(SessionPaths.messages, { sessionID: parent.id })}?limit=1`, {
|
const messages = yield* request(`${pathFor(SessionPaths.messages, { sessionID: parent.id })}?limit=1`, {
|
||||||
headers,
|
|
||||||
})
|
|
||||||
const messagePage = await json<MessageV2.WithParts[]>(messages)
|
|
||||||
const nextCursor = messages.headers.get("x-next-cursor")
|
|
||||||
expect(nextCursor).toBeTruthy()
|
|
||||||
expect(messagePage[0]?.parts[0]).toMatchObject({ type: "text" })
|
|
||||||
|
|
||||||
expect(
|
|
||||||
(
|
|
||||||
await app().request(`${pathFor(SessionPaths.messages, { sessionID: parent.id })}?before=${nextCursor}`, {
|
|
||||||
headers,
|
headers,
|
||||||
})
|
})
|
||||||
).status,
|
const messagePage = yield* json<MessageV2.WithParts[]>(messages)
|
||||||
).toBe(400)
|
const nextCursor = messages.headers.get("x-next-cursor")
|
||||||
expect(
|
expect(nextCursor).toBeTruthy()
|
||||||
(
|
expect(messagePage[0]?.parts[0]).toMatchObject({ type: "text" })
|
||||||
await app().request(`${pathFor(SessionPaths.messages, { sessionID: parent.id })}?limit=1&before=invalid`, {
|
|
||||||
|
expect(
|
||||||
|
(yield* request(`${pathFor(SessionPaths.messages, { sessionID: parent.id })}?before=${nextCursor}`, {
|
||||||
|
headers,
|
||||||
|
})).status,
|
||||||
|
).toBe(400)
|
||||||
|
expect(
|
||||||
|
(yield* request(`${pathFor(SessionPaths.messages, { sessionID: parent.id })}?limit=1&before=invalid`, {
|
||||||
|
headers,
|
||||||
|
})).status,
|
||||||
|
).toBe(400)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<MessageV2.WithParts>(
|
||||||
|
pathFor(SessionPaths.message, { sessionID: parent.id, messageID: message.info.id }),
|
||||||
|
{ headers },
|
||||||
|
),
|
||||||
|
).toMatchObject({ info: { id: message.info.id } })
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
it.live(
|
||||||
|
"serves lifecycle mutation routes through Hono bridge",
|
||||||
|
withTmp({ git: true, config: { formatter: false, lsp: false, share: "disabled" } }, (tmp) =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
||||||
|
|
||||||
|
const createdEmpty = yield* requestJson<Session.Info>(SessionPaths.create, {
|
||||||
|
method: "POST",
|
||||||
headers,
|
headers,
|
||||||
})
|
})
|
||||||
).status,
|
expect(createdEmpty.id).toBeTruthy()
|
||||||
).toBe(400)
|
|
||||||
|
|
||||||
expect(
|
const created = yield* requestJson<Session.Info>(SessionPaths.create, {
|
||||||
await json<MessageV2.WithParts>(
|
method: "POST",
|
||||||
await app().request(pathFor(SessionPaths.message, { sessionID: parent.id, messageID: message.info.id }), {
|
|
||||||
headers,
|
headers,
|
||||||
}),
|
body: JSON.stringify({ title: "created" }),
|
||||||
),
|
})
|
||||||
).toMatchObject({ info: { id: message.info.id } })
|
expect(created.title).toBe("created")
|
||||||
})
|
|
||||||
|
|
||||||
test("serves lifecycle mutation routes through Hono bridge", async () => {
|
const updated = yield* requestJson<Session.Info>(pathFor(SessionPaths.update, { sessionID: created.id }), {
|
||||||
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false, share: "disabled" } })
|
|
||||||
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
|
||||||
|
|
||||||
const createdEmpty = await json<Session.Info>(
|
|
||||||
await app().request(SessionPaths.create, {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
expect(createdEmpty.id).toBeTruthy()
|
|
||||||
|
|
||||||
const created = await json<Session.Info>(
|
|
||||||
await app().request(SessionPaths.create, {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify({ title: "created" }),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
expect(created.title).toBe("created")
|
|
||||||
|
|
||||||
const updated = await json<Session.Info>(
|
|
||||||
await app().request(pathFor(SessionPaths.update, { sessionID: created.id }), {
|
|
||||||
method: "PATCH",
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify({ title: "updated", time: { archived: 1 } }),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
expect(updated).toMatchObject({ id: created.id, title: "updated", time: { archived: 1 } })
|
|
||||||
|
|
||||||
const forked = await json<Session.Info>(
|
|
||||||
await app().request(pathFor(SessionPaths.fork, { sessionID: created.id }), {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify({}),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
expect(forked.id).not.toBe(created.id)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await json<boolean>(
|
|
||||||
await app().request(pathFor(SessionPaths.abort, { sessionID: created.id }), { method: "POST", headers }),
|
|
||||||
),
|
|
||||||
).toBe(true)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await json<boolean>(
|
|
||||||
await app().request(pathFor(SessionPaths.remove, { sessionID: created.id }), { method: "DELETE", headers }),
|
|
||||||
),
|
|
||||||
).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
test("serves message mutation routes through Hono bridge", async () => {
|
|
||||||
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false } })
|
|
||||||
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
|
||||||
const session = await createSession(tmp.path, { title: "messages" })
|
|
||||||
const first = await createTextMessage(tmp.path, session.id, "first")
|
|
||||||
const second = await createTextMessage(tmp.path, session.id, "second")
|
|
||||||
|
|
||||||
const updated = await json<MessageV2.Part>(
|
|
||||||
await app().request(
|
|
||||||
pathFor(SessionPaths.updatePart, {
|
|
||||||
sessionID: session.id,
|
|
||||||
messageID: first.info.id,
|
|
||||||
partID: first.part.id,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
headers,
|
headers,
|
||||||
body: JSON.stringify({ ...first.part, text: "updated" }),
|
body: JSON.stringify({ title: "updated", time: { archived: 1 } }),
|
||||||
},
|
})
|
||||||
),
|
expect(updated).toMatchObject({ id: created.id, title: "updated", time: { archived: 1 } })
|
||||||
)
|
|
||||||
expect(updated).toMatchObject({ id: first.part.id, type: "text", text: "updated" })
|
|
||||||
|
|
||||||
expect(
|
const forked = yield* requestJson<Session.Info>(pathFor(SessionPaths.fork, { sessionID: created.id }), {
|
||||||
await json<boolean>(
|
method: "POST",
|
||||||
await app().request(
|
headers,
|
||||||
pathFor(SessionPaths.deletePart, {
|
body: JSON.stringify({}),
|
||||||
|
})
|
||||||
|
expect(forked.id).not.toBe(created.id)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<boolean>(pathFor(SessionPaths.abort, { sessionID: created.id }), {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
}),
|
||||||
|
).toBe(true)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<boolean>(pathFor(SessionPaths.remove, { sessionID: created.id }), {
|
||||||
|
method: "DELETE",
|
||||||
|
headers,
|
||||||
|
}),
|
||||||
|
).toBe(true)
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
it.live(
|
||||||
|
"serves message mutation routes through Hono bridge",
|
||||||
|
withTmp({ git: true, config: { formatter: false, lsp: false } }, (tmp) =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
||||||
|
const session = yield* createSession(tmp.path, { title: "messages" })
|
||||||
|
const first = yield* createTextMessage(tmp.path, session.id, "first")
|
||||||
|
const second = yield* createTextMessage(tmp.path, session.id, "second")
|
||||||
|
|
||||||
|
const updated = yield* requestJson<MessageV2.Part>(
|
||||||
|
pathFor(SessionPaths.updatePart, {
|
||||||
sessionID: session.id,
|
sessionID: session.id,
|
||||||
messageID: first.info.id,
|
messageID: first.info.id,
|
||||||
partID: first.part.id,
|
partID: first.part.id,
|
||||||
}),
|
}),
|
||||||
{ method: "DELETE", headers },
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).toBe(true)
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await json<boolean>(
|
|
||||||
await app().request(pathFor(SessionPaths.deleteMessage, { sessionID: session.id, messageID: second.info.id }), {
|
|
||||||
method: "DELETE",
|
|
||||||
headers,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
test("serves remaining non-LLM session mutation routes through Hono bridge", async () => {
|
|
||||||
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false } })
|
|
||||||
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
|
||||||
const session = await createSession(tmp.path, { title: "remaining" })
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await json<Session.Info>(
|
|
||||||
await app().request(pathFor(SessionPaths.revert, { sessionID: session.id }), {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify({ messageID: MessageID.ascending() }),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
).toMatchObject({ id: session.id })
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await json<Session.Info>(
|
|
||||||
await app().request(pathFor(SessionPaths.unrevert, { sessionID: session.id }), {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
).toMatchObject({ id: session.id })
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await json<boolean>(
|
|
||||||
await app().request(
|
|
||||||
pathFor(SessionPaths.permissions, {
|
|
||||||
sessionID: session.id,
|
|
||||||
permissionID: String(PermissionID.ascending()),
|
|
||||||
}),
|
|
||||||
{
|
{
|
||||||
|
method: "PATCH",
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify({ ...first.part, text: "updated" }),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
expect(updated).toMatchObject({ id: first.part.id, type: "text", text: "updated" })
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<boolean>(
|
||||||
|
pathFor(SessionPaths.deletePart, {
|
||||||
|
sessionID: session.id,
|
||||||
|
messageID: first.info.id,
|
||||||
|
partID: first.part.id,
|
||||||
|
}),
|
||||||
|
{ method: "DELETE", headers },
|
||||||
|
),
|
||||||
|
).toBe(true)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<boolean>(
|
||||||
|
pathFor(SessionPaths.deleteMessage, { sessionID: session.id, messageID: second.info.id }),
|
||||||
|
{ method: "DELETE", headers },
|
||||||
|
),
|
||||||
|
).toBe(true)
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
it.live(
|
||||||
|
"serves remaining non-LLM session mutation routes through Hono bridge",
|
||||||
|
withTmp({ git: true, config: { formatter: false, lsp: false } }, (tmp) =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
|
||||||
|
const session = yield* createSession(tmp.path, { title: "remaining" })
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<Session.Info>(pathFor(SessionPaths.revert, { sessionID: session.id }), {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers,
|
headers,
|
||||||
body: JSON.stringify({ response: "once" }),
|
body: JSON.stringify({ messageID: MessageID.ascending() }),
|
||||||
},
|
}),
|
||||||
),
|
).toMatchObject({ id: session.id })
|
||||||
),
|
|
||||||
).toBe(true)
|
expect(
|
||||||
})
|
yield* requestJson<Session.Info>(pathFor(SessionPaths.unrevert, { sessionID: session.id }), {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
}),
|
||||||
|
).toMatchObject({ id: session.id })
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* requestJson<boolean>(
|
||||||
|
pathFor(SessionPaths.permissions, {
|
||||||
|
sessionID: session.id,
|
||||||
|
permissionID: String(PermissionID.ascending()),
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify({ response: "once" }),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
).toBe(true)
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
import { Schema } from "effect"
|
||||||
|
import { ProjectID } from "../../src/project/schema"
|
||||||
|
import { SessionID } from "../../src/session/schema"
|
||||||
|
import { Session } from "../../src/session/session"
|
||||||
|
|
||||||
|
const info = {
|
||||||
|
id: SessionID.descending(),
|
||||||
|
slug: "test-session",
|
||||||
|
projectID: ProjectID.global,
|
||||||
|
workspaceID: undefined,
|
||||||
|
directory: "/tmp/opencode",
|
||||||
|
parentID: undefined,
|
||||||
|
summary: undefined,
|
||||||
|
share: undefined,
|
||||||
|
title: "Test session",
|
||||||
|
version: "1.0.0",
|
||||||
|
time: {
|
||||||
|
created: 1,
|
||||||
|
updated: 2,
|
||||||
|
compacting: undefined,
|
||||||
|
archived: undefined,
|
||||||
|
},
|
||||||
|
permission: undefined,
|
||||||
|
revert: undefined,
|
||||||
|
} satisfies Session.Info
|
||||||
|
|
||||||
|
describe("Session schema", () => {
|
||||||
|
test("encodes undefined optional session fields as omitted keys", () => {
|
||||||
|
const encoded = Schema.encodeUnknownSync(Session.Info)(info) as Record<string, unknown>
|
||||||
|
|
||||||
|
for (const key of ["workspaceID", "parentID", "summary", "share", "permission", "revert"]) {
|
||||||
|
expect(Object.hasOwn(encoded, key)).toBe(false)
|
||||||
|
}
|
||||||
|
expect(Object.hasOwn(encoded.time as Record<string, unknown>, "compacting")).toBe(false)
|
||||||
|
expect(Object.hasOwn(encoded.time as Record<string, unknown>, "archived")).toBe(false)
|
||||||
|
expect(JSON.stringify(encoded)).not.toContain("parentID")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("encodes undefined optional global session project fields as omitted keys", () => {
|
||||||
|
const encoded = Schema.encodeUnknownSync(Session.GlobalInfo)({
|
||||||
|
...info,
|
||||||
|
project: {
|
||||||
|
id: ProjectID.global,
|
||||||
|
name: undefined,
|
||||||
|
worktree: "/tmp/opencode",
|
||||||
|
},
|
||||||
|
}) as Record<string, unknown>
|
||||||
|
|
||||||
|
expect(Object.hasOwn(encoded, "parentID")).toBe(false)
|
||||||
|
expect(Object.hasOwn(encoded.project as Record<string, unknown>, "name")).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user