test: remove remaining legacy warning mock leaks

This commit is contained in:
YeonGyu-Kim
2026-04-04 19:52:25 +09:00
parent a4db240d47
commit 29a830dd89
4 changed files with 86 additions and 59 deletions
+31 -30
View File
@@ -1,5 +1,6 @@
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test" import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
import type { MigrationResult } from "./auto-migrate-runner" import type { MigrationResult } from "./auto-migrate-runner"
import { createLegacyPluginToastHook } from "./hook"
const mockCheckForLegacyPluginEntry = mock(() => ({ const mockCheckForLegacyPluginEntry = mock(() => ({
hasLegacyEntry: false, hasLegacyEntry: false,
@@ -40,24 +41,6 @@ function createEvent(type: string, parentID?: string) {
} }
} }
async function importFreshModule() {
mock.module("../../shared/legacy-plugin-warning", () => ({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
}))
mock.module("../../shared/logger", () => ({
log: mockLog,
}))
mock.module("./auto-migrate-runner", () => ({
autoMigrateLegacyPluginEntry: mockAutoMigrate,
}))
const module = await import(`./hook?t=${Date.now()}-${Math.random()}`)
mock.restore()
return module
}
describe("createLegacyPluginToastHook", () => { describe("createLegacyPluginToastHook", () => {
beforeEach(() => { beforeEach(() => {
mockCheckForLegacyPluginEntry.mockReset() mockCheckForLegacyPluginEntry.mockReset()
@@ -77,8 +60,11 @@ describe("createLegacyPluginToastHook", () => {
describe("#given no legacy entry exists", () => { describe("#given no legacy entry exists", () => {
it("#then does not show a toast", async () => { it("#then does not show a toast", async () => {
// given // given
const { createLegacyPluginToastHook } = await importFreshModule() const hook = createLegacyPluginToastHook(createMockCtx(), {
const hook = createLegacyPluginToastHook(createMockCtx()) checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
autoMigrateLegacyPluginEntry: mockAutoMigrate,
})
// when // when
await hook.event(createEvent("session.created")) await hook.event(createEvent("session.created"))
@@ -102,8 +88,11 @@ describe("createLegacyPluginToastHook", () => {
to: "oh-my-openagent", to: "oh-my-openagent",
configPath: "/tmp/opencode.json", configPath: "/tmp/opencode.json",
}) })
const { createLegacyPluginToastHook } = await importFreshModule() const hook = createLegacyPluginToastHook(createMockCtx(), {
const hook = createLegacyPluginToastHook(createMockCtx()) checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
autoMigrateLegacyPluginEntry: mockAutoMigrate,
})
// when // when
await hook.event(createEvent("session.created")) await hook.event(createEvent("session.created"))
@@ -129,8 +118,11 @@ describe("createLegacyPluginToastHook", () => {
to: null, to: null,
configPath: "/tmp/opencode.json", configPath: "/tmp/opencode.json",
}) })
const { createLegacyPluginToastHook } = await importFreshModule() const hook = createLegacyPluginToastHook(createMockCtx(), {
const hook = createLegacyPluginToastHook(createMockCtx()) checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
autoMigrateLegacyPluginEntry: mockAutoMigrate,
})
// when // when
await hook.event(createEvent("session.created")) await hook.event(createEvent("session.created"))
@@ -156,8 +148,11 @@ describe("createLegacyPluginToastHook", () => {
to: "oh-my-openagent", to: "oh-my-openagent",
configPath: "/tmp/opencode.json", configPath: "/tmp/opencode.json",
}) })
const { createLegacyPluginToastHook } = await importFreshModule() const hook = createLegacyPluginToastHook(createMockCtx(), {
const hook = createLegacyPluginToastHook(createMockCtx()) checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
autoMigrateLegacyPluginEntry: mockAutoMigrate,
})
// when // when
await hook.event(createEvent("session.created")) await hook.event(createEvent("session.created"))
@@ -176,8 +171,11 @@ describe("createLegacyPluginToastHook", () => {
hasCanonicalEntry: false, hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"], legacyEntries: ["oh-my-opencode"],
}) })
const { createLegacyPluginToastHook } = await importFreshModule() const hook = createLegacyPluginToastHook(createMockCtx(), {
const hook = createLegacyPluginToastHook(createMockCtx()) checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
autoMigrateLegacyPluginEntry: mockAutoMigrate,
})
// when // when
await hook.event(createEvent("session.deleted")) await hook.event(createEvent("session.deleted"))
@@ -195,8 +193,11 @@ describe("createLegacyPluginToastHook", () => {
hasCanonicalEntry: false, hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"], legacyEntries: ["oh-my-opencode"],
}) })
const { createLegacyPluginToastHook } = await importFreshModule() const hook = createLegacyPluginToastHook(createMockCtx(), {
const hook = createLegacyPluginToastHook(createMockCtx()) checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
autoMigrateLegacyPluginEntry: mockAutoMigrate,
})
// when // when
await hook.event(createEvent("session.created", "parent-session-id")) await hook.event(createEvent("session.created", "parent-session-id"))
+14 -5
View File
@@ -5,8 +5,17 @@ import { log } from "../../shared/logger"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity" import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
import { autoMigrateLegacyPluginEntry } from "./auto-migrate-runner" import { autoMigrateLegacyPluginEntry } from "./auto-migrate-runner"
export function createLegacyPluginToastHook(ctx: PluginInput) { type LegacyPluginToastDeps = {
checkForLegacyPluginEntry?: typeof checkForLegacyPluginEntry
log?: typeof log
autoMigrateLegacyPluginEntry?: typeof autoMigrateLegacyPluginEntry
}
export function createLegacyPluginToastHook(ctx: PluginInput, deps: LegacyPluginToastDeps = {}) {
let fired = false let fired = false
const checkForLegacyPluginEntryFn = deps.checkForLegacyPluginEntry ?? checkForLegacyPluginEntry
const logFn = deps.log ?? log
const autoMigrateLegacyPluginEntryFn = deps.autoMigrateLegacyPluginEntry ?? autoMigrateLegacyPluginEntry
return { return {
event: async ({ event }: { event: { type: string; properties?: unknown } }) => { event: async ({ event }: { event: { type: string; properties?: unknown } }) => {
@@ -17,13 +26,13 @@ export function createLegacyPluginToastHook(ctx: PluginInput) {
fired = true fired = true
const result = checkForLegacyPluginEntry() const result = checkForLegacyPluginEntryFn()
if (!result.hasLegacyEntry) return if (!result.hasLegacyEntry) return
const migration = autoMigrateLegacyPluginEntry() const migration = autoMigrateLegacyPluginEntryFn()
if (migration.migrated) { if (migration.migrated) {
log("[legacy-plugin-toast] Auto-migrated opencode.json plugin entry", { logFn("[legacy-plugin-toast] Auto-migrated opencode.json plugin entry", {
from: migration.from, from: migration.from,
to: migration.to, to: migration.to,
}) })
@@ -39,7 +48,7 @@ export function createLegacyPluginToastHook(ctx: PluginInput) {
}) })
.catch(() => {}) .catch(() => {})
} else { } else {
log("[legacy-plugin-toast] Legacy entry detected but migration failed", { logFn("[legacy-plugin-toast] Legacy entry detected but migration failed", {
legacyEntries: result.legacyEntries, legacyEntries: result.legacyEntries,
}) })
@@ -2,6 +2,7 @@
import { afterAll, afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test" import { afterAll, afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test"
import type { LegacyPluginCheckResult } from "./legacy-plugin-warning" import type { LegacyPluginCheckResult } from "./legacy-plugin-warning"
import { logLegacyPluginStartupWarning } from "./log-legacy-plugin-startup-warning"
function createLegacyPluginCheckResult( function createLegacyPluginCheckResult(
overrides: Partial<LegacyPluginCheckResult> = {}, overrides: Partial<LegacyPluginCheckResult> = {},
@@ -25,22 +26,8 @@ afterAll(() => {
}) })
async function importFreshStartupWarningModule(): Promise<typeof import("./log-legacy-plugin-startup-warning")> { async function importFreshStartupWarningModule(): Promise<typeof import("./log-legacy-plugin-startup-warning")> {
mock.module("./legacy-plugin-warning", () => ({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
}))
mock.module("./logger", () => ({
log: mockLog,
}))
mock.module("./migrate-legacy-plugin-entry", () => ({
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
}))
const module = await import(`./log-legacy-plugin-startup-warning?test=${Date.now()}-${Math.random()}`)
mock.restore()
consoleWarnSpy = spyOn(console, "warn").mockImplementation(() => {}) consoleWarnSpy = spyOn(console, "warn").mockImplementation(() => {})
return module return { logLegacyPluginStartupWarning }
} }
describe("logLegacyPluginStartupWarning", () => { describe("logLegacyPluginStartupWarning", () => {
@@ -69,7 +56,11 @@ describe("logLegacyPluginStartupWarning", () => {
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule() const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
//#when //#when
logLegacyPluginStartupWarning() logLegacyPluginStartupWarning({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
})
//#then //#then
expect(mockLog).toHaveBeenCalledTimes(1) expect(mockLog).toHaveBeenCalledTimes(1)
@@ -93,7 +84,11 @@ describe("logLegacyPluginStartupWarning", () => {
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule() const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
//#when //#when
logLegacyPluginStartupWarning() logLegacyPluginStartupWarning({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
})
//#then //#then
expect(consoleWarnSpy).toHaveBeenCalled() expect(consoleWarnSpy).toHaveBeenCalled()
@@ -112,7 +107,11 @@ describe("logLegacyPluginStartupWarning", () => {
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule() const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
//#when //#when
logLegacyPluginStartupWarning() logLegacyPluginStartupWarning({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
})
//#then //#then
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith("/tmp/opencode.json") expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith("/tmp/opencode.json")
@@ -125,7 +124,11 @@ describe("logLegacyPluginStartupWarning", () => {
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule() const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
//#when //#when
logLegacyPluginStartupWarning() logLegacyPluginStartupWarning({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
})
//#then //#then
expect(mockLog).not.toHaveBeenCalled() expect(mockLog).not.toHaveBeenCalled()
@@ -145,7 +148,11 @@ describe("logLegacyPluginStartupWarning", () => {
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule() const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
//#when //#when
logLegacyPluginStartupWarning() logLegacyPluginStartupWarning({
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
log: mockLog,
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
})
//#then //#then
const calls = consoleWarnSpy.mock.calls.map((call: string[]) => call[0] ?? "") const calls = consoleWarnSpy.mock.calls.map((call: string[]) => call[0] ?? "")
@@ -4,15 +4,25 @@ import { migrateLegacyPluginEntry } from "./migrate-legacy-plugin-entry"
import { toCanonicalEntry } from "./plugin-entry-migrator" import { toCanonicalEntry } from "./plugin-entry-migrator"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "./plugin-identity" import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "./plugin-identity"
export function logLegacyPluginStartupWarning(): void { type LogLegacyPluginStartupWarningDeps = {
const result = checkForLegacyPluginEntry() checkForLegacyPluginEntry?: typeof checkForLegacyPluginEntry
log?: typeof log
migrateLegacyPluginEntry?: typeof migrateLegacyPluginEntry
}
export function logLegacyPluginStartupWarning(deps: LogLegacyPluginStartupWarningDeps = {}): void {
const checkForLegacyPluginEntryFn = deps.checkForLegacyPluginEntry ?? checkForLegacyPluginEntry
const logFn = deps.log ?? log
const migrateLegacyPluginEntryFn = deps.migrateLegacyPluginEntry ?? migrateLegacyPluginEntry
const result = checkForLegacyPluginEntryFn()
if (!result.hasLegacyEntry) { if (!result.hasLegacyEntry) {
return return
} }
const suggestedEntries = result.legacyEntries.map(toCanonicalEntry) const suggestedEntries = result.legacyEntries.map(toCanonicalEntry)
log("[OhMyOpenCodePlugin] Legacy plugin entry detected in OpenCode config", { logFn("[OhMyOpenCodePlugin] Legacy plugin entry detected in OpenCode config", {
legacyEntries: result.legacyEntries, legacyEntries: result.legacyEntries,
suggestedEntries, suggestedEntries,
hasCanonicalEntry: result.hasCanonicalEntry, hasCanonicalEntry: result.hasCanonicalEntry,
@@ -24,7 +34,7 @@ export function logLegacyPluginStartupWarning(): void {
+ ` Attempting auto-migration...`, + ` Attempting auto-migration...`,
) )
const migrated = migrateLegacyPluginEntry(result.configPath!) const migrated = migrateLegacyPluginEntryFn(result.configPath!)
if (migrated) { if (migrated) {
console.warn(`[oh-my-openagent] Auto-migrated opencode.json: ${result.legacyEntries.join(", ")} -> ${suggestedEntries.join(", ")}`) console.warn(`[oh-my-openagent] Auto-migrated opencode.json: ${result.legacyEntries.join(", ")} -> ${suggestedEntries.join(", ")}`)
} else { } else {