UNPKG

wa-chat-server

Version:

Watson Assistant powered chat server

128 lines (127 loc) 5.46 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const Session_1 = require("../../class/Session/Session"); const SessionStorageMockFactory_1 = require("../mock/SessionStorageMockFactory"); const SessionStorageInitErrorMock_1 = require("../mock/SessionStorageInitErrorMock"); const ISessionConfig_1 = require("../../interface/ISessionConfig"); class SessionTest { getConfig(storageName = ISessionConfig_1.SessionType.ProcessMemory) { const sessionConfig = { storageName, }; return sessionConfig; } getMockSessionStorageFactory(sessionID) { const mockSessionStorageFactory = { createStorage: () => { return new ((0, SessionStorageMockFactory_1.SessionStorageMockFactory)())(sessionID); }, config: {}, }; return mockSessionStorageFactory; } getInitErrorMockSessionStorageFactory() { const initErrorMockSessionStorageFactory = { createStorage: () => { return new SessionStorageInitErrorMock_1.SessionStorageInitErrorMock(); }, config: {}, }; return initErrorMockSessionStorageFactory; } getFactories(sessionID, initError = false, storageName = ISessionConfig_1.SessionType.ProcessMemory) { const factory = initError ? this.getInitErrorMockSessionStorageFactory() : this.getMockSessionStorageFactory(sessionID); return new Map([[storageName, factory]]); } existingsessionIdTest() { return __awaiter(this, void 0, void 0, function* () { it('existing_session', () => __awaiter(this, void 0, void 0, function* () { expect.assertions(0); // cache storage yield new Session_1.Session('ID', this.getConfig(), this.getFactories('ID')).start(); // use storage from cache const session = new Session_1.Session('ID', this.getConfig(), this.getFactories('ID')); yield session.start(); yield session.save(); session.getStorage().check(); })); }); } nonExistingsessionIdTest() { return __awaiter(this, void 0, void 0, function* () { it('non_existing_session', () => __awaiter(this, void 0, void 0, function* () { expect.assertions(0); const session = new Session_1.Session('ID', this.getConfig(), this.getFactories()); yield session.start(); yield session.save(); session.getStorage().check(); })); }); } sessionErrorTest() { return __awaiter(this, void 0, void 0, function* () { it('error', () => __awaiter(this, void 0, void 0, function* () { expect.assertions(1); try { const session = new Session_1.Session('ID', this.getConfig('Err'), this.getFactories('ID', true, 'Err')); yield session.start(); yield session.save(); } catch (e) { expect(true).toStrictEqual(true); } })); }); } updateContextTest() { return __awaiter(this, void 0, void 0, function* () { it('updateContext', () => __awaiter(this, void 0, void 0, function* () { expect.assertions(1); const session = new Session_1.Session('ID'); yield session.start(); session.data.assistants = { MASTER: { context: { 'context-param1': 'VALUE1', 'context-param2': 'VALUE2', }, }, }; const newPayloadMOCK = { result: { context: { 'context-param2': 'VALUE2_NEW', 'context-param3': 'VALUE3', }, }, }; session.updateContext(newPayloadMOCK); expect(session.data.assistants['MASTER'].context).toStrictEqual({ 'context-param1': 'VALUE1', 'context-param2': 'VALUE2_NEW', 'context-param3': 'VALUE3', }); })); }); } test() { return __awaiter(this, void 0, void 0, function* () { this.existingsessionIdTest(); this.nonExistingsessionIdTest(); this.sessionErrorTest(); this.updateContextTest(); }); } } new SessionTest().test();