wa-chat-server
Version:
Watson Assistant powered chat server
108 lines (107 loc) • 4.98 kB
JavaScript
;
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 SessionConfigProcessMemory_1 = require("../../class/Session/SessionConfigProcessMemory");
const SessionStorageProcessMemory_1 = require("../../class/Session/SessionStorageProcessMemory");
const SessionExpiredError_1 = require("../../exception/SessionExpiredError");
const SessionDoesNotExistError_1 = require("../../exception/SessionDoesNotExistError");
class SessionStorageProcessMemoryTest {
getConfig(lifeTimeMS = 0) {
const config = new SessionConfigProcessMemory_1.SessionConfigProcessMemory();
config.lifeTimeMS = lifeTimeMS;
return config;
}
readWriteTest() {
return __awaiter(this, void 0, void 0, function* () {
it('read_write', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const storage = new SessionStorageProcessMemory_1.SessionStorageProcessMemory(this.getConfig(3600 * 1000));
yield storage.init();
const sessionId = yield storage.create();
const sessDataIn = {};
yield storage.write(sessionId, sessDataIn);
// trigger cleanup
yield storage.write(yield storage.create(), {});
const sessDataOut = yield storage.read(sessionId);
expect(sessDataOut).toStrictEqual(sessDataIn);
}));
});
}
expiredSessionTest() {
return __awaiter(this, void 0, void 0, function* () {
it('expired_session', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const storage = new SessionStorageProcessMemory_1.SessionStorageProcessMemory(this.getConfig(-1));
yield storage.init();
const sessionId = yield storage.create();
yield storage.write(sessionId, {});
try {
yield storage.read(sessionId);
}
catch (e) {
if (!(e instanceof SessionExpiredError_1.SessionExpiredError)) {
throw e;
}
expect(true).toStrictEqual(true);
}
}));
});
}
nonExistingSessionTest() {
return __awaiter(this, void 0, void 0, function* () {
it('non_existing_session', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const storage = new SessionStorageProcessMemory_1.SessionStorageProcessMemory(this.getConfig());
yield storage.init();
try {
yield storage.read('ID');
}
catch (e) {
if (!(e instanceof SessionDoesNotExistError_1.SessionDoesNotExistError)) {
throw e;
}
expect(true).toStrictEqual(true);
}
}));
});
}
cleanupTest() {
return __awaiter(this, void 0, void 0, function* () {
it('cleanup_test', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const storage = new SessionStorageProcessMemory_1.SessionStorageProcessMemory(this.getConfig(-1));
yield storage.init();
try {
const sessionId1 = yield storage.create();
const sessionId2 = yield storage.create();
yield storage.write(sessionId1, {});
yield storage.write(sessionId2, {});
yield storage.read(sessionId1);
}
catch (e) {
if (!(e instanceof SessionDoesNotExistError_1.SessionDoesNotExistError)) {
throw e;
}
expect(true).toStrictEqual(true);
}
}));
});
}
test() {
return __awaiter(this, void 0, void 0, function* () {
this.readWriteTest();
this.expiredSessionTest();
this.nonExistingSessionTest();
this.cleanupTest();
});
}
}
new SessionStorageProcessMemoryTest().test();