new(all): Unit tests infra.

This commit is contained in:
Rodrigo Rodriguez 2024-08-17 21:35:09 -03:00
parent dcf20934cb
commit 3f9e3b040e
8 changed files with 115 additions and 7 deletions

47
.test-init.ts Normal file
View file

@ -0,0 +1,47 @@
import { expect, test } from 'vitest';
import { GBServer } from './src/app';
import { RootData } from './src/RootData';
import { GBMinInstance } from 'botlib';
import { Mutex } from 'async-mutex';
export default function init() {
const min = {
packages: null,
appPackages: null,
botId: 'gbtest',
instance: {botId: 'gbtest'},
core: {},
conversationalService: {},
kbService: {},
adminService: {},
deployService: {},
textServices: {},
bot: {},
dialogs: {},
userState: {},
userProfile: {},
whatsAppDirectLine: {},
cbMap: {},
scriptMap: {},
sandBoxMap: {},
gbappServices: {}
}
GBServer.globals = new RootData();
GBServer.globals.server = null;
GBServer.globals.httpsServer = null;
GBServer.globals.webSessions = {};
GBServer.globals.processes = [0, { pid: 1, proc: {step: {}}}];
GBServer.globals.files = {};
GBServer.globals.appPackages = [];
GBServer.globals.sysPackages = [];
GBServer.globals.minInstances = [min];
GBServer.globals.minBoot = min;
GBServer.globals.wwwroot = null;
GBServer.globals.entryPointDialog = null;
GBServer.globals.debuggers = [];
GBServer.globals.indexSemaphore = new Mutex();
GBServer.globals.users = {1: {userId: 1}};
}

View file

@ -427,7 +427,7 @@ export class DialogKeywords {
* @example TALK TOLIST (array,member)
*
*/
public async getToLst(pid, array, member) {
public async getToLst({pid, array, member}) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
if (!array) {
@ -1366,7 +1366,7 @@ export class DialogKeywords {
const step = proc.step;
const min = GBServer.globals.minInstances.filter(p => p.instance.instanceId == proc.instanceId)[0];
const sec = new SecService();
const user = await sec.getUserFromId(min.instance.instanceId, proc.userId);
const user = GBServer.globals.users [proc.userId];
const params = user ? JSON.parse(user.params) : {};
return {
min,

View file

@ -92,6 +92,7 @@ export class SystemKeywords {
}
public async append({ pid, args }) {
if (!args) return [];
let array = [].concat(...args);
return array.filter(function (item, pos) {
return item;

View file

@ -0,0 +1,16 @@
import { expect, test } from 'vitest';
import { DialogKeywords } from '../services/DialogKeywords';
import init from '../../../.test-init'
init();
const dk = new DialogKeywords();
const pid = 1;
test('TOLIST', async () => {
const obj = [{a:1, b:2}, {a:2, b:4}];
expect(await dk.getToLst({ pid, array: obj, member:'a' }))
.toBe("1,2");
});

View file

@ -1,4 +1,4 @@
import { GBVMService } from './GBVMService';
import { GBVMService } from '../services/GBVMService';
import { expect, test } from 'vitest'
test('Default', () => {

View file

@ -0,0 +1,36 @@
import { GBVMService } from '../services/GBVMService';
import { expect, test } from 'vitest';
import { SystemKeywords } from '../services/SystemKeywords';
const s = new SystemKeywords();
const pid = 1;
test('APPEND', async () => {
expect(await s.append({ pid, args: [1, 1, 1, 1] })).toStrictEqual([1, 1, 1, 1]);
expect(await s.append({ pid, args: [1] })).toStrictEqual([1]);
expect(await s.append({ pid, args: [] })).toStrictEqual([]);
expect(await s.append({ pid, args: null })).toStrictEqual([]);
});
test('COMPARE', () => {
expect(GBVMService.compare(1, 1)).toBeTruthy();
expect(GBVMService.compare({ a: 1 }, { a: 1 })).toBeTruthy();
expect(GBVMService.compare({ a: 1 }, { a: 2 })).toBeFalsy();
expect(GBVMService.compare({ a: 1, b: 2 }, { a: 1, b: 2 })).toBeTruthy();
});
test('Parse Storage Field', async () => {
const s = new GBVMService();
expect(await s.parseField('name STRING(30)')).toStrictEqual({
name: 'name',
definition: {
allowNull: true,
unique: false,
primaryKey: false,
size: 30,
autoIncrement: false,
type: 'STRING'
}
});
});

View file

@ -8,6 +8,7 @@ import * as Fs from 'fs';
import mkdirp from 'mkdirp';
import urlJoin from 'url-join';
import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
import { GBServer } from '../../../src/app.js';
/**
@ -52,7 +53,8 @@ export class SecService extends GBService {
user.displayName = displayName;
user.email = email;
user.defaultChannel = channelName;
GBServer.globals.users [user.userId] = user;
return await user.save();
}
@ -74,6 +76,7 @@ export class SecService extends GBService {
const user = await GuaribasUser.findOne(options);
user.conversationReference = conversationReference;
GBServer.globals.users [user.userId] = user;
await user.save();
}
@ -82,6 +85,7 @@ export class SecService extends GBService {
const user = await GuaribasUser.findOne(options);
user.conversationReference = conversationReference;
GBServer.globals.users [user.userId] = user;
await user.save();
}
@ -92,7 +96,7 @@ export class SecService extends GBService {
}
});
user.locale = locale;
GBServer.globals.users [user.userId] = user;
return await user.save();
}
@ -103,7 +107,7 @@ export class SecService extends GBService {
}
});
user.hearOnDialog = dialogName;
GBServer.globals.users [user.userId] = user;
return await user.save();
}
@ -114,7 +118,7 @@ export class SecService extends GBService {
}
});
user.instanceId = instanceId;
GBServer.globals.users [user.userId] = user;
return await user.save();
}
@ -160,9 +164,11 @@ export class SecService extends GBService {
agent.instanceId = user.instanceId;
agent.agentMode = 'self';
agent.agentSystemId = null;
GBServer.globals.users [agent.userId] = user;
await agent.save();
}
GBServer.globals.users [user.userId] = user;
await user.save();
return user;
@ -306,6 +312,7 @@ export class SecService extends GBService {
}
obj[name] = value;
user.params = JSON.stringify(obj);
GBServer.globals.users [userId] = user;
return await user.save();
}
}

View file

@ -58,6 +58,7 @@ export class RootData {
public debugConversationId: any; // Used to self-message during debug.
public debuggers: any[]; // Client of attached Debugger instances by botId.
public chatGPT: any; // ChatGPT API handle (shared Browser).
public users: any[]; // Loaded users.
public dk;
public wa;
public sys;