From c55603782c8c234d9b32a3ec1864a1adce2f4031 Mon Sep 17 00:00:00 2001 From: acamq <179265037+acamq@users.noreply.github.com> Date: Wed, 11 Mar 2026 08:08:30 -0600 Subject: [PATCH] fix(auto-update): handle null JSON.parse and restore mocks on test failure --- .../checker/sync-package-json.test.ts | 76 ++++++++++--------- .../checker/sync-package-json.ts | 2 +- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/hooks/auto-update-checker/checker/sync-package-json.test.ts b/src/hooks/auto-update-checker/checker/sync-package-json.test.ts index a854e9395..3d5fe2325 100644 --- a/src/hooks/auto-update-checker/checker/sync-package-json.test.ts +++ b/src/hooks/auto-update-checker/checker/sync-package-json.test.ts @@ -241,25 +241,27 @@ describe("syncCachePackageJsonToIntent", () => { renameSync: fs.renameSync, })) - const { syncCachePackageJsonToIntent } = await import("./sync-package-json") + try { + const { syncCachePackageJsonToIntent } = await import("./sync-package-json") - const pluginInfo: PluginEntryInfo = { - entry: "oh-my-opencode@latest", - isPinned: false, - pinnedVersion: "latest", - configPath: "/tmp/opencode.json", + const pluginInfo: PluginEntryInfo = { + entry: "oh-my-opencode@latest", + isPinned: false, + pinnedVersion: "latest", + configPath: "/tmp/opencode.json", + } + + const result = syncCachePackageJsonToIntent(pluginInfo) + + expect(result.synced).toBe(false) + expect(result.error).toBe("write_error") + } finally { + mock.module("node:fs", () => ({ + ...fs, + writeFileSync: originalWriteFileSync, + renameSync: originalRenameSync, + })) } - - const result = syncCachePackageJsonToIntent(pluginInfo) - - expect(result.synced).toBe(false) - expect(result.error).toBe("write_error") - - mock.module("node:fs", () => ({ - ...fs, - writeFileSync: originalWriteFileSync, - renameSync: originalRenameSync, - })) }) }) @@ -289,27 +291,29 @@ describe("syncCachePackageJsonToIntent", () => { }), })) - const { syncCachePackageJsonToIntent } = await import("./sync-package-json") + try { + const { syncCachePackageJsonToIntent } = await import("./sync-package-json") - const pluginInfo: PluginEntryInfo = { - entry: "oh-my-opencode@latest", - isPinned: false, - pinnedVersion: "latest", - configPath: "/tmp/opencode.json", + const pluginInfo: PluginEntryInfo = { + entry: "oh-my-opencode@latest", + isPinned: false, + pinnedVersion: "latest", + configPath: "/tmp/opencode.json", + } + + const result = syncCachePackageJsonToIntent(pluginInfo) + + expect(result.synced).toBe(false) + expect(result.error).toBe("write_error") + expect(tempFilePath).not.toBeNull() + expect(existsSync(tempFilePath!)).toBe(false) + } finally { + mock.module("node:fs", () => ({ + ...fs, + writeFileSync: originalWriteFileSync, + renameSync: originalRenameSync, + })) } - - const result = syncCachePackageJsonToIntent(pluginInfo) - - expect(result.synced).toBe(false) - expect(result.error).toBe("write_error") - expect(tempFilePath).not.toBeNull() - expect(existsSync(tempFilePath!)).toBe(false) - - mock.module("node:fs", () => ({ - ...fs, - writeFileSync: originalWriteFileSync, - renameSync: originalRenameSync, - })) }) }) }) diff --git a/src/hooks/auto-update-checker/checker/sync-package-json.ts b/src/hooks/auto-update-checker/checker/sync-package-json.ts index 3b98f8988..841e307fc 100644 --- a/src/hooks/auto-update-checker/checker/sync-package-json.ts +++ b/src/hooks/auto-update-checker/checker/sync-package-json.ts @@ -57,7 +57,7 @@ export function syncCachePackageJsonToIntent(pluginInfo: PluginEntryInfo): SyncR return { synced: false, error: "parse_error", message: "Failed to parse cache package.json (malformed JSON)" } } - if (!pkgJson.dependencies?.[PACKAGE_NAME]) { + if (!pkgJson || !pkgJson.dependencies?.[PACKAGE_NAME]) { log("[auto-update-checker] Plugin not in cache package.json dependencies, nothing to sync") return { synced: false, error: "plugin_not_in_deps", message: "Plugin not in cache package.json dependencies" } }