import { describe, test, expect } from "bun:test" import { createSessionManagerTools } from "./tools" import type { ToolContext } from "@opencode-ai/plugin/tool" import type { PluginInput } from "@opencode-ai/plugin" import type { SessionInfo, SessionMessage, SearchResult, SessionMetadata, TodoItem } from "./types" const projectDir = "/Users/yeongyu/local-workspaces/oh-my-opencode" const mockCtx = { directory: projectDir } as PluginInput const mockContext: ToolContext = { sessionID: "test-session", messageID: "test-message", agent: "test-agent", directory: projectDir, worktree: projectDir, abort: new AbortController().signal, metadata: () => {}, ask: async () => {}, } function createTestTools() { return createSessionManagerTools(mockCtx, { setStorageClient: () => {}, getMainSessions: async (): Promise => [ { id: "ses_test123", projectID: "project-1", directory: projectDir, time: { created: Date.now(), updated: Date.now() }, }, { id: "ses_test456", projectID: "project-1", directory: projectDir, time: { created: Date.now(), updated: Date.now() }, }, ], filterSessionsByDate: async (sessionIDs) => sessionIDs, formatSessionList: async (sessionIDs) => `sessions:${sessionIDs.join(",")}`, sessionExists: async (sessionID) => sessionID === "ses_test123", readSessionMessages: async (sessionID): Promise => sessionID === "ses_test123" ? [ { id: `${sessionID}-msg`, role: "user", time: { created: Date.now() }, parts: [{ id: `${sessionID}-part`, type: "text", text: "hello" }], }, ] : [], readSessionTodos: async (): Promise => [], formatSessionMessages: (messages) => `messages:${messages.length}`, getAllSessions: async () => ["ses_test123", "ses_test456"], searchInSession: async (sessionID): Promise => [ { session_id: sessionID, message_id: `${sessionID}-msg`, excerpt: "test snippet", role: "user", match_count: 1, }, ], formatSearchResults: (results) => `results:${results.length}`, getSessionInfo: async (sessionID): Promise => sessionID === "ses_test123" ? { id: sessionID, message_count: 1, first_message: new Date(), last_message: new Date(), agents_used: ["test-agent"], has_todos: false, has_transcript: false, todos: [], transcript_entries: 0, } : null, formatSessionInfo: (info) => `info:${info.id}`, }) } describe("session-manager tools", () => { test("session_list executes without error", async () => { const { session_list } = createTestTools() const result = await session_list.execute({}, mockContext) expect(typeof result).toBe("string") }) test("session_list respects limit parameter", async () => { const { session_list } = createTestTools() const result = await session_list.execute({ limit: 5 }, mockContext) expect(typeof result).toBe("string") }) test("session_list filters by date range", async () => { const { session_list } = createTestTools() const result = await session_list.execute( { from_date: "2025-12-01T00:00:00Z", to_date: "2025-12-31T23:59:59Z", }, mockContext, ) expect(typeof result).toBe("string") }) test("session_list filters by project_path", async () => { const { session_list } = createTestTools() //#given const projectPath = "/Users/yeongyu/local-workspaces/oh-my-opencode" //#when const result = await session_list.execute({ project_path: projectPath }, mockContext) //#then expect(typeof result).toBe("string") }) test("session_list uses ctx.directory as default project_path", async () => { const { session_list } = createTestTools() //#given - no project_path provided //#when const result = await session_list.execute({}, mockContext) //#then expect(typeof result).toBe("string") }) test("#given newer sessions outside the to_date window #when session_list runs with to_date and limit #then the limit applies after the date filter", async () => { //#given const newestFirst: SessionMetadata[] = ["ses_new_1", "ses_new_2", "ses_old_1", "ses_old_2"].map((id, index) => ({ id, projectID: "project-1", directory: projectDir, time: { created: 4 - index, updated: 4 - index }, })) const { session_list } = createSessionManagerTools(mockCtx, { setStorageClient: () => {}, getMainSessions: async ({ limit }) => newestFirst.slice(0, limit), filterSessionsByDate: async (sessionIDs) => sessionIDs.filter((id) => id.startsWith("ses_old_")), formatSessionList: async (sessionIDs) => `sessions:${sessionIDs.join(",")}`, }) //#when const result = await session_list.execute({ to_date: "2025-12-01T00:00:00Z", limit: 2 }, mockContext) //#then expect(result).toBe("sessions:ses_old_1,ses_old_2") }) test("session_read handles non-existent session", async () => { const { session_read } = createTestTools() const result = await session_read.execute({ session_id: "ses_nonexistent" }, mockContext) expect(result).toContain("not found") }) test("session_read executes with valid parameters", async () => { const { session_read } = createTestTools() const result = await session_read.execute( { session_id: "ses_test123", include_todos: true, include_transcript: true, }, mockContext, ) expect(typeof result).toBe("string") }) test("session_read respects limit parameter", async () => { const { session_read } = createTestTools() const result = await session_read.execute( { session_id: "ses_test123", limit: 10, }, mockContext, ) expect(typeof result).toBe("string") }) test("session_search executes without error", async () => { const { session_search } = createTestTools() const result = await session_search.execute({ query: "test" }, mockContext) expect(typeof result).toBe("string") }) test("session_search filters by session_id", async () => { const { session_search } = createTestTools() const result = await session_search.execute( { query: "test", session_id: "ses_test123", }, mockContext, ) expect(typeof result).toBe("string") }) test("session_search respects case_sensitive parameter", async () => { const { session_search } = createTestTools() const result = await session_search.execute( { query: "TEST", case_sensitive: true, }, mockContext, ) expect(typeof result).toBe("string") }) test("session_search respects limit parameter", async () => { const { session_search } = createTestTools() const result = await session_search.execute( { query: "test", limit: 5, }, mockContext, ) expect(typeof result).toBe("string") }) test("session_info handles non-existent session", async () => { const { session_info } = createTestTools() const result = await session_info.execute({ session_id: "ses_nonexistent" }, mockContext) expect(result).toContain("not found") }) test("#given a session read that rejects with a plain error body #when session_search targets that session #then the error text is shown, not [object Object] (#5854)", async () => { //#given const { session_search } = createSessionManagerTools(mockCtx, { setStorageClient: () => {}, searchInSession: async () => { throw { name: "NotFoundError", data: { message: "Session not found: ses_gone" } } }, }) //#when const result = await session_search.execute({ query: "x", session_id: "ses_gone" }, mockContext) //#then expect(result).toBe("Error: NotFoundError: Session not found: ses_gone") }) test("#given one unreadable session among several #when session_search scans for a query with no match #then the scan finishes and names the skipped session (#5854)", async () => { //#given const scanned: string[] = [] const { session_search } = createSessionManagerTools(mockCtx, { setStorageClient: () => {}, getAllSessions: async () => ["ses_a", "ses_gone", "ses_b"], searchInSession: async (sessionID): Promise => { scanned.push(sessionID) if (sessionID === "ses_gone") throw { name: "NotFoundError", data: { message: "Session not found: ses_gone" } } return [] }, formatSearchResults: (results) => `results:${results.length}`, }) //#when const result = await session_search.execute({ query: "zzqxjnomatch" }, mockContext) //#then expect(scanned).toEqual(["ses_a", "ses_gone", "ses_b"]) expect(result).toBe( "results:0\n\nSkipped 1 session that could not be read: ses_gone (NotFoundError: Session not found: ses_gone)", ) }) test("session_info executes with valid session", async () => { const { session_info } = createTestTools() const result = await session_info.execute({ session_id: "ses_test123" }, mockContext) expect(typeof result).toBe("string") }) })