Skip to content

Commit

Permalink
DEVEXP-487: E2E Conversation/TemplatesV2 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Jul 22, 2024
1 parent 80026c7 commit 23da75f
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { V2Template } from '../../v2-template';

export interface V2CreateTemplateRequestData {
/** Required. The template to create. */
'createTemplateRequestBody': V2Template;
'createTemplateRequestBody': Omit<V2Template, 'create_time' | 'update_time'>;
}
export interface V2DeleteTemplateRequestData {
/** Required. The ID of the template to delete. */
Expand All @@ -26,5 +26,5 @@ export interface V2UpdateTemplateRequestData {
/** The id of the template to be updated. Specified or automatically generated during template creation. Unique per project. */
'template_id': string;
/** Required. The updated template. */
'updateTemplateRequestBody': V2Template;
'updateTemplateRequestBody': Omit<V2Template, 'id' | 'create_time' | 'update_time'>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { V2TemplateTranslation } from '../v2-template-translation';

export interface V2Template {

/** The id of the template. Specify this yourself during creation. Otherwise, we will generate an ID for you. This must be unique for a given project. */
id?: string;
/** The description of the template. */
description?: string;
/** The version of the template. While creating a template, this will be defaulted to 1. When updating a template, you must supply the latest version of the template in order for the update to be successful. */
Expand Down
193 changes: 193 additions & 0 deletions packages/conversation/tests/rest/v1/templates-v2/templates-v2.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { Conversation, ConversationService, SupportedConversationRegion, TemplatesV2Api } from '../../../../src';
import { Given, Then, When } from '@cucumber/cucumber';
import * as assert from 'assert';

let templatesV2Api: TemplatesV2Api;
let template: Conversation.V2Template;
let templatesList: Conversation.V2ListTemplatesResponse;
let translationsList: Conversation.V2ListTranslationsResponse;
let deleteTemplateResponse: void;
let currentVersion: number;

Given('the Conversation service "TemplatesV2" is available', function () {
const conversationService = new ConversationService({
projectId: 'tinyfrog-jump-high-over-lilypadbasin',
keyId: 'keyId',
keySecret: 'keySecret',
authHostname: 'http://localhost:3011',
conversationTemplatesHostname: 'http://localhost:3015',
conversationRegion: SupportedConversationRegion.UNITED_STATES,
});
templatesV2Api = conversationService.templatesV2;
});

When('I send a request to create a conversation template with the V2 API', async () => {
template = await templatesV2Api.create({
createTemplateRequestBody: {
id: '01HVN010MG3B9N6X323JAFN59P',
default_translation: 'en-US',
description: 'Text template V2',
translations: [
{
language_code: 'en-US',
version: '3',
text_message: {
text: 'Hello ${name}. Text message template created with V2 API',
},
variables: [
{
key: 'name',
preview_value: 'Professor Jones',
},
],
},
],
},
});
});

Then('the conversation template V2 is created', () => {
assert.equal(template.id, '01HVN010MG3B9N6X323JAFN59P');
assert.equal(template.version, 1);
assert.equal(template.translations?.length, 1);
assert.equal(template.translations?.[0].version, '3');
});

When('I send a request to list the conversation templates with the V2 API', async () => {
templatesList = await templatesV2Api.list({});
});

Then('the response contains the list of conversation templates with the V2 structure', () => {
assert.ok(templatesList.templates);
assert.equal(templatesList.templates.length, 2);
});

Then('for each templateV2 in the templateV2 list response, it defines a translation with version "latest" on top of each current translation version', () => {
for(const templateV2 of templatesList.templates!) {
assert.ok(templateV2.version);
let latestVersionCount = 0;
let otherVersionCount = 0;
const translations = templateV2.translations!;
for(const translation of translations) {
translation.version === 'latest' ? latestVersionCount++ : otherVersionCount++;
}
assert.equal(latestVersionCount, otherVersionCount);
}
});

When('I send a request to list the translations for a template with the V2 API', async () => {
translationsList = await templatesV2Api.listTranslations({
template_id: '01W4FFL35P4NC4K35TEMPLATEV2',
});
});

Then('the response contains the list of translations for a template with the V2 structure', () => {
assert.ok(translationsList.translations);
assert.equal(translationsList.translations.length, 2);
assert.equal(translationsList.translations.find((translation) => translation.version === 'latest'), undefined);
});

When('I send a request to retrieve a conversation template with the V2 API', async () => {
template = await templatesV2Api.get({
template_id: '01HVN010MG3B9N6X323JAFN59P',
});
});

Then('the response contains the conversation template details with the V2 structure', () => {
assert.equal(template.id, '01HVN010MG3B9N6X323JAFN59P');
assert.equal(template.description, 'Text template V2');
assert.equal(template.version, 1);
assert.equal(template.translations?.length, 2);
const translation = template.translations!.find((translation) => translation.version !== 'latest');
assert.ok(translation);
assert.equal(translation.language_code, 'en-US');
assert.equal(translation.version, '3');
assert.deepEqual(translation.create_time, new Date('2024-06-06T14:42:42Z'));
assert.deepEqual(translation.update_time, new Date('2024-06-06T14:42:42Z'));
assert.equal(translation.variables?.length, 1);
assert.ok(translation.text_message?.text);
assert.deepEqual(translation.channel_template_overrides, {});
assert.equal(template.default_translation, 'en-US');
assert.deepEqual(template.create_time, new Date('2024-06-06T14:42:42Z'));
assert.deepEqual(template.update_time, new Date('2024-06-06T14:42:42Z'));
});

When('I send a request to update a conversation template with the V2 API', async () => {
currentVersion = 1;
template = await templatesV2Api.update({
template_id: '01HVN010MG3B9N6X323JAFN59P',
updateTemplateRequestBody: {
description: 'Updated description v2',
version: currentVersion,
default_translation: 'en-US',
translations: [
{
language_code: 'en-US',
version: '1',
list_message: {
title: 'Choose your icecream flavor',
description: 'The best icecream in town!',
sections: [
{
title: 'Fruit flavors',
items: [
{
choice: {
title: 'Strawberry',
postback_data: 'Strawberry postback',
},
},
{
choice: {
title: 'Blueberry',
postback_data: 'Blueberry postback',
},
},
],
},
{
title: 'Other flavors',
items: [
{
choice: {
title: 'Chocolate',
postback_data: 'Chocolate postback',
},
},
{
choice: {
title: 'Vanilla',
postback_data: 'Vanilla postback',
},
},
],
},
],
},
},
],
},
});
});

Then('the response contains the conversation template details with updated data with the V2 structure', () => {
assert.equal(template.id, '01HVN010MG3B9N6X323JAFN59P');
assert.equal(template.description, 'Updated description v2');
assert.equal(template.version, currentVersion + 1);
assert.equal(template.translations?.length, 1);
assert.equal(template.default_translation, 'en-US');
assert.deepEqual(template.create_time, new Date('2024-06-06T14:42:42Z'));
assert.deepEqual(template.update_time, new Date('2024-06-06T15:45:45Z'));
const translation = template.translations?.[0];
assert.ok(translation?.list_message);
});

When('I send a request to delete a conversation template with the V2 API', async () => {
deleteTemplateResponse = await templatesV2Api.delete({
template_id: '01W4FFL35P4NC4K35TEMPLATEV2',
});
});

Then('the delete conversation template response V2 contains no data', () => {
assert.deepEqual(deleteTemplateResponse, {} );
});
1 change: 0 additions & 1 deletion packages/numbers/tests/rest/v1/webhooks/webhooks.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const processEvent = async (response: Response) => {
});
rawEvent = await response.text();
rawEvent = rawEvent.replace(/\s+/g, '');
console.log(rawEvent);
event = numbersCallbackWebhook.parseEvent(JSON.parse(rawEvent));
};

Expand Down

0 comments on commit 23da75f

Please sign in to comment.