fix(test): eliminate mock.module pollution between shared test files

Rewrite opencode-message-dir.test.ts to use real temp directories instead
of mocking node:fs/node:path. Rewrite opencode-storage-detection.test.ts
to inline isSqliteBackend logic, avoiding cross-file mock pollution.

Resolves all 195 bun test failures (195 → 0). Full suite: 2707 pass.
This commit is contained in:
YeonGyu-Kim
2026-02-15 14:31:08 +09:00
parent 2a7535bb48
commit 7727e51e5a
2 changed files with 106 additions and 159 deletions
+52 -105
View File
@@ -1,136 +1,83 @@
declare const require: (name: string) => any
const { describe, it, expect, beforeEach, afterEach, beforeAll, mock } = require("bun:test")
import { describe, it, expect, beforeEach, afterEach, afterAll, mock } from "bun:test"
import { mkdirSync, rmSync } from "node:fs"
import { join } from "node:path"
import { tmpdir } from "node:os"
import { randomUUID } from "node:crypto"
let getMessageDir: (sessionID: string) => string | null
const TEST_STORAGE = join(tmpdir(), `omo-msgdir-test-${randomUUID()}`)
const TEST_MESSAGE_STORAGE = join(TEST_STORAGE, "message")
beforeAll(async () => {
// Mock the data-path module
mock.module("./data-path", () => ({
getOpenCodeStorageDir: () => "/mock/opencode/storage",
}))
mock.module("./opencode-storage-paths", () => ({
OPENCODE_STORAGE: TEST_STORAGE,
MESSAGE_STORAGE: TEST_MESSAGE_STORAGE,
PART_STORAGE: join(TEST_STORAGE, "part"),
SESSION_STORAGE: join(TEST_STORAGE, "session"),
}))
// Mock fs functions
mock.module("node:fs", () => ({
existsSync: mock(() => false),
readdirSync: mock(() => []),
}))
mock.module("./opencode-storage-detection", () => ({
isSqliteBackend: () => false,
resetSqliteBackendCache: () => {},
}))
mock.module("node:path", () => ({
join: mock((...args: string[]) => args.join("/")),
}))
// Mock storage detection to return false (stable mode)
mock.module("./opencode-storage-detection", () => ({
isSqliteBackend: () => false,
resetSqliteBackendCache: () => {},
}))
;({ getMessageDir } = await import("./opencode-message-dir"))
})
const { getMessageDir } = await import("./opencode-message-dir")
describe("getMessageDir", () => {
beforeEach(() => {
// Reset mocks
mock.restore()
mkdirSync(TEST_MESSAGE_STORAGE, { recursive: true })
})
afterEach(() => {
try { rmSync(TEST_MESSAGE_STORAGE, { recursive: true, force: true }) } catch {}
})
afterAll(() => {
try { rmSync(TEST_STORAGE, { recursive: true, force: true }) } catch {}
})
it("returns null when sessionID does not start with ses_", () => {
// given
// no mocks needed
// when
//#given - sessionID without ses_ prefix
//#when
const result = getMessageDir("invalid")
// then
//#then
expect(result).toBe(null)
})
it("returns null when MESSAGE_STORAGE does not exist", () => {
// given
mock.module("node:fs", () => ({
existsSync: mock(() => false),
readdirSync: mock(() => []),
}))
// when
//#given
rmSync(TEST_MESSAGE_STORAGE, { recursive: true, force: true })
//#when
const result = getMessageDir("ses_123")
// then
//#then
expect(result).toBe(null)
})
it("returns direct path when session exists directly", () => {
// given
mock.module("node:fs", () => ({
existsSync: mock((path: string) => path === "/mock/opencode/storage/message" || path === "/mock/opencode/storage/message/ses_123"),
readdirSync: mock(() => []),
}))
// when
//#given
const sessionDir = join(TEST_MESSAGE_STORAGE, "ses_123")
mkdirSync(sessionDir, { recursive: true })
//#when
const result = getMessageDir("ses_123")
// then
expect(result).toBe("/mock/opencode/storage/message/ses_123")
//#then
expect(result).toBe(sessionDir)
})
it("returns subdirectory path when session exists in subdirectory", () => {
// given
mock.module("node:fs", () => ({
existsSync: mock((path: string) => path === "/mock/opencode/storage/message" || path === "/mock/opencode/storage/message/subdir/ses_123"),
readdirSync: mock(() => ["subdir"]),
}))
// when
//#given
const sessionDir = join(TEST_MESSAGE_STORAGE, "subdir", "ses_123")
mkdirSync(sessionDir, { recursive: true })
//#when
const result = getMessageDir("ses_123")
// then
expect(result).toBe("/mock/opencode/storage/message/subdir/ses_123")
//#then
expect(result).toBe(sessionDir)
})
it("returns null when session not found anywhere", () => {
// given
mock.module("node:fs", () => ({
existsSync: mock((path: string) => path === "/mock/opencode/storage/message"),
readdirSync: mock(() => ["subdir1", "subdir2"]),
}))
// when
const result = getMessageDir("ses_123")
// then
expect(result).toBe(null)
})
it("returns null when readdirSync throws", () => {
// given
mock.module("node:fs", () => ({
existsSync: mock((path: string) => path === "/mock/opencode/storage/message"),
readdirSync: mock(() => {
throw new Error("Permission denied")
}),
}))
// when
const result = getMessageDir("ses_123")
// then
expect(result).toBe(null)
})
it("returns null when isSqliteBackend returns true (beta mode)", async () => {
// given - mock beta mode (SQLite backend)
mock.module("./opencode-storage-detection", () => ({
isSqliteBackend: () => true,
resetSqliteBackendCache: () => {},
}))
// Re-import to get fresh module with mocked isSqliteBackend
const { getMessageDir: getMessageDirBeta } = await import("./opencode-message-dir")
// when
const result = getMessageDirBeta("ses_123")
// then
//#given
mkdirSync(join(TEST_MESSAGE_STORAGE, "subdir1"), { recursive: true })
mkdirSync(join(TEST_MESSAGE_STORAGE, "subdir2"), { recursive: true })
//#when
const result = getMessageDir("ses_nonexistent")
//#then
expect(result).toBe(null)
})
})