@@ -2,11 +2,25 @@ import {isDevelopment} from './context/local.js'
22import { currentProcessIsGlobal , inferPackageManagerForGlobalCLI } from './is-global.js'
33import { checkForCachedNewVersion , packageManagerFromUserAgent , PackageManager } from './node-package-manager.js'
44import { exec , isCI } from './system.js'
5- import { cliInstallCommand , getOutputUpdateCLIReminder , runCLIUpgrade , versionToAutoUpgrade } from './upgrade.js'
5+ import {
6+ cliInstallCommand ,
7+ getOutputUpdateCLIReminder ,
8+ hasBlockingAutoUpgradeNotification ,
9+ runCLIUpgrade ,
10+ versionToAutoUpgrade ,
11+ } from './upgrade.js'
12+ import { Notification , fetchNotifications } from './notifications-system.js'
613import { isPreReleaseVersion } from './version.js'
714import { getAutoUpgradeEnabled } from '../../private/node/conf-store.js'
815import { vi , describe , test , expect , beforeEach } from 'vitest'
916
17+ vi . mock ( './notifications-system.js' , async ( importOriginal ) => {
18+ const actual : any = await importOriginal ( )
19+ return {
20+ ...actual ,
21+ fetchNotifications : vi . fn ( ) ,
22+ }
23+ } )
1024vi . mock ( './context/local.js' )
1125vi . mock ( './is-global.js' )
1226vi . mock ( './node-package-manager.js' )
@@ -253,3 +267,56 @@ describe('versionToAutoUpgrade', () => {
253267 vi . mocked ( isPreReleaseVersion ) . mockReturnValue ( false )
254268 } )
255269} )
270+
271+ describe ( 'hasBlockingAutoUpgradeNotification' , ( ) => {
272+ function notification ( overrides : Partial < Notification > = { } ) : Notification {
273+ return {
274+ id : 'block-autoupgrade' ,
275+ message : 'Auto-upgrade temporarily disabled' ,
276+ type : 'error' ,
277+ frequency : 'always' ,
278+ ownerChannel : '#cli' ,
279+ surface : 'autoupgrade' ,
280+ ...overrides ,
281+ }
282+ }
283+
284+ test ( 'returns true when an error notification on the autoupgrade surface is active' , async ( ) => {
285+ vi . mocked ( fetchNotifications ) . mockResolvedValue ( { notifications : [ notification ( ) ] } )
286+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( true )
287+ } )
288+
289+ test ( 'returns false for non-error notifications on the autoupgrade surface' , async ( ) => {
290+ vi . mocked ( fetchNotifications ) . mockResolvedValue ( {
291+ notifications : [ notification ( { type : 'warning' } ) , notification ( { type : 'info' } ) ] ,
292+ } )
293+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( false )
294+ } )
295+
296+ test ( 'returns false for error notifications on other surfaces' , async ( ) => {
297+ vi . mocked ( fetchNotifications ) . mockResolvedValue ( { notifications : [ notification ( { surface : 'app' } ) ] } )
298+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( false )
299+ } )
300+
301+ test ( 'returns false when the current CLI version is below minVersion' , async ( ) => {
302+ vi . mocked ( fetchNotifications ) . mockResolvedValue ( { notifications : [ notification ( { minVersion : '999.0.0' } ) ] } )
303+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( false )
304+ } )
305+
306+ test ( 'returns false when the current CLI version is above maxVersion' , async ( ) => {
307+ vi . mocked ( fetchNotifications ) . mockResolvedValue ( { notifications : [ notification ( { maxVersion : '0.0.1' } ) ] } )
308+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( false )
309+ } )
310+
311+ test ( 'returns false when the notification window is in the past' , async ( ) => {
312+ vi . mocked ( fetchNotifications ) . mockResolvedValue ( {
313+ notifications : [ notification ( { minDate : '2000-01-01' , maxDate : '2000-01-02' } ) ] ,
314+ } )
315+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( false )
316+ } )
317+
318+ test ( 'fails open and returns false when fetching notifications throws' , async ( ) => {
319+ vi . mocked ( fetchNotifications ) . mockRejectedValue ( new Error ( 'network down' ) )
320+ await expect ( hasBlockingAutoUpgradeNotification ( ) ) . resolves . toBe ( false )
321+ } )
322+ } )
0 commit comments