Testing a script with variables
A script test is a step-by-step description of a user’s interaction with a bot and its expected reactions. If you use the $client
and $session
variables in your script, you can test the usage of these objects with the <context>
tag.
Let’s consider testing a bot script with different features for authorized and unauthorized users. For example, a company HR bot where employees need to sign in to interact with it. Otherwise, the main bot features will be unavailable.
Script
An example of a script where a user should sign in:
patterns:
$mobilePhoneNumber = $regexp<\+?1\d+>
theme: /
state: Start
q: $regex</start>
a: Hello! I am a Tovie AI HR bot. Please, sign in to continue.
state: VacationRequest
q!: * vacation *
if: $client.isAuthorized
a: I’ve submitted a request.
# ... The code block for submitting the request.
else:
go!: /Unauthorized
state: Unauthorized
a: You’re not authorized. To sign in, enter the phone number and I will send a confirmation code to you.
state: GetPhoneNumber
q: * $mobilePhoneNumber *
script:
# An HTTP request to API is executed. The phone number is extracted from $parseTree.
var res = $http.post("https://example.com?phone=" + $parseTree._mobilePhoneNumber);
# The back end generates a confirmation code, sends an SMS message to the client, and returns the code in the response.
$session.authCode = res.data.authCode;
a: Enter the confirmation code. I have sent it to {{$parseTree._mobilePhoneNumber}}.
state: GetAuthCode
q: $regexp<\d{4}>
# The code entered by the user is compared with the one returned by the back end.
if: $session.authCode === $parseTree.text
script:
$client.isAuthorized = true;
a: I’ve submitted the request.
# ... The code block for submitting the request.
else:
a: The code is incorrect. Please, try again.
go: ..
Script test
The script section where the user is authorized may be covered by a test with the <context>
tag:
<test>
<test-case id="AuthorizedUser">
<q>/start</q>
<a>Hello! I am a Tovie AI HR bot. Please, sign in to continue.</a>
<context var="client">
{
"isAuthorized": true
}
</context>
<q>I want to go on vacation.</q>
<a>I’ve submitted a request.</a>
</test-case>
</test>
The same script section can be tested with the <mockData>
tag. When using this tag, you need to set up fake responses to HTTP requests (mock objects). Also, you will have to duplicate the authorization script in other test cases.
<test>
<test-case id="AuthorizedUser">
<q>/start</q>
<a>Hello! I am a Tovie AI HR bot. Please, sign in to continue.</a>
<q>I want to go on vacation.</q>
<a>You’re not authorized. To sign in, enter the phone number and I will send a confirmation code to you.</a>
<mockData>
<query method="post">https://example.com?phone=${phone}</query>
<parameters>
<phone>12025550165</phone>
</parameters>
<response>
{
"authCode": "7878"
}
</response>
</mockData>
<q>12025550165</q>
<a>Enter the confirmation code. I have sent it to 12025550165.</a>
<q>7878</q>
<a>I’ve submitted the request.</a>
</test-case>
</test>