test: make unsafe test coercion explicit
Move test coercion out of a hidden global and require each test to import the helper so review tools and runtime scripts can see the unsafe boundary. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { buildBackgroundTaskNotificationText } from "./background-task-notification-template"
|
||||
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||
|
||||
describe("buildBackgroundTaskNotificationText", () => {
|
||||
describe("#given one task still running after a completed task notification", () => {
|
||||
@@ -134,7 +135,7 @@ Use \`background_output(task_id="<id>")\` to retrieve each result.
|
||||
const notification = buildBackgroundTaskNotificationText({
|
||||
task: {
|
||||
id: "bg_abc123",
|
||||
description: testCoerce<string>(undefined),
|
||||
description: unsafeTestValue<string>(undefined),
|
||||
status: "completed",
|
||||
},
|
||||
duration: "5s",
|
||||
@@ -142,8 +143,8 @@ Use \`background_output(task_id="<id>")\` to retrieve each result.
|
||||
allComplete: true,
|
||||
remainingCount: 0,
|
||||
completedTasks: [
|
||||
{ id: "bg_abc123", description: testCoerce<string>(undefined), status: "completed" },
|
||||
{ id: "bg_def456", description: testCoerce<string>(undefined), status: "completed" },
|
||||
{ id: "bg_abc123", description: unsafeTestValue<string>(undefined), status: "completed" },
|
||||
{ id: "bg_def456", description: unsafeTestValue<string>(undefined), status: "completed" },
|
||||
],
|
||||
})
|
||||
|
||||
@@ -230,7 +231,7 @@ Use \`background_output(task_id="<id>")\` to retrieve each result.
|
||||
const notification = buildBackgroundTaskNotificationText({
|
||||
task: {
|
||||
id: "bg_xyz789",
|
||||
description: testCoerce<string>(undefined),
|
||||
description: unsafeTestValue<string>(undefined),
|
||||
status: "completed",
|
||||
},
|
||||
duration: "3s",
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
setCompactionAgentConfigCheckpoint,
|
||||
} from "../../shared/compaction-agent-config-checkpoint"
|
||||
import { getCompactionPartStorageDir } from "../../shared/compaction-marker"
|
||||
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||
|
||||
describe("isCompactionAgent", () => {
|
||||
describe("#given agent name variations", () => {
|
||||
@@ -49,7 +50,7 @@ describe("isCompactionAgent", () => {
|
||||
|
||||
test("returns false for null", () => {
|
||||
// when
|
||||
const result = isCompactionAgent(testCoerce<string>(null))
|
||||
const result = isCompactionAgent(unsafeTestValue<string>(null))
|
||||
|
||||
// then
|
||||
expect(result).toBe(false)
|
||||
|
||||
@@ -6,6 +6,7 @@ import { tmpdir } from "node:os"
|
||||
import type { BackgroundTaskConfig } from "../../config/schema"
|
||||
import { BackgroundManager } from "./manager"
|
||||
import type { BackgroundTask } from "./types"
|
||||
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||
|
||||
function createManager(config?: BackgroundTaskConfig): BackgroundManager {
|
||||
const client = {
|
||||
@@ -16,8 +17,8 @@ function createManager(config?: BackgroundTaskConfig): BackgroundManager {
|
||||
},
|
||||
}
|
||||
|
||||
const manager = new BackgroundManager({ pluginContext: testCoerce<PluginInput>({ client, directory: tmpdir() }), config: config })
|
||||
const testManager = testCoerce<{
|
||||
const manager = new BackgroundManager({ pluginContext: unsafeTestValue<PluginInput>({ client, directory: tmpdir() }), config: config })
|
||||
const testManager = unsafeTestValue<{
|
||||
enqueueNotificationForParent: (sessionId: string, fn: () => Promise<void>) => Promise<void>
|
||||
notifyParentSession: (task: BackgroundTask) => Promise<void>
|
||||
tasks: Map<string, BackgroundTask>
|
||||
@@ -32,7 +33,7 @@ function createManager(config?: BackgroundTaskConfig): BackgroundManager {
|
||||
}
|
||||
|
||||
function getTaskMap(manager: BackgroundManager): Map<string, BackgroundTask> {
|
||||
return (testCoerce<{ tasks: Map<string, BackgroundTask> }>(manager)).tasks
|
||||
return (unsafeTestValue<{ tasks: Map<string, BackgroundTask> }>(manager)).tasks
|
||||
}
|
||||
|
||||
async function flushAsyncWork() {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os"
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
|
||||
import { BackgroundManager } from "./manager"
|
||||
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||
|
||||
describe("BackgroundManager session permission", () => {
|
||||
test("passes query directory when loading the parent session", async () => {
|
||||
@@ -21,7 +22,7 @@ describe("BackgroundManager session permission", () => {
|
||||
},
|
||||
}
|
||||
const directory = tmpdir()
|
||||
const manager = new BackgroundManager({ pluginContext: testCoerce<PluginInput>({ client, directory }) })
|
||||
const manager = new BackgroundManager({ pluginContext: unsafeTestValue<PluginInput>({ client, directory }) })
|
||||
|
||||
// when
|
||||
await manager.launch({
|
||||
@@ -62,7 +63,7 @@ describe("BackgroundManager session permission", () => {
|
||||
abort: async () => ({}),
|
||||
},
|
||||
}
|
||||
const manager = new BackgroundManager({ pluginContext: testCoerce<PluginInput>({ client, directory: tmpdir() }) })
|
||||
const manager = new BackgroundManager({ pluginContext: unsafeTestValue<PluginInput>({ client, directory: tmpdir() }) })
|
||||
|
||||
// when
|
||||
await manager.launch({
|
||||
|
||||
@@ -2,12 +2,13 @@ import { describe, expect, mock, test } from "bun:test"
|
||||
|
||||
import type { OpencodeClient } from "./opencode-client"
|
||||
import { verifySessionExists } from "./session-existence"
|
||||
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||
|
||||
describe("verifySessionExists", () => {
|
||||
test("passes query directory to session lookup when provided", async () => {
|
||||
// given
|
||||
const get = mock(async () => ({ data: { id: "session-123" } }))
|
||||
const client = testCoerce<OpencodeClient>({
|
||||
const client = unsafeTestValue<OpencodeClient>({
|
||||
session: {
|
||||
get,
|
||||
},
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
DEFAULT_MAX_SUBAGENT_DEPTH,
|
||||
createSubagentDepthLimitError,
|
||||
} from "./subagent-spawn-limits"
|
||||
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||
|
||||
function createMockClient(sessionGet: OpencodeClient["session"]["get"]): OpencodeClient {
|
||||
return {
|
||||
@@ -20,7 +21,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
test("passes query.directory to each session.get call", async () => {
|
||||
// given
|
||||
const sessionGetCalls: Array<Record<string, unknown>> = []
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async (input) => {
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async (input) => {
|
||||
sessionGetCalls.push(input as Record<string, unknown>)
|
||||
if (input.path.id === "child-session") {
|
||||
return { data: { id: "child-session", parentID: "root-session" } }
|
||||
@@ -50,7 +51,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
describe("#given session.get returns an SDK error response", () => {
|
||||
test("throws a fail-closed spawn blocked error", async () => {
|
||||
// given
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async () => ({
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async () => ({
|
||||
error: "lookup failed",
|
||||
data: undefined,
|
||||
}))))
|
||||
@@ -66,7 +67,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
describe("#given session.get returns no session data", () => {
|
||||
test("throws a fail-closed spawn blocked error", async () => {
|
||||
// given
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async () => ({
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async () => ({
|
||||
data: undefined,
|
||||
}))))
|
||||
|
||||
@@ -81,7 +82,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
describe("depth calculation smoke tests (regression guard)", () => {
|
||||
test("root session (no parentID) reports depth 0 and childDepth 1", async () => {
|
||||
// given - a root session with no parent
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
if (opts.path.id === "root-session") {
|
||||
return { data: { id: "root-session", parentID: undefined } }
|
||||
}
|
||||
@@ -99,7 +100,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
|
||||
test("depth-1 child reports childDepth 2", async () => {
|
||||
// given - child -> root chain
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
if (opts.path.id === "child-1") {
|
||||
return { data: { id: "child-1", parentID: "root-session" } }
|
||||
}
|
||||
@@ -120,7 +121,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
|
||||
test("depth-2 grandchild reports childDepth 3", async () => {
|
||||
// given - grandchild -> child -> root chain
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const sessions: Record<string, { id: string; parentID?: string }> = {
|
||||
"grandchild": { id: "grandchild", parentID: "child" },
|
||||
"child": { id: "child", parentID: "root" },
|
||||
@@ -153,7 +154,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
}
|
||||
}
|
||||
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const session = sessions[opts.path.id]
|
||||
if (session) return { data: session }
|
||||
return { error: "not found", data: undefined }
|
||||
@@ -170,7 +171,7 @@ describe("resolveSubagentSpawnContext", () => {
|
||||
|
||||
test("detects parent cycle and throws", async () => {
|
||||
// given - A -> B -> A (cycle)
|
||||
const client = createMockClient(testCoerce<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const client = createMockClient(unsafeTestValue<OpencodeClient["session"]["get"]>((async (opts) => {
|
||||
const sessions: Record<string, { id: string; parentID?: string }> = {
|
||||
"session-a": { id: "session-a", parentID: "session-b" },
|
||||
"session-b": { id: "session-b", parentID: "session-a" },
|
||||
|
||||
Reference in New Issue
Block a user