Skip to content

Commit

Permalink
Merge pull request #49 from yeuai/dev-typo
Browse files Browse the repository at this point in the history
Dev typo
  • Loading branch information
vunb authored Mar 22, 2021
2 parents 7be3d6d + 7c29951 commit 3d71d43
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ To define a list of items, just enter item in a new line which started with symb

## comment

Comments make your code clearer, you can add a comment in BotScript document by add symbol `#` at the begining of a line:
Comments make your code clearer, you can add a comment in BotScript document by starting the symbol `#` at the begining of a line or followed by a space:

```bash
# here is a comment
Expand Down
2 changes: 1 addition & 1 deletion examples/register.bot
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ register_account post https://botscript.ai/api/account/register
@ register_account post /api/account/register

! yesno
- yes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yeuai/botscript",
"version": "1.5.2",
"version": "1.5.3",
"description": "A text-based scripting language and bot engine for Conversational User Interfaces (CUI)",
"main": "dist/engine",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/engine/botscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export class BotScript extends EventEmitter {
.replace(/\n +/g, '\n')
// remove comments
.replace(/^#.*$\n/igm, '')
// remove inline comment
.replace(/# .*$\n/igm, '')
// separate definition struct (normalize)
.replace(/^!/gm, '\n!')
// concat multiple lines (normalize)
Expand Down Expand Up @@ -370,7 +372,7 @@ export class BotScript extends EventEmitter {
return false;
}
const vTestResult = utils.evaluate(x.expr, req.variables);
this.logger.info(`Evaluate test: ${vTestResult}|`, x.type, x.expr, x.value);
this.logger.info(`Evaluate test: ${vTestResult} is ${!!vTestResult}|`, x.type, x.expr, x.value);
return vTestResult;
});

Expand Down
7 changes: 7 additions & 0 deletions src/lib/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const REGEX_HEADER_SEPARATOR = / *: */;
const REGEX_IP = /^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])(\.(?!$)|(?=$))){4}$/;

export {
REGEX_IP,
REGEX_HEADER_SEPARATOR,
};
5 changes: 4 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Struct, Request } from '../common';
import { TestConditionalCallback, Types } from '../interfaces/types';
import { Logger } from './logger';
import { interpolate } from './template';
import { REGEX_HEADER_SEPARATOR } from './regex';

const logger = new Logger('Utils');

Expand Down Expand Up @@ -91,7 +92,9 @@ export function evaluate(code: string, context: any) {
*/
export function callHttpService(command: Struct, req: Request) {
const vIsGetMethod = /^get$/i.test(command.options[0]);
const headers = command.body.map(x => x.split(':'));
const headers = command.body
.filter(x => x.split(REGEX_HEADER_SEPARATOR).length === 2)
.map(x => x.split(REGEX_HEADER_SEPARATOR).map(kv => kv.trim()));
const method = vIsGetMethod ? 'GET' : 'POST';
const url = interpolate(command.options[1], req.variables);
const body = vIsGetMethod ? undefined : req.variables;
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/delay.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

// Setup axios interceptors
const mock = new MockAdapter(axios);
// Mock specific requests, but let unmatched ones through
mock
.onPost('/api/account/register').reply(200, {
reg_result_message: 'ok',
})
.onGet('/api/nlu').reply(200, {
intent: 'who',
entities: [{ id: 1, name: 'John Smith' }],
})
.onGet('/api/nlu/react').reply(200, {
intent: 'react_positive',
entities: [{ id: 1, name: 'John Smith' }],
})
.onGet('/api/data/list').reply(200, {
people: [{
name: 'Vũ',
age: 30,
}, {
name: 'Toàn',
age: 20,
}, {
name: 'Cường',
age: 25,
},
],
})
.onAny().passThrough();

/**
* sleep utils
* @param m
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/register-bot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe('Register.bot (e2e)', async () => {
expect(req.speechResponse).match(/xác nhn thông tin/i);
await bot.handleAsync(req.enter('yes'));

expect(req.speechResponse).match(/đăng ký thành công/i);
const {reg_username, reg_result_message} = req.variables;
const vResult = `Bạn đã đăng ký thành công, tài khoản ${reg_username}: ok!`;

expect(reg_result_message).match(/ok/);
expect(req.speechResponse).eq(vResult);
bot.logger.info('Chi tiết tài khoản: ', req.variables);
});

Expand Down
3 changes: 2 additions & 1 deletion test/engine/conditions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BotScript, Request } from '../../src/engine';
import { REGEX_IP } from '../../src/lib/regex';
import { assert } from 'chai';

describe('BotScript: Conditional dialogue', () => {
Expand Down Expand Up @@ -170,7 +171,7 @@ describe('BotScript: Conditional dialogue', () => {
const req = new Request('what is my ip');
await condBot.handleAsync(req);
assert.match(req.speechResponse, /here is your ip/i, 'bot reply');
assert.match(req.variables.ip, /^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])(\.(?!$)|(?=$))){4}$/, 'match ip');
assert.match(req.variables.ip, REGEX_IP, 'match ip');
});

it('should handle conditional redirect', async () => {
Expand Down
29 changes: 0 additions & 29 deletions test/features/directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
import { assert } from 'chai';
import { BotScript, Request } from '../../src/engine';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const mock = new MockAdapter(axios);
// Mock specific requests, but let unmatched ones through
mock
.onGet('/api/nlu').reply(200, {
intent: 'who',
entities: [{ id: 1, name: 'John Smith' }],
})
.onGet('/api/nlu/react').reply(200, {
intent: 'react_positive',
entities: [{ id: 1, name: 'John Smith' }],
})
.onGet('/api/data/list').reply(200, {
people: [{
name: 'Vũ',
age: 30,
}, {
name: 'Toàn',
age: 20,
}, {
name: 'Cường',
age: 25,
},
],
})
.onAny()
.passThrough();

describe('Feature: Directive', () => {

Expand Down

0 comments on commit 3d71d43

Please sign in to comment.