92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
// Baixa fixtures oficiais do sherpa-onnx para `src-tauri/tests/fixtures/`.
|
|
//
|
|
// O áudio `parakeet-sample.wav` vem dentro do tarball oficial do modelo
|
|
// Parakeet TDT v2 int8. É o áudio usado nos docs do sherpa-onnx para validar
|
|
// a CLI `sherpa-onnx-offline`. Funciona com o v3 (mesma família de modelo).
|
|
//
|
|
// Uso:
|
|
// node scripts/setup-fixtures.mjs
|
|
|
|
import { execFileSync } from "node:child_process";
|
|
import {
|
|
existsSync,
|
|
mkdirSync,
|
|
createWriteStream,
|
|
statSync,
|
|
renameSync,
|
|
rmSync,
|
|
} from "node:fs";
|
|
import { dirname, join, resolve, sep as pathSep } from "node:path";
|
|
import { pipeline } from "node:stream/promises";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
const repoRoot = resolve(__dirname, "..");
|
|
const fixturesDir = join(repoRoot, "src-tauri", "tests", "fixtures");
|
|
mkdirSync(fixturesDir, { recursive: true });
|
|
|
|
const FIXTURES = [
|
|
{
|
|
name: "parakeet-sample.wav",
|
|
url: "https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-parakeet-tdt-0.6b-v2-int8.tar.bz2",
|
|
extractFrom: "sherpa-onnx-nemo-parakeet-tdt-0.6b-v2-int8/test_wavs/0.wav",
|
|
minSize: 10_000,
|
|
},
|
|
];
|
|
|
|
async function download(url, dest) {
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
throw new Error(
|
|
`download falhou: ${res.status} ${res.statusText} em ${url}`,
|
|
);
|
|
}
|
|
if (!res.body) {
|
|
throw new Error(`resposta vazia em ${url}`);
|
|
}
|
|
await pipeline(res.body, createWriteStream(dest));
|
|
return statSync(dest).size;
|
|
}
|
|
|
|
for (const fixture of FIXTURES) {
|
|
const dest = join(fixturesDir, fixture.name);
|
|
if (existsSync(dest) && statSync(dest).size > fixture.minSize) {
|
|
console.log(
|
|
`✓ fixture já presente em ${dest} (${statSync(dest).size} bytes)`,
|
|
);
|
|
continue;
|
|
}
|
|
console.log(`↓ baixando ${fixture.url}`);
|
|
const tmpArchive = join(fixturesDir, "fixture.tar.bz2");
|
|
const size = await download(fixture.url, tmpArchive);
|
|
if (size < 1_000_000) {
|
|
throw new Error(
|
|
`fixture ${fixture.name} tarball muito pequeno: ${size} bytes`,
|
|
);
|
|
}
|
|
// `tar` no Windows tem problema com paths absolutos em `-C`; usa `cwd`+relativo.
|
|
const relArchive = tmpArchive.startsWith(fixturesDir + pathSep)
|
|
? tmpArchive.slice(fixturesDir.length + 1)
|
|
: tmpArchive;
|
|
execFileSync("tar", ["-xjf", relArchive], {
|
|
cwd: fixturesDir,
|
|
stdio: "inherit",
|
|
});
|
|
const extractedPath = join(fixturesDir, fixture.extractFrom);
|
|
if (!existsSync(extractedPath)) {
|
|
throw new Error(
|
|
`caminho extraído ${extractedPath} não existe; verificar nome interno`,
|
|
);
|
|
}
|
|
renameSync(extractedPath, dest);
|
|
rmSync(tmpArchive, { force: true });
|
|
const finalSize = statSync(dest).size;
|
|
if (finalSize < fixture.minSize) {
|
|
throw new Error(
|
|
`fixture ${fixture.name} parece corrompida: ${finalSize} bytes (esperado > ${fixture.minSize})`,
|
|
);
|
|
}
|
|
console.log(`✓ fixture pronta em ${dest} (${finalSize} bytes)`);
|
|
}
|