1- import { Expect , SetupFixture , Teardown , Test } from 'alsatian' ;
1+ import { AsyncTest , Expect , SetupFixture , SpyOn , Teardown , Test } from 'alsatian' ;
22
33import { AppManager } from '../../src/server/AppManager' ;
44import { AppBridges } from '../../src/server/bridges' ;
@@ -14,7 +14,7 @@ import {
1414 AppOutboundCommunicationProviderManager ,
1515} from '../../src/server/managers' ;
1616import type { AppLogStorage , AppMetadataStorage , AppSourceStorage } from '../../src/server/storage' ;
17- import { SimpleClass , TestInfastructureSetup } from '../test-data/utilities' ;
17+ import { SimpleClass , TestData , TestInfastructureSetup } from '../test-data/utilities' ;
1818
1919export class AppManagerTestFixture {
2020 private testingInfastructure : TestInfastructureSetup ;
@@ -121,4 +121,142 @@ export class AppManagerTestFixture {
121121 Expect ( manager . getVideoConfProviderManager ( ) instanceof AppVideoConfProviderManager ) . toBe ( true ) ;
122122 Expect ( manager . getOutboundCommunicationProviderManager ( ) instanceof AppOutboundCommunicationProviderManager ) . toBe ( true ) ;
123123 }
124+
125+ @AsyncTest ( 'Update Apps Marketplace Info - Apps without subscription info are skipped' )
126+ public async updateAppsMarketplaceInfoSkipsAppsWithoutSubscriptionInfo ( ) {
127+ const manager = new AppManager ( {
128+ metadataStorage : this . testingInfastructure . getAppStorage ( ) ,
129+ logStorage : this . testingInfastructure . getLogStorage ( ) ,
130+ bridges : this . testingInfastructure . getAppBridges ( ) ,
131+ sourceStorage : this . testingInfastructure . getSourceStorage ( ) ,
132+ } ) ;
133+
134+ const appsOverview = TestData . getAppsOverview ( ) ;
135+ appsOverview [ 0 ] . latest . subscriptionInfo = undefined ; // No subscription info
136+
137+ // Mock the apps Map to return our mock app
138+ ( manager as any ) . apps = new Map ( [ [ 'test-app' , TestData . getMockApp ( TestData . getAppStorageItem ( ) , manager ) ] ] ) ;
139+
140+ const updatePartialAndReturnDocumentSpy = SpyOn ( manager . getStorage ( ) , 'updatePartialAndReturnDocument' ) ;
141+ updatePartialAndReturnDocumentSpy . andReturn ( Promise . resolve ( ) ) ;
142+
143+ // Should not throw and complete successfully
144+ await manager . updateAppsMarketplaceInfo ( appsOverview ) ;
145+
146+ Expect ( updatePartialAndReturnDocumentSpy ) . not . toHaveBeenCalled ( ) ;
147+ }
148+
149+ @AsyncTest ( 'Update Apps Marketplace Info - Apps not found in manager are skipped' )
150+ public async updateAppsMarketplaceInfoSkipsAppsNotInManager ( ) {
151+ const manager = new AppManager ( {
152+ metadataStorage : this . testingInfastructure . getAppStorage ( ) ,
153+ logStorage : this . testingInfastructure . getLogStorage ( ) ,
154+ bridges : this . testingInfastructure . getAppBridges ( ) ,
155+ sourceStorage : this . testingInfastructure . getSourceStorage ( ) ,
156+ } ) ;
157+
158+ const appsOverview = TestData . getAppsOverview ( ) ;
159+ appsOverview [ 0 ] . latest . id = 'nonexistent-app' ; // App not in manager
160+
161+ // Mock the apps Map to return our mock app
162+ ( manager as any ) . apps = new Map ( [ [ 'test-app' , TestData . getMockApp ( TestData . getAppStorageItem ( ) , manager ) ] ] ) ;
163+
164+ const updatePartialAndReturnDocumentSpy = SpyOn ( manager . getStorage ( ) , 'updatePartialAndReturnDocument' ) ;
165+ updatePartialAndReturnDocumentSpy . andReturn ( Promise . resolve ( ) ) ;
166+
167+ // Should not throw and complete successfully
168+ await manager . updateAppsMarketplaceInfo ( appsOverview ) ;
169+
170+ Expect ( updatePartialAndReturnDocumentSpy ) . not . toHaveBeenCalled ( ) ;
171+ }
172+
173+ @AsyncTest ( 'Update Apps Marketplace Info - Apps with same license are skipped' )
174+ public async updateAppsMarketplaceInfoSkipsAppsWithSameLicense ( ) {
175+ const manager = new AppManager ( {
176+ metadataStorage : this . testingInfastructure . getAppStorage ( ) ,
177+ logStorage : this . testingInfastructure . getLogStorage ( ) ,
178+ bridges : this . testingInfastructure . getAppBridges ( ) ,
179+ sourceStorage : this . testingInfastructure . getSourceStorage ( ) ,
180+ } ) ;
181+
182+ const sameLicenseData = 'same-license-data' ;
183+ const existingSubscriptionInfo = TestData . getMarketplaceSubscriptionInfo ( {
184+ license : { license : sameLicenseData , version : 1 , expireDate : new Date ( '2023-01-01' ) } ,
185+ } ) ;
186+
187+ const mockStorageItem = TestData . getAppStorageItem ( {
188+ marketplaceInfo : [ TestData . getMarketplaceInfo ( { subscriptionInfo : existingSubscriptionInfo } ) ] ,
189+ } ) ;
190+
191+ const mockApp = TestData . getMockApp ( mockStorageItem , manager ) ;
192+
193+ // Mock the apps Map to return our mock app
194+ ( manager as any ) . apps = new Map ( [ [ 'test-app' , mockApp ] ] ) ;
195+
196+ const appsOverview = TestData . getAppsOverview (
197+ TestData . getMarketplaceSubscriptionInfo ( {
198+ license : { license : sameLicenseData , version : 1 , expireDate : new Date ( '2023-01-01' ) } ,
199+ } ) ,
200+ ) ;
201+
202+ const updatePartialAndReturnDocumentSpy = SpyOn ( manager . getStorage ( ) , 'updatePartialAndReturnDocument' ) ;
203+ updatePartialAndReturnDocumentSpy . andReturn ( Promise . resolve ( ) ) ;
204+
205+ // Should not throw and complete successfully
206+ await manager . updateAppsMarketplaceInfo ( appsOverview ) ;
207+
208+ // Verify the subscription info was not updated (should remain the same)
209+ Expect ( mockStorageItem . marketplaceInfo [ 0 ] . subscriptionInfo . seats ) . toBe ( 10 ) ; // Original value
210+ Expect ( updatePartialAndReturnDocumentSpy ) . not . toHaveBeenCalled ( ) ;
211+ }
212+
213+ @AsyncTest ( 'Update Apps Marketplace Info - Subscription info is updated and app is signed' )
214+ public async updateAppsMarketplaceInfoUpdatesSubscriptionAndSignsApp ( ) {
215+ const manager = new AppManager ( {
216+ metadataStorage : this . testingInfastructure . getAppStorage ( ) ,
217+ logStorage : this . testingInfastructure . getLogStorage ( ) ,
218+ bridges : this . testingInfastructure . getAppBridges ( ) ,
219+ sourceStorage : this . testingInfastructure . getSourceStorage ( ) ,
220+ } ) ;
221+
222+ const existingSubscriptionInfo = TestData . getMarketplaceSubscriptionInfo ( {
223+ license : { license : 'old-license-data' , version : 1 , expireDate : new Date ( '2023-01-01' ) } ,
224+ } ) ;
225+
226+ const newSubscriptionInfo = TestData . getMarketplaceSubscriptionInfo ( {
227+ seats : 20 ,
228+ maxSeats : 200 ,
229+ startDate : '2023-02-01' ,
230+ periodEnd : '2024-01-31' ,
231+ license : { license : 'new-license-data' , version : 1 , expireDate : new Date ( '2026-01-01' ) } ,
232+ } ) ;
233+
234+ const mockStorageItem = TestData . getAppStorageItem ( {
235+ marketplaceInfo : [ TestData . getMarketplaceInfo ( { subscriptionInfo : existingSubscriptionInfo } ) ] ,
236+ } ) ;
237+
238+ const mockApp = TestData . getMockApp ( mockStorageItem , manager ) ;
239+
240+ // eslint-disable-next-line no-return-assign
241+ SpyOn ( manager . getSignatureManager ( ) , 'signApp' ) . andReturn ( Promise . resolve ( 'signed-app-data' ) ) ;
242+ SpyOn ( mockApp , 'validateLicense' ) . andReturn ( Promise . resolve ( ) ) ;
243+
244+ const updatePartialAndReturnDocumentSpy = SpyOn ( manager . getStorage ( ) , 'updatePartialAndReturnDocument' ) ;
245+ updatePartialAndReturnDocumentSpy . andReturn ( Promise . resolve ( mockStorageItem ) ) ;
246+
247+ // Mock the apps Map and dependencies
248+ ( manager as any ) . apps = new Map ( [ [ 'test-app' , mockApp ] ] ) ;
249+
250+ const appsOverview = TestData . getAppsOverview ( newSubscriptionInfo ) ;
251+
252+ await manager . updateAppsMarketplaceInfo ( appsOverview ) ;
253+
254+ const expectedStorageItem = mockApp . getStorageItem ( ) ;
255+
256+ // Verify the subscription info was updated
257+ Expect ( expectedStorageItem . marketplaceInfo [ 0 ] . subscriptionInfo . seats ) . toBe ( 20 ) ;
258+ Expect ( expectedStorageItem . marketplaceInfo [ 0 ] . subscriptionInfo . license . license ) . toBe ( 'new-license-data' ) ;
259+ Expect ( expectedStorageItem . signature ) . toBe ( 'signed-app-data' ) ;
260+ Expect ( updatePartialAndReturnDocumentSpy ) . toHaveBeenCalled ( ) . exactly ( 1 ) . times ;
261+ }
124262}
0 commit comments