test(hooks): restore module mock cleanup

This commit is contained in:
YeonGyu-Kim
2026-05-30 15:35:40 +09:00
parent fa3e3de9e6
commit 08f4a4ac50
8 changed files with 38 additions and 9 deletions
@@ -1,5 +1,5 @@
/// <reference types="bun-types" /> /// <reference types="bun-types" />
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test" import { afterAll, afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
import type { AutoCompactState } from "./types" import type { AutoCompactState } from "./types"
@@ -38,6 +38,10 @@ mock.module("../../features/hook-message-injector", () => ({
import { _resetForTesting as resetSessionState, updateSessionAgent } from "../../features/claude-code-session-state/state" import { _resetForTesting as resetSessionState, updateSessionAgent } from "../../features/claude-code-session-state/state"
import { runAggressiveTruncationStrategy } from "./aggressive-truncation-strategy" import { runAggressiveTruncationStrategy } from "./aggressive-truncation-strategy"
afterAll(() => {
mock.restore()
})
type FakeClient = { type FakeClient = {
session: { session: {
promptAsync: (input: PromptAsyncCall) => Promise<unknown> promptAsync: (input: PromptAsyncCall) => Promise<unknown>
@@ -18,8 +18,8 @@ mock.module("./tool-result-storage", () => ({
truncateToolResult, truncateToolResult,
})) }))
async function importFreshStorage(): Promise<typeof import("./storage")> { async function importFreshStorage(): Promise<typeof import("./target-token-truncation")> {
return import(`./storage?test=${Date.now()}-${Math.random()}`) return import(`./target-token-truncation?test=${Date.now()}-${Math.random()}`)
} }
afterAll(() => { afterAll(() => {
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test" import { afterAll, afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs" import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os" import { tmpdir } from "node:os"
import { join } from "node:path" import { join } from "node:path"
@@ -16,6 +16,10 @@ mock.module("../../shared/opencode-storage-detection", () => ({
resetSqliteBackendCache: () => {}, resetSqliteBackendCache: () => {},
})) }))
afterAll(() => {
mock.restore()
})
type LongTimerCallback = (...args: unknown[]) => void | Promise<void> type LongTimerCallback = (...args: unknown[]) => void | Promise<void>
describe("atlas background task retry", () => { describe("atlas background task retry", () => {
+6 -1
View File
@@ -1,5 +1,5 @@
import type { PluginInput } from "@opencode-ai/plugin" import type { PluginInput } from "@opencode-ai/plugin"
import { describe, expect, mock, test } from "bun:test" import { afterAll, describe, expect, mock, test } from "bun:test"
type CreateAutoUpdateCheckerHook = typeof import("./hook").createAutoUpdateCheckerHook type CreateAutoUpdateCheckerHook = typeof import("./hook").createAutoUpdateCheckerHook
type HookOptions = Parameters<CreateAutoUpdateCheckerHook>[1] type HookOptions = Parameters<CreateAutoUpdateCheckerHook>[1]
@@ -34,6 +34,10 @@ mock.module("./hook/deferred-startup-check", () => ({
scheduleDeferredStartupCheck: scheduleDeferredStartupCheckMock, scheduleDeferredStartupCheck: scheduleDeferredStartupCheckMock,
})) }))
afterAll(() => {
mock.restore()
})
const createPluginInput = (): PluginInput => ({ const createPluginInput = (): PluginInput => ({
client: {} as PluginInput["client"], client: {} as PluginInput["client"],
directory: "/tmp/project", directory: "/tmp/project",
@@ -41,6 +45,7 @@ const createPluginInput = (): PluginInput => ({
worktree: "/tmp/project", worktree: "/tmp/project",
serverUrl: new URL("https://example.com"), serverUrl: new URL("https://example.com"),
$: {} as PluginInput["$"], $: {} as PluginInput["$"],
experimental_workspace: { register: () => {} },
} satisfies PluginInput) } satisfies PluginInput)
const createDeps = (overrides: Partial<HookDeps> = {}) => { const createDeps = (overrides: Partial<HookDeps> = {}) => {
@@ -1,4 +1,4 @@
const { afterEach, beforeEach, describe, expect, mock, test } = require("bun:test") const { afterAll, afterEach, beforeEach, describe, expect, mock, test } = require("bun:test")
const capturedOptions: Array<Record<string, unknown>> = [] const capturedOptions: Array<Record<string, unknown>> = []
@@ -19,6 +19,10 @@ mock.module("./execute-http-hook", () => ({
const { dispatchHook } = await import("./dispatch-hook") const { dispatchHook } = await import("./dispatch-hook")
afterAll(() => {
mock.restore()
})
describe("dispatchHook", () => { describe("dispatchHook", () => {
beforeEach(() => { beforeEach(() => {
mockExecuteHookCommand.mockClear() mockExecuteHookCommand.mockClear()
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test" import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs" import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os" import { tmpdir } from "node:os"
import { join } from "node:path" import { join } from "node:path"
@@ -14,6 +14,10 @@ mock.module("./plugin-entry-migrator.ts", () => ({
const autoMigrateModulePromise = import("./auto-migrate") const autoMigrateModulePromise = import("./auto-migrate")
afterAll(() => {
mock.restore()
})
describe("autoMigrateLegacyPluginEntry", () => { describe("autoMigrateLegacyPluginEntry", () => {
let testConfigDir = "" let testConfigDir = ""
+5 -1
View File
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test" import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
let scheduledDeferredCheck: (() => void) | null = null let scheduledDeferredCheck: (() => void) | null = null
mock.module("../auto-update-checker/hook/deferred-startup-check", () => ({ mock.module("../auto-update-checker/hook/deferred-startup-check", () => ({
@@ -9,6 +9,10 @@ mock.module("../auto-update-checker/hook/deferred-startup-check", () => ({
const { createAutoUpdateCheckerHook } = await import("../auto-update-checker/hook") const { createAutoUpdateCheckerHook } = await import("../auto-update-checker/hook")
afterAll(() => {
mock.restore()
})
const mockShowConfigErrorsIfAny = mock(async () => {}) const mockShowConfigErrorsIfAny = mock(async () => {})
const mockShowModelCacheWarningIfNeeded = mock(async () => {}) const mockShowModelCacheWarningIfNeeded = mock(async () => {})
const mockUpdateAndShowConnectedProvidersCacheStatus = mock(async () => {}) const mockUpdateAndShowConnectedProvidersCacheStatus = mock(async () => {})
@@ -1,4 +1,4 @@
const { afterEach, beforeEach, describe, expect, mock, test } = require("bun:test") const { afterAll, afterEach, beforeEach, describe, expect, mock, test } = require("bun:test")
const mockSetPluginHooksConfigs = mock(() => {}) const mockSetPluginHooksConfigs = mock(() => {})
@@ -8,6 +8,10 @@ mock.module("../hooks/claude-code-hooks/config", () => ({
const { applyHookConfig } = await import("./hook-config-handler") const { applyHookConfig } = await import("./hook-config-handler")
afterAll(() => {
mock.restore()
})
describe("applyHookConfig", () => { describe("applyHookConfig", () => {
beforeEach(() => { beforeEach(() => {
mockSetPluginHooksConfigs.mockClear() mockSetPluginHooksConfigs.mockClear()