Skip to content

Commit e76a09d

Browse files
Copilotd-gubert
andcommitted
chore: convert AppManager.spec.ts from Alsatian to NodeJS test framework (#37668)
Co-authored-by: Douglas Gubert <[email protected]>
1 parent fd79197 commit e76a09d

File tree

1 file changed

+271
-0
lines changed

1 file changed

+271
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
import * as assert from 'node:assert';
2+
import { describe, it, beforeEach, afterEach, mock } from 'node:test';
3+
4+
import { AppManager } from '../../../src/server/AppManager';
5+
import { AppBridges } from '../../../src/server/bridges';
6+
import { AppCompiler, AppPackageParser } from '../../../src/server/compiler';
7+
import {
8+
AppAccessorManager,
9+
AppApiManager,
10+
AppExternalComponentManager,
11+
AppListenerManager,
12+
AppSettingsManager,
13+
AppSlashCommandManager,
14+
AppVideoConfProviderManager,
15+
AppOutboundCommunicationProviderManager,
16+
} from '../../../src/server/managers';
17+
import type { AppLogStorage, AppMetadataStorage, AppSourceStorage } from '../../../src/server/storage';
18+
import { SimpleClass, TestData, TestInfastructureSetup } from '../../test-data/utilities';
19+
20+
describe('AppManager', () => {
21+
let testingInfastructure: TestInfastructureSetup;
22+
23+
beforeEach(() => {
24+
testingInfastructure = new TestInfastructureSetup();
25+
});
26+
27+
afterEach(() => {
28+
AppManager.Instance = undefined;
29+
});
30+
31+
it('Setup of the AppManager', () => {
32+
const manager = new AppManager({
33+
metadataStorage: testingInfastructure.getAppStorage(),
34+
logStorage: testingInfastructure.getLogStorage(),
35+
bridges: testingInfastructure.getAppBridges(),
36+
sourceStorage: testingInfastructure.getSourceStorage(),
37+
});
38+
39+
assert.strictEqual(manager.getStorage(), testingInfastructure.getAppStorage());
40+
assert.strictEqual(manager.getLogStorage(), testingInfastructure.getLogStorage());
41+
// NOTE: manager.getBridges() returns a proxy, so they are vlaue equality instead of reference equality
42+
assert.deepStrictEqual(manager.getBridges(), testingInfastructure.getAppBridges());
43+
assert.strictEqual(manager.areAppsLoaded(), false);
44+
45+
assert.throws(
46+
() =>
47+
new AppManager({
48+
metadataStorage: {} as AppMetadataStorage,
49+
logStorage: {} as AppLogStorage,
50+
bridges: {} as AppBridges,
51+
sourceStorage: {} as AppSourceStorage,
52+
}),
53+
{
54+
name: 'Error',
55+
message: 'There is already a valid AppManager instance',
56+
},
57+
);
58+
});
59+
60+
it('Invalid Storage and Bridge', () => {
61+
const invalid = new SimpleClass();
62+
63+
assert.throws(
64+
() =>
65+
new AppManager({
66+
metadataStorage: invalid as any,
67+
logStorage: invalid as any,
68+
bridges: invalid as any,
69+
sourceStorage: invalid as any,
70+
}),
71+
{
72+
name: 'Error',
73+
message: 'Invalid instance of the AppMetadataStorage',
74+
},
75+
);
76+
77+
assert.throws(
78+
() =>
79+
new AppManager({
80+
metadataStorage: testingInfastructure.getAppStorage(),
81+
logStorage: invalid as any,
82+
bridges: invalid as any,
83+
sourceStorage: invalid as any,
84+
}),
85+
{
86+
name: 'Error',
87+
message: 'Invalid instance of the AppLogStorage',
88+
},
89+
);
90+
91+
assert.throws(
92+
() =>
93+
new AppManager({
94+
metadataStorage: testingInfastructure.getAppStorage(),
95+
logStorage: testingInfastructure.getLogStorage(),
96+
bridges: invalid as any,
97+
sourceStorage: invalid as any,
98+
}),
99+
{
100+
name: 'Error',
101+
message: 'Invalid instance of the AppBridges',
102+
},
103+
);
104+
105+
assert.throws(
106+
() =>
107+
new AppManager({
108+
metadataStorage: testingInfastructure.getAppStorage(),
109+
logStorage: testingInfastructure.getLogStorage(),
110+
bridges: testingInfastructure.getAppBridges(),
111+
sourceStorage: invalid as any,
112+
}),
113+
{
114+
name: 'Error',
115+
message: 'Invalid instance of the AppSourceStorage',
116+
},
117+
);
118+
});
119+
120+
it('Ensure Managers are Valid Types', () => {
121+
const manager = new AppManager({
122+
metadataStorage: testingInfastructure.getAppStorage(),
123+
logStorage: testingInfastructure.getLogStorage(),
124+
bridges: testingInfastructure.getAppBridges(),
125+
sourceStorage: testingInfastructure.getSourceStorage(),
126+
});
127+
128+
assert.ok(manager.getParser() instanceof AppPackageParser);
129+
assert.ok(manager.getCompiler() instanceof AppCompiler);
130+
assert.ok(manager.getAccessorManager() instanceof AppAccessorManager);
131+
assert.ok(manager.getBridges() instanceof AppBridges);
132+
assert.ok(manager.getListenerManager() instanceof AppListenerManager);
133+
assert.ok(manager.getCommandManager() instanceof AppSlashCommandManager);
134+
assert.ok(manager.getExternalComponentManager() instanceof AppExternalComponentManager);
135+
assert.ok(manager.getApiManager() instanceof AppApiManager);
136+
assert.ok(manager.getSettingsManager() instanceof AppSettingsManager);
137+
assert.ok(manager.getVideoConfProviderManager() instanceof AppVideoConfProviderManager);
138+
assert.ok(manager.getOutboundCommunicationProviderManager() instanceof AppOutboundCommunicationProviderManager);
139+
});
140+
141+
it('Update Apps Marketplace Info - Apps without subscription info are skipped', async () => {
142+
const manager = new AppManager({
143+
metadataStorage: testingInfastructure.getAppStorage(),
144+
logStorage: testingInfastructure.getLogStorage(),
145+
bridges: testingInfastructure.getAppBridges(),
146+
sourceStorage: testingInfastructure.getSourceStorage(),
147+
});
148+
149+
const appsOverview = TestData.getAppsOverview();
150+
appsOverview[0].latest.subscriptionInfo = undefined; // No subscription info
151+
152+
// Mock the apps Map to return our mock app
153+
(manager as any).apps = new Map([['test-app', TestData.getMockApp(TestData.getAppStorageItem(), manager)]]);
154+
155+
const updatePartialAndReturnDocumentSpy = mock.method(manager.getStorage(), 'updatePartialAndReturnDocument', () => Promise.resolve());
156+
157+
// Should not throw and complete successfully
158+
await manager.updateAppsMarketplaceInfo(appsOverview);
159+
160+
assert.strictEqual(updatePartialAndReturnDocumentSpy.mock.calls.length, 0);
161+
});
162+
163+
it('Update Apps Marketplace Info - Apps not found in manager are skipped', async () => {
164+
const manager = new AppManager({
165+
metadataStorage: testingInfastructure.getAppStorage(),
166+
logStorage: testingInfastructure.getLogStorage(),
167+
bridges: testingInfastructure.getAppBridges(),
168+
sourceStorage: testingInfastructure.getSourceStorage(),
169+
});
170+
171+
const appsOverview = TestData.getAppsOverview();
172+
appsOverview[0].latest.id = 'nonexistent-app'; // App not in manager
173+
174+
// Mock the apps Map to return our mock app
175+
(manager as any).apps = new Map([['test-app', TestData.getMockApp(TestData.getAppStorageItem(), manager)]]);
176+
177+
const updatePartialAndReturnDocumentSpy = mock.method(manager.getStorage(), 'updatePartialAndReturnDocument', () => Promise.resolve());
178+
179+
// Should not throw and complete successfully
180+
await manager.updateAppsMarketplaceInfo(appsOverview);
181+
182+
assert.strictEqual(updatePartialAndReturnDocumentSpy.mock.calls.length, 0);
183+
});
184+
185+
it('Update Apps Marketplace Info - Apps with same license are skipped', async () => {
186+
const manager = new AppManager({
187+
metadataStorage: testingInfastructure.getAppStorage(),
188+
logStorage: testingInfastructure.getLogStorage(),
189+
bridges: testingInfastructure.getAppBridges(),
190+
sourceStorage: testingInfastructure.getSourceStorage(),
191+
});
192+
193+
const sameLicenseData = 'same-license-data';
194+
const existingSubscriptionInfo = TestData.getMarketplaceSubscriptionInfo({
195+
license: { license: sameLicenseData, version: 1, expireDate: new Date('2023-01-01') },
196+
});
197+
198+
const mockStorageItem = TestData.getAppStorageItem({
199+
marketplaceInfo: [TestData.getMarketplaceInfo({ subscriptionInfo: existingSubscriptionInfo })],
200+
});
201+
202+
const mockApp = TestData.getMockApp(mockStorageItem, manager);
203+
204+
// Mock the apps Map to return our mock app
205+
(manager as any).apps = new Map([['test-app', mockApp]]);
206+
207+
const appsOverview = TestData.getAppsOverview(
208+
TestData.getMarketplaceSubscriptionInfo({
209+
license: { license: sameLicenseData, version: 1, expireDate: new Date('2023-01-01') },
210+
}),
211+
);
212+
213+
const updatePartialAndReturnDocumentSpy = mock.method(manager.getStorage(), 'updatePartialAndReturnDocument', () => Promise.resolve());
214+
215+
// Should not throw and complete successfully
216+
await manager.updateAppsMarketplaceInfo(appsOverview);
217+
218+
// Verify the subscription info was not updated (should remain the same)
219+
assert.strictEqual(mockStorageItem.marketplaceInfo[0].subscriptionInfo.seats, 10); // Original value
220+
assert.strictEqual(updatePartialAndReturnDocumentSpy.mock.calls.length, 0);
221+
});
222+
223+
it('Update Apps Marketplace Info - Subscription info is updated and app is signed', async () => {
224+
const manager = new AppManager({
225+
metadataStorage: testingInfastructure.getAppStorage(),
226+
logStorage: testingInfastructure.getLogStorage(),
227+
bridges: testingInfastructure.getAppBridges(),
228+
sourceStorage: testingInfastructure.getSourceStorage(),
229+
});
230+
231+
const existingSubscriptionInfo = TestData.getMarketplaceSubscriptionInfo({
232+
license: { license: 'old-license-data', version: 1, expireDate: new Date('2023-01-01') },
233+
});
234+
235+
const newSubscriptionInfo = TestData.getMarketplaceSubscriptionInfo({
236+
seats: 20,
237+
maxSeats: 200,
238+
startDate: '2023-02-01',
239+
periodEnd: '2024-01-31',
240+
license: { license: 'new-license-data', version: 1, expireDate: new Date('2026-01-01') },
241+
});
242+
243+
const mockStorageItem = TestData.getAppStorageItem({
244+
marketplaceInfo: [TestData.getMarketplaceInfo({ subscriptionInfo: existingSubscriptionInfo })],
245+
});
246+
247+
const mockApp = TestData.getMockApp(mockStorageItem, manager);
248+
249+
mock.method(manager.getSignatureManager(), 'signApp', () => Promise.resolve('signed-app-data'));
250+
mock.method(mockApp, 'validateLicense', () => Promise.resolve());
251+
252+
const updatePartialAndReturnDocumentSpy = mock.method(manager.getStorage(), 'updatePartialAndReturnDocument', () =>
253+
Promise.resolve(mockStorageItem),
254+
);
255+
256+
// Mock the apps Map and dependencies
257+
(manager as any).apps = new Map([['test-app', mockApp]]);
258+
259+
const appsOverview = TestData.getAppsOverview(newSubscriptionInfo);
260+
261+
await manager.updateAppsMarketplaceInfo(appsOverview);
262+
263+
const expectedStorageItem = mockApp.getStorageItem();
264+
265+
// Verify the subscription info was updated
266+
assert.strictEqual(expectedStorageItem.marketplaceInfo[0].subscriptionInfo.seats, 20);
267+
assert.strictEqual(expectedStorageItem.marketplaceInfo[0].subscriptionInfo.license.license, 'new-license-data');
268+
assert.strictEqual(expectedStorageItem.signature, 'signed-app-data');
269+
assert.strictEqual(updatePartialAndReturnDocumentSpy.mock.calls.length, 1);
270+
});
271+
});

0 commit comments

Comments
 (0)