test(core): update main test setup and core tests
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
+113
-62
@@ -1,8 +1,12 @@
|
||||
/// <reference types="bun-types" />
|
||||
|
||||
import { afterAll, beforeEach, describe, expect, it, mock, spyOn } from "bun:test"
|
||||
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test"
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
|
||||
import { OhMyOpenCodeConfigSchema } from "./config/schema/oh-my-opencode-config"
|
||||
import { createManagers } from "./create-managers"
|
||||
import * as openclawRuntimeDispatch from "./openclaw/runtime-dispatch"
|
||||
import { createModelCacheState } from "./plugin-state"
|
||||
|
||||
const markServerRunningInProcess = mock(() => {})
|
||||
let backgroundManagerOptions: {
|
||||
@@ -10,57 +14,63 @@ let backgroundManagerOptions: {
|
||||
} | null = null
|
||||
const trackedPaneBySession = new Map<string, string>()
|
||||
|
||||
mock.module("./features/background-agent", () => ({
|
||||
BackgroundManager: class BackgroundManager {
|
||||
constructor(_ctx: unknown, _config: unknown, options: typeof backgroundManagerOptions) {
|
||||
backgroundManagerOptions = options
|
||||
class MockBackgroundManager {
|
||||
constructor(
|
||||
_ctx: PluginInput,
|
||||
_config?: unknown,
|
||||
options?: {
|
||||
tmuxConfig?: unknown
|
||||
onSubagentSessionCreated?: (event: { sessionID: string; parentID: string; title: string }) => Promise<void>
|
||||
onShutdown?: () => void | Promise<void>
|
||||
enableParentSessionNotifications?: boolean
|
||||
},
|
||||
) {
|
||||
backgroundManagerOptions = options ?? null
|
||||
}
|
||||
}
|
||||
|
||||
class MockSkillMcpManager {
|
||||
constructor(..._args: unknown[]) {}
|
||||
}
|
||||
|
||||
class MockTmuxSessionManager {
|
||||
constructor(_ctx: PluginInput, _config: unknown) {}
|
||||
|
||||
async cleanup(): Promise<void> {}
|
||||
|
||||
async onSessionCreated(event: { properties?: { info?: { id?: string } } }): Promise<void> {
|
||||
const sessionID = event.properties?.info?.id
|
||||
if (sessionID) {
|
||||
trackedPaneBySession.set(sessionID, `%pane-${sessionID}`)
|
||||
}
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
mock.module("./features/skill-mcp-manager", () => ({
|
||||
SkillMcpManager: class SkillMcpManager {
|
||||
constructor(..._args: unknown[]) {}
|
||||
},
|
||||
}))
|
||||
getTrackedPaneId(sessionID: string): string | undefined {
|
||||
return trackedPaneBySession.get(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
mock.module("./features/task-toast-manager", () => ({
|
||||
initTaskToastManager: mock(() => {}),
|
||||
}))
|
||||
function createConfigHandler(): ReturnType<typeof import("./plugin-handlers").createConfigHandler> {
|
||||
return async () => {}
|
||||
}
|
||||
|
||||
mock.module("./features/tmux-subagent", () => ({
|
||||
TmuxSessionManager: class TmuxSessionManager {
|
||||
constructor(..._args: unknown[]) {}
|
||||
function initTaskToastManager(): ReturnType<typeof import("./features/task-toast-manager").initTaskToastManager> {
|
||||
return {} as ReturnType<typeof import("./features/task-toast-manager").initTaskToastManager>
|
||||
}
|
||||
|
||||
async cleanup(): Promise<void> {}
|
||||
async onSessionCreated(event: { properties?: { info?: { id?: string } } }): Promise<void> {
|
||||
const sessionID = event.properties?.info?.id
|
||||
if (sessionID) {
|
||||
trackedPaneBySession.set(sessionID, `%pane-${sessionID}`)
|
||||
}
|
||||
}
|
||||
function registerManagerForCleanup(): void {}
|
||||
|
||||
getTrackedPaneId(sessionID: string): string | undefined {
|
||||
return trackedPaneBySession.get(sessionID)
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
||||
mock.module("./features/background-agent/process-cleanup", () => ({
|
||||
registerManagerForCleanup: mock(() => {}),
|
||||
}))
|
||||
|
||||
mock.module("./plugin-handlers", () => ({
|
||||
createConfigHandler: mock(() => ({ kind: "config-handler" })),
|
||||
}))
|
||||
|
||||
mock.module("./shared/tmux/tmux-utils/server-health", () => ({
|
||||
isServerRunning: mock(async () => true),
|
||||
markServerRunningInProcess,
|
||||
resetServerCheck: mock(() => {}),
|
||||
}))
|
||||
|
||||
const { createManagers } = await import("./create-managers")
|
||||
function createDeps(): NonNullable<Parameters<typeof createManagers>[0]["deps"]> {
|
||||
return {
|
||||
BackgroundManagerClass: MockBackgroundManager as typeof import("./features/background-agent").BackgroundManager,
|
||||
SkillMcpManagerClass: MockSkillMcpManager as typeof import("./features/skill-mcp-manager").SkillMcpManager,
|
||||
TmuxSessionManagerClass: MockTmuxSessionManager as typeof import("./features/tmux-subagent").TmuxSessionManager,
|
||||
initTaskToastManagerFn: initTaskToastManager,
|
||||
registerManagerForCleanupFn: registerManagerForCleanup,
|
||||
createConfigHandlerFn: createConfigHandler,
|
||||
markServerRunningInProcessFn: markServerRunningInProcess,
|
||||
}
|
||||
}
|
||||
|
||||
function createTmuxConfig(enabled: boolean) {
|
||||
return {
|
||||
@@ -73,28 +83,67 @@ function createTmuxConfig(enabled: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
function createContext(directory: string): PluginInput {
|
||||
const shell = Object.assign(
|
||||
() => {
|
||||
throw new Error("shell should not be called in this test")
|
||||
},
|
||||
{
|
||||
braces: () => [],
|
||||
escape: (input: string) => input,
|
||||
env() {
|
||||
return shell
|
||||
},
|
||||
cwd() {
|
||||
return shell
|
||||
},
|
||||
nothrow() {
|
||||
return shell
|
||||
},
|
||||
throws() {
|
||||
return shell
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
project: {
|
||||
id: "project-id",
|
||||
worktree: directory,
|
||||
time: { created: Date.now() },
|
||||
},
|
||||
directory,
|
||||
worktree: directory,
|
||||
serverUrl: new URL("http://localhost:4096"),
|
||||
$: shell,
|
||||
client: {} as PluginInput["client"],
|
||||
}
|
||||
}
|
||||
|
||||
describe("createManagers", () => {
|
||||
const dispatchOpenClawEvent = spyOn(openclawRuntimeDispatch, "dispatchOpenClawEvent")
|
||||
let dispatchOpenClawEvent: ReturnType<typeof spyOn>
|
||||
|
||||
beforeEach(() => {
|
||||
dispatchOpenClawEvent = spyOn(openclawRuntimeDispatch, "dispatchOpenClawEvent")
|
||||
markServerRunningInProcess.mockClear()
|
||||
dispatchOpenClawEvent.mockReset()
|
||||
backgroundManagerOptions = null
|
||||
trackedPaneBySession.clear()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
mock.restore()
|
||||
afterEach(() => {
|
||||
dispatchOpenClawEvent.mockRestore()
|
||||
})
|
||||
|
||||
it("#given tmux integration is disabled #when managers are created #then it does not mark the tmux server as running", () => {
|
||||
const args = {
|
||||
ctx: { directory: "/tmp", client: {} },
|
||||
pluginConfig: {},
|
||||
ctx: createContext("/tmp"),
|
||||
pluginConfig: OhMyOpenCodeConfigSchema.parse({}),
|
||||
tmuxConfig: createTmuxConfig(false),
|
||||
modelCacheState: {},
|
||||
modelCacheState: createModelCacheState(),
|
||||
backgroundNotificationHookEnabled: false,
|
||||
} as Parameters<typeof createManagers>[0]
|
||||
deps: createDeps(),
|
||||
}
|
||||
|
||||
createManagers(args)
|
||||
|
||||
@@ -103,12 +152,13 @@ describe("createManagers", () => {
|
||||
|
||||
it("#given tmux integration is enabled #when managers are created #then it marks the tmux server as running", () => {
|
||||
const args = {
|
||||
ctx: { directory: "/tmp", client: {} },
|
||||
pluginConfig: {},
|
||||
ctx: createContext("/tmp"),
|
||||
pluginConfig: OhMyOpenCodeConfigSchema.parse({}),
|
||||
tmuxConfig: createTmuxConfig(true),
|
||||
modelCacheState: {},
|
||||
modelCacheState: createModelCacheState(),
|
||||
backgroundNotificationHookEnabled: false,
|
||||
} as Parameters<typeof createManagers>[0]
|
||||
deps: createDeps(),
|
||||
}
|
||||
|
||||
createManagers(args)
|
||||
|
||||
@@ -117,18 +167,19 @@ describe("createManagers", () => {
|
||||
|
||||
it("#given openclaw is enabled #when the background session-created callback runs #then it dispatches openclaw with the tracked pane id", async () => {
|
||||
const args = {
|
||||
ctx: { directory: "/tmp/project", client: {} },
|
||||
pluginConfig: {
|
||||
ctx: createContext("/tmp/project"),
|
||||
pluginConfig: OhMyOpenCodeConfigSchema.parse({
|
||||
openclaw: {
|
||||
enabled: true,
|
||||
gateways: {},
|
||||
hooks: {},
|
||||
},
|
||||
},
|
||||
}),
|
||||
tmuxConfig: createTmuxConfig(true),
|
||||
modelCacheState: {},
|
||||
modelCacheState: createModelCacheState(),
|
||||
backgroundNotificationHookEnabled: false,
|
||||
} as Parameters<typeof createManagers>[0]
|
||||
deps: createDeps(),
|
||||
}
|
||||
|
||||
createManagers(args)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user