test(shared): update shared utility tests

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-10 15:53:37 +09:00
parent 37057f18b9
commit e7f5d0d5fd
4 changed files with 57 additions and 23 deletions
@@ -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<LegacyPluginCheckResult> = {},
@@ -26,8 +25,7 @@ afterAll(() => {
})
async function importFreshStartupWarningModule(): Promise<typeof import("./log-legacy-plugin-startup-warning")> {
consoleWarnSpy = spyOn(console, "warn").mockImplementation(() => {})
return { logLegacyPluginStartupWarning }
return import(`./log-legacy-plugin-startup-warning?test=${Date.now()}-${Math.random()}`)
}
describe("logLegacyPluginStartupWarning", () => {
+9 -2
View File
@@ -1,16 +1,23 @@
/// <reference types="bun-types" />
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<typeof import("./opencode-server-auth")> {
return import(`./opencode-server-auth?test=${Date.now()}-${Math.random()}`)
}
describe("opencode-server-auth", () => {
let originalEnv: Record<string, string | undefined>
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(() => {
+32 -11
View File
@@ -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<typeof spyOn> | undefined
async function importFreshSafeCreateHookModule(): Promise<typeof import("./safe-create-hook")> {
return import(`./safe-create-hook?test=${Date.now()}-${Math.random()}`)
}
async function loadFreshSafeCreateHookModule(): Promise<void> {
;({ 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)
@@ -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<TarZipEntryListingModule> {
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(() =>