Fix session storage tests with proper module mocking for temp directories

Tests now properly mock the constants module before importing storage functions,
ensuring test data is read/written to temp directories instead of real paths.
This fixes test isolation issues and allows tests to run independently.

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2025-12-25 18:43:52 +09:00
parent 48476e7257
commit 9ba580e51f

View File

@@ -1,13 +1,23 @@
import { describe, test, expect, beforeEach, afterEach } from "bun:test" import { describe, test, expect, beforeEach, afterEach, mock } from "bun:test"
import { mkdirSync, writeFileSync, rmSync, existsSync } from "node:fs" import { mkdirSync, writeFileSync, rmSync, existsSync } from "node:fs"
import { join } from "node:path" import { join } from "node:path"
import { tmpdir } from "node:os" import { tmpdir } from "node:os"
import { getAllSessions, getMessageDir, sessionExists, readSessionMessages, readSessionTodos, getSessionInfo } from "./storage"
const TEST_DIR = join(tmpdir(), "omo-test-session-manager") const TEST_DIR = join(tmpdir(), "omo-test-session-manager")
const TEST_MESSAGE_STORAGE = join(TEST_DIR, "message") const TEST_MESSAGE_STORAGE = join(TEST_DIR, "message")
const TEST_PART_STORAGE = join(TEST_DIR, "part") const TEST_PART_STORAGE = join(TEST_DIR, "part")
const TEST_TODO_DIR = join(TEST_DIR, "todos") const TEST_TODO_DIR = join(TEST_DIR, "todos")
const TEST_TRANSCRIPT_DIR = join(TEST_DIR, "transcripts")
mock.module("./constants", () => ({
OPENCODE_STORAGE: TEST_DIR,
MESSAGE_STORAGE: TEST_MESSAGE_STORAGE,
PART_STORAGE: TEST_PART_STORAGE,
TODO_DIR: TEST_TODO_DIR,
TRANSCRIPT_DIR: TEST_TRANSCRIPT_DIR,
}))
const { getAllSessions, getMessageDir, sessionExists, readSessionMessages, readSessionTodos, getSessionInfo } = await import("./storage")
describe("session-manager storage", () => { describe("session-manager storage", () => {
beforeEach(() => { beforeEach(() => {
@@ -18,6 +28,7 @@ describe("session-manager storage", () => {
mkdirSync(TEST_MESSAGE_STORAGE, { recursive: true }) mkdirSync(TEST_MESSAGE_STORAGE, { recursive: true })
mkdirSync(TEST_PART_STORAGE, { recursive: true }) mkdirSync(TEST_PART_STORAGE, { recursive: true })
mkdirSync(TEST_TODO_DIR, { recursive: true }) mkdirSync(TEST_TODO_DIR, { recursive: true })
mkdirSync(TEST_TRANSCRIPT_DIR, { recursive: true })
}) })
afterEach(() => { afterEach(() => {
@@ -30,6 +41,7 @@ describe("session-manager storage", () => {
const sessions = getAllSessions() const sessions = getAllSessions()
expect(Array.isArray(sessions)).toBe(true) expect(Array.isArray(sessions)).toBe(true)
expect(sessions).toEqual([])
}) })
test("getMessageDir finds session in direct path", () => { test("getMessageDir finds session in direct path", () => {
@@ -40,7 +52,7 @@ describe("session-manager storage", () => {
const result = getMessageDir(sessionID) const result = getMessageDir(sessionID)
expect(result).toBe("") expect(result).toBe(sessionPath)
}) })
test("sessionExists returns false for non-existent session", () => { test("sessionExists returns false for non-existent session", () => {
@@ -49,6 +61,17 @@ describe("session-manager storage", () => {
expect(exists).toBe(false) expect(exists).toBe(false)
}) })
test("sessionExists returns true for existing session", () => {
const sessionID = "ses_exists"
const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID)
mkdirSync(sessionPath, { recursive: true })
writeFileSync(join(sessionPath, "msg_001.json"), JSON.stringify({ id: "msg_001" }))
const exists = sessionExists(sessionID)
expect(exists).toBe(true)
})
test("readSessionMessages returns empty array for non-existent session", () => { test("readSessionMessages returns empty array for non-existent session", () => {
const messages = readSessionMessages("ses_nonexistent") const messages = readSessionMessages("ses_nonexistent")
@@ -60,18 +83,20 @@ describe("session-manager storage", () => {
const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID) const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID)
mkdirSync(sessionPath, { recursive: true }) mkdirSync(sessionPath, { recursive: true })
writeFileSync(
join(sessionPath, "msg_001.json"),
JSON.stringify({ id: "msg_001", role: "user", time: { created: 1000 } })
)
writeFileSync( writeFileSync(
join(sessionPath, "msg_002.json"), join(sessionPath, "msg_002.json"),
JSON.stringify({ id: "msg_002", role: "assistant", time: { created: 2000 } }) JSON.stringify({ id: "msg_002", role: "assistant", time: { created: 2000 } })
) )
writeFileSync(
join(sessionPath, "msg_001.json"),
JSON.stringify({ id: "msg_001", role: "user", time: { created: 1000 } })
)
const messages = readSessionMessages(sessionID) const messages = readSessionMessages(sessionID)
expect(messages.length).toBeGreaterThanOrEqual(0) expect(messages.length).toBe(2)
expect(messages[0].id).toBe("msg_001")
expect(messages[1].id).toBe("msg_002")
}) })
test("readSessionTodos returns empty array when no todos exist", () => { test("readSessionTodos returns empty array when no todos exist", () => {
@@ -91,13 +116,14 @@ describe("session-manager storage", () => {
const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID) const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID)
mkdirSync(sessionPath, { recursive: true }) mkdirSync(sessionPath, { recursive: true })
const now = Date.now()
writeFileSync( writeFileSync(
join(sessionPath, "msg_001.json"), join(sessionPath, "msg_001.json"),
JSON.stringify({ JSON.stringify({
id: "msg_001", id: "msg_001",
role: "user", role: "user",
agent: "build", agent: "build",
time: { created: Date.now() - 10000 }, time: { created: now - 10000 },
}) })
) )
writeFileSync( writeFileSync(
@@ -106,12 +132,16 @@ describe("session-manager storage", () => {
id: "msg_002", id: "msg_002",
role: "assistant", role: "assistant",
agent: "oracle", agent: "oracle",
time: { created: Date.now() }, time: { created: now },
}) })
) )
const info = getSessionInfo(sessionID) const info = getSessionInfo(sessionID)
expect(info).not.toBeNull() expect(info).not.toBeNull()
expect(info?.id).toBe(sessionID)
expect(info?.message_count).toBe(2)
expect(info?.agents_used).toContain("build")
expect(info?.agents_used).toContain("oracle")
}) })
}) })