|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { GetAudioResponse } from '../../gen/component/audioin/v1/audioin_pb'; |
| 5 | +import { GetPropertiesResponse } from '../../gen/common/v1/common_pb'; |
| 6 | +import { RobotClient } from '../../robot'; |
| 7 | +import { type AudioChunk } from './audio-in'; |
| 8 | +import { AudioInClient } from './client'; |
| 9 | +import { AudioCodec } from '../../audio-common'; |
| 10 | +vi.mock('../../robot'); |
| 11 | + |
| 12 | +import type { PartialMessage } from '@bufbuild/protobuf'; |
| 13 | +import { createClient, createRouterTransport } from '@connectrpc/connect'; |
| 14 | +import { |
| 15 | + createWritableIterable, |
| 16 | + type WritableIterable, |
| 17 | +} from '@connectrpc/connect/protocol'; |
| 18 | +import { AudioInService } from '../../gen/component/audioin/v1/audioin_connect'; |
| 19 | + |
| 20 | +let audioin: AudioInClient; |
| 21 | + |
| 22 | +let testAudioStream: WritableIterable<PartialMessage<GetAudioResponse>>; |
| 23 | + |
| 24 | +const testProperties = new GetPropertiesResponse({ |
| 25 | + supportedCodecs: [AudioCodec.PCM16, AudioCodec.MP3, AudioCodec.PCM32_FLOAT], |
| 26 | + sampleRateHz: 48_000, |
| 27 | + numChannels: 2, |
| 28 | +}); |
| 29 | + |
| 30 | +describe('AudioInClient tests', () => { |
| 31 | + beforeEach(() => { |
| 32 | + testAudioStream = |
| 33 | + createWritableIterable<PartialMessage<GetAudioResponse>>(); |
| 34 | + |
| 35 | + const mockTransport = createRouterTransport(({ service }) => { |
| 36 | + service(AudioInService, { |
| 37 | + getAudio: () => { |
| 38 | + return testAudioStream; |
| 39 | + }, |
| 40 | + getProperties: () => { |
| 41 | + return testProperties; |
| 42 | + }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + RobotClient.prototype.createServiceClient = vi |
| 47 | + .fn() |
| 48 | + .mockImplementation(() => createClient(AudioInService, mockTransport)); |
| 49 | + |
| 50 | + audioin = new AudioInClient(new RobotClient('host'), 'test-audio-in'); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + vi.clearAllMocks(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('getAudio tests', () => { |
| 58 | + it('getAudio streams audio chunks', async () => { |
| 59 | + const audioChunks: AudioChunk[] = []; |
| 60 | + |
| 61 | + const streamProm = (async () => { |
| 62 | + for await (const chunk of audioin.getAudio( |
| 63 | + AudioCodec.PCM16, |
| 64 | + 1.1, |
| 65 | + BigInt(0) |
| 66 | + )) { |
| 67 | + audioChunks.push(chunk); |
| 68 | + } |
| 69 | + })(); |
| 70 | + |
| 71 | + await testAudioStream.write({ |
| 72 | + audio: { |
| 73 | + audioData: new Uint8Array([4, 5, 6]), |
| 74 | + audioInfo: { |
| 75 | + codec: AudioCodec.PCM16, |
| 76 | + sampleRateHz: 48_000, |
| 77 | + numChannels: 2, |
| 78 | + }, |
| 79 | + startTimestampNanoseconds: BigInt(1000), |
| 80 | + endTimestampNanoseconds: BigInt(2000), |
| 81 | + sequence: 1, |
| 82 | + }, |
| 83 | + requestId: 'test-request-1', |
| 84 | + }); |
| 85 | + |
| 86 | + await testAudioStream.write({ |
| 87 | + audio: { |
| 88 | + audioData: new Uint8Array([7, 8, 9]), |
| 89 | + audioInfo: { |
| 90 | + codec: AudioCodec.PCM16, |
| 91 | + sampleRateHz: 48_000, |
| 92 | + numChannels: 2, |
| 93 | + }, |
| 94 | + startTimestampNanoseconds: BigInt(2000), |
| 95 | + endTimestampNanoseconds: BigInt(3000), |
| 96 | + sequence: 2, |
| 97 | + }, |
| 98 | + requestId: 'test-request-1', |
| 99 | + }); |
| 100 | + |
| 101 | + testAudioStream.close(); |
| 102 | + await streamProm; |
| 103 | + |
| 104 | + expect(audioChunks.length).toEqual(2); |
| 105 | + |
| 106 | + const chunk1 = audioChunks[0]!; |
| 107 | + expect(chunk1.audioData).toEqual(new Uint8Array([4, 5, 6])); |
| 108 | + expect(chunk1.sequence).toEqual(1); |
| 109 | + |
| 110 | + const chunk2 = audioChunks[1]!; |
| 111 | + expect(chunk2.audioData).toEqual(new Uint8Array([7, 8, 9])); |
| 112 | + expect(chunk2.sequence).toEqual(2); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('getProperties tests', () => { |
| 117 | + it('getProperties returns audio properties', async () => { |
| 118 | + const properties = await audioin.getProperties(); |
| 119 | + |
| 120 | + expect(properties.supportedCodecs).toEqual([ |
| 121 | + AudioCodec.PCM16, |
| 122 | + AudioCodec.MP3, |
| 123 | + AudioCodec.PCM32_FLOAT, |
| 124 | + ]); |
| 125 | + expect(properties.sampleRateHz).toEqual(48_000); |
| 126 | + expect(properties.numChannels).toEqual(2); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments