Skip to content

Commit

Permalink
chore(core): split and sort routing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Oct 11, 2021
1 parent 765c569 commit a0eb609
Show file tree
Hide file tree
Showing 14 changed files with 1,098 additions and 1,066 deletions.
2 changes: 1 addition & 1 deletion packages/botonic-core/src/models/legacy-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface BotRequest {
input: Input
lastRoutePath: RoutePath
session: Session
dataProvider: DataProvider
dataProvider?: DataProvider
}

/** The response of the bot for the triggered actions, which can be
Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-core/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function runPlugins(
lastRoutePath: RoutePath,
response: string | null = null,
messageEvents: Partial<BotonicEvent>[] | null = null,
dataProvider: DataProvider
dataProvider?: DataProvider
): Promise<void> {
for (const key in plugins) {
const p = await plugins[key]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotonicOutputParser } from '../src/output-parser'
import { BotonicOutputParser } from '../../src/output-parser'

// @ts-ignore
const { expect } = global
Expand Down
17 changes: 17 additions & 0 deletions packages/botonic-core/tests/helpers/routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PATH_PAYLOAD_IDENTIFIER, PROVIDER, Session } from '../../src'

export function testRoute(): any {
return {}
}

export function testSession(): Session {
return {
user: { id: 'userid', provider: PROVIDER.DEV },
bot: { id: 'bot_id' },
is_first_interaction: true,
__retries: 0,
}
}

export const createPathPayload = (pathWithParams: string): string =>
`${PATH_PAYLOAD_IDENTIFIER}${pathWithParams}`
2 changes: 1 addition & 1 deletion packages/botonic-core/tests/parsing/carousel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotonicOutputParserTester } from '../helpers'
import { BotonicOutputParserTester } from '../helpers/parsing'

const tester = new BotonicOutputParserTester()
describe('Parsing Carousel responses', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-core/tests/parsing/custom.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotonicOutputParserTester } from '../helpers'
import { BotonicOutputParserTester } from '../helpers/parsing'

const tester = new BotonicOutputParserTester()
describe('Parsing Location responses', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-core/tests/parsing/location.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotonicOutputParserTester } from '../helpers'
import { BotonicOutputParserTester } from '../helpers/parsing'

// TODO: Location Messages coming without typing nor delay

Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-core/tests/parsing/media.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotonicOutputParserTester } from '../helpers'
import { BotonicOutputParserTester } from '../helpers/parsing'

const tester = new BotonicOutputParserTester()
describe('Parsing Media responses', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-core/tests/parsing/text.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jest/expect-expect */
import { PATH_PAYLOAD_IDENTIFIER } from '../../src'
import { BotonicOutputParserTester } from '../helpers'
import { BotonicOutputParserTester } from '../helpers/parsing'

// TODO: Test with Markdown, test with Markdown + new lines

Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-core/tests/parsing/user-events.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotonicOutputParserTester } from '../helpers'
import { BotonicOutputParserTester } from '../helpers/parsing'

const tester = new BotonicOutputParserTester()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Router } from '../../src'

describe('TEST: Get route by path', () => {
const externalRoutes = [
{ path: '', action: 'Flow1.2' },
{ path: 'child', action: 'ChildAction' },
]
const router = new Router([
{ path: 'initial', action: 'Initial' },
{
path: 'flow-1',
action: 'Flow1',
childRoutes: [
{
path: '1',
action: 'Flow1.1',
childRoutes: [
{ path: '1', action: 'Flow1.1.1' },
{ path: '2', action: 'Flow1.1.2' },
{ path: '3', action: 'Flow1.1.3' },
],
},
{ path: '2', childRoutes: externalRoutes },
{
path: '3',
action: 'Flow1.3',
childRoutes: [
{ path: '1', action: 'Flow1.3.1' },
{ path: '2', action: 'Flow1.3.2' },
{ path: '3', action: 'Flow1.3.3' },
],
},
],
},
{ path: '404', action: '404Action' },
])

it('path exists', () => {
expect(router.getRouteByPath('initial')).toEqual({
path: 'initial',
action: 'Initial',
})
expect(router.getRouteByPath('flow-1/1')).toEqual({
path: '1',
action: 'Flow1.1',
childRoutes: [
{ path: '1', action: 'Flow1.1.1' },
{ path: '2', action: 'Flow1.1.2' },
{ path: '3', action: 'Flow1.1.3' },
],
})
expect(router.getRouteByPath('flow-1/3/2')).toEqual({
action: 'Flow1.3.2',
path: '2',
})
})
it('path exists in composed child routes', () => {
expect(router.getRouteByPath('flow-1/2')).toEqual({
path: '2',
childRoutes: [
{ action: 'Flow1.2', path: '' },
{ action: 'ChildAction', path: 'child' },
],
})
expect(router.getRouteByPath('flow-1/2/child')).toEqual({
path: 'child',
action: 'ChildAction',
})
})
it('path does not exist', () => {
expect(router.getRouteByPath('')).toBeNull()
expect(router.getRouteByPath('foo')).toBeNull()
expect(router.getRouteByPath('flow-1/3/2/6')).toBeNull()
})
})
172 changes: 172 additions & 0 deletions packages/botonic-core/tests/routing/router.match-route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { BotRequest, Input } from '../../src'
import { Router } from '../../src/routing'
import { testRoute, testSession } from '../helpers/routing'

const textInput: Input = { type: 'text', text: 'hi' }
const textInputComplex: Input = { type: 'text', text: 'Cömplêx input &% 🚀' }
const textPayloadInput: Input = { type: 'text', text: 'hi', payload: 'foo' }
const postbackInput: Input = { type: 'postback', payload: 'foo' }

const audioInput: Input = {
type: 'audio',
src: 'data:audio/mpeg;base64,iVBORw0KG',
}
const documentInput: Input = {
type: 'document',
src: 'data:application/pdf;base64,iVBORw0KG',
}
const imageInput: Input = {
type: 'image',
src: 'data:image/png;base64,iVBORw0KG',
}
const videoInput: Input = {
type: 'video',
src: 'data:video/mp4;base64,iVBORw0KG',
}

const requestInput: BotRequest = {
input: textInput,
session: { ...testSession(), organization: 'myOrg' },
lastRoutePath: 'initial',
}

describe('TEST: Match route by MATCHER <> INPUT', () => {
const router = new Router([])
const matchTextProp = (matcher, textInput) =>
router.matchRoute(
testRoute(),
'text',
matcher,
textInput,
testSession(),
null
)
const matchPayloadProp = (matcher, payload) =>
router.matchRoute(
testRoute(),
'payload',
matcher,
payload,
testSession(),
null
)
const matchTypeProp = (type, input) =>
router.matchRoute(testRoute(), 'type', type, input, testSession(), null)
const matchRequestProp = (matcher, request) =>
router.matchRoute(
testRoute(),
'request',
matcher,
request.input,
request.session,
request.lastRoutePath
)
it('text <> text', () => {
expect(matchTextProp('hi', textInput)).toBeTruthy()
expect(matchTextProp('hii', textInput)).toBeFalsy()
expect(matchTextProp('bye', textInput)).toBeFalsy()
expect(matchTextProp('', textInput)).toBeFalsy()
expect(matchTextProp(null, textInput)).toBeFalsy()
expect(matchTextProp('Cömplêx input &% 🚀', textInputComplex)).toBeTruthy()
expect(matchTextProp(' Cömplêx input &% 🚀', textInputComplex)).toBeFalsy() // has a space at the beginning
})
it('regex <> text', () => {
expect(matchTextProp(/hi/, textInput)).toBeTruthy()
expect(matchTextProp(/bye/, textInput)).toBeFalsy()
expect(matchTextProp(/🚀/, textInputComplex)).toBeTruthy()
expect(matchTextProp(/complex/, textInputComplex)).toBeFalsy()
})
it('function <> text', () => {
expect(matchTextProp(v => v.startsWith('hi'), textInput)).toBeTruthy()
expect(matchTextProp(v => !v.startsWith('hi'), textInput)).toBeFalsy()
})
it('input <> text', () => {
expect(
router.matchRoute(
testRoute(),
'input',
i => i.text.startsWith('hi'),
textInput,
testSession(),
null
)
).toBeTruthy()
expect(
router.matchRoute(
testRoute(),
'input',
i => !i.text.startsWith('hi'),
textInput,
testSession(),
null
)
).toBeFalsy()
})
it('text <> text payload', () => {
expect(matchPayloadProp('foo', textPayloadInput)).toBeTruthy()
expect(matchPayloadProp('fooo', textPayloadInput)).toBeFalsy()
expect(matchPayloadProp('bar', textPayloadInput)).toBeFalsy()
expect(matchPayloadProp('', textPayloadInput)).toBeFalsy()
expect(matchPayloadProp(null, textPayloadInput)).toBeFalsy()
})
it('regex <> text payload', () => {
expect(matchPayloadProp(/foo/, textPayloadInput)).toBeTruthy()
expect(matchPayloadProp(/bar/, textPayloadInput)).toBeFalsy()
})
it('function <> text payload', () => {
expect(
matchPayloadProp(v => v.startsWith('fo'), textPayloadInput)
).toBeTruthy()
expect(
matchPayloadProp(v => !v.startsWith('fo'), textPayloadInput)
).toBeFalsy()
})
it('text <> postback', () => {
expect(matchPayloadProp('foo', postbackInput)).toBeTruthy()
expect(matchPayloadProp('fooo', postbackInput)).toBeFalsy()
expect(matchPayloadProp('bar', postbackInput)).toBeFalsy()
expect(matchPayloadProp('', postbackInput)).toBeFalsy()
expect(matchPayloadProp(null, postbackInput)).toBeFalsy()
})
it('regex <> postback', () => {
expect(matchPayloadProp(/foo/, postbackInput)).toBeTruthy()
expect(matchPayloadProp(/bar/, postbackInput)).toBeFalsy()
})
it('function <> postback', () => {
expect(
matchPayloadProp(v => v.startsWith('fo'), postbackInput)
).toBeTruthy()
expect(
matchPayloadProp(v => !v.startsWith('fo'), postbackInput)
).toBeFalsy()
})
it('function <> request', () => {
expect(
matchRequestProp(
request =>
request.input.text === 'hi' &&
request.session.organization === 'myOrg' &&
request.lastRoutePath === 'initial',
requestInput
)
).toBeTruthy()
expect(
matchRequestProp(
request =>
request.input.text === 'hello' &&
request.session.organization === 'myOrg' &&
request.lastRoutePath === 'initial',
requestInput
)
).toBeFalsy()
})
it('type <> audio, document, image, video', () => {
expect(matchTypeProp('audio', audioInput)).toBeTruthy()
expect(matchTypeProp('document', documentInput)).toBeTruthy()
expect(matchTypeProp('image', imageInput)).toBeTruthy()
expect(matchTypeProp('video', videoInput)).toBeTruthy()
})
it('type <> other inputs', () => {
expect(matchTypeProp(/.*/, { type: 'anyOtherInput' })).toBeTruthy()
})
})
Loading

0 comments on commit a0eb609

Please sign in to comment.