Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions packages/opencode/script/prebuild-test-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env bun
// Build a pre-compiled `opencode` binary for subprocess tests, then expose
// it at `dist/test-cli/bin/opencode` for the harness to consume.
//
// Why: each `bun run --conditions=browser src/index.ts <cmd>` spawn pays
// ~15s of JIT + plugin init + DB migration in isolation mode. The
// pre-compiled binary cuts that to ~5s — a 3x improvement on subprocess
// tests that touch the DB (mcp, providers list, etc.).
//
// Usage:
// bun script/prebuild-test-cli.ts
// export OPENCODE_TEST_CLI_PATH="$PWD/dist/test-cli/bin/opencode"
// bun test test/cli/
//
// The harness (see test/lib/cli-process.ts) reads OPENCODE_TEST_CLI_PATH; if
// set, it spawns the binary directly instead of `bun run src/index.ts`. If
// unset, it falls back to dev mode — so this script is strictly opt-in.
//
// Build cost amortizes after ~1 spawn that touches the DB. Recommended for
// CI, manual `bun test test/cli/` runs, and any local iteration where the
// CLI surface itself isn't under change. Skip for normal src/* editing — the
// dev path picks up source changes without rebuild.
import { $ } from "bun"
import fs from "node:fs/promises"
import path from "node:path"

const dir = path.resolve(import.meta.dirname, "..")
process.chdir(dir)

const platform = process.platform === "win32" ? "win32" : process.platform === "darwin" ? "darwin" : "linux"
const arch = process.arch === "x64" ? "x64" : process.arch === "arm64" ? "arm64" : "x64"
const targetDir = path.join(dir, "dist", `opencode-${platform}-${arch}`)
const binaryName = process.platform === "win32" ? "opencode.exe" : "opencode"
const builtBinary = path.join(targetDir, "bin", binaryName)
const stableBinary = path.join(dir, "dist", "test-cli", "bin", binaryName)

const force = process.argv.includes("--force")

// Walk src/ and return the newest mtime seen. Faster than `git status` for
// the freshness check and works for uncommitted edits. Returns 0 on error
// so a missing src/ tree forces a rebuild via the comparison below.
async function newestMtimeMs(root: string): Promise<number> {
let max = 0
async function walk(p: string) {
let entries: { name: string; isDirectory: () => boolean; isFile: () => boolean }[]
try {
entries = await fs.readdir(p, { withFileTypes: true })
} catch {
return
}
for (const entry of entries) {
const full = path.join(p, entry.name)
if (entry.isDirectory()) {
if (entry.name === "node_modules" || entry.name === "dist") continue
await walk(full)
} else if (entry.isFile()) {
const stat = await fs.stat(full).catch(() => null)
if (stat && stat.mtimeMs > max) max = stat.mtimeMs
}
}
}
await walk(root)
return max
}

async function fresh(): Promise<boolean> {
const binStat = await fs.stat(builtBinary).catch(() => null)
if (!binStat) return false
const srcMs = await newestMtimeMs(path.join(dir, "src"))
return binStat.mtimeMs > srcMs
}

if (!force && (await fresh())) {
console.log(`Test CLI binary is up to date: ${builtBinary}`)
} else {
console.log(`Building test CLI binary for ${platform}-${arch}...`)
const start = Date.now()
await $`bun script/build.ts --single --skip-embed-web-ui --skip-install`
console.log(`Build complete in ${Date.now() - start}ms: ${builtBinary}`)
}

// Verify the binary exists and is executable before symlinking — catches
// a silently-failed build that left a stale or partial output behind.
await fs.access(builtBinary, fs.constants.X_OK).catch(() => {
throw new Error(`Built binary missing or not executable: ${builtBinary}`)
})

await fs.mkdir(path.dirname(stableBinary), { recursive: true })
await fs.rm(stableBinary, { force: true })
await fs.symlink(builtBinary, stableBinary)
console.log(`Symlinked stable path: ${stableBinary}`)
console.log(``)
console.log(`To use in tests:`)
console.log(` export OPENCODE_TEST_CLI_PATH="${stableBinary}"`)
console.log(` bun test test/cli/`)
4 changes: 3 additions & 1 deletion packages/opencode/test/cli/help/help-snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ describe("opencode CLI help-text snapshots", () => {
expect(normalize(result.stderr)).toMatchSnapshot(`opencode ${argv.join(" ")} --help`)
}
if (failures.length > 0) {
throw new Error(`Help text failed for:\n ${failures.join("\n ")}`)
// Keep the failure in the Effect channel — symmetric with the
// Effect.fail inside the partition above, not a defect.
yield* Effect.fail(new Error(`Help text failed for:\n ${failures.join("\n ")}`))
}
}),
180_000,
Expand Down
113 changes: 113 additions & 0 deletions packages/opencode/test/cli/smokes/read-only.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Tier-A smoke tests for read-only commands. Each test asserts only that the
// command exits 0 and produces *some* output in the isolated harness env.
//
// These are not behavioral tests — they're the cheapest possible signal that
// the dependency-layer wiring (config load, DB init, server boot, provider
// resolution) doesn't crash for the broad class of "no inputs, no side
// effects" commands. A regression in any shared layer (an Effect.fail that
// propagates out of a service constructor, a renamed env var, a broken DB
// migration) will fail one or more of these tests.
//
// If a future change should make one of these commands intentionally fail in
// an empty env, update the assertion + add a note explaining the new contract.
//
// Speed: each test pays ~1.5s for bun startup. 7 tests serialize within this
// file. Tracked as task #16 — investigate bun pre-warm if the suite grows.
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { cliIt } from "../../lib/cli-process"

describe("opencode read-only commands (smoke)", () => {
// `mcp list` reads MCP server config and pings each one. With the empty
// OPENCODE_CONFIG_CONTENT={} we provide, no servers should be configured
// and the command should report that cleanly.
cliIt.live(
"mcp list: exits 0",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["mcp", "list"])
opencode.expectExit(r, 0, "mcp list")
}),
60_000,
)

// `providers list` enumerates credentials + env-resolved providers.
// (Not config-injected ones — those don't appear here by design.) We just
// assert it produced the two section headers, which proves the resolver
// walked both sources without crashing.
cliIt.live(
"providers list: exits 0 and prints credential/environment sections",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["providers", "list"])
opencode.expectExit(r, 0, "providers list")
expect(r.stdout).toContain("Credentials")
expect(r.stdout).toContain("Environment")
}),
60_000,
)

// `models` lists models from configured providers. Our test/test-model
// should appear because it's wired into the test provider config.
cliIt.live(
"models: exits 0 and lists the test model",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["models"])
opencode.expectExit(r, 0, "models")
expect(r.stdout).toContain("test/test-model")
}),
60_000,
)

// `agent list` walks the agent config. Empty config means no agents
// configured; the command should still exit 0 with a "no agents" line or
// similar. We don't pin the message — just exit cleanly.
cliIt.live(
"agent list: exits 0",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["agent", "list"])
opencode.expectExit(r, 0, "agent list")
}),
60_000,
)

// `session list` reads the session DB. Fresh OPENCODE_TEST_HOME means
// empty DB. Exit 0 with no sessions.
cliIt.live(
"session list: exits 0",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["session", "list"])
opencode.expectExit(r, 0, "session list")
}),
60_000,
)

// `stats` aggregates token usage from the session DB. Empty DB → all zeros.
cliIt.live(
"stats: exits 0",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["stats"])
opencode.expectExit(r, 0, "stats")
}),
60_000,
)

// `db path` prints the DB file location. Under harness isolation the DB
// resolves to SQLite's `:memory:` (no on-disk pollution between tests);
// in production it'd be a path under OPENCODE_TEST_HOME / XDG_DATA_HOME.
// Accept either form — both prove the resolver ran without crashing.
cliIt.live(
"db path: exits 0 and prints a path or :memory:",
({ opencode }) =>
Effect.gen(function* () {
const r = yield* opencode.spawn(["db", "path"])
opencode.expectExit(r, 0, "db path")
expect(r.stdout.trim()).toMatch(/^(:memory:|[/\\].+\.(db|sqlite|sqlite3))$/i)
}),
60_000,
)
})
16 changes: 13 additions & 3 deletions packages/opencode/test/lib/cli-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ import { it } from "./effect"
const opencodeRoot = path.resolve(import.meta.dir, "../../")
const cliEntry = path.join(opencodeRoot, "src/index.ts")

// Argv prefix for spawning the CLI. If OPENCODE_TEST_CLI_PATH is set,
// subprocess tests spawn the pre-built binary directly (~3x speedup on
// isolation-env spawns that hit DB migration; produced by
// `bun script/prebuild-test-cli.ts`). Otherwise falls back to dev mode —
// strictly opt-in, default behavior unchanged.
const prebuiltCli = process.env["OPENCODE_TEST_CLI_PATH"]
const cliArgv: readonly string[] = prebuiltCli
? [prebuiltCli]
: ["bun", "run", "--conditions=browser", cliEntry]

export const testModelID = "test/test-model"

// Wrap a Bun subprocess pipe (or any ReadableStream<Uint8Array>) as a Stream.
Expand Down Expand Up @@ -196,7 +206,7 @@ export function withCliFixture<A, E>(
Effect.promise(async () => {
const start = Date.now()
// Process.run pipes stdout/stderr by default and returns them as Buffers.
const result = await Process.run(["bun", "run", "--conditions=browser", cliEntry, ...args], {
const result = await Process.run([...cliArgv, ...args], {
cwd: home,
timeout: opts?.timeoutMs ?? 30_000,
env: { ...process.env, ...env, ...opts?.env },
Expand Down Expand Up @@ -235,7 +245,7 @@ export function withCliFixture<A, E>(
// as a finalizer error during test teardown.
const proc = yield* Effect.acquireRelease(
Effect.sync(() =>
Bun.spawn(["bun", "run", "--conditions=browser", cliEntry, ...argv], {
Bun.spawn([...cliArgv, ...argv], {
cwd: home,
env: { ...process.env, ...env, ...opts?.env },
stdout: "pipe",
Expand Down Expand Up @@ -306,7 +316,7 @@ export function withCliFixture<A, E>(
// Either way we await proc.exited so the test scope doesn't leak.
const proc = yield* Effect.acquireRelease(
Effect.sync(() =>
Bun.spawn(["bun", "run", "--conditions=browser", cliEntry, ...argv], {
Bun.spawn([...cliArgv, ...argv], {
cwd: opts?.cwd ?? home,
env: { ...process.env, ...env, ...opts?.env },
stdin: "pipe",
Expand Down
Loading