Files
oh-my-opencode/src/hooks/task-resume-info/index.test.ts
T
YeonGyu-Kim f64a7112bf fix: guard output.output in tool after-hooks for MCP tools (#1720)
MCP tool responses can have undefined output.output, causing TypeError
crashes in tool.execute.after hooks.

Changes:
- comment-checker/hook.ts: guard output.output with ?? '' before toLowerCase()
- edit-error-recovery/hook.ts: guard output.output with ?? '' before toLowerCase()
- task-resume-info/hook.ts: extract output.output ?? '' into outputText before all string operations
- Added tests for undefined output.output in edit-error-recovery and task-resume-info
2026-02-11 15:49:56 +09:00

102 lines
2.8 KiB
TypeScript

import { describe, it, expect } from "bun:test"
import { createTaskResumeInfoHook } from "./index"
describe("createTaskResumeInfoHook", () => {
const hook = createTaskResumeInfoHook()
const afterHook = hook["tool.execute.after"]
const createInput = (tool: string) => ({
tool,
sessionID: "test-session",
callID: "test-call-id",
})
describe("#given MCP tool with undefined output.output", () => {
describe("#when tool.execute.after is called", () => {
it("#then should not crash", async () => {
const input = createInput("task")
const output = {
title: "delegate_task",
output: undefined as unknown as string,
metadata: {},
}
await afterHook(input, output)
expect(output.output).toBeUndefined()
})
})
})
describe("#given non-target tool", () => {
describe("#when tool is not in TARGET_TOOLS", () => {
it("#then should not modify output", async () => {
const input = createInput("Read")
const output = {
title: "Read",
output: "some output",
metadata: {},
}
await afterHook(input, output)
expect(output.output).toBe("some output")
})
})
})
describe("#given target tool with session ID in output", () => {
describe("#when output contains a session ID", () => {
it("#then should append resume info", async () => {
const input = createInput("call_omo_agent")
const output = {
title: "delegate_task",
output: "Task completed.\nSession ID: ses_abc123",
metadata: {},
}
await afterHook(input, output)
expect(output.output).toContain("to continue:")
expect(output.output).toContain("ses_abc123")
})
})
})
describe("#given target tool with error output", () => {
describe("#when output starts with Error:", () => {
it("#then should not modify output", async () => {
const input = createInput("task")
const output = {
title: "task",
output: "Error: something went wrong",
metadata: {},
}
await afterHook(input, output)
expect(output.output).toBe("Error: something went wrong")
})
})
})
describe("#given target tool with already-continued output", () => {
describe("#when output already contains continuation info", () => {
it("#then should not add duplicate", async () => {
const input = createInput("task")
const output = {
title: "task",
output:
'Done.\nSession ID: ses_abc123\nto continue: task(session_id="ses_abc123", prompt="...")',
metadata: {},
}
await afterHook(input, output)
const matches = output.output.match(/to continue:/g)
expect(matches?.length).toBe(1)
})
})
})
})