Browse Source

fix(auto-update): avoid unsafe migration toast

Alvin Unreal 2 months ago
parent
commit
86ad00e10b

+ 1 - 0
src/hooks/auto-update-checker/checker.test.ts

@@ -265,6 +265,7 @@ describe('auto-update-checker/checker', () => {
         latestVersion: null,
         latestMajorVersion: '2.0.0',
         blockedByMajor: true,
+        unsafeReason: 'unparseable-current-version',
       });
 
       globalThis.fetch = originalFetch;

+ 1 - 0
src/hooks/auto-update-checker/checker.ts

@@ -414,6 +414,7 @@ export async function getLatestCompatibleVersion(
       latestVersion: null,
       latestMajorVersion: latestVersion,
       blockedByMajor: latestVersion !== null,
+      unsafeReason: latestVersion ? 'unparseable-current-version' : undefined,
     };
   }
 

+ 36 - 0
src/hooks/auto-update-checker/index.test.ts

@@ -371,4 +371,40 @@ describe('auto-update-checker/index', () => {
     expect(cacheMocks.preparePackageUpdate).not.toHaveBeenCalled();
     expect(crossSpawnMock).not.toHaveBeenCalled();
   });
+
+  test('does not show migration copy for unparseable current versions', async () => {
+    checkerMocks.findPluginEntry.mockImplementation(() => ({
+      pinnedVersion: 'workspace:*',
+      isPinned: true,
+    }));
+    checkerMocks.getCachedVersion.mockImplementation(() => null);
+    checkerMocks.getLatestCompatibleVersion.mockImplementation(async () => ({
+      latestVersion: null,
+      latestMajorVersion: '1.9.0',
+      blockedByMajor: true,
+      unsafeReason: 'unparseable-current-version',
+    }));
+
+    const { createAutoUpdateCheckerHook } = await import(
+      `./index?test=${importCounter++}`
+    );
+    const { ctx, showToast } = createCtx();
+
+    const hook = createAutoUpdateCheckerHook(ctx as never);
+    hook.event({ event: { type: 'session.created', properties: {} } });
+    await waitForCalls(showToast);
+
+    expect(showToast).toHaveBeenCalledTimes(1);
+    expect(showToast).toHaveBeenCalledWith({
+      body: {
+        title: 'OMO-Slim 1.9.0',
+        message:
+          'v1.9.0 available. Auto-update skipped because the current version could not be compared safely.',
+        variant: 'info',
+        duration: 8000,
+      },
+    });
+    expect(cacheMocks.preparePackageUpdate).not.toHaveBeenCalled();
+    expect(crossSpawnMock).not.toHaveBeenCalled();
+  });
 });

+ 16 - 0
src/hooks/auto-update-checker/index.ts

@@ -78,6 +78,22 @@ async function runBackgroundUpdateCheck(
 
   const channel = extractChannel(pluginInfo.pinnedVersion ?? currentVersion);
   const latestInfo = await getLatestCompatibleVersion(currentVersion, channel);
+  if (latestInfo.unsafeReason === 'unparseable-current-version') {
+    log(
+      `[auto-update-checker] Current version is not semver; skipping auto-update: ${currentVersion}`,
+    );
+    if (latestInfo.latestMajorVersion) {
+      showToast(
+        ctx,
+        `OMO-Slim ${latestInfo.latestMajorVersion}`,
+        `v${latestInfo.latestMajorVersion} available. Auto-update skipped because the current version could not be compared safely.`,
+        'info',
+        8000,
+      );
+    }
+    return;
+  }
+
   if (latestInfo.blockedByMajor && latestInfo.latestMajorVersion) {
     showMajorUpgradeToast(ctx, latestInfo.latestMajorVersion);
     log(

+ 1 - 0
src/hooks/auto-update-checker/types.ts

@@ -12,6 +12,7 @@ export interface CompatibleVersionResult {
   latestVersion: string | null;
   latestMajorVersion: string | null;
   blockedByMajor: boolean;
+  unsafeReason?: 'unparseable-current-version';
 }
 
 export interface OpencodeConfig {