Files
oh-my-opencode/src/shared/opencode-storage-detection.test.ts
T
YeonGyu-Kim 5eebef953b refactor(shared): unify MESSAGE_STORAGE/PART_STORAGE constants into single source
- Add src/shared/opencode-storage-paths.ts with consolidated constants

- Update imports in hook-message-injector and session-manager

- Add src/shared/opencode-storage-detection.ts with isSqliteBackend()

- Add OPENCODE_SQLITE_VERSION constant

- Export all from shared/index.ts
2026-02-16 16:13:40 +09:00

94 lines
2.7 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach, vi } from "bun:test"
import { existsSync } from "node:fs"
import { join } from "node:path"
import { isSqliteBackend, resetSqliteBackendCache } from "./opencode-storage-detection"
import { getDataDir } from "./data-path"
import { isOpenCodeVersionAtLeast, OPENCODE_SQLITE_VERSION } from "./opencode-version"
// Mock the dependencies
const mockExistsSync = vi.fn()
const mockGetDataDir = vi.fn()
const mockIsOpenCodeVersionAtLeast = vi.fn()
vi.mock("node:fs", () => ({
existsSync: mockExistsSync,
}))
vi.mock("./data-path", () => ({
getDataDir: mockGetDataDir,
}))
vi.mock("./opencode-version", () => ({
isOpenCodeVersionAtLeast: mockIsOpenCodeVersionAtLeast,
OPENCODE_SQLITE_VERSION: "1.1.53",
}))
describe("isSqliteBackend", () => {
beforeEach(() => {
// Reset the cached result
resetSqliteBackendCache()
})
afterEach(() => {
vi.clearAllMocks()
})
it("returns false when version is below threshold", () => {
// given
mockIsOpenCodeVersionAtLeast.mockReturnValue(false)
mockGetDataDir.mockReturnValue("/home/user/.local/share")
mockExistsSync.mockReturnValue(true)
// when
const result = isSqliteBackend()
// then
expect(result).toBe(false)
expect(mockIsOpenCodeVersionAtLeast).toHaveBeenCalledWith(OPENCODE_SQLITE_VERSION)
})
it("returns false when DB file does not exist", () => {
// given
mockIsOpenCodeVersionAtLeast.mockReturnValue(true)
mockGetDataDir.mockReturnValue("/home/user/.local/share")
mockExistsSync.mockReturnValue(false)
// when
const result = isSqliteBackend()
// then
expect(result).toBe(false)
expect(mockExistsSync).toHaveBeenCalledWith(join("/home/user/.local/share", "opencode", "opencode.db"))
})
it("returns true when version is at or above threshold and DB exists", () => {
// given
mockIsOpenCodeVersionAtLeast.mockReturnValue(true)
mockGetDataDir.mockReturnValue("/home/user/.local/share")
mockExistsSync.mockReturnValue(true)
// when
const result = isSqliteBackend()
// then
expect(result).toBe(true)
expect(mockIsOpenCodeVersionAtLeast).toHaveBeenCalledWith(OPENCODE_SQLITE_VERSION)
expect(mockExistsSync).toHaveBeenCalledWith(join("/home/user/.local/share", "opencode", "opencode.db"))
})
it("caches the result and does not re-check on subsequent calls", () => {
// given
mockIsOpenCodeVersionAtLeast.mockReturnValue(true)
mockGetDataDir.mockReturnValue("/home/user/.local/share")
mockExistsSync.mockReturnValue(true)
// when
isSqliteBackend()
isSqliteBackend()
isSqliteBackend()
// then
expect(mockIsOpenCodeVersionAtLeast).toHaveBeenCalledTimes(1)
expect(mockExistsSync).toHaveBeenCalledTimes(1)
})
})