mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-14 03:56:55 +03:00
test(app): mock session wait and parse e2e mock payloads (#53095)
This commit is contained in:
committed by
GitHub
Unverified
parent
f8cafd8362
commit
44579a5d98
@@ -1,16 +1,31 @@
|
||||
import { Schema, SchemaGetter } from "effect"
|
||||
import { Predicate, Schema, SchemaGetter } from "effect"
|
||||
import { HttpApi, HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "effect/unstable/httpapi"
|
||||
import { Pty } from "@opencode/schema/pty"
|
||||
import { Worktree } from "@opencode/schema/worktree"
|
||||
|
||||
// Handlers answer plain fixture data: undefined properties are dropped, and other non-JSON values become null.
|
||||
const Json = Schema.Json.pipe(
|
||||
Schema.decodeTo(Schema.Unknown, {
|
||||
decode: SchemaGetter.passthrough(),
|
||||
encode: SchemaGetter.transform(jsonValue),
|
||||
encode: SchemaGetter.transform(function json(value): Schema.Json {
|
||||
if (value === null || Predicate.isString(value) || Predicate.isBoolean(value)) return value
|
||||
|
||||
if (Predicate.isNumber(value)) return Number.isFinite(value) ? value : null
|
||||
|
||||
if (Array.isArray(value)) return value.map(json)
|
||||
|
||||
if (!Predicate.isObject(value)) return null
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).flatMap(([key, item]) => (item === undefined ? [] : [[key, json(item)]])),
|
||||
)
|
||||
}),
|
||||
}),
|
||||
HttpApiSchema.asJson(),
|
||||
)
|
||||
|
||||
const JsonPayload = Schema.Unknown.pipe(HttpApiSchema.asJson())
|
||||
|
||||
const Query = Schema.Struct({
|
||||
directory: Schema.optional(Schema.String),
|
||||
parentID: Schema.optional(Schema.String),
|
||||
@@ -23,8 +38,11 @@ const Query = Schema.Struct({
|
||||
type: Schema.optional(Schema.String),
|
||||
mode: Schema.optional(Schema.String),
|
||||
})
|
||||
|
||||
const SessionParams = { sessionID: Schema.String }
|
||||
|
||||
const PtyParams = { ptyID: Pty.ID }
|
||||
|
||||
const NoContent = HttpApiSchema.NoContent
|
||||
|
||||
export class MockNotFound extends Schema.TaggedError<MockNotFound>()("MockNotFound", {
|
||||
@@ -45,10 +63,16 @@ export class MockShellNotFound extends Schema.TaggedError<MockShellNotFound>()("
|
||||
message: Schema.String,
|
||||
}) {}
|
||||
|
||||
// The server's error for a request without its password.
|
||||
export class MockUnauthorized extends Schema.TaggedError<MockUnauthorized>()("UnauthorizedError", {
|
||||
message: Schema.String,
|
||||
}) {}
|
||||
|
||||
// A mutation the scenario did not configure a handler for.
|
||||
export class MockUnsupported extends Schema.TaggedError<MockUnsupported>()("MockUnsupported", {
|
||||
message: Schema.String,
|
||||
}) {}
|
||||
|
||||
const Unsupported = MockUnsupported.pipe(HttpApiSchema.status(501))
|
||||
|
||||
const Group = HttpApiGroup.make("mock")
|
||||
@@ -368,6 +392,12 @@ const Group = HttpApiGroup.make("mock")
|
||||
)
|
||||
.add(
|
||||
HttpApiEndpoint.post("sessionInterrupt", "/api/session/:sessionID/interrupt", {
|
||||
params: SessionParams,
|
||||
success: Json,
|
||||
}),
|
||||
)
|
||||
.add(
|
||||
HttpApiEndpoint.post("sessionWait", "/api/experimental/session/:sessionID/wait", {
|
||||
params: SessionParams,
|
||||
success: NoContent,
|
||||
}),
|
||||
@@ -409,13 +439,3 @@ const Group = HttpApiGroup.make("mock")
|
||||
)
|
||||
|
||||
export const MockApi = HttpApi.make("mock").add(Group)
|
||||
|
||||
function jsonValue(value: unknown): Schema.Json {
|
||||
if (value === null || typeof value === "string" || typeof value === "boolean") return value
|
||||
if (typeof value === "number") return Number.isFinite(value) ? value : null
|
||||
if (Array.isArray(value)) return value.map(jsonValue)
|
||||
if (!value || typeof value !== "object") return null
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).flatMap(([key, item]) => (item === undefined ? [] : [[key, jsonValue(item)]])),
|
||||
)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user