2026-02-15 15:09:59 +09:00
|
|
|
/// <reference types="bun-types" />
|
|
|
|
|
|
2026-02-10 19:20:59 +09:00
|
|
|
import { describe, test, expect, mock } from "bun:test"
|
|
|
|
|
import type { BackgroundManager } from "../../features/background-agent"
|
2026-02-15 15:09:59 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
2026-02-10 19:20:59 +09:00
|
|
|
import { createBackgroundTask } from "./create-background-task"
|
2026-05-12 15:38:31 +09:00
|
|
|
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
2026-02-10 19:20:59 +09:00
|
|
|
|
|
|
|
|
describe("createBackgroundTask", () => {
|
2026-03-31 15:11:05 -07:00
|
|
|
const launchMock = mock(async (): Promise<{
|
|
|
|
|
id: string
|
2026-05-02 03:01:03 +09:00
|
|
|
sessionId: string | null
|
2026-03-31 15:11:05 -07:00
|
|
|
description: string
|
|
|
|
|
agent: string
|
|
|
|
|
status: string
|
|
|
|
|
}> => ({
|
2026-02-15 15:09:59 +09:00
|
|
|
id: "test-task-id",
|
2026-05-02 03:01:03 +09:00
|
|
|
sessionId: null,
|
2026-02-15 15:09:59 +09:00
|
|
|
description: "Test task",
|
|
|
|
|
agent: "test-agent",
|
|
|
|
|
status: "pending",
|
|
|
|
|
}))
|
|
|
|
|
const getTaskMock = mock()
|
|
|
|
|
|
2026-05-12 15:38:31 +09:00
|
|
|
const mockManager = unsafeTestValue<BackgroundManager>({
|
2026-02-15 15:09:59 +09:00
|
|
|
launch: launchMock,
|
|
|
|
|
getTask: getTaskMock,
|
2026-05-12 14:23:05 +09:00
|
|
|
})
|
2026-02-10 19:20:59 +09:00
|
|
|
|
2026-05-12 15:38:31 +09:00
|
|
|
const mockClient = unsafeTestValue<PluginInput["client"]>({
|
2026-02-15 15:09:59 +09:00
|
|
|
session: {
|
|
|
|
|
messages: mock(() => Promise.resolve({ data: [] })),
|
|
|
|
|
},
|
2026-05-12 14:23:05 +09:00
|
|
|
})
|
2026-02-15 15:09:59 +09:00
|
|
|
|
|
|
|
|
const tool = createBackgroundTask(mockManager, mockClient)
|
2026-02-10 19:20:59 +09:00
|
|
|
|
|
|
|
|
const testContext = {
|
|
|
|
|
sessionID: "test-session",
|
|
|
|
|
messageID: "test-message",
|
|
|
|
|
agent: "test-agent",
|
2026-03-31 15:11:05 -07:00
|
|
|
directory: "/Users/yeongyu/local-workspaces/omo",
|
|
|
|
|
worktree: "/Users/yeongyu/local-workspaces/omo",
|
2026-02-10 19:20:59 +09:00
|
|
|
abort: new AbortController().signal,
|
2026-03-31 15:11:05 -07:00
|
|
|
metadata: () => {},
|
|
|
|
|
ask: async () => {},
|
2026-02-10 19:20:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const testArgs = {
|
|
|
|
|
description: "Test background task",
|
|
|
|
|
prompt: "Test prompt",
|
|
|
|
|
agent: "test-agent",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test("detects interrupted task as failure", async () => {
|
|
|
|
|
//#given
|
2026-02-15 15:09:59 +09:00
|
|
|
launchMock.mockResolvedValueOnce({
|
2026-02-10 19:20:59 +09:00
|
|
|
id: "test-task-id",
|
2026-05-02 03:01:03 +09:00
|
|
|
sessionId: null,
|
2026-02-10 19:20:59 +09:00
|
|
|
description: "Test task",
|
|
|
|
|
agent: "test-agent",
|
|
|
|
|
status: "pending",
|
|
|
|
|
})
|
2026-02-15 15:09:59 +09:00
|
|
|
getTaskMock.mockReturnValueOnce({
|
2026-02-10 19:20:59 +09:00
|
|
|
id: "test-task-id",
|
2026-05-02 03:01:03 +09:00
|
|
|
sessionId: null,
|
2026-02-10 19:20:59 +09:00
|
|
|
description: "Test task",
|
|
|
|
|
agent: "test-agent",
|
|
|
|
|
status: "interrupt",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//#when
|
|
|
|
|
const result = await tool.execute(testArgs, testContext)
|
|
|
|
|
|
|
|
|
|
//#then
|
|
|
|
|
expect(result).toContain("Task entered error state")
|
|
|
|
|
expect(result).toContain("test-task-id")
|
|
|
|
|
})
|
2026-03-31 15:11:05 -07:00
|
|
|
|
|
|
|
|
test("keeps launched background task alive when parent aborts before session id resolves", async () => {
|
|
|
|
|
//#given - background launch should survive parent abort during session-id wait
|
|
|
|
|
const abortController = new AbortController()
|
|
|
|
|
launchMock.mockResolvedValueOnce({
|
|
|
|
|
id: "test-task-id",
|
2026-05-02 03:01:03 +09:00
|
|
|
sessionId: null,
|
2026-03-31 15:11:05 -07:00
|
|
|
description: "Test task",
|
|
|
|
|
agent: "test-agent",
|
|
|
|
|
status: "pending",
|
|
|
|
|
})
|
|
|
|
|
getTaskMock.mockImplementationOnce(() => {
|
|
|
|
|
abortController.abort()
|
|
|
|
|
return {
|
|
|
|
|
id: "test-task-id",
|
2026-05-02 03:01:03 +09:00
|
|
|
sessionId: null,
|
2026-03-31 15:11:05 -07:00
|
|
|
description: "Test task",
|
|
|
|
|
agent: "test-agent",
|
|
|
|
|
status: "pending",
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//#when
|
|
|
|
|
const result = await tool.execute(testArgs, {
|
|
|
|
|
...testContext,
|
|
|
|
|
abort: abortController.signal,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//#then - tool should still report successful launch instead of cancelling child task
|
|
|
|
|
expect(result).toContain("Background task launched successfully.")
|
|
|
|
|
expect(result).toContain("Task ID: test-task-id")
|
|
|
|
|
expect(result).not.toContain("Task aborted and cancelled while waiting for session to start")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test("keeps sibling background task alive when two tasks start concurrently", async () => {
|
|
|
|
|
//#given - one aborted parent call should not interrupt a sibling launch from the same parent session
|
|
|
|
|
const firstAbortController = new AbortController()
|
|
|
|
|
const secondAbortController = new AbortController()
|
|
|
|
|
const states = new Map([
|
2026-05-02 03:01:03 +09:00
|
|
|
["task-1", { reads: 0, abortOnFirstRead: true, sessionId: "ses-1" }],
|
|
|
|
|
["task-2", { reads: 0, abortOnFirstRead: false, sessionId: "ses-2" }],
|
2026-03-31 15:11:05 -07:00
|
|
|
])
|
|
|
|
|
let launchCount = 0
|
|
|
|
|
launchMock.mockImplementation(async () => {
|
|
|
|
|
launchCount += 1
|
|
|
|
|
return launchCount === 1
|
2026-05-02 03:01:03 +09:00
|
|
|
? { id: "task-1", sessionId: null, description: "Task 1", agent: "test-agent", status: "pending" }
|
|
|
|
|
: { id: "task-2", sessionId: null, description: "Task 2", agent: "test-agent", status: "pending" }
|
2026-03-31 15:11:05 -07:00
|
|
|
})
|
|
|
|
|
getTaskMock.mockImplementation((taskID: string) => {
|
|
|
|
|
const state = states.get(taskID)
|
|
|
|
|
if (!state) return undefined
|
|
|
|
|
state.reads += 1
|
|
|
|
|
if (state.abortOnFirstRead && state.reads === 1) {
|
|
|
|
|
firstAbortController.abort()
|
|
|
|
|
}
|
|
|
|
|
return state.reads >= 2
|
2026-05-02 03:01:03 +09:00
|
|
|
? { id: taskID, sessionId: state.sessionId, description: "Task", agent: "test-agent", status: "pending" }
|
|
|
|
|
: { id: taskID, sessionId: null, description: "Task", agent: "test-agent", status: "pending" }
|
2026-03-31 15:11:05 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//#when
|
|
|
|
|
const [firstResult, secondResult] = await Promise.all([
|
|
|
|
|
tool.execute(testArgs, {
|
|
|
|
|
...testContext,
|
|
|
|
|
abort: firstAbortController.signal,
|
|
|
|
|
}),
|
|
|
|
|
tool.execute(testArgs, {
|
|
|
|
|
...testContext,
|
|
|
|
|
abort: secondAbortController.signal,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
//#then - both launches still succeed and the sibling is not marked interrupted
|
|
|
|
|
expect(firstResult).toContain("Background task launched successfully.")
|
|
|
|
|
expect(secondResult).toContain("Background task launched successfully.")
|
|
|
|
|
expect(secondResult).toContain("Task ID: task-2")
|
|
|
|
|
expect(secondResult).not.toContain("interrupt")
|
|
|
|
|
})
|
2026-02-15 15:09:59 +09:00
|
|
|
})
|