|
| 1 | +import { AsyncTest, Expect, Setup, SetupFixture, SpyOn } from 'alsatian'; |
| 2 | + |
| 3 | +import { AppStatus } from '../../../src/definition/AppStatus'; |
| 4 | +import type { AppManager } from '../../../src/server/AppManager'; |
| 5 | +import type { IParseAppPackageResult } from '../../../src/server/compiler'; |
| 6 | +import { AppRuntimeManager } from '../../../src/server/managers/AppRuntimeManager'; |
| 7 | +import type { DenoRuntimeSubprocessController } from '../../../src/server/runtime/deno/AppsEngineDenoRuntime'; |
| 8 | +import type { IAppStorageItem } from '../../../src/server/storage'; |
| 9 | + |
| 10 | +export class AppRuntimeManagerTestFixture { |
| 11 | + private mockManager: AppManager; |
| 12 | + |
| 13 | + private runtimeManager: AppRuntimeManager; |
| 14 | + |
| 15 | + private mockAppPackage: IParseAppPackageResult; |
| 16 | + |
| 17 | + private mockStorageItem: IAppStorageItem; |
| 18 | + |
| 19 | + private mockSubprocessController: DenoRuntimeSubprocessController; |
| 20 | + |
| 21 | + @SetupFixture |
| 22 | + public setupFixture() { |
| 23 | + this.mockManager = { |
| 24 | + getAccessorManager() { |
| 25 | + return {} as any; |
| 26 | + }, |
| 27 | + getApiManager() { |
| 28 | + return {} as any; |
| 29 | + }, |
| 30 | + getLogStorage() { |
| 31 | + return {} as any; |
| 32 | + }, |
| 33 | + getBridges() { |
| 34 | + return {} as any; |
| 35 | + }, |
| 36 | + } as AppManager; |
| 37 | + |
| 38 | + this.mockAppPackage = { |
| 39 | + info: { |
| 40 | + id: 'test-app', |
| 41 | + name: 'Test App', |
| 42 | + nameSlug: 'test-app', |
| 43 | + version: '1.0.0', |
| 44 | + description: 'Test app for unit testing', |
| 45 | + author: { |
| 46 | + name: 'Test Author', |
| 47 | + homepage: 'https://test.com', |
| 48 | + support: 'https://test.com/support', |
| 49 | + }, |
| 50 | + permissions: [], |
| 51 | + requiredApiVersion: '1.0.0', |
| 52 | + classFile: 'main.js', |
| 53 | + iconFile: 'icon.png', |
| 54 | + implements: [], |
| 55 | + }, |
| 56 | + files: { |
| 57 | + 'main.js': 'console.log("Hello World");', |
| 58 | + }, |
| 59 | + languageContent: {} as unknown as IParseAppPackageResult['languageContent'], |
| 60 | + implemented: {} as unknown as IParseAppPackageResult['implemented'], |
| 61 | + } as IParseAppPackageResult; |
| 62 | + |
| 63 | + this.mockStorageItem = { |
| 64 | + id: 'test-app', |
| 65 | + status: AppStatus.MANUALLY_ENABLED, |
| 66 | + info: this.mockAppPackage.info, |
| 67 | + createdAt: new Date(), |
| 68 | + updatedAt: new Date(), |
| 69 | + } as IAppStorageItem; |
| 70 | + |
| 71 | + this.mockSubprocessController = { |
| 72 | + async setupApp() { |
| 73 | + return Promise.resolve(); |
| 74 | + }, |
| 75 | + |
| 76 | + async stopApp() { |
| 77 | + return Promise.resolve(); |
| 78 | + }, |
| 79 | + |
| 80 | + getAppId() { |
| 81 | + return 'test-app'; |
| 82 | + }, |
| 83 | + } as DenoRuntimeSubprocessController; |
| 84 | + } |
| 85 | + |
| 86 | + @Setup |
| 87 | + public setup() { |
| 88 | + this.runtimeManager = new AppRuntimeManager(this.mockManager, () => this.mockSubprocessController); |
| 89 | + } |
| 90 | + |
| 91 | + @AsyncTest('Starts runtime for app successfully') |
| 92 | + public async startRuntimeForAppSuccessfully() { |
| 93 | + await Expect(() => this.runtimeManager.startRuntimeForApp(this.mockAppPackage, this.mockStorageItem)).not.toThrowAsync(); |
| 94 | + |
| 95 | + /* eslint-disable-next-line dot-notation -- We need to access the property like this for the compile not to complain */ |
| 96 | + Expect(this.runtimeManager['subprocesses'][this.mockAppPackage.info.id]).toBe(this.mockSubprocessController); |
| 97 | + } |
| 98 | + |
| 99 | + @AsyncTest('Fails to start runtime for app that already has a runtime') |
| 100 | + public async startMultipleRuntimesForSameApp() { |
| 101 | + await Expect(() => this.runtimeManager.startRuntimeForApp(this.mockAppPackage, this.mockStorageItem)).not.toThrowAsync(); |
| 102 | + |
| 103 | + await Expect(() => this.runtimeManager.startRuntimeForApp(this.mockAppPackage, this.mockStorageItem)).toThrowErrorAsync( |
| 104 | + Error, |
| 105 | + 'App already has an associated runtime', |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + @AsyncTest('Starts multiple runtimes for app successfully with force option') |
| 110 | + public async startMultipleRuntimesForSameAppWithForceOption() { |
| 111 | + await Expect(() => this.runtimeManager.startRuntimeForApp(this.mockAppPackage, this.mockStorageItem)).not.toThrowAsync(); |
| 112 | + |
| 113 | + await Expect(() => |
| 114 | + this.runtimeManager.startRuntimeForApp(this.mockAppPackage, this.mockStorageItem, { force: true }), |
| 115 | + ).not.toThrowAsync(); |
| 116 | + |
| 117 | + /* eslint-disable-next-line dot-notation -- We need to access the property like this for the compile not to complain */ |
| 118 | + Expect(this.runtimeManager['subprocesses'][this.mockAppPackage.info.id]).toBe(this.mockSubprocessController); |
| 119 | + } |
| 120 | + |
| 121 | + @AsyncTest() |
| 122 | + public async startRuntimeThatFailsToSetup() { |
| 123 | + SpyOn(this.mockSubprocessController, 'setupApp').andReturn(Promise.reject(new Error('Nope'))); |
| 124 | + |
| 125 | + await Expect(() => this.runtimeManager.startRuntimeForApp(this.mockAppPackage, this.mockStorageItem)).toThrowErrorAsync(Error, 'Nope'); |
| 126 | + |
| 127 | + /* eslint-disable-next-line dot-notation -- We need to access the property like this for the compile not to complain */ |
| 128 | + Expect(this.runtimeManager['subprocesses'][this.mockAppPackage.info.id]).not.toBeDefined(); |
| 129 | + } |
| 130 | +} |
0 commit comments