fix(protocol): produce valid MP3 records

Use the dedicated audio conversion path for record output so mono QQ voice records produce valid MP3 data.

Add release-target smoke coverage and preserve conversion and cleanup failures for diagnostics.

Fixes #239
This commit is contained in:
DrSmoothl committed 2026-07-17 18:17:48 +08:00
1 parent 1568942ae6
commit b3838d204f
6 files changed
+182 -26

No files matched your search

@@ -1,21 +1,22 @@
import { afterAll, describe, expect, it } from 'vitest';
import { mkdtempSync, readdirSync, rmSync, writeFileSync } from 'fs';
import { afterAll, describe, expect, it, vi } from 'vitest';
import { mkdtempSync, promises as fsPromises, readdirSync, rmSync, writeFileSync } from 'fs';
import os from 'os';
import path from 'path';
import { AUDIO_OUT_FORMATS, convertAudioBytes, isAudioOutFormat } from '@snowluma/protocol/highway/ffmpeg-addon';
// convertAudioBytes is the pure (#165) transcode seam: write input → addon
// convertFile → read output as base64 → always clean up temp files. The native
// addon is file-based and can't run in CI, so a fake addon stands in.
// convertAudioBytes is the pure (#165) transcode seam: write input → the
// addon's dedicated audio transcoder → read output as base64 → always clean up
// temp files. Unit tests inject a fake; audio-transcode-native.test.ts covers
// each bundled release-platform addon in the build matrices.
const tmpDir = mkdtempSync(path.join(os.tmpdir(), 'sl-audio-test-'));
afterAll(() => rmSync(tmpDir, { recursive: true, force: true }));
/** Fake addon: writes deterministic bytes to the output path and reports ok. */
const fakeOk = {
convertFile: async (_in: string, out: string, _fmt: string) => {
decodeAudioToFmt: async (_in: string, out: string, format: string) => {
writeFileSync(out, Buffer.from('WAVDATA')); // stand-in converted payload
return { success: true };
return { result: true, sampleRate: 24_000, channels: 1, format };
},
};
@@ -38,14 +39,21 @@ describe('convertAudioBytes', () => {
it('rejects an unsupported format before touching the addon', async () => {
let called = false;
const spy = { convertFile: async () => { called = true; return { success: true }; } };
const spy = {
decodeAudioToFmt: async (_in: string, _out: string, format: string) => {
called = true;
return { result: true, sampleRate: 24_000, channels: 1, format };
},
};
await expect(convertAudioBytes(new Uint8Array([1]), 'opus', { addon: spy, tmpDir })).rejects.toThrow(/unsupported out_format/);
expect(called).toBe(false);
});
it('throws and cleans up when the addon reports failure', async () => {
const fakeFail = { convertFile: async () => ({ success: false }) };
await expect(convertAudioBytes(new Uint8Array([9]), 'mp3', { addon: fakeFail, tmpDir })).rejects.toThrow(/conversion failed/);
it('propagates a native conversion rejection and cleans up', async () => {
const fakeFail = {
decodeAudioToFmt: async () => { throw new Error('native conversion failed'); },
};
await expect(convertAudioBytes(new Uint8Array([9]), 'mp3', { addon: fakeFail, tmpDir })).rejects.toThrow(/native conversion failed/);
// no leftover temp files for this run
const leftover = readdirSync(tmpDir).filter((n) => n.endsWith('.mp3') || n.endsWith('.in'));
expect(leftover).toEqual([]);
@@ -64,4 +72,29 @@ describe('convertAudioBytes', () => {
const leftover = readdirSync(tmpDir).filter((n) => n.endsWith('.flac') || n.endsWith('.in'));
expect(leftover).toEqual([]);
});
it('surfaces temporary-file cleanup failures', async () => {
const rmSpy = vi.spyOn(fsPromises, 'rm').mockRejectedValue(new Error('cleanup denied'));
try {
await expect(
convertAudioBytes(new Uint8Array([6]), 'wav', { addon: fakeOk, tmpDir }),
).rejects.toThrow(/temporary audio file cleanup failed/);
} finally {
rmSpy.mockRestore();
}
});
it('reports both the conversion and cleanup causes when both fail', async () => {
const nativeFailure = {
decodeAudioToFmt: async () => { throw new Error('native conversion failed'); },
};
const rmSpy = vi.spyOn(fsPromises, 'rm').mockRejectedValue(new Error('cleanup denied'));
try {
await expect(
convertAudioBytes(new Uint8Array([7]), 'mp3', { addon: nativeFailure, tmpDir }),
).rejects.toThrow(/native conversion failed.*cleanup denied/);
} finally {
rmSpy.mockRestore();
}
});
});