test: isolate auto-update checker modules
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
|
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
|
||||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
|
|
||||||
@@ -6,19 +6,30 @@ const TEST_CACHE_DIR = join(import.meta.dir, "__test-cache__")
|
|||||||
const TEST_OPENCODE_CACHE_DIR = join(TEST_CACHE_DIR, "opencode")
|
const TEST_OPENCODE_CACHE_DIR = join(TEST_CACHE_DIR, "opencode")
|
||||||
const TEST_USER_CONFIG_DIR = "/tmp/opencode-config"
|
const TEST_USER_CONFIG_DIR = "/tmp/opencode-config"
|
||||||
|
|
||||||
mock.module("./constants", () => ({
|
let importCounter = 0
|
||||||
CACHE_DIR: TEST_OPENCODE_CACHE_DIR,
|
|
||||||
USER_CONFIG_DIR: TEST_USER_CONFIG_DIR,
|
|
||||||
PACKAGE_NAME: "oh-my-opencode",
|
|
||||||
}))
|
|
||||||
|
|
||||||
mock.module("../../shared/logger", () => ({
|
async function importFreshCacheModule(): Promise<typeof import("./cache")> {
|
||||||
log: () => {},
|
mock.module("./constants", () => ({
|
||||||
}))
|
CACHE_DIR: TEST_OPENCODE_CACHE_DIR,
|
||||||
|
USER_CONFIG_DIR: TEST_USER_CONFIG_DIR,
|
||||||
|
PACKAGE_NAME: "oh-my-opencode",
|
||||||
|
NPM_REGISTRY_URL: "https://registry.npmjs.org/-/package/oh-my-opencode/dist-tags",
|
||||||
|
NPM_FETCH_TIMEOUT: 5000,
|
||||||
|
VERSION_FILE: join(TEST_OPENCODE_CACHE_DIR, "version"),
|
||||||
|
USER_OPENCODE_CONFIG: join(TEST_USER_CONFIG_DIR, "opencode.json"),
|
||||||
|
USER_OPENCODE_CONFIG_JSONC: join(TEST_USER_CONFIG_DIR, "opencode.jsonc"),
|
||||||
|
INSTALLED_PACKAGE_JSON: join(TEST_OPENCODE_CACHE_DIR, "node_modules", "oh-my-opencode", "package.json"),
|
||||||
|
getWindowsAppdataDir: () => null,
|
||||||
|
}))
|
||||||
|
|
||||||
afterAll(() => {
|
mock.module("../../shared/logger", () => ({
|
||||||
|
log: () => {},
|
||||||
|
}))
|
||||||
|
|
||||||
|
const cacheModule = await import(`./cache?test=${importCounter++}`)
|
||||||
mock.restore()
|
mock.restore()
|
||||||
})
|
return cacheModule
|
||||||
|
}
|
||||||
|
|
||||||
function resetTestCache(): void {
|
function resetTestCache(): void {
|
||||||
if (existsSync(TEST_CACHE_DIR)) {
|
if (existsSync(TEST_CACHE_DIR)) {
|
||||||
@@ -66,7 +77,7 @@ describe("invalidatePackage", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("invalidates the installed package from the OpenCode cache directory", async () => {
|
it("invalidates the installed package from the OpenCode cache directory", async () => {
|
||||||
const { invalidatePackage } = await import("./cache")
|
const { invalidatePackage } = await importFreshCacheModule()
|
||||||
|
|
||||||
const result = invalidatePackage()
|
const result = invalidatePackage()
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,34 @@
|
|||||||
import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
|
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
|
||||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
import type { PluginEntryInfo } from "./plugin-entry"
|
import type { PluginEntryInfo } from "./plugin-entry"
|
||||||
|
|
||||||
const TEST_CACHE_DIR = join(import.meta.dir, "__test-sync-cache__")
|
const TEST_CACHE_DIR = join(import.meta.dir, "__test-sync-cache__")
|
||||||
|
|
||||||
mock.module("../constants", () => ({
|
let importCounter = 0
|
||||||
CACHE_DIR: TEST_CACHE_DIR,
|
|
||||||
PACKAGE_NAME: "oh-my-opencode",
|
|
||||||
NPM_REGISTRY_URL: "https://registry.npmjs.org/-/package/oh-my-opencode/dist-tags",
|
|
||||||
NPM_FETCH_TIMEOUT: 5000,
|
|
||||||
VERSION_FILE: join(TEST_CACHE_DIR, "version"),
|
|
||||||
USER_CONFIG_DIR: "/tmp/opencode-config",
|
|
||||||
USER_OPENCODE_CONFIG: "/tmp/opencode-config/opencode.json",
|
|
||||||
USER_OPENCODE_CONFIG_JSONC: "/tmp/opencode-config/opencode.jsonc",
|
|
||||||
INSTALLED_PACKAGE_JSON: join(TEST_CACHE_DIR, "node_modules", "oh-my-opencode", "package.json"),
|
|
||||||
getWindowsAppdataDir: () => null,
|
|
||||||
}))
|
|
||||||
|
|
||||||
mock.module("../../../shared/logger", () => ({
|
async function importFreshSyncPackageJsonModule(): Promise<typeof import("./sync-package-json")> {
|
||||||
log: () => {},
|
mock.module("../constants", () => ({
|
||||||
}))
|
CACHE_DIR: TEST_CACHE_DIR,
|
||||||
|
PACKAGE_NAME: "oh-my-opencode",
|
||||||
|
NPM_REGISTRY_URL: "https://registry.npmjs.org/-/package/oh-my-opencode/dist-tags",
|
||||||
|
NPM_FETCH_TIMEOUT: 5000,
|
||||||
|
VERSION_FILE: join(TEST_CACHE_DIR, "version"),
|
||||||
|
USER_CONFIG_DIR: "/tmp/opencode-config",
|
||||||
|
USER_OPENCODE_CONFIG: "/tmp/opencode-config/opencode.json",
|
||||||
|
USER_OPENCODE_CONFIG_JSONC: "/tmp/opencode-config/opencode.jsonc",
|
||||||
|
INSTALLED_PACKAGE_JSON: join(TEST_CACHE_DIR, "node_modules", "oh-my-opencode", "package.json"),
|
||||||
|
getWindowsAppdataDir: () => null,
|
||||||
|
}))
|
||||||
|
|
||||||
afterAll(() => {
|
mock.module("../../../shared/logger", () => ({
|
||||||
|
log: () => {},
|
||||||
|
}))
|
||||||
|
|
||||||
|
const syncPackageJsonModule = await import(`./sync-package-json?test=${importCounter++}`)
|
||||||
mock.restore()
|
mock.restore()
|
||||||
})
|
return syncPackageJsonModule
|
||||||
|
}
|
||||||
|
|
||||||
function resetTestCache(currentVersion = "3.10.0"): void {
|
function resetTestCache(currentVersion = "3.10.0"): void {
|
||||||
if (existsSync(TEST_CACHE_DIR)) {
|
if (existsSync(TEST_CACHE_DIR)) {
|
||||||
@@ -62,7 +66,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
describe("#given cache package.json with pinned semver version", () => {
|
describe("#given cache package.json with pinned semver version", () => {
|
||||||
describe("#when opencode.json intent is latest tag", () => {
|
describe("#when opencode.json intent is latest tag", () => {
|
||||||
it("#then updates package.json to use latest", async () => {
|
it("#then updates package.json to use latest", async () => {
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -81,7 +85,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
|
|
||||||
describe("#when opencode.json intent is next tag", () => {
|
describe("#when opencode.json intent is next tag", () => {
|
||||||
it("#then updates package.json to use next", async () => {
|
it("#then updates package.json to use next", async () => {
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@next",
|
entry: "oh-my-opencode@next",
|
||||||
@@ -100,7 +104,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
|
|
||||||
describe("#when opencode.json has no version (implies latest)", () => {
|
describe("#when opencode.json has no version (implies latest)", () => {
|
||||||
it("#then updates package.json to use latest", async () => {
|
it("#then updates package.json to use latest", async () => {
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode",
|
entry: "oh-my-opencode",
|
||||||
@@ -121,7 +125,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
describe("#given cache package.json already matches intent", () => {
|
describe("#given cache package.json already matches intent", () => {
|
||||||
it("#then returns synced false with no error", async () => {
|
it("#then returns synced false with no error", async () => {
|
||||||
resetTestCache("latest")
|
resetTestCache("latest")
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -141,7 +145,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
describe("#given cache package.json does not exist", () => {
|
describe("#given cache package.json does not exist", () => {
|
||||||
it("#then returns file_not_found error", async () => {
|
it("#then returns file_not_found error", async () => {
|
||||||
cleanupTestCache()
|
cleanupTestCache()
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -166,7 +170,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
JSON.stringify({ dependencies: { other: "1.0.0" } }, null, 2)
|
JSON.stringify({ dependencies: { other: "1.0.0" } }, null, 2)
|
||||||
)
|
)
|
||||||
|
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -185,7 +189,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
describe("#given user explicitly changed from one semver to another", () => {
|
describe("#given user explicitly changed from one semver to another", () => {
|
||||||
it("#then updates package.json to new version", async () => {
|
it("#then updates package.json to new version", async () => {
|
||||||
resetTestCache("3.9.0")
|
resetTestCache("3.9.0")
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@3.10.0",
|
entry: "oh-my-opencode@3.10.0",
|
||||||
@@ -204,7 +208,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
|
|
||||||
describe("#given cache package.json with other dependencies", () => {
|
describe("#given cache package.json with other dependencies", () => {
|
||||||
it("#then other dependencies are preserved when updating plugin version", async () => {
|
it("#then other dependencies are preserved when updating plugin version", async () => {
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -230,7 +234,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||||
writeFileSync(join(TEST_CACHE_DIR, "package.json"), "{ invalid json }")
|
writeFileSync(join(TEST_CACHE_DIR, "package.json"), "{ invalid json }")
|
||||||
|
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -268,7 +272,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
@@ -318,7 +322,7 @@ describe("syncCachePackageJsonToIntent", () => {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
|
const { syncCachePackageJsonToIntent } = await importFreshSyncPackageJsonModule()
|
||||||
|
|
||||||
const pluginInfo: PluginEntryInfo = {
|
const pluginInfo: PluginEntryInfo = {
|
||||||
entry: "oh-my-opencode@latest",
|
entry: "oh-my-opencode@latest",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
|
import { beforeEach, describe, expect, it, mock } from "bun:test"
|
||||||
|
|
||||||
type PluginEntry = {
|
type PluginEntry = {
|
||||||
entry: string
|
entry: string
|
||||||
@@ -35,28 +35,29 @@ const mockShowAutoUpdatedToast = mock(
|
|||||||
|
|
||||||
const mockSyncCachePackageJsonToIntent = mock(() => false)
|
const mockSyncCachePackageJsonToIntent = mock(() => false)
|
||||||
|
|
||||||
mock.module("../checker", () => ({
|
let importCounter = 0
|
||||||
findPluginEntry: mockFindPluginEntry,
|
|
||||||
getCachedVersion: mockGetCachedVersion,
|
|
||||||
getLatestVersion: mockGetLatestVersion,
|
|
||||||
revertPinnedVersion: mock(() => false),
|
|
||||||
syncCachePackageJsonToIntent: mockSyncCachePackageJsonToIntent,
|
|
||||||
}))
|
|
||||||
mock.module("../version-channel", () => ({ extractChannel: mockExtractChannel }))
|
|
||||||
mock.module("../cache", () => ({ invalidatePackage: mockInvalidatePackage }))
|
|
||||||
mock.module("../../../cli/config-manager", () => ({ runBunInstallWithDetails: mockRunBunInstallWithDetails }))
|
|
||||||
mock.module("./update-toasts", () => ({
|
|
||||||
showUpdateAvailableToast: mockShowUpdateAvailableToast,
|
|
||||||
showAutoUpdatedToast: mockShowAutoUpdatedToast,
|
|
||||||
}))
|
|
||||||
mock.module("../../../shared/logger", () => ({ log: () => {} }))
|
|
||||||
|
|
||||||
afterAll(() => {
|
async function importFreshBackgroundUpdateCheck(): Promise<typeof import("./background-update-check")> {
|
||||||
|
mock.module("../checker", () => ({
|
||||||
|
findPluginEntry: mockFindPluginEntry,
|
||||||
|
getCachedVersion: mockGetCachedVersion,
|
||||||
|
getLatestVersion: mockGetLatestVersion,
|
||||||
|
revertPinnedVersion: mock(() => false),
|
||||||
|
syncCachePackageJsonToIntent: mockSyncCachePackageJsonToIntent,
|
||||||
|
}))
|
||||||
|
mock.module("../version-channel", () => ({ extractChannel: mockExtractChannel }))
|
||||||
|
mock.module("../cache", () => ({ invalidatePackage: mockInvalidatePackage }))
|
||||||
|
mock.module("../../../cli/config-manager", () => ({ runBunInstallWithDetails: mockRunBunInstallWithDetails }))
|
||||||
|
mock.module("./update-toasts", () => ({
|
||||||
|
showUpdateAvailableToast: mockShowUpdateAvailableToast,
|
||||||
|
showAutoUpdatedToast: mockShowAutoUpdatedToast,
|
||||||
|
}))
|
||||||
|
mock.module("../../../shared/logger", () => ({ log: () => {} }))
|
||||||
|
|
||||||
|
const backgroundUpdateCheckModule = await import(`./background-update-check?test=${importCounter++}`)
|
||||||
mock.restore()
|
mock.restore()
|
||||||
})
|
return backgroundUpdateCheckModule
|
||||||
|
}
|
||||||
const modulePath = "./background-update-check?test"
|
|
||||||
const { runBackgroundUpdateCheck } = await import(modulePath)
|
|
||||||
|
|
||||||
describe("runBackgroundUpdateCheck", () => {
|
describe("runBackgroundUpdateCheck", () => {
|
||||||
const mockCtx = { directory: "/test" } as PluginInput
|
const mockCtx = { directory: "/test" } as PluginInput
|
||||||
@@ -85,6 +86,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given no plugin entry found", () => {
|
describe("#given no plugin entry found", () => {
|
||||||
it("returns early without showing any toast", async () => {
|
it("returns early without showing any toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockFindPluginEntry.mockReturnValue(null)
|
mockFindPluginEntry.mockReturnValue(null)
|
||||||
//#when
|
//#when
|
||||||
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
||||||
@@ -99,6 +101,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given no version available", () => {
|
describe("#given no version available", () => {
|
||||||
it("returns early when neither cached nor pinned version exists", async () => {
|
it("returns early when neither cached nor pinned version exists", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockFindPluginEntry.mockReturnValue(createPluginEntry({ entry: "oh-my-opencode" }))
|
mockFindPluginEntry.mockReturnValue(createPluginEntry({ entry: "oh-my-opencode" }))
|
||||||
mockGetCachedVersion.mockReturnValue(null)
|
mockGetCachedVersion.mockReturnValue(null)
|
||||||
//#when
|
//#when
|
||||||
@@ -114,6 +117,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given latest version fetch fails", () => {
|
describe("#given latest version fetch fails", () => {
|
||||||
it("returns early without toasts", async () => {
|
it("returns early without toasts", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockGetLatestVersion.mockResolvedValue(null)
|
mockGetLatestVersion.mockResolvedValue(null)
|
||||||
//#when
|
//#when
|
||||||
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
||||||
@@ -128,6 +132,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given already on latest version", () => {
|
describe("#given already on latest version", () => {
|
||||||
it("returns early without any action", async () => {
|
it("returns early without any action", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockGetCachedVersion.mockReturnValue("3.4.0")
|
mockGetCachedVersion.mockReturnValue("3.4.0")
|
||||||
mockGetLatestVersion.mockResolvedValue("3.4.0")
|
mockGetLatestVersion.mockResolvedValue("3.4.0")
|
||||||
//#when
|
//#when
|
||||||
@@ -143,6 +148,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given update available with autoUpdate disabled", () => {
|
describe("#given update available with autoUpdate disabled", () => {
|
||||||
it("shows update notification but does not install", async () => {
|
it("shows update notification but does not install", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
const autoUpdate = false
|
const autoUpdate = false
|
||||||
//#when
|
//#when
|
||||||
await runBackgroundUpdateCheck(mockCtx, autoUpdate, getToastMessage)
|
await runBackgroundUpdateCheck(mockCtx, autoUpdate, getToastMessage)
|
||||||
@@ -156,6 +162,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given user has pinned a specific version", () => {
|
describe("#given user has pinned a specific version", () => {
|
||||||
it("shows pinned-version toast without auto-updating", async () => {
|
it("shows pinned-version toast without auto-updating", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockFindPluginEntry.mockReturnValue(createPluginEntry({ isPinned: true, pinnedVersion: "3.4.0" }))
|
mockFindPluginEntry.mockReturnValue(createPluginEntry({ isPinned: true, pinnedVersion: "3.4.0" }))
|
||||||
//#when
|
//#when
|
||||||
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
||||||
@@ -167,6 +174,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
|
|
||||||
it("toast message mentions version pinned", async () => {
|
it("toast message mentions version pinned", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
let capturedToastMessage: ToastMessageGetter | undefined
|
let capturedToastMessage: ToastMessageGetter | undefined
|
||||||
mockFindPluginEntry.mockReturnValue(createPluginEntry({ isPinned: true, pinnedVersion: "3.4.0" }))
|
mockFindPluginEntry.mockReturnValue(createPluginEntry({ isPinned: true, pinnedVersion: "3.4.0" }))
|
||||||
mockShowUpdateAvailableToast.mockImplementation(
|
mockShowUpdateAvailableToast.mockImplementation(
|
||||||
@@ -191,6 +199,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given unpinned with auto-update and install succeeds", () => {
|
describe("#given unpinned with auto-update and install succeeds", () => {
|
||||||
it("syncs cache, invalidates, installs, and shows auto-updated toast", async () => {
|
it("syncs cache, invalidates, installs, and shows auto-updated toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockRunBunInstallWithDetails.mockResolvedValue({ success: true })
|
mockRunBunInstallWithDetails.mockResolvedValue({ success: true })
|
||||||
//#when
|
//#when
|
||||||
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
||||||
@@ -204,6 +213,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
|
|
||||||
it("syncs before invalidate and install (correct order)", async () => {
|
it("syncs before invalidate and install (correct order)", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
const callOrder: string[] = []
|
const callOrder: string[] = []
|
||||||
mockSyncCachePackageJsonToIntent.mockImplementation(() => {
|
mockSyncCachePackageJsonToIntent.mockImplementation(() => {
|
||||||
callOrder.push("sync")
|
callOrder.push("sync")
|
||||||
@@ -226,6 +236,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given unpinned with auto-update and install fails", () => {
|
describe("#given unpinned with auto-update and install fails", () => {
|
||||||
it("falls back to notification-only toast", async () => {
|
it("falls back to notification-only toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockRunBunInstallWithDetails.mockResolvedValue({ success: false })
|
mockRunBunInstallWithDetails.mockResolvedValue({ success: false })
|
||||||
//#when
|
//#when
|
||||||
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
await runBackgroundUpdateCheck(mockCtx, true, getToastMessage)
|
||||||
@@ -239,6 +250,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given sync fails with file_not_found", () => {
|
describe("#given sync fails with file_not_found", () => {
|
||||||
it("aborts update and shows notification-only toast", async () => {
|
it("aborts update and shows notification-only toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
||||||
synced: false,
|
synced: false,
|
||||||
error: "file_not_found",
|
error: "file_not_found",
|
||||||
@@ -258,6 +270,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given sync fails with plugin_not_in_deps", () => {
|
describe("#given sync fails with plugin_not_in_deps", () => {
|
||||||
it("aborts update and shows notification-only toast", async () => {
|
it("aborts update and shows notification-only toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
||||||
synced: false,
|
synced: false,
|
||||||
error: "plugin_not_in_deps",
|
error: "plugin_not_in_deps",
|
||||||
@@ -277,6 +290,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given sync fails with parse_error", () => {
|
describe("#given sync fails with parse_error", () => {
|
||||||
it("aborts update and shows notification-only toast", async () => {
|
it("aborts update and shows notification-only toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
||||||
synced: false,
|
synced: false,
|
||||||
error: "parse_error",
|
error: "parse_error",
|
||||||
@@ -296,6 +310,7 @@ describe("runBackgroundUpdateCheck", () => {
|
|||||||
describe("#given sync fails with write_error", () => {
|
describe("#given sync fails with write_error", () => {
|
||||||
it("aborts update and shows notification-only toast", async () => {
|
it("aborts update and shows notification-only toast", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
mockSyncCachePackageJsonToIntent.mockReturnValue({
|
||||||
synced: false,
|
synced: false,
|
||||||
error: "write_error",
|
error: "write_error",
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import { afterAll, afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
|
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test"
|
||||||
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
|
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
|
import * as shared from "../../../shared"
|
||||||
|
|
||||||
type PluginEntry = {
|
type PluginEntry = {
|
||||||
entry: string
|
entry: string
|
||||||
@@ -45,68 +46,71 @@ const mockRunBunInstallWithDetails = mock(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
mock.module("../checker", () => ({
|
let importCounter = 0
|
||||||
findPluginEntry: mockFindPluginEntry,
|
let getOpenCodeCacheDirSpy: { mockRestore: () => void } | undefined
|
||||||
getCachedVersion: mockGetCachedVersion,
|
let getOpenCodeConfigPathsSpy: { mockRestore: () => void } | undefined
|
||||||
getLatestVersion: mockGetLatestVersion,
|
|
||||||
revertPinnedVersion: mock(() => false),
|
async function importFreshBackgroundUpdateCheck(): Promise<typeof import("./background-update-check")> {
|
||||||
syncCachePackageJsonToIntent: mockSyncCachePackageJsonToIntent,
|
mock.module("../checker", () => ({
|
||||||
}))
|
findPluginEntry: mockFindPluginEntry,
|
||||||
mock.module("../version-channel", () => ({ extractChannel: mockExtractChannel }))
|
getCachedVersion: mockGetCachedVersion,
|
||||||
mock.module("../cache", () => ({ invalidatePackage: mockInvalidatePackage }))
|
getLatestVersion: mockGetLatestVersion,
|
||||||
mock.module("../../../cli/config-manager", () => ({
|
revertPinnedVersion: mock(() => false),
|
||||||
runBunInstallWithDetails: mockRunBunInstallWithDetails,
|
syncCachePackageJsonToIntent: mockSyncCachePackageJsonToIntent,
|
||||||
}))
|
}))
|
||||||
mock.module("./update-toasts", () => ({
|
mock.module("../version-channel", () => ({ extractChannel: mockExtractChannel }))
|
||||||
showUpdateAvailableToast: mockShowUpdateAvailableToast,
|
mock.module("../cache", () => ({ invalidatePackage: mockInvalidatePackage }))
|
||||||
showAutoUpdatedToast: mockShowAutoUpdatedToast,
|
mock.module("../../../cli/config-manager", () => ({
|
||||||
}))
|
runBunInstallWithDetails: mockRunBunInstallWithDetails,
|
||||||
mock.module("../../../shared/logger", () => ({ log: () => {} }))
|
}))
|
||||||
mock.module("../../../shared", () => ({
|
mock.module("./update-toasts", () => ({
|
||||||
getOpenCodeCacheDir: () => TEST_CACHE_DIR,
|
showUpdateAvailableToast: mockShowUpdateAvailableToast,
|
||||||
getOpenCodeConfigPaths: () => ({
|
showAutoUpdatedToast: mockShowAutoUpdatedToast,
|
||||||
|
}))
|
||||||
|
mock.module("../../../shared/logger", () => ({ log: () => {} }))
|
||||||
|
getOpenCodeCacheDirSpy = spyOn(shared, "getOpenCodeCacheDir").mockReturnValue(TEST_CACHE_DIR)
|
||||||
|
getOpenCodeConfigPathsSpy = spyOn(shared, "getOpenCodeConfigPaths").mockReturnValue({
|
||||||
configDir: TEST_CONFIG_DIR,
|
configDir: TEST_CONFIG_DIR,
|
||||||
configJson: join(TEST_CONFIG_DIR, "opencode.json"),
|
configJson: join(TEST_CONFIG_DIR, "opencode.json"),
|
||||||
configJsonc: join(TEST_CONFIG_DIR, "opencode.jsonc"),
|
configJsonc: join(TEST_CONFIG_DIR, "opencode.jsonc"),
|
||||||
packageJson: join(TEST_CONFIG_DIR, "package.json"),
|
packageJson: join(TEST_CONFIG_DIR, "package.json"),
|
||||||
omoConfig: join(TEST_CONFIG_DIR, "oh-my-opencode.json"),
|
omoConfig: join(TEST_CONFIG_DIR, "oh-my-opencode.json"),
|
||||||
}),
|
})
|
||||||
getOpenCodeConfigDir: () => TEST_CONFIG_DIR,
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Mock constants BEFORE importing the module
|
mock.module("../constants", () => ({
|
||||||
const ORIGINAL_PACKAGE_NAME = "oh-my-opencode"
|
PACKAGE_NAME: "oh-my-opencode",
|
||||||
mock.module("../constants", () => ({
|
CACHE_DIR: TEST_CACHE_DIR,
|
||||||
PACKAGE_NAME: ORIGINAL_PACKAGE_NAME,
|
USER_CONFIG_DIR: TEST_CONFIG_DIR,
|
||||||
CACHE_DIR: TEST_CACHE_DIR,
|
NPM_REGISTRY_URL: "https://registry.npmjs.org/-/package/oh-my-opencode/dist-tags",
|
||||||
USER_CONFIG_DIR: TEST_CONFIG_DIR,
|
NPM_FETCH_TIMEOUT: 5000,
|
||||||
}))
|
VERSION_FILE: join(TEST_CACHE_DIR, "version"),
|
||||||
|
USER_OPENCODE_CONFIG: join(TEST_CONFIG_DIR, "opencode.json"),
|
||||||
|
USER_OPENCODE_CONFIG_JSONC: join(TEST_CONFIG_DIR, "opencode.jsonc"),
|
||||||
|
INSTALLED_PACKAGE_JSON: join(TEST_CACHE_DIR, "node_modules", "oh-my-opencode", "package.json"),
|
||||||
|
getWindowsAppdataDir: () => null,
|
||||||
|
}))
|
||||||
|
|
||||||
// Need to mock getOpenCodeCacheDir and getOpenCodeConfigPaths before importing the module
|
mock.module("../../../shared/data-path", () => ({
|
||||||
mock.module("../../../shared/data-path", () => ({
|
getDataDir: () => join(TEST_DIR, "data"),
|
||||||
getDataDir: () => join(TEST_DIR, "data"),
|
getOpenCodeStorageDir: () => join(TEST_DIR, "data", "opencode", "storage"),
|
||||||
getOpenCodeStorageDir: () => join(TEST_DIR, "data", "opencode", "storage"),
|
getCacheDir: () => TEST_DIR,
|
||||||
getCacheDir: () => TEST_DIR,
|
getOmoOpenCodeCacheDir: () => join(TEST_DIR, "oh-my-opencode"),
|
||||||
getOmoOpenCodeCacheDir: () => join(TEST_DIR, "oh-my-opencode"),
|
getOpenCodeCacheDir: () => TEST_CACHE_DIR,
|
||||||
getOpenCodeCacheDir: () => TEST_CACHE_DIR,
|
}))
|
||||||
}))
|
mock.module("../../../shared/opencode-config-dir", () => ({
|
||||||
mock.module("../../../shared/opencode-config-dir", () => ({
|
getOpenCodeConfigDir: () => TEST_CONFIG_DIR,
|
||||||
getOpenCodeConfigDir: () => TEST_CONFIG_DIR,
|
getOpenCodeConfigPaths: () => ({
|
||||||
getOpenCodeConfigPaths: () => ({
|
configDir: TEST_CONFIG_DIR,
|
||||||
configDir: TEST_CONFIG_DIR,
|
configJson: join(TEST_CONFIG_DIR, "opencode.json"),
|
||||||
configJson: join(TEST_CONFIG_DIR, "opencode.json"),
|
configJsonc: join(TEST_CONFIG_DIR, "opencode.jsonc"),
|
||||||
configJsonc: join(TEST_CONFIG_DIR, "opencode.jsonc"),
|
packageJson: join(TEST_CONFIG_DIR, "package.json"),
|
||||||
packageJson: join(TEST_CONFIG_DIR, "package.json"),
|
omoConfig: join(TEST_CONFIG_DIR, "oh-my-opencode.json"),
|
||||||
omoConfig: join(TEST_CONFIG_DIR, "oh-my-opencode.json"),
|
}),
|
||||||
}),
|
}))
|
||||||
}))
|
|
||||||
|
|
||||||
afterAll(() => {
|
const backgroundUpdateCheckModule = await import(`./background-update-check?test=${importCounter++}`)
|
||||||
mock.restore()
|
return backgroundUpdateCheckModule
|
||||||
})
|
}
|
||||||
|
|
||||||
const modulePath = "./background-update-check?test"
|
|
||||||
const { runBackgroundUpdateCheck } = await import(modulePath)
|
|
||||||
|
|
||||||
describe("workspace resolution", () => {
|
describe("workspace resolution", () => {
|
||||||
const mockCtx = { directory: "/test" } as PluginInput
|
const mockCtx = { directory: "/test" } as PluginInput
|
||||||
@@ -138,6 +142,11 @@ describe("workspace resolution", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
getOpenCodeCacheDirSpy?.mockRestore()
|
||||||
|
getOpenCodeConfigPathsSpy?.mockRestore()
|
||||||
|
getOpenCodeCacheDirSpy = undefined
|
||||||
|
getOpenCodeConfigPathsSpy = undefined
|
||||||
|
mock.restore()
|
||||||
if (existsSync(TEST_DIR)) {
|
if (existsSync(TEST_DIR)) {
|
||||||
rmSync(TEST_DIR, { recursive: true, force: true })
|
rmSync(TEST_DIR, { recursive: true, force: true })
|
||||||
}
|
}
|
||||||
@@ -146,6 +155,7 @@ describe("workspace resolution", () => {
|
|||||||
describe("#given config-dir install exists but cache-dir does not", () => {
|
describe("#given config-dir install exists but cache-dir does not", () => {
|
||||||
it("installs to config-dir, not cache-dir", async () => {
|
it("installs to config-dir, not cache-dir", async () => {
|
||||||
//#given - config-dir has installation, cache-dir does not
|
//#given - config-dir has installation, cache-dir does not
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mkdirSync(join(TEST_CONFIG_DIR, "node_modules", "oh-my-opencode"), { recursive: true })
|
mkdirSync(join(TEST_CONFIG_DIR, "node_modules", "oh-my-opencode"), { recursive: true })
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
join(TEST_CONFIG_DIR, "package.json"),
|
join(TEST_CONFIG_DIR, "package.json"),
|
||||||
@@ -171,6 +181,7 @@ describe("workspace resolution", () => {
|
|||||||
describe("#given both config-dir and cache-dir exist", () => {
|
describe("#given both config-dir and cache-dir exist", () => {
|
||||||
it("prefers config-dir over cache-dir", async () => {
|
it("prefers config-dir over cache-dir", async () => {
|
||||||
//#given - both directories have installations
|
//#given - both directories have installations
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mkdirSync(join(TEST_CONFIG_DIR, "node_modules", "oh-my-opencode"), { recursive: true })
|
mkdirSync(join(TEST_CONFIG_DIR, "node_modules", "oh-my-opencode"), { recursive: true })
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
join(TEST_CONFIG_DIR, "package.json"),
|
join(TEST_CONFIG_DIR, "package.json"),
|
||||||
@@ -203,6 +214,7 @@ describe("workspace resolution", () => {
|
|||||||
describe("#given only cache-dir install exists", () => {
|
describe("#given only cache-dir install exists", () => {
|
||||||
it("falls back to cache-dir", async () => {
|
it("falls back to cache-dir", async () => {
|
||||||
//#given - only cache-dir has installation
|
//#given - only cache-dir has installation
|
||||||
|
const { runBackgroundUpdateCheck } = await importFreshBackgroundUpdateCheck()
|
||||||
mkdirSync(join(TEST_CACHE_DIR, "node_modules", "oh-my-opencode"), { recursive: true })
|
mkdirSync(join(TEST_CACHE_DIR, "node_modules", "oh-my-opencode"), { recursive: true })
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
join(TEST_CACHE_DIR, "package.json"),
|
join(TEST_CACHE_DIR, "package.json"),
|
||||||
|
|||||||
Reference in New Issue
Block a user