mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-13 23:52:06 +00:00
207 lines
7.2 KiB
TypeScript
207 lines
7.2 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { Effect, Exit, Stream } from "effect"
|
|
import { ChildProcess } from "effect/unstable/process"
|
|
import { AppProcess } from "@opencode-ai/core/process"
|
|
import { testEffect } from "../lib/effect"
|
|
|
|
const it = testEffect(AppProcess.defaultLayer)
|
|
|
|
const NODE = process.execPath
|
|
const cmd = (...args: string[]) => ChildProcess.make(NODE, args)
|
|
|
|
describe("AppProcess", () => {
|
|
describe("run", () => {
|
|
it.effect(
|
|
"captures stdout and exit code zero",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc.run(cmd("-e", "process.stdout.write('hi\\n')"))
|
|
expect(result.exitCode).toBe(0)
|
|
expect(result.stdout.toString("utf8")).toBe("hi\n")
|
|
expect(result.truncated).toBe(false)
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"non-zero exit returns RunResult; caller can require success",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc.run(cmd("-e", "process.exit(1)"))
|
|
expect(result.exitCode).toBe(1)
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"requireSuccess fails on non-zero exit",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const exit = yield* Effect.exit(
|
|
svc.run(cmd("-e", "process.exit(1)")).pipe(Effect.flatMap(AppProcess.requireSuccess)),
|
|
)
|
|
expect(Exit.isFailure(exit)).toBe(true)
|
|
if (Exit.isFailure(exit)) {
|
|
const reason = exit.cause.reasons[0]
|
|
if (reason && reason._tag === "Fail") {
|
|
expect(reason.error).toBeInstanceOf(AppProcess.AppProcessError)
|
|
expect((reason.error as AppProcess.AppProcessError).exitCode).toBe(1)
|
|
} else {
|
|
throw new Error("expected fail reason")
|
|
}
|
|
}
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"requireSuccess succeeds on exit 0",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc.run(cmd("-e", "process.exit(0)")).pipe(Effect.flatMap(AppProcess.requireSuccess))
|
|
expect(result.exitCode).toBe(0)
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"requireExitIn allowlists multiple exit codes",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const requireZeroOrOne = AppProcess.requireExitIn([0, 1])
|
|
const okZero = yield* svc.run(cmd("-e", "process.exit(0)")).pipe(Effect.flatMap(requireZeroOrOne))
|
|
expect(okZero.exitCode).toBe(0)
|
|
const okOne = yield* svc.run(cmd("-e", "process.exit(1)")).pipe(Effect.flatMap(requireZeroOrOne))
|
|
expect(okOne.exitCode).toBe(1)
|
|
const exit = yield* Effect.exit(svc.run(cmd("-e", "process.exit(2)")).pipe(Effect.flatMap(requireZeroOrOne)))
|
|
expect(Exit.isFailure(exit)).toBe(true)
|
|
if (Exit.isFailure(exit)) {
|
|
const reason = exit.cause.reasons[0]
|
|
if (reason && reason._tag === "Fail") {
|
|
expect(reason.error).toBeInstanceOf(AppProcess.AppProcessError)
|
|
expect((reason.error as AppProcess.AppProcessError).exitCode).toBe(2)
|
|
}
|
|
}
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"truncates output when maxOutputBytes is set",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc.run(cmd("-e", "process.stdout.write('0123456789')"), { maxOutputBytes: 5 })
|
|
expect(result.exitCode).toBe(0)
|
|
expect(result.truncated).toBe(true)
|
|
expect(result.stdout.length).toBe(5)
|
|
expect(result.stdout.toString("utf8")).toBe("01234")
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"result includes command description",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc.run(cmd("-e", "process.stdout.write('hi')"))
|
|
expect(result.command).toBe(`${NODE} -e process.stdout.write('hi')`)
|
|
}),
|
|
)
|
|
})
|
|
|
|
describe("inherited platform methods", () => {
|
|
it.effect(
|
|
"string returns stdout as string",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const out = yield* svc.string(cmd("-e", "process.stdout.write('hi\\n')"))
|
|
expect(out).toBe("hi\n")
|
|
}),
|
|
)
|
|
|
|
it.effect(
|
|
"lines returns the platform's array of lines",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const out = yield* svc.lines(cmd("-e", "process.stdout.write('a\\nb\\n')"))
|
|
expect(Array.from(out)).toEqual(["a", "b"])
|
|
}),
|
|
)
|
|
})
|
|
|
|
describe("runStream", () => {
|
|
it.live(
|
|
"emits lines incrementally and ends cleanly on exit 0",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc
|
|
.runStream(cmd("-e", "console.log('one'); console.log('two'); console.log('three')"))
|
|
.pipe(Stream.runCollect)
|
|
expect(Array.from(result)).toEqual(["one", "two", "three"])
|
|
}),
|
|
)
|
|
|
|
it.live(
|
|
"fails with AppProcessError when exit not in okExitCodes",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const exit = yield* Effect.exit(
|
|
svc.runStream(cmd("-e", "console.log('a'); process.exit(2)"), { okExitCodes: [0] }).pipe(Stream.runCollect),
|
|
)
|
|
expect(Exit.isFailure(exit)).toBe(true)
|
|
if (Exit.isFailure(exit)) {
|
|
const reason = exit.cause.reasons[0]
|
|
if (reason && reason._tag === "Fail") {
|
|
expect(reason.error).toBeInstanceOf(AppProcess.AppProcessError)
|
|
}
|
|
}
|
|
}),
|
|
)
|
|
|
|
it.live(
|
|
"okExitCodes allowlist treats non-zero as success",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc
|
|
.runStream(cmd("-e", "console.log('only'); process.exit(1)"), { okExitCodes: [0, 1] })
|
|
.pipe(Stream.runCollect)
|
|
expect(Array.from(result)).toEqual(["only"])
|
|
}),
|
|
)
|
|
|
|
it.live(
|
|
"without okExitCodes, never fails on exit code",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const result = yield* svc.runStream(cmd("-e", "console.log('only'); process.exit(7)")).pipe(Stream.runCollect)
|
|
expect(Array.from(result)).toEqual(["only"])
|
|
}),
|
|
)
|
|
|
|
it.live(
|
|
"AbortSignal interrupts the stream",
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const controller = new AbortController()
|
|
setTimeout(() => controller.abort(), 50)
|
|
const exit = yield* Effect.exit(
|
|
svc
|
|
.runStream(cmd("-e", "setInterval(() => console.log('tick'), 100); setTimeout(() => {}, 60_000)"), {
|
|
signal: controller.signal,
|
|
})
|
|
.pipe(Stream.runCollect),
|
|
)
|
|
expect(Exit.isFailure(exit)).toBe(true)
|
|
}),
|
|
)
|
|
})
|
|
|
|
describe("spawn (inherited)", () => {
|
|
it.live(
|
|
"returns the platform ChildProcessHandle for advanced use",
|
|
Effect.scoped(
|
|
Effect.gen(function* () {
|
|
const svc = yield* AppProcess.Service
|
|
const handle = yield* svc.spawn(cmd("-e", "setInterval(() => {}, 1_000)"))
|
|
expect(yield* handle.isRunning).toBe(true)
|
|
yield* handle.kill()
|
|
}),
|
|
),
|
|
)
|
|
})
|
|
})
|