add experimental permission HttpApi slice (#22385)
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { AppRuntime } from "../../src/effect/app-runtime"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { Question } from "../../src/question"
|
||||
import { Server } from "../../src/server/server"
|
||||
import { SessionID } from "../../src/session/schema"
|
||||
import { Log } from "../../src/util/log"
|
||||
import { tmpdir } from "../fixture/fixture"
|
||||
|
||||
Log.init({ print: false })
|
||||
|
||||
const ask = (input: { sessionID: SessionID; questions: ReadonlyArray<Question.Info> }) =>
|
||||
AppRuntime.runPromise(Question.Service.use((svc) => svc.ask(input)))
|
||||
|
||||
afterEach(async () => {
|
||||
await Instance.disposeAll()
|
||||
})
|
||||
|
||||
describe("experimental question httpapi", () => {
|
||||
test("lists pending questions, replies, and serves docs", async () => {
|
||||
await using tmp = await tmpdir({ git: true })
|
||||
const app = Server.Default().app
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
"x-opencode-directory": tmp.path,
|
||||
}
|
||||
const questions: ReadonlyArray<Question.Info> = [
|
||||
{
|
||||
question: "What would you like to do?",
|
||||
header: "Action",
|
||||
options: [
|
||||
{ label: "Option 1", description: "First option" },
|
||||
{ label: "Option 2", description: "Second option" },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
let pending!: ReturnType<typeof ask>
|
||||
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
pending = ask({
|
||||
sessionID: SessionID.make("ses_test"),
|
||||
questions,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const list = await app.request("/experimental/httpapi/question", {
|
||||
headers,
|
||||
})
|
||||
|
||||
expect(list.status).toBe(200)
|
||||
const items = await list.json()
|
||||
expect(items).toHaveLength(1)
|
||||
expect(items[0]).toMatchObject({ questions })
|
||||
|
||||
const doc = await app.request("/experimental/httpapi/question/doc", {
|
||||
headers,
|
||||
})
|
||||
|
||||
expect(doc.status).toBe(200)
|
||||
const spec = await doc.json()
|
||||
expect(spec.paths["/experimental/httpapi/question"]?.get?.operationId).toBe("question.list")
|
||||
expect(spec.paths["/experimental/httpapi/question/{requestID}/reply"]?.post?.operationId).toBe("question.reply")
|
||||
|
||||
const reply = await app.request(`/experimental/httpapi/question/${items[0].id}/reply`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({ answers: [["Option 1"]] }),
|
||||
})
|
||||
|
||||
expect(reply.status).toBe(200)
|
||||
expect(await reply.json()).toBe(true)
|
||||
expect(await pending).toEqual([["Option 1"]])
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,13 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Schema } from "effect"
|
||||
import z from "zod"
|
||||
|
||||
import { zod } from "../../src/util/effect-zod"
|
||||
import { zod, ZodOverride } from "../../src/util/effect-zod"
|
||||
|
||||
function json(schema: z.ZodTypeAny) {
|
||||
const { $schema: _, ...rest } = z.toJSONSchema(schema)
|
||||
return rest
|
||||
}
|
||||
|
||||
describe("util.effect-zod", () => {
|
||||
test("converts class schemas for route dto shapes", () => {
|
||||
@@ -58,4 +64,126 @@ describe("util.effect-zod", () => {
|
||||
test("throws for unsupported tuple schemas", () => {
|
||||
expect(() => zod(Schema.Tuple([Schema.String, Schema.Number]))).toThrow("unsupported effect schema")
|
||||
})
|
||||
|
||||
test("string literal unions produce z.enum with enum in JSON Schema", () => {
|
||||
const Action = Schema.Literals(["allow", "deny", "ask"])
|
||||
const out = zod(Action)
|
||||
|
||||
expect(out.parse("allow")).toBe("allow")
|
||||
expect(out.parse("deny")).toBe("deny")
|
||||
expect(() => out.parse("nope")).toThrow()
|
||||
|
||||
// Matches native z.enum JSON Schema output
|
||||
const bridged = json(out)
|
||||
const native = json(z.enum(["allow", "deny", "ask"]))
|
||||
expect(bridged).toEqual(native)
|
||||
expect(bridged.enum).toEqual(["allow", "deny", "ask"])
|
||||
})
|
||||
|
||||
test("ZodOverride annotation provides the Zod schema for branded IDs", () => {
|
||||
const override = z.string().startsWith("per")
|
||||
const ID = Schema.String.annotate({ [ZodOverride]: override }).pipe(Schema.brand("TestID"))
|
||||
|
||||
const Parent = Schema.Struct({ id: ID, name: Schema.String })
|
||||
const out = zod(Parent)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((out as any).parse({ id: "per_abc", name: "test" })).toEqual({ id: "per_abc", name: "test" })
|
||||
|
||||
const schema = json(out) as any
|
||||
expect(schema.properties.id).toEqual({ type: "string", pattern: "^per.*" })
|
||||
})
|
||||
|
||||
test("Schema.Class nested in a parent preserves ref via identifier", () => {
|
||||
class Inner extends Schema.Class<Inner>("MyInner")({
|
||||
value: Schema.String,
|
||||
}) {}
|
||||
|
||||
class Outer extends Schema.Class<Outer>("MyOuter")({
|
||||
inner: Inner,
|
||||
}) {}
|
||||
|
||||
const out = zod(Outer)
|
||||
expect(out.meta()?.ref).toBe("MyOuter")
|
||||
|
||||
const shape = (out as any).shape ?? (out as any)._def?.shape?.()
|
||||
expect(shape.inner.meta()?.ref).toBe("MyInner")
|
||||
})
|
||||
|
||||
test("Schema.Class preserves identifier and uses enum format", () => {
|
||||
class Rule extends Schema.Class<Rule>("PermissionRule")({
|
||||
permission: Schema.String,
|
||||
pattern: Schema.String,
|
||||
action: Schema.Literals(["allow", "deny", "ask"]),
|
||||
}) {}
|
||||
|
||||
const out = zod(Rule)
|
||||
expect(out.meta()?.ref).toBe("PermissionRule")
|
||||
|
||||
const schema = json(out) as any
|
||||
expect(schema.properties.action).toEqual({
|
||||
type: "string",
|
||||
enum: ["allow", "deny", "ask"],
|
||||
})
|
||||
})
|
||||
|
||||
test("ZodOverride on ID carries pattern through Schema.Class", () => {
|
||||
const ID = Schema.String.annotate({
|
||||
[ZodOverride]: z.string().startsWith("per"),
|
||||
})
|
||||
|
||||
class Request extends Schema.Class<Request>("TestRequest")({
|
||||
id: ID,
|
||||
name: Schema.String,
|
||||
}) {}
|
||||
|
||||
const schema = json(zod(Request)) as any
|
||||
expect(schema.properties.id).toEqual({ type: "string", pattern: "^per.*" })
|
||||
expect(schema.properties.name).toEqual({ type: "string" })
|
||||
})
|
||||
|
||||
test("Permission schemas match original Zod equivalents", () => {
|
||||
const MsgID = Schema.String.annotate({ [ZodOverride]: z.string().startsWith("msg") })
|
||||
const PerID = Schema.String.annotate({ [ZodOverride]: z.string().startsWith("per") })
|
||||
const SesID = Schema.String.annotate({ [ZodOverride]: z.string().startsWith("ses") })
|
||||
|
||||
class Tool extends Schema.Class<Tool>("PermissionTool")({
|
||||
messageID: MsgID,
|
||||
callID: Schema.String,
|
||||
}) {}
|
||||
|
||||
class Request extends Schema.Class<Request>("PermissionRequest")({
|
||||
id: PerID,
|
||||
sessionID: SesID,
|
||||
permission: Schema.String,
|
||||
patterns: Schema.Array(Schema.String),
|
||||
metadata: Schema.Record(Schema.String, Schema.Unknown),
|
||||
always: Schema.Array(Schema.String),
|
||||
tool: Schema.optional(Tool),
|
||||
}) {}
|
||||
|
||||
const bridged = json(zod(Request)) as any
|
||||
expect(bridged.properties.id).toEqual({ type: "string", pattern: "^per.*" })
|
||||
expect(bridged.properties.sessionID).toEqual({ type: "string", pattern: "^ses.*" })
|
||||
expect(bridged.properties.permission).toEqual({ type: "string" })
|
||||
expect(bridged.required?.sort()).toEqual(["id", "sessionID", "permission", "patterns", "metadata", "always"].sort())
|
||||
|
||||
// Tool field is present with the ref from Schema.Class identifier
|
||||
const toolSchema = json(zod(Tool)) as any
|
||||
expect(toolSchema.properties.messageID).toEqual({ type: "string", pattern: "^msg.*" })
|
||||
expect(toolSchema.properties.callID).toEqual({ type: "string" })
|
||||
})
|
||||
|
||||
test("ZodOverride survives Schema.brand", () => {
|
||||
const override = z.string().startsWith("ses")
|
||||
const ID = Schema.String.annotate({ [ZodOverride]: override }).pipe(Schema.brand("SessionID"))
|
||||
|
||||
// The branded schema's AST still has the override
|
||||
class Parent extends Schema.Class<Parent>("Parent")({
|
||||
sessionID: ID,
|
||||
}) {}
|
||||
|
||||
const schema = json(zod(Parent)) as any
|
||||
expect(schema.properties.sessionID).toEqual({ type: "string", pattern: "^ses.*" })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user