From e7f5d0d5fd98f92e12120c9f505e09b76a73a871 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Fri, 10 Apr 2026 15:53:37 +0900 Subject: [PATCH] test(shared): update shared utility tests Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .../log-legacy-plugin-startup-warning.test.ts | 4 +- src/shared/opencode-server-auth.test.ts | 11 ++++- src/shared/safe-create-hook.test.ts | 43 ++++++++++++++----- .../tar-zip-entry-listing.test.ts | 22 +++++++--- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/shared/log-legacy-plugin-startup-warning.test.ts b/src/shared/log-legacy-plugin-startup-warning.test.ts index d8f84d60e..917f40927 100644 --- a/src/shared/log-legacy-plugin-startup-warning.test.ts +++ b/src/shared/log-legacy-plugin-startup-warning.test.ts @@ -2,7 +2,6 @@ import { afterAll, afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test" import type { LegacyPluginCheckResult } from "./legacy-plugin-warning" -import { logLegacyPluginStartupWarning } from "./log-legacy-plugin-startup-warning" function createLegacyPluginCheckResult( overrides: Partial = {}, @@ -26,8 +25,7 @@ afterAll(() => { }) async function importFreshStartupWarningModule(): Promise { - consoleWarnSpy = spyOn(console, "warn").mockImplementation(() => {}) - return { logLegacyPluginStartupWarning } + return import(`./log-legacy-plugin-startup-warning?test=${Date.now()}-${Math.random()}`) } describe("logLegacyPluginStartupWarning", () => { diff --git a/src/shared/opencode-server-auth.test.ts b/src/shared/opencode-server-auth.test.ts index 87b419fd8..0dcf18d5d 100644 --- a/src/shared/opencode-server-auth.test.ts +++ b/src/shared/opencode-server-auth.test.ts @@ -1,16 +1,23 @@ /// import { describe, test, expect, beforeEach, afterEach } from "bun:test" -import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth" + +let getServerBasicAuthHeader: (typeof import("./opencode-server-auth"))["getServerBasicAuthHeader"] +let injectServerAuthIntoClient: (typeof import("./opencode-server-auth"))["injectServerAuthIntoClient"] + +async function importFreshOpencodeServerAuthModule(): Promise { + return import(`./opencode-server-auth?test=${Date.now()}-${Math.random()}`) +} describe("opencode-server-auth", () => { let originalEnv: Record - beforeEach(() => { + beforeEach(async () => { originalEnv = { OPENCODE_SERVER_PASSWORD: process.env.OPENCODE_SERVER_PASSWORD, OPENCODE_SERVER_USERNAME: process.env.OPENCODE_SERVER_USERNAME, } + ;({ getServerBasicAuthHeader, injectServerAuthIntoClient } = await importFreshOpencodeServerAuthModule()) }) afterEach(() => { diff --git a/src/shared/safe-create-hook.test.ts b/src/shared/safe-create-hook.test.ts index 72c326a66..6eff05c21 100644 --- a/src/shared/safe-create-hook.test.ts +++ b/src/shared/safe-create-hook.test.ts @@ -1,14 +1,29 @@ -import { describe, test, expect, spyOn, afterEach } from "bun:test" +import { describe, test, expect, spyOn, beforeEach, afterEach } from "bun:test" import * as shared from "./logger" -import { safeCreateHook } from "./safe-create-hook" + +let safeCreateHook: (typeof import("./safe-create-hook"))["safeCreateHook"] +let logSpy: ReturnType | undefined + +async function importFreshSafeCreateHookModule(): Promise { + return import(`./safe-create-hook?test=${Date.now()}-${Math.random()}`) +} + +async function loadFreshSafeCreateHookModule(): Promise { + ;({ safeCreateHook } = await importFreshSafeCreateHookModule()) +} + +beforeEach(() => { + logSpy = undefined +}) afterEach(() => { - ;(shared.log as any)?.mockRestore?.() + logSpy?.mockRestore() }) describe("safeCreateHook", () => { - test("returns hook object when factory succeeds", () => { + test("returns hook object when factory succeeds", async () => { //#given + await loadFreshSafeCreateHookModule() const hook = { handler: () => {} } const factory = () => hook @@ -19,9 +34,11 @@ describe("safeCreateHook", () => { expect(result).toBe(hook) }) - test("returns null when factory throws", () => { + test("returns null when factory throws", async () => { //#given - spyOn(shared, "log").mockImplementation(() => {}) + logSpy = spyOn(shared, "log") + logSpy.mockImplementation(() => {}) + await loadFreshSafeCreateHookModule() const factory = () => { throw new Error("boom") } @@ -33,9 +50,11 @@ describe("safeCreateHook", () => { expect(result).toBeNull() }) - test("logs error when factory throws", () => { + test("logs error when factory throws", async () => { //#given - const logSpy = spyOn(shared, "log").mockImplementation(() => {}) + logSpy = spyOn(shared, "log") + logSpy.mockImplementation(() => {}) + await loadFreshSafeCreateHookModule() const factory = () => { throw new Error("boom") } @@ -50,8 +69,9 @@ describe("safeCreateHook", () => { expect(callArgs[0]).toContain("Hook creation failed") }) - test("propagates error when enabled is false", () => { + test("propagates error when enabled is false", async () => { //#given + await loadFreshSafeCreateHookModule() const factory = () => { throw new Error("boom") } @@ -60,9 +80,10 @@ describe("safeCreateHook", () => { expect(() => safeCreateHook("test-hook", factory, { enabled: false })).toThrow("boom") }) - test("returns null for factory returning undefined", () => { + test("returns null for factory returning undefined", async () => { //#given - const factory = () => undefined as any + await loadFreshSafeCreateHookModule() + const factory = (): undefined => undefined //#when const result = safeCreateHook("test-hook", factory) diff --git a/src/shared/zip-entry-listing/tar-zip-entry-listing.test.ts b/src/shared/zip-entry-listing/tar-zip-entry-listing.test.ts index 106bb29fa..04469fa18 100644 --- a/src/shared/zip-entry-listing/tar-zip-entry-listing.test.ts +++ b/src/shared/zip-entry-listing/tar-zip-entry-listing.test.ts @@ -1,7 +1,12 @@ import { afterEach, describe, expect, it, mock, spyOn } from "bun:test" import * as logger from "../logger" -import { parseTarListingOutput } from "./tar-zip-entry-listing" + +type TarZipEntryListingModule = typeof import("./tar-zip-entry-listing") + +async function importFreshTarZipEntryListingModule(): Promise { + return await import(`./tar-zip-entry-listing?test=${Date.now()}-${Math.random()}`) +} function createTarFileLine(fileName: string): string { return `-rw-r--r-- 1 user group 123 Jan 01 12:34 ${fileName}` @@ -40,10 +45,11 @@ describe("parseTarListingOutput", () => { mock.restore() }) - describe("#given tar output with any unparsed lines", () => { - it("#when parsing the output #then throws immediately (fail-closed)", () => { + describe("#given tar output with any unparsed lines", () => { + it("#when parsing the output #then throws immediately (fail-closed)", async () => { // given const logSpy = spyOn(logger, "log").mockImplementation(() => {}) + const { parseTarListingOutput } = await importFreshTarZipEntryListingModule() const listedOutput = [ createTarFileLine("file-1.txt"), createTarFileLine("file-2.txt"), @@ -59,10 +65,11 @@ describe("parseTarListingOutput", () => { }) }) - describe("#given tar output with multiple unparsed lines", () => { - it("#when parsing the output #then throws with count details", () => { + describe("#given tar output with multiple unparsed lines", () => { + it("#when parsing the output #then throws with count details", async () => { // given const logSpy = spyOn(logger, "log").mockImplementation(() => {}) + const { parseTarListingOutput } = await importFreshTarZipEntryListingModule() const listedOutput = [ createTarFileLine("file-1.txt"), createTarFileLine("file-2.txt"), @@ -90,10 +97,11 @@ describe("parseTarListingOutput", () => { }) }) - describe("#given tar output where every non-empty line is unparsed", () => { - it("#when parsing the output #then rejects the listing", () => { + describe("#given tar output where every non-empty line is unparsed", () => { + it("#when parsing the output #then rejects the listing", async () => { // given const logSpy = spyOn(logger, "log").mockImplementation(() => {}) + const { parseTarListingOutput } = await importFreshTarZipEntryListingModule() // when const thrownError = captureThrownError(() =>