Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Добавлены тесты для подписок #113

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
354 changes: 176 additions & 178 deletions cypress/integration/autolistening.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Server } from 'mock-socket';

import { Message } from '../../src/proto';
import { createAssistantClient, MessageNames, EmotionId } from '../../src';
import { createAssistantClient, MessageNames } from '../../src';
import { createMessage, createVoiceMessage } from '../support/helpers/clientMethods.helpers';
import { initServer, initAssistantClient } from '../support/helpers/init';

Expand All @@ -17,183 +17,181 @@ describe('Автослушание', () => {
});

afterEach(() => {
server?.stop();
server?.stop();
});

it('Озвучивание прекращается при установке disableDubbing=true', (done) => {
let currentEmotion: EmotionId = 'idle';

server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text } = message;

if (text?.data === 'hello') {
[1,2,3,4,5,6,7,8].forEach((i) => {
if (i === 4) {
assistantClient.changeSettings({ disableDubbing: true });
}

socket.send(
createVoiceMessage({
messageId,
last: i === 8 ? 1 : -1,
})
);

expect(currentEmotion).eq(i >= 4 ? 'idle' : 'talk', 'Остановка озвучки работает');

if (i === 8) {
done();
}
});
}
});
});

assistantClient.on('assistant', ({ emotion }) => {
if (typeof emotion !== 'undefined') {
currentEmotion = emotion;
}
});

assistantClient.changeSettings({ disableDubbing: false });

assistantClient.start();

assistantClient.sendText('hello');
});

it('Автослушание не запускается при disableListening=true', () => {
server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text, voice } = message;

if (voice) {
throw new Error('Слушание запускаться не должно');
}

if (text?.data === 'hello') {
socket.send(
createMessage({
messageId,
messageName: MessageNames.ANSWER_TO_USER,
systemMessage: {
auto_listening: true,
items: [{ bubble: { text: 'Hello!' } }],
},
last: 1,
})
);
}
});
});

assistantClient.changeSettings({
disableDubbing: true,
disableListening: true,
});

assistantClient.start();

assistantClient.sendText('hello');

cy.wait(2000);
});

it('При установке disableDubbing=true после запуска слушания ответ не озвучивается, запускается автослушание', () => {
let hadListening = false;

server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text, voice } = message;

if (voice) {
hadListening = true;
}

if (text?.data === 'hello') {
socket.send(
createVoiceMessage({
messageId,
messageName: MessageNames.ANSWER_TO_USER,
systemMessage: {
auto_listening: true,
},
last: 1,
})
);
}
});
});

assistantClient.on('assistant', ({ emotion }) => {
if (emotion === 'talk') {
throw new Error('Озвучивание запускаться не должно');
}
});

assistantClient.changeSettings({
disableDubbing: true,
disableListening: false,
});

assistantClient.start();

assistantClient.sendText('hello');

// Не используем done(), чтобы в любом случае пройти обе проверки
cy.wait(2000).then(() => {
expect(hadListening).eq(true, 'Автослушание должно запуститься');
});
});

const testAutolisteningAfterVoiceAnswer = (done: () => void, disableDubbing: boolean) => {
let isTestDone = false;

server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text, voice } = message;

if (voice && !isTestDone) {
done();

isTestDone = true;
}

if (text?.data === 'hello') {
socket.send(
createVoiceMessage({
messageId,
messageName: MessageNames.ANSWER_TO_USER,
systemMessage: {
auto_listening: true,
},
last: 1,
})
);
}
});
});

assistantClient.changeSettings({
disableDubbing,
disableListening: false,
});

assistantClient.start();

assistantClient.sendText('hello');
};

it('Автослушание после голосового ответа работает (при disableDubbing=false)', (done) => {
testAutolisteningAfterVoiceAnswer(done, false);
});

it('Автослушание после голосового ответа работает (при disableDubbing=true)', (done) => {
testAutolisteningAfterVoiceAnswer(done, true);
});
it('Озвучивание прекращается при установке disableDubbing=true', (done) => {
let ttsStatus: 'start' | 'stop' | null = null;

server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text } = message;

if (text?.data === 'hello') {
[1, 2, 3, 4, 5, 6, 7, 8].forEach((i) => {
if (i === 4) {
assistantClient.changeSettings({ disableDubbing: true });
}

socket.send(
createVoiceMessage({
messageId,
last: i === 8 ? 1 : -1,
}),
);

expect(ttsStatus).eq(i >= 4 ? 'stop' : 'start', 'Остановка озвучки работает');

if (i === 8) {
done();
}
});
}
});
});

assistantClient.on('tts', ({ status }) => {
ttsStatus = status;
});

assistantClient.changeSettings({ disableDubbing: false });

assistantClient.start();

assistantClient.sendText('hello');
});

it('Автослушание не запускается при disableListening=true', () => {
server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text, voice } = message;

if (voice) {
throw new Error('Слушание запускаться не должно');
}

if (text?.data === 'hello') {
socket.send(
createMessage({
messageId,
messageName: MessageNames.ANSWER_TO_USER,
systemMessage: {
auto_listening: true,
items: [{ bubble: { text: 'Hello!' } }],
},
last: 1,
}),
);
}
});
});

assistantClient.changeSettings({
disableDubbing: true,
disableListening: true,
});

assistantClient.start();

assistantClient.sendText('hello');

cy.wait(2000);
});

it('При установке disableDubbing=true после запуска слушания ответ не озвучивается, запускается автослушание', () => {
let hadListening = false;

server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text, voice } = message;

if (voice) {
hadListening = true;
}

if (text?.data === 'hello') {
socket.send(
createVoiceMessage({
messageId,
messageName: MessageNames.ANSWER_TO_USER,
systemMessage: {
auto_listening: true,
},
last: 1,
}),
);
}
});
});

assistantClient.on('tts', ({ status }) => {
if (status === 'start') {
throw new Error('Озвучивание запускаться не должно');
}
});

assistantClient.changeSettings({
disableDubbing: true,
disableListening: false,
});

assistantClient.start();

assistantClient.sendText('hello');

// Не используем done(), чтобы в любом случае пройти обе проверки
cy.wait(2000).then(() => {
expect(hadListening).eq(true, 'Автослушание должно запуститься');
});
});

const testAutolisteningAfterVoiceAnswer = (done: () => void, disableDubbing: boolean) => {
let isTestDone = false;

server.on('connection', (socket) => {
socket.on('message', (data) => {
const message = Message.decode((data as Uint8Array).slice(4));
const { messageId, text, voice } = message;

if (voice && !isTestDone) {
done();

isTestDone = true;
}

if (text?.data === 'hello') {
socket.send(
createVoiceMessage({
messageId,
messageName: MessageNames.ANSWER_TO_USER,
systemMessage: {
auto_listening: true,
},
last: 1,
}),
);
}
});
});

assistantClient.changeSettings({
disableDubbing,
disableListening: false,
});

assistantClient.start();

assistantClient.sendText('hello');
};

it('Автослушание после голосового ответа работает (при disableDubbing=false)', (done) => {
testAutolisteningAfterVoiceAnswer(done, false);
});

it('Автослушание после голосового ответа работает (при disableDubbing=true)', (done) => {
testAutolisteningAfterVoiceAnswer(done, true);
});
});
Loading
Loading