Skip to content

Commit 9d513e1

Browse files
committed
test(plugin): add tests for debug logging when OPENCODER_DEBUG is set
Signed-off-by: leocavalcante <[email protected]>
1 parent dda159f commit 9d513e1

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

tests/plugin.test.ts

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "bun:test"
1+
import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test"
22
import type { PluginInput } from "@opencode-ai/plugin"
33
import type { Event } from "@opencode-ai/sdk"
44
import { OpenCoderPlugin } from "../src/plugin"
@@ -67,3 +67,138 @@ describe("OpenCoderPlugin", () => {
6767
await expect(result["tool.execute.after"]?.(input, output)).resolves.toBeUndefined()
6868
})
6969
})
70+
71+
describe("OpenCoderPlugin debug logging", () => {
72+
const createMockContext = () =>
73+
({
74+
project: {},
75+
client: {},
76+
$: () => {},
77+
directory: "/tmp/test-project",
78+
worktree: "/tmp/test-project",
79+
serverUrl: new URL("http://localhost:3000"),
80+
}) as unknown as PluginInput
81+
82+
let originalDebug: string | undefined
83+
let consoleLogSpy: ReturnType<typeof spyOn>
84+
let logCalls: unknown[][]
85+
86+
beforeEach(() => {
87+
originalDebug = process.env.OPENCODER_DEBUG
88+
logCalls = []
89+
consoleLogSpy = spyOn(console, "log").mockImplementation((...args: unknown[]) => {
90+
logCalls.push(args)
91+
})
92+
})
93+
94+
afterEach(() => {
95+
if (originalDebug === undefined) {
96+
delete process.env.OPENCODER_DEBUG
97+
} else {
98+
process.env.OPENCODER_DEBUG = originalDebug
99+
}
100+
consoleLogSpy.mockRestore()
101+
})
102+
103+
it("should log event when OPENCODER_DEBUG=1", async () => {
104+
process.env.OPENCODER_DEBUG = "1"
105+
const result = await OpenCoderPlugin(createMockContext())
106+
107+
const mockEvent: Event = {
108+
type: "session.idle",
109+
properties: { sessionID: "test-123" },
110+
}
111+
await result.event?.({ event: mockEvent })
112+
113+
expect(logCalls.length).toBe(1)
114+
const [prefix, message, jsonStr] = logCalls[0] as [string, string, string]
115+
expect(prefix).toMatch(/^\[\d{4}-\d{2}-\d{2}T.*\] \[opencoder\]$/)
116+
expect(message).toBe("Event received")
117+
const parsed = JSON.parse(jsonStr)
118+
expect(parsed.type).toBe("session.idle")
119+
expect(parsed.properties).toEqual(["sessionID"])
120+
expect(parsed.directory).toBe("/tmp/test-project")
121+
})
122+
123+
it("should log tool.execute.before when OPENCODER_DEBUG=1", async () => {
124+
process.env.OPENCODER_DEBUG = "1"
125+
const result = await OpenCoderPlugin(createMockContext())
126+
127+
const input = { tool: "bash", sessionID: "test-123", callID: "call-456" }
128+
const output = { args: { command: "ls", workdir: "/tmp" } }
129+
await result["tool.execute.before"]?.(input, output)
130+
131+
expect(logCalls.length).toBe(1)
132+
const [prefix, message, jsonStr] = logCalls[0] as [string, string, string]
133+
expect(prefix).toMatch(/^\[\d{4}-\d{2}-\d{2}T.*\] \[opencoder\]$/)
134+
expect(message).toBe("Tool executing")
135+
const parsed = JSON.parse(jsonStr)
136+
expect(parsed.tool).toBe("bash")
137+
expect(parsed.sessionID).toBe("test-123")
138+
expect(parsed.callID).toBe("call-456")
139+
expect(parsed.argsKeys).toEqual(["command", "workdir"])
140+
expect(parsed.directory).toBe("/tmp/test-project")
141+
})
142+
143+
it("should log tool.execute.after when OPENCODER_DEBUG=1", async () => {
144+
process.env.OPENCODER_DEBUG = "1"
145+
const result = await OpenCoderPlugin(createMockContext())
146+
147+
const input = { tool: "bash", sessionID: "test-123", callID: "call-456" }
148+
const output = { title: "Command executed", output: "file1.txt\nfile2.txt", metadata: {} }
149+
await result["tool.execute.after"]?.(input, output)
150+
151+
expect(logCalls.length).toBe(1)
152+
const [prefix, message, jsonStr] = logCalls[0] as [string, string, string]
153+
expect(prefix).toMatch(/^\[\d{4}-\d{2}-\d{2}T.*\] \[opencoder\]$/)
154+
expect(message).toBe("Tool completed")
155+
const parsed = JSON.parse(jsonStr)
156+
expect(parsed.tool).toBe("bash")
157+
expect(parsed.sessionID).toBe("test-123")
158+
expect(parsed.callID).toBe("call-456")
159+
expect(parsed.title).toBe("Command executed")
160+
expect(parsed.outputLength).toBe(19) // "file1.txt\nfile2.txt".length
161+
expect(parsed.directory).toBe("/tmp/test-project")
162+
})
163+
164+
it("should handle tool.execute.after with undefined output", async () => {
165+
process.env.OPENCODER_DEBUG = "1"
166+
const result = await OpenCoderPlugin(createMockContext())
167+
168+
const input = { tool: "bash", sessionID: "test-123", callID: "call-456" }
169+
// biome-ignore lint/suspicious/noExplicitAny: Testing edge case with undefined output
170+
const output = { title: "Command executed", output: undefined, metadata: {} } as any
171+
await result["tool.execute.after"]?.(input, output)
172+
173+
expect(logCalls.length).toBe(1)
174+
const [, , jsonStr] = logCalls[0] as [string, string, string]
175+
const parsed = JSON.parse(jsonStr)
176+
expect(parsed.outputLength).toBe(0)
177+
})
178+
179+
it("should not log when OPENCODER_DEBUG is not set", async () => {
180+
delete process.env.OPENCODER_DEBUG
181+
const result = await OpenCoderPlugin(createMockContext())
182+
183+
const mockEvent: Event = {
184+
type: "session.idle",
185+
properties: { sessionID: "test-123" },
186+
}
187+
await result.event?.({ event: mockEvent })
188+
189+
expect(logCalls.length).toBe(0)
190+
})
191+
192+
it("should not log when OPENCODER_DEBUG is set to other values", async () => {
193+
process.env.OPENCODER_DEBUG = "true"
194+
const result = await OpenCoderPlugin(createMockContext())
195+
196+
const mockEvent: Event = {
197+
type: "session.idle",
198+
properties: { sessionID: "test-123" },
199+
}
200+
await result.event?.({ event: mockEvent })
201+
202+
expect(logCalls.length).toBe(0)
203+
})
204+
})

0 commit comments

Comments
 (0)