From 9ba580e51f6dc7d6534eb3aeb624d72d730063a7 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Thu, 25 Dec 2025 18:43:52 +0900 Subject: [PATCH] Fix session storage tests with proper module mocking for temp directories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- src/tools/session-manager/storage.test.ts | 50 ++++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/tools/session-manager/storage.test.ts b/src/tools/session-manager/storage.test.ts index e9ae2a6..65d21a5 100644 --- a/src/tools/session-manager/storage.test.ts +++ b/src/tools/session-manager/storage.test.ts @@ -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 { join } from "node:path" 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_MESSAGE_STORAGE = join(TEST_DIR, "message") const TEST_PART_STORAGE = join(TEST_DIR, "part") 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", () => { beforeEach(() => { @@ -18,6 +28,7 @@ describe("session-manager storage", () => { mkdirSync(TEST_MESSAGE_STORAGE, { recursive: true }) mkdirSync(TEST_PART_STORAGE, { recursive: true }) mkdirSync(TEST_TODO_DIR, { recursive: true }) + mkdirSync(TEST_TRANSCRIPT_DIR, { recursive: true }) }) afterEach(() => { @@ -30,6 +41,7 @@ describe("session-manager storage", () => { const sessions = getAllSessions() expect(Array.isArray(sessions)).toBe(true) + expect(sessions).toEqual([]) }) test("getMessageDir finds session in direct path", () => { @@ -40,7 +52,7 @@ describe("session-manager storage", () => { const result = getMessageDir(sessionID) - expect(result).toBe("") + expect(result).toBe(sessionPath) }) test("sessionExists returns false for non-existent session", () => { @@ -49,6 +61,17 @@ describe("session-manager storage", () => { 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", () => { const messages = readSessionMessages("ses_nonexistent") @@ -60,18 +83,20 @@ describe("session-manager storage", () => { const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID) mkdirSync(sessionPath, { recursive: true }) - writeFileSync( - join(sessionPath, "msg_001.json"), - JSON.stringify({ id: "msg_001", role: "user", time: { created: 1000 } }) - ) writeFileSync( join(sessionPath, "msg_002.json"), 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) - 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", () => { @@ -91,13 +116,14 @@ describe("session-manager storage", () => { const sessionPath = join(TEST_MESSAGE_STORAGE, sessionID) mkdirSync(sessionPath, { recursive: true }) + const now = Date.now() writeFileSync( join(sessionPath, "msg_001.json"), JSON.stringify({ id: "msg_001", role: "user", agent: "build", - time: { created: Date.now() - 10000 }, + time: { created: now - 10000 }, }) ) writeFileSync( @@ -106,12 +132,16 @@ describe("session-manager storage", () => { id: "msg_002", role: "assistant", agent: "oracle", - time: { created: Date.now() }, + time: { created: now }, }) ) const info = getSessionInfo(sessionID) 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") }) })