|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const {assert} = require('chai'); |
| 19 | +const {after, before, describe, it} = require('mocha'); |
| 20 | +const cp = require('child_process'); |
| 21 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 22 | + |
| 23 | +const { |
| 24 | + ContactCenterInsightsClient, |
| 25 | +} = require('@google-cloud/contact-center-insights'); |
| 26 | +const client = new ContactCenterInsightsClient(); |
| 27 | + |
| 28 | +describe('CreateIssueModel', () => { |
| 29 | + const minConversationCount = 10000; |
| 30 | + const pageSize = 1000; |
| 31 | + let projectId; |
| 32 | + let conversationCount; |
| 33 | + let issueModelName; |
| 34 | + |
| 35 | + before(async () => { |
| 36 | + projectId = await client.getProjectId(); |
| 37 | + |
| 38 | + // Check if the project has the minimum number of conversations required to create an issue model. |
| 39 | + // See https://cloud.google.com/contact-center/insights/docs/topic-model. |
| 40 | + conversationCount = 0; |
| 41 | + let pageToken = ''; |
| 42 | + let listResponse; |
| 43 | + while (conversationCount < minConversationCount) { |
| 44 | + if (pageToken) { |
| 45 | + [listResponse] = await client.listConversations({ |
| 46 | + parent: client.locationPath(projectId, 'us-central1'), |
| 47 | + pageSize: pageSize, |
| 48 | + pageToken: pageToken, |
| 49 | + }); |
| 50 | + } else { |
| 51 | + [listResponse] = await client.listConversations({ |
| 52 | + parent: client.locationPath(projectId, 'us-central1'), |
| 53 | + pageSize: pageSize, |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + if (!listResponse.conversations) { |
| 58 | + break; |
| 59 | + } |
| 60 | + conversationCount += listResponse.conversations.length; |
| 61 | + |
| 62 | + if (!listResponse.nextPageToken) { |
| 63 | + break; |
| 64 | + } |
| 65 | + pageToken = listResponse.nextPageToken; |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + after(async () => { |
| 70 | + if (conversationCount >= minConversationCount) { |
| 71 | + client.deleteIssueModel({ |
| 72 | + name: issueModelName, |
| 73 | + }); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('should create an issue model', async () => { |
| 78 | + if (conversationCount >= minConversationCount) { |
| 79 | + const stdout = execSync(`node ./createIssueModel.js ${projectId}`); |
| 80 | + issueModelName = stdout.slice(8); |
| 81 | + assert.match( |
| 82 | + stdout, |
| 83 | + new RegExp( |
| 84 | + 'Created projects/[0-9]+/locations/us-central1/issueModels/[0-9]+' |
| 85 | + ) |
| 86 | + ); |
| 87 | + } |
| 88 | + }); |
| 89 | +}); |
0 commit comments