Skip to content

Commit

Permalink
fix: Refresh expression for intents and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Oct 6, 2019
1 parent 2466c47 commit c8a682d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/ActionsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export class ActionsBuilder<Context=void, ReturnType=void> {
public build(): Matcher<Action<Context, ReturnType, any>> {
const graph = this.builder.build();
return new GraphMatcher(this.language, graph, {
mapper: m => m.data
mapper: m => {
m.data.refreshExpression();
return m.data;
}
}) as any;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/IntentsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export class IntentsBuilder<Intents extends Intent<any, any> = never> {
public build(): Matcher<Intents> {
const graph = this.builder.build();
return new GraphMatcher(this.language, graph, {
mapper: m => m.data
mapper: m => {
m.data.refreshExpression();
return m.data;
}
}) as any;
}
}
Expand Down
87 changes: 87 additions & 0 deletions test/value-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PhrasesBuilder } from '../src/resolver/PhrasesBuilder';

import { optionsValue, dateIntervalValue, enumerationValue, customValue } from '../src/values';
import { newPhrases } from '../src/resolver/newPhrases';
import { IntentsBuilder } from '../src/IntentsBuilder';

describe('Value: Options', function() {

Expand Down Expand Up @@ -401,9 +402,95 @@ describe('Value: Options', function() {
return resolver2.matchPartial('test', { now: new Date(2018, 0, 2) })
.then(r => {
expect(r.length).toEqual(2);

const expression = r[0].expression;

expect(expression[0].type).toEqual('value');
expect((expression[0] as any).value).toEqual('test');

expect((expression[1] as any).value).not.toBeUndefined();
});
});

});

describe('With values in another graph', () => {
const queryOptions = optionsValue()
.add({
id: 'deadline',
phrases: newPhrases()
.value('deadline', dateIntervalValue())
.phrase('with deadline {deadline}')
.phrase('deadline {deadline}')
.build()
})
.add({
id: 'completed',
phrases: newPhrases()
.value('completed', dateIntervalValue())
.phrase('completed {completed}')
.build()
})
.build();

const phrases = newPhrases()
.value('queryOptions', queryOptions)
.phrase('Things {queryOptions}')
.build();

const intents = new IntentsBuilder(en)
.add('test', phrases)
.build();

it('Full match', () => {
return intents.match('things with deadline jan 12th', { now: new Date(2018, 0, 2) })
.then(r => {
expect(r).not.toBeNull();

const v = r.values.queryOptions.get('deadline');
expect(v.option).toEqual('deadline');
expect(v.values.deadline).toEqual({
start: { year: 2018, month: 1, dayOfMonth: 12 },
end: { year: 2018, month: 1, dayOfMonth: 12 }
});
});
});

it('Multiple options', () => {
return intents.match('things with deadline jan 12th and completed today', { now: new Date(2018, 0, 2) })
.then(r => {
expect(r).not.toBeNull();

const v0 = r.values.queryOptions.get('deadline');
expect(v0.option).toEqual('deadline');
expect(v0.values.deadline).toEqual({
start: { year: 2018, month: 1, dayOfMonth: 12 },
end: { year: 2018, month: 1, dayOfMonth: 12 }
});

const v1 = r.values.queryOptions.get('completed');
expect(v1.option).toEqual('completed');
expect(v1.values.completed).toEqual({
start: { year: 2018, month: 1, dayOfMonth: 2 },
end: { year: 2018, month: 1, dayOfMonth: 2 }
});
});
});

it('Partial match with enum first', () => {
return intents.matchPartial('thing', { now: new Date(2018, 0, 2) })
.then(r => {
expect(r.length).toEqual(2);

const expression = r[0].expression;

expect(expression[0].type).toEqual('text');
expect((expression[0] as any).value).toEqual('Things');

expect(expression[1].type).toEqual('value');
expect((expression[1] as any).value).not.toBeUndefined();
});
});
});

});

0 comments on commit c8a682d

Please sign in to comment.