|
| 1 | +import * as fs from 'fs/promises'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { describe, it, beforeEach, afterEach, mock } from 'node:test'; |
| 4 | +import * as assert from 'node:assert'; |
| 5 | + |
| 6 | +import { AppStatus } from '../../../../src/definition/AppStatus'; |
| 7 | +import { UserStatusConnection, UserType } from '../../../../src/definition/users'; |
| 8 | +import type { AppManager } from '../../../../src/server/AppManager'; |
| 9 | +import type { IParseAppPackageResult } from '../../../../src/server/compiler'; |
| 10 | +import { AppAccessorManager, AppApiManager } from '../../../../src/server/managers'; |
| 11 | +import { DenoRuntimeSubprocessController } from '../../../../src/server/runtime/deno/AppsEngineDenoRuntime'; |
| 12 | +import type { IAppStorageItem } from '../../../../src/server/storage'; |
| 13 | +import { TestInfastructureSetup } from '../../../test-data/utilities'; |
| 14 | + |
| 15 | +describe('DenoRuntimeSubprocessController', () => { |
| 16 | + let manager: AppManager; |
| 17 | + let controller: DenoRuntimeSubprocessController; |
| 18 | + let appPackage: IParseAppPackageResult; |
| 19 | + let appStorageItem: IAppStorageItem; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + const infrastructure = new TestInfastructureSetup(); |
| 23 | + manager = infrastructure.getMockManager(); |
| 24 | + |
| 25 | + const accessors = new AppAccessorManager(manager); |
| 26 | + manager.getAccessorManager = () => accessors; |
| 27 | + |
| 28 | + const api = new AppApiManager(manager); |
| 29 | + manager.getApiManager = () => api; |
| 30 | + |
| 31 | + const appPackageBuffer = await fs.readFile(path.join(__dirname, '../../../test-data/apps/hello-world-test_0.0.1.zip')); |
| 32 | + appPackage = await manager.getParser().unpackageApp(appPackageBuffer); |
| 33 | + |
| 34 | + appStorageItem = { |
| 35 | + id: 'hello-world-test', |
| 36 | + status: AppStatus.MANUALLY_ENABLED, |
| 37 | + } as IAppStorageItem; |
| 38 | + |
| 39 | + controller = new DenoRuntimeSubprocessController(manager, appPackage, appStorageItem); |
| 40 | + await controller.setupApp(); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + await controller.stopApp(); |
| 45 | + mock.restoreAll(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('correctly identifies a call to the HTTP accessor', async () => { |
| 49 | + const httpBridge = manager.getBridges().getHttpBridge(); |
| 50 | + const doCallSpy = mock.method(httpBridge, 'doCall'); |
| 51 | + |
| 52 | + // eslint-disable-next-line |
| 53 | + const r = await (controller as any).handleAccessorMessage({ |
| 54 | + type: 'request', |
| 55 | + payload: { |
| 56 | + jsonrpc: '2.0', |
| 57 | + id: 'test', |
| 58 | + method: 'accessor:getHttp:get', |
| 59 | + params: ['https://google.com', { content: "{ test: 'test' }" }], |
| 60 | + serialize: () => '', |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + assert.strictEqual(doCallSpy.mock.calls.length, 1, 'doCallSpy.mock.calls.length'); |
| 65 | + const callArgs = doCallSpy.mock.calls[0].arguments; |
| 66 | + assert.partialDeepStrictEqual(callArgs[0], { |
| 67 | + appId: '9c1d62ca-e40f-456f-8601-17c823a16c68', |
| 68 | + method: 'get', |
| 69 | + url: 'https://google.com', |
| 70 | + }, 'callArgs[0]'); |
| 71 | + |
| 72 | + assert.deepStrictEqual(r.result, { |
| 73 | + method: 'get', |
| 74 | + url: 'https://google.com', |
| 75 | + content: "{ test: 'test' }", |
| 76 | + statusCode: 200, |
| 77 | + headers: {}, |
| 78 | + }, 'r.result'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('correctly identifies a call to the IRead accessor', async () => { |
| 82 | + const userBridge = manager.getBridges().getUserBridge(); |
| 83 | + const doGetByUsernameSpy = mock.method(userBridge, 'doGetByUsername', () => |
| 84 | + Promise.resolve({ |
| 85 | + id: 'id', |
| 86 | + username: 'rocket.cat', |
| 87 | + isEnabled: true, |
| 88 | + emails: [], |
| 89 | + name: 'name', |
| 90 | + roles: [], |
| 91 | + type: UserType.USER, |
| 92 | + active: true, |
| 93 | + utcOffset: 0, |
| 94 | + status: 'offline', |
| 95 | + statusConnection: UserStatusConnection.OFFLINE, |
| 96 | + lastLoginAt: new Date(), |
| 97 | + createdAt: new Date(), |
| 98 | + updatedAt: new Date(), |
| 99 | + }), |
| 100 | + ); |
| 101 | + |
| 102 | + // eslint-disable-next-line |
| 103 | + const { id, result } = await (controller as any).handleAccessorMessage({ |
| 104 | + type: 'request', |
| 105 | + payload: { |
| 106 | + jsonrpc: '2.0', |
| 107 | + id: 'test', |
| 108 | + method: 'accessor:getReader:getUserReader:getByUsername', |
| 109 | + params: ['rocket.cat'], |
| 110 | + serialize: () => '', |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + assert.strictEqual(doGetByUsernameSpy.mock.calls.length, 1); |
| 115 | + assert.deepStrictEqual(doGetByUsernameSpy.mock.calls[0].arguments, ['rocket.cat', '9c1d62ca-e40f-456f-8601-17c823a16c68']); |
| 116 | + |
| 117 | + assert.strictEqual(id, 'test'); |
| 118 | + assert.strictEqual((result as any).username, 'rocket.cat'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('correctly identifies a call to the IEnvironmentReader accessor via IRead', async () => { |
| 122 | + // eslint-disable-next-line |
| 123 | + const { id, result } = await (controller as any).handleAccessorMessage({ |
| 124 | + type: 'request', |
| 125 | + payload: { |
| 126 | + jsonrpc: '2.0', |
| 127 | + id: 'requestId', |
| 128 | + method: 'accessor:getReader:getEnvironmentReader:getServerSettings:getOneById', |
| 129 | + params: ['setting test id'], |
| 130 | + serialize: () => '', |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + assert.strictEqual(id, 'requestId'); |
| 135 | + assert.strictEqual((result as any).id, 'setting test id'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('correctly identifies a call to create a visitor via the LivechatCreator', async () => { |
| 139 | + const livechatBridge = manager.getBridges().getLivechatBridge(); |
| 140 | + const doCreateVisitorSpy = mock.method(livechatBridge, 'doCreateVisitor', () => Promise.resolve('random id')); |
| 141 | + |
| 142 | + // eslint-disable-next-line |
| 143 | + const { id, result } = await (controller as any).handleAccessorMessage({ |
| 144 | + type: 'request', |
| 145 | + payload: { |
| 146 | + jsonrpc: '2.0', |
| 147 | + id: 'requestId', |
| 148 | + method: 'accessor:getModifier:getCreator:getLivechatCreator:createVisitor', |
| 149 | + params: [ |
| 150 | + { |
| 151 | + id: 'random id', |
| 152 | + token: 'random token', |
| 153 | + username: 'random username for visitor', |
| 154 | + name: 'Random Visitor', |
| 155 | + }, |
| 156 | + ], |
| 157 | + serialize: () => '', |
| 158 | + }, |
| 159 | + }); |
| 160 | + |
| 161 | + assert.strictEqual(doCreateVisitorSpy.mock.calls.length, 1); |
| 162 | + assert.deepStrictEqual(doCreateVisitorSpy.mock.calls[0].arguments, [ |
| 163 | + { |
| 164 | + id: 'random id', |
| 165 | + token: 'random token', |
| 166 | + username: 'random username for visitor', |
| 167 | + name: 'Random Visitor', |
| 168 | + }, |
| 169 | + '9c1d62ca-e40f-456f-8601-17c823a16c68', |
| 170 | + ]); |
| 171 | + |
| 172 | + assert.strictEqual(id, 'requestId'); |
| 173 | + assert.strictEqual(result, 'random id'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('correctly identifies a call to the message bridge', async () => { |
| 177 | + const messageBridge = manager.getBridges().getMessageBridge(); |
| 178 | + const doCreateSpy = mock.method(messageBridge, 'doCreate', () => Promise.resolve('random-message-id')); |
| 179 | + |
| 180 | + const messageParam = { |
| 181 | + room: { id: '123' }, |
| 182 | + sender: { id: '456' }, |
| 183 | + text: 'Hello World', |
| 184 | + alias: 'alias', |
| 185 | + avatarUrl: 'https://avatars.com/123', |
| 186 | + }; |
| 187 | + |
| 188 | + // eslint-disable-next-line |
| 189 | + const response = await (controller as any).handleBridgeMessage({ |
| 190 | + type: 'request', |
| 191 | + payload: { |
| 192 | + jsonrpc: '2.0', |
| 193 | + id: 'requestId', |
| 194 | + method: 'bridges:getMessageBridge:doCreate', |
| 195 | + params: [messageParam, 'APP_ID'], |
| 196 | + serialize: () => '', |
| 197 | + }, |
| 198 | + }); |
| 199 | + const { id, result } = response as any; |
| 200 | + |
| 201 | + assert.strictEqual(doCreateSpy.mock.calls.length, 1); |
| 202 | + assert.deepStrictEqual(doCreateSpy.mock.calls[0].arguments, [messageParam, '9c1d62ca-e40f-456f-8601-17c823a16c68']); |
| 203 | + |
| 204 | + assert.strictEqual(id, 'requestId'); |
| 205 | + assert.strictEqual(result, 'random-message-id'); |
| 206 | + }); |
| 207 | +}); |
0 commit comments