Skip to content

Commit

Permalink
fix: update timestamp (#60)
Browse files Browse the repository at this point in the history
* fix: update timestamp

* test: new
  • Loading branch information
leartgjoni-voiceflow authored May 28, 2020
1 parent 8c5b466 commit e63819b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
5 changes: 5 additions & 0 deletions lib/constants/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export enum Frame {
CALLED_COMMAND = 'calledCommand',
}

export enum Variables {
TIMESTAMP = 'timestamp',
}

export default {
Storage,
Turn,
Frame,
Variables,
};
2 changes: 1 addition & 1 deletion lib/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// eslint-disable-next-line import/prefer-default-export
export { default as Flags, Turn as T, Storage as S, Frame as F } from './flags';
export { default as Flags, Turn as T, Storage as S, Frame as F, Variables as V } from './flags';

export const TEST_VERSION_ID = '__TEST__';
4 changes: 2 additions & 2 deletions lib/services/alexa/request/lifecycle/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, Frame, Store } from '@voiceflow/client';
import { HandlerInput } from 'ask-sdk';

import { F, S } from '@/lib/constants';
import { F, S, V } from '@/lib/constants';
import { createResumeFrame, RESUME_DIAGRAM_ID } from '@/lib/services/voiceflow/diagrams/resume';
import { StreamAction } from '@/lib/services/voiceflow/handlers/stream';

Expand Down Expand Up @@ -50,7 +50,7 @@ export const initializeGenerator = (utils: typeof utilsObj) => async (context: C

// default global variables
variables.merge({
timestamp: Math.floor(Date.now() / 1000),
[V.TIMESTAMP]: 0,
locale: storage.get(S.LOCALE),
user_id: storage.get(S.USER),
sessions: storage.get(S.SESSIONS),
Expand Down
5 changes: 3 additions & 2 deletions lib/services/alexa/request/lifecycle/update.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Context } from '@voiceflow/client';

import { T } from '@/lib/constants';
import { T, V } from '@/lib/constants';

const update = async (context: Context): Promise<void> => {
const { turn } = context;
const { turn, variables } = context;

turn.set(T.REQUEST, context.getRequest());
variables.set(V.TIMESTAMP, Math.floor(Date.now() / 1000));

await context.update();
};
Expand Down
3 changes: 2 additions & 1 deletion lib/services/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventType, State } from '@voiceflow/client';
import { StreamAction as TraceStreamAction } from '@voiceflow/client/build/lib/Context/Trace';
import { IntentRequest as AlexaIntentRequest } from 'ask-sdk-model';

import { S, T, TEST_VERSION_ID } from '@/lib/constants';
import { S, T, TEST_VERSION_ID, V } from '@/lib/constants';
import { StreamAction, StreamPlay } from '@/lib/services/voiceflow/handlers/stream';
import { RequestType } from '@/lib/services/voiceflow/types';

Expand All @@ -29,6 +29,7 @@ const TestManager = (services: Services, config: Config, utils = utilsObj) => {
context.setEvent(EventType.handlerWillHandle, (event) => context.trace.block(event.block.blockID));

context.turn.set(T.REQUEST, request);
context.variables.set(V.TIMESTAMP, Math.floor(Date.now() / 1000));

await context.update();

Expand Down
12 changes: 1 addition & 11 deletions tests/lib/services/alexa/request/lifecycle/initialize.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import { initializeGenerator, VAR_VF } from '@/lib/services/alexa/request/lifecy
import { StreamAction } from '@/lib/services/voiceflow/handlers/stream';

describe('initialize lifecycle unit tests', async () => {
let clock: sinon.SinonFakeTimers;

beforeEach(() => {
clock = sinon.useFakeTimers(Date.now()); // fake Date.now
});
afterEach(() => {
clock.restore(); // restore Date.now
sinon.restore();
});

describe('initialize', () => {
const generateFakes = () => {
const resumeFrame = { foo: 'bar' };
Expand Down Expand Up @@ -129,7 +119,7 @@ describe('initialize lifecycle unit tests', async () => {
expect(context.storage.set.args[5]).to.eql([S.REPEAT, metaObj.repeat]);
expect(context.variables.merge.args[0]).to.eql([
{
timestamp: Math.floor(clock.now / 1000),
timestamp: 0,
locale,
user_id: userId,
sessions: 1,
Expand Down
20 changes: 18 additions & 2 deletions tests/lib/services/alexa/request/lifecycle/update.unit.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { expect } from 'chai';
import sinon from 'sinon';

import { T } from '@/lib/constants';
import { T, V } from '@/lib/constants';
import update from '@/lib/services/alexa/request/lifecycle/update';

describe('update lifecycle unit tests', () => {
let clock: sinon.SinonFakeTimers;

beforeEach(() => {
clock = sinon.useFakeTimers(Date.now()); // fake Date.now
});
afterEach(() => {
clock.restore(); // restore Date.now
sinon.restore();
});

describe('update', () => {
it('works correctly', async () => {
const request = { foo: 'bar' };
const context = { turn: { set: sinon.stub() }, update: sinon.stub(), getRequest: sinon.stub().returns(request) };
const context = {
variables: { set: sinon.stub() },
turn: { set: sinon.stub() },
update: sinon.stub(),
getRequest: sinon.stub().returns(request),
};

await update(context as any);
expect(context.turn.set.args).to.eql([[T.REQUEST, request]]);
expect(context.variables.set.args).to.eql([[V.TIMESTAMP, Math.floor(clock.now / 1000)]]);
expect(context.update.callCount).to.eql(1);
});
});
Expand Down
18 changes: 17 additions & 1 deletion tests/lib/services/test/index.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ import { StreamAction as TraceStreamAction } from '@voiceflow/client/build/lib/C
import { expect } from 'chai';
import sinon from 'sinon';

import { T, TEST_VERSION_ID } from '@/lib/constants';
import { T, TEST_VERSION_ID, V } from '@/lib/constants';
import TestManager from '@/lib/services/test';
import { StreamAction } from '@/lib/services/voiceflow/handlers/stream';

describe('test manager unit tests', () => {
let clock: sinon.SinonFakeTimers;

beforeEach(() => {
clock = sinon.useFakeTimers(Date.now()); // fake Date.now
});
afterEach(() => {
clock.restore(); // restore Date.now
sinon.restore();
});

describe('invoke', () => {
it('works correctly', async () => {
const rawState = { foo: 'bar' };
Expand All @@ -25,6 +35,7 @@ describe('test manager unit tests', () => {
stack: {
isEmpty: sinon.stub().returns(false), // stack no empty
},
variables: { set: sinon.stub() },
update: sinon.stub(),
getRawState: sinon.stub().returns(rawState),
trace: { get: sinon.stub().returns(trace), block: sinon.stub() },
Expand Down Expand Up @@ -65,6 +76,7 @@ describe('test manager unit tests', () => {
fn(event);
expect(context.trace.block.args).to.eql([[event.block.blockID]]);
expect(context.turn.set.args).to.eql([[T.REQUEST, request]]);
expect(context.variables.set.args).to.eql([[V.TIMESTAMP, Math.floor(clock.now / 1000)]]);
expect(context.update.callCount).to.eql(1);
});

Expand All @@ -83,6 +95,7 @@ describe('test manager unit tests', () => {
stack: {
isEmpty: sinon.stub().returns(true), // stack is empty
},
variables: { set: sinon.stub() },
update: sinon.stub(),
getRawState: sinon.stub().returns(rawState),
trace: { get: sinon.stub().returns(trace), block: sinon.stub(), end: sinon.stub() },
Expand Down Expand Up @@ -125,6 +138,7 @@ describe('test manager unit tests', () => {
stack: {
isEmpty: sinon.stub().returns(true), // stack is empty
},
variables: { set: sinon.stub() },
update: sinon.stub(),
getRawState: sinon.stub().returns(rawState),
trace: { get: sinon.stub().returns(trace), block: sinon.stub(), end: sinon.stub(), stream: sinon.stub() },
Expand Down Expand Up @@ -167,6 +181,7 @@ describe('test manager unit tests', () => {
stack: {
isEmpty: sinon.stub().returns(true), // stack is empty
},
variables: { set: sinon.stub() },
update: sinon.stub(),
getRawState: sinon.stub().returns(rawState),
trace: { get: sinon.stub().returns(trace), block: sinon.stub(), end: sinon.stub(), stream: sinon.stub() },
Expand Down Expand Up @@ -209,6 +224,7 @@ describe('test manager unit tests', () => {
stack: {
isEmpty: sinon.stub().returns(true), // stack is empty
},
variables: { set: sinon.stub() },
update: sinon.stub(),
getRawState: sinon.stub().returns(rawState),
trace: { get: sinon.stub().returns(trace), block: sinon.stub(), end: sinon.stub(), stream: sinon.stub() },
Expand Down

0 comments on commit e63819b

Please sign in to comment.