test(events): use baseline snapshot pattern for console spy isolation

Replace exact call count assertions with delta-based checks:
- capture errorSpy.mock.calls.length before processing events
- slice to only check calls made during this test's execution
- use try/finally to guarantee mockRestore() even on assertion failure

This prevents test pollution from cross-file spy leakage in CI batch runs.
This commit is contained in:
YeonGyu-Kim
2026-02-21 05:08:58 +09:00
parent 70b814a852
commit 165c8122f6

View File

@@ -1,4 +1,4 @@
import { describe, it, expect, spyOn } from "bun:test"
import { afterEach, beforeEach, describe, it, expect, spyOn } from "bun:test"
import { createEventState, processEvents, serializeError, type EventState } from "./events"
import type { RunContext, EventPayload } from "./types"
@@ -100,12 +100,21 @@ describe("event handling", () => {
const events = toAsyncIterable([payload])
// when
await processEvents(ctx, events, state)
const baselineCallCount = errorSpy.mock.calls.length
// then
expect(errorSpy).not.toHaveBeenCalled()
errorSpy.mockRestore()
try {
// when
await processEvents(ctx, events, state)
// then
const newCalls = errorSpy.mock.calls.slice(baselineCallCount)
const hasEventTrace = newCalls.some((call) =>
String(call?.[0] ?? "").includes("custom.event"),
)
expect(hasEventTrace).toBe(false)
} finally {
errorSpy.mockRestore()
}
})
it("logs full event traces when verbose is enabled", async () => {
@@ -121,14 +130,21 @@ describe("event handling", () => {
const events = toAsyncIterable([payload])
// when
await processEvents(ctx, events, state)
const baselineCallCount = errorSpy.mock.calls.length
// then
expect(errorSpy).toHaveBeenCalledTimes(1)
const firstCall = errorSpy.mock.calls[0]
expect(String(firstCall?.[0] ?? "")).toContain("custom.event")
errorSpy.mockRestore()
try {
// when
await processEvents(ctx, events, state)
// then
const newCalls = errorSpy.mock.calls.slice(baselineCallCount)
const hasEventTrace = newCalls.some((call) =>
String(call?.[0] ?? "").includes("custom.event"),
)
expect(hasEventTrace).toBe(true)
} finally {
errorSpy.mockRestore()
}
})
it("session.idle sets mainSessionIdle to true for matching session", async () => {