+42
![autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)








Saumya Talwani
GitHub
Poojan
sahil-infocusp
非法操作
Pandaaaa906
Asuka Minato
heyszt
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Ijas
gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
木之本澪
KinomotoMio
不做了睡大觉
User
Copilot
edvatar
-LAN-
Leilei
HaKu
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
wangxiaolei
Varun Chawla
Stephen Zhou
yyh
yyh
tda
root
Sisyphus
Niels Kaspers
hj24
Tyson Cung
Stephen Zhou
FFXN
slegarraga
99
Br1an
L1nSn0w
Yunlu Wen
akkoaya
盐粒 Yanli
lif
weiguang li
Copilot
crazywoola
HanWenbo
Coding On Star
CodingOnStar
Stable Genius
Stable Genius
ふるい
Xiyuan Chen
f50e44b24a
Signed-off-by: edvatar <[email protected]> Signed-off-by: -LAN- <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: majiayu000 <[email protected]> Co-authored-by: Poojan <[email protected]> Co-authored-by: sahil-infocusp <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Pandaaaa906 <[email protected]> Co-authored-by: Asuka Minato <[email protected]> Co-authored-by: heyszt <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ijas <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: 木之本澪 <[email protected]> Co-authored-by: KinomotoMio <[email protected]> Co-authored-by: 不做了睡大觉 <[email protected]> Co-authored-by: User <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: edvatar <[email protected]> Co-authored-by: -LAN- <[email protected]> Co-authored-by: Leilei <[email protected]> Co-authored-by: HaKu <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: wangxiaolei <[email protected]> Co-authored-by: Varun Chawla <[email protected]> Co-authored-by: Stephen Zhou <[email protected]> Co-authored-by: yyh <[email protected]> Co-authored-by: yyh <[email protected]> Co-authored-by: tda <[email protected]> Co-authored-by: root <root@DESKTOP-KQLO90N> Co-authored-by: Sisyphus <[email protected]> Co-authored-by: Niels Kaspers <[email protected]> Co-authored-by: hj24 <[email protected]> Co-authored-by: Tyson Cung <[email protected]> Co-authored-by: Stephen Zhou <[email protected]> Co-authored-by: FFXN <[email protected]> Co-authored-by: slegarraga <[email protected]> Co-authored-by: 99 <[email protected]> Co-authored-by: Br1an <[email protected]> Co-authored-by: L1nSn0w <[email protected]> Co-authored-by: Yunlu Wen <[email protected]> Co-authored-by: akkoaya <[email protected]> Co-authored-by: 盐粒 Yanli <[email protected]> Co-authored-by: lif <[email protected]> Co-authored-by: weiguang li <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: HanWenbo <[email protected]> Co-authored-by: Coding On Star <[email protected]> Co-authored-by: CodingOnStar <[email protected]> Co-authored-by: Stable Genius <[email protected]> Co-authored-by: Stable Genius <[email protected]> Co-authored-by: ふるい <[email protected]> Co-authored-by: Xiyuan Chen <[email protected]>
197 lines
5.3 KiB
TypeScript
197 lines
5.3 KiB
TypeScript
import { convertToMp3 } from '../utils'
|
|
|
|
// ── Hoisted mocks ──
|
|
|
|
const mocks = vi.hoisted(() => {
|
|
const readHeader = vi.fn()
|
|
const encodeBuffer = vi.fn()
|
|
const flush = vi.fn()
|
|
|
|
return { readHeader, encodeBuffer, flush }
|
|
})
|
|
|
|
vi.mock('lamejs', () => ({
|
|
default: {
|
|
WavHeader: {
|
|
readHeader: mocks.readHeader,
|
|
},
|
|
Mp3Encoder: class MockMp3Encoder {
|
|
encodeBuffer = mocks.encodeBuffer
|
|
flush = mocks.flush
|
|
},
|
|
},
|
|
}))
|
|
|
|
vi.mock('lamejs/src/js/BitStream', () => ({ default: {} }))
|
|
vi.mock('lamejs/src/js/Lame', () => ({ default: {} }))
|
|
vi.mock('lamejs/src/js/MPEGMode', () => ({ default: {} }))
|
|
|
|
// ── helpers ──
|
|
|
|
/** Build a fake recorder whose getChannelData returns DataView-like objects with .buffer and .byteLength. */
|
|
function createMockRecorder(opts: {
|
|
channels: number
|
|
sampleRate: number
|
|
leftSamples: number[]
|
|
rightSamples?: number[]
|
|
}) {
|
|
const toDataView = (samples: number[]) => {
|
|
const buf = new ArrayBuffer(samples.length * 2)
|
|
const view = new DataView(buf)
|
|
samples.forEach((v, i) => {
|
|
view.setInt16(i * 2, v, true)
|
|
})
|
|
return view
|
|
}
|
|
|
|
const leftView = toDataView(opts.leftSamples)
|
|
const rightView = opts.rightSamples ? toDataView(opts.rightSamples) : null
|
|
|
|
mocks.readHeader.mockReturnValue({
|
|
channels: opts.channels,
|
|
sampleRate: opts.sampleRate,
|
|
})
|
|
|
|
return {
|
|
getWAV: vi.fn(() => new ArrayBuffer(44)),
|
|
getChannelData: vi.fn(() => ({
|
|
left: leftView,
|
|
right: rightView,
|
|
})),
|
|
}
|
|
}
|
|
|
|
describe('convertToMp3', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('should convert mono WAV data to an MP3 blob', () => {
|
|
const recorder = createMockRecorder({
|
|
channels: 1,
|
|
sampleRate: 44100,
|
|
leftSamples: [100, 200, 300, 400],
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array([1, 2, 3]))
|
|
mocks.flush.mockReturnValue(new Int8Array([4, 5]))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(result).toBeInstanceOf(Blob)
|
|
expect(result.type).toBe('audio/mp3')
|
|
expect(mocks.encodeBuffer).toHaveBeenCalled()
|
|
// Mono: encodeBuffer called with only left data
|
|
const firstCall = mocks.encodeBuffer.mock.calls[0]
|
|
expect(firstCall).toHaveLength(1)
|
|
expect(mocks.flush).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should convert stereo WAV data to an MP3 blob', () => {
|
|
const recorder = createMockRecorder({
|
|
channels: 2,
|
|
sampleRate: 48000,
|
|
leftSamples: [100, 200],
|
|
rightSamples: [300, 400],
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array([10, 20]))
|
|
mocks.flush.mockReturnValue(new Int8Array([30]))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(result).toBeInstanceOf(Blob)
|
|
expect(result.type).toBe('audio/mp3')
|
|
// Stereo: encodeBuffer called with left AND right
|
|
const firstCall = mocks.encodeBuffer.mock.calls[0]
|
|
expect(firstCall).toHaveLength(2)
|
|
})
|
|
|
|
it('should skip empty encoded buffers', () => {
|
|
const recorder = createMockRecorder({
|
|
channels: 1,
|
|
sampleRate: 44100,
|
|
leftSamples: [100, 200],
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array(0))
|
|
mocks.flush.mockReturnValue(new Int8Array(0))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(result).toBeInstanceOf(Blob)
|
|
expect(result.type).toBe('audio/mp3')
|
|
expect(result.size).toBe(0)
|
|
})
|
|
|
|
it('should include flush data when flush returns non-empty buffer', () => {
|
|
const recorder = createMockRecorder({
|
|
channels: 1,
|
|
sampleRate: 22050,
|
|
leftSamples: [1],
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array(0))
|
|
mocks.flush.mockReturnValue(new Int8Array([99, 98, 97]))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(result).toBeInstanceOf(Blob)
|
|
expect(result.size).toBe(3)
|
|
})
|
|
|
|
it('should omit flush data when flush returns empty buffer', () => {
|
|
const recorder = createMockRecorder({
|
|
channels: 1,
|
|
sampleRate: 44100,
|
|
leftSamples: [10, 20],
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array([1, 2]))
|
|
mocks.flush.mockReturnValue(new Int8Array(0))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(result).toBeInstanceOf(Blob)
|
|
expect(result.size).toBe(2)
|
|
})
|
|
|
|
it('should process multiple chunks when sample count exceeds maxSamples (1152)', () => {
|
|
const samples = Array.from({ length: 2400 }, (_, i) => i % 32767)
|
|
const recorder = createMockRecorder({
|
|
channels: 1,
|
|
sampleRate: 44100,
|
|
leftSamples: samples,
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array([1]))
|
|
mocks.flush.mockReturnValue(new Int8Array(0))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(mocks.encodeBuffer.mock.calls.length).toBeGreaterThan(1)
|
|
expect(result).toBeInstanceOf(Blob)
|
|
})
|
|
|
|
it('should encode stereo with right channel subarray', () => {
|
|
const recorder = createMockRecorder({
|
|
channels: 2,
|
|
sampleRate: 44100,
|
|
leftSamples: [100, 200, 300],
|
|
rightSamples: [400, 500, 600],
|
|
})
|
|
|
|
mocks.encodeBuffer.mockReturnValue(new Int8Array([5, 6, 7]))
|
|
mocks.flush.mockReturnValue(new Int8Array([8]))
|
|
|
|
const result = convertToMp3(recorder)
|
|
|
|
expect(result).toBeInstanceOf(Blob)
|
|
for (const call of mocks.encodeBuffer.mock.calls) {
|
|
expect(call).toHaveLength(2)
|
|
expect(call[0]).toBeInstanceOf(Int16Array)
|
|
expect(call[1]).toBeInstanceOf(Int16Array)
|
|
}
|
|
})
|
|
})
|