2026-02-14 17:50:08 +09:00
|
|
|
declare const require: (name: string) => any
|
|
|
|
|
const { describe, it, expect, beforeEach, afterEach, beforeAll, mock } = require("bun:test")
|
2026-02-14 17:41:40 +09:00
|
|
|
|
2026-02-14 17:50:08 +09:00
|
|
|
let getMessageDir: (sessionID: string) => string | null
|
2026-02-14 17:41:40 +09:00
|
|
|
|
2026-02-14 17:50:08 +09:00
|
|
|
beforeAll(async () => {
|
|
|
|
|
// Mock the data-path module
|
|
|
|
|
mock.module("./data-path", () => ({
|
|
|
|
|
getOpenCodeStorageDir: () => "/mock/opencode/storage",
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
2026-02-14 17:50:08 +09:00
|
|
|
// Mock fs functions
|
|
|
|
|
mock.module("node:fs", () => ({
|
|
|
|
|
existsSync: mock(() => false),
|
|
|
|
|
readdirSync: mock(() => []),
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
2026-02-14 17:50:08 +09:00
|
|
|
mock.module("node:path", () => ({
|
|
|
|
|
join: mock((...args: string[]) => args.join("/")),
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
;({ getMessageDir } = await import("./opencode-message-dir"))
|
|
|
|
|
})
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
describe("getMessageDir", () => {
|
|
|
|
|
beforeEach(() => {
|
2026-02-14 17:50:08 +09:00
|
|
|
// Reset mocks
|
|
|
|
|
mock.restore()
|
2026-02-14 17:41:40 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-14 17:50:08 +09:00
|
|
|
it("returns null when sessionID does not start with ses_", () => {
|
|
|
|
|
// given
|
|
|
|
|
// no mocks needed
|
|
|
|
|
|
|
|
|
|
// when
|
|
|
|
|
const result = getMessageDir("invalid")
|
|
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
expect(result).toBe(null)
|
2026-02-14 17:41:40 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns null when MESSAGE_STORAGE does not exist", () => {
|
|
|
|
|
// given
|
2026-02-14 17:50:08 +09:00
|
|
|
mock.module("node:fs", () => ({
|
|
|
|
|
existsSync: mock(() => false),
|
|
|
|
|
readdirSync: mock(() => []),
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-02-14 17:50:08 +09:00
|
|
|
const result = getMessageDir("ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
expect(result).toBe(null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns direct path when session exists directly", () => {
|
|
|
|
|
// given
|
2026-02-14 17:50:08 +09:00
|
|
|
mock.module("node:fs", () => ({
|
|
|
|
|
existsSync: mock((path: string) => path === "/mock/opencode/storage/message" || path === "/mock/opencode/storage/message/ses_123"),
|
|
|
|
|
readdirSync: mock(() => []),
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-02-14 17:50:08 +09:00
|
|
|
const result = getMessageDir("ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// then
|
2026-02-14 17:50:08 +09:00
|
|
|
expect(result).toBe("/mock/opencode/storage/message/ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns subdirectory path when session exists in subdirectory", () => {
|
|
|
|
|
// given
|
2026-02-14 17:50:08 +09:00
|
|
|
mock.module("node:fs", () => ({
|
|
|
|
|
existsSync: mock((path: string) => path === "/mock/opencode/storage/message" || path === "/mock/opencode/storage/message/subdir/ses_123"),
|
|
|
|
|
readdirSync: mock(() => ["subdir"]),
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-02-14 17:50:08 +09:00
|
|
|
const result = getMessageDir("ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// then
|
2026-02-14 17:50:08 +09:00
|
|
|
expect(result).toBe("/mock/opencode/storage/message/subdir/ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns null when session not found anywhere", () => {
|
|
|
|
|
// given
|
2026-02-14 17:50:08 +09:00
|
|
|
mock.module("node:fs", () => ({
|
|
|
|
|
existsSync: mock((path: string) => path === "/mock/opencode/storage/message"),
|
|
|
|
|
readdirSync: mock(() => ["subdir1", "subdir2"]),
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-02-14 17:50:08 +09:00
|
|
|
const result = getMessageDir("ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
expect(result).toBe(null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns null when readdirSync throws", () => {
|
|
|
|
|
// given
|
2026-02-14 17:50:08 +09:00
|
|
|
mock.module("node:fs", () => ({
|
|
|
|
|
existsSync: mock((path: string) => path === "/mock/opencode/storage/message"),
|
|
|
|
|
readdirSync: mock(() => {
|
|
|
|
|
throw new Error("Permission denied")
|
|
|
|
|
}),
|
|
|
|
|
}))
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-02-14 17:50:08 +09:00
|
|
|
const result = getMessageDir("ses_123")
|
2026-02-14 17:41:40 +09:00
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
expect(result).toBe(null)
|
|
|
|
|
})
|
|
|
|
|
})
|