From cffd70f2f9b24475de0b772f028361cca7c0d551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 4 Nov 2026 20:21:59 +0000 Subject: [PATCH] fix(cli): a track with no beats answers an empty result, not an error (#7173) * fix(cli): a track with no beats answers an empty result, not an error * fix(cli): keep beats run under the complexity gate --- docs/packages/cli.mdx | 4 ++-- packages/cli/src/commands/beats.test.ts | 23 ++++++++++++++++++++--- packages/cli/src/commands/beats.ts | 17 +++++++++++++---- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index 3fd98a30d8..ba1a0dcaa4 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -778,8 +778,8 @@ into the audio; `strength` (0–1) is relative loudness. Beats you add, move, or delete in Studio save back to the same file. A track without a steady beat (ambient, rubato, too short) gets no beat file: -the command fails and writes nothing rather than inventing a grid. Results -match Studio's to within a frame or two — Studio decodes in the browser, often +the command answers an empty result (`count: 0`, exit 0) and writes nothing rather +than inventing a grid. Results match Studio's to within a frame or two — Studio decodes in the browser, often at a different sample rate, which can shift a beat slightly. ### `audio` diff --git a/packages/cli/src/commands/beats.test.ts b/packages/cli/src/commands/beats.test.ts index 865de24511..1ad9504548 100644 --- a/packages/cli/src/commands/beats.test.ts +++ b/packages/cli/src/commands/beats.test.ts @@ -132,14 +132,31 @@ describe.skipIf(!canDecode)("hyperframes beats", () => { expect(out).toMatch(/No music track found/); }); - it("writes nothing for a track with no steady beat", async () => { + it("answers a track with no steady beat with an empty result and writes nothing", async () => { const dir = project("silent", null); const { exitCode, out } = await runBeats(dir); - expect(exitCode).toBe(1); - expect(out).toMatch(/No beats detected in assets\/song\.wav/); + expect(exitCode).toBe(0); + expect(JSON.parse(out)).toEqual({ + ok: true, + track: "assets/song.wav", + file: null, + count: 0, + bpm: null, + }); expect(existsSync(join(dir, "beats"))).toBe(false); }); + it("answers one ambient file with an empty beat list, for a person and for --json", async () => { + const dir = project("silent-file", null); + const song = join(dir, "assets", "song.wav"); + const json = await runBeats(song); + expect(json.exitCode).toBe(0); + expect(JSON.parse(json.out)).toMatchObject({ ok: true, file: song, count: 0, beatTimes: [] }); + const person = await runBeats(song, false); + expect(person.exitCode).toBe(0); + expect(person.out).toMatch(/No beats in /); + }); + it("refuses a music src that resolves outside the project", async () => { // An encoded `../` survives URL normalization and decodes to a parent path, so // without a containment check the command reads ../../secret.wav and writes diff --git a/packages/cli/src/commands/beats.ts b/packages/cli/src/commands/beats.ts index 459d8f5cea..925796dd57 100644 --- a/packages/cli/src/commands/beats.ts +++ b/packages/cli/src/commands/beats.ts @@ -96,9 +96,16 @@ function report(file: string, result: MusicBeatAnalysis, json: boolean): void { ); } +function answerNoBeats(rel: string, json: boolean): void { + if (json) { + console.log(JSON.stringify({ ok: true, track: rel, file: null, count: 0, bpm: null }, null, 2)); + } else { + console.log(c.dim(`No beats in ${rel}; nothing written.`)); + } +} + async function analyzeFile(audioPath: string, json: boolean): Promise { const result = await analyzeMusicFromSignal(await decode(audioPath)); - if (result.beatTimes.length === 0) fail(`No beats detected in ${audioPath}.`); if (json) { console.log( JSON.stringify( @@ -115,6 +122,10 @@ async function analyzeFile(audioPath: string, json: boolean): Promise { ); return; } + if (result.beatTimes.length === 0) { + console.log(c.dim(`No beats in ${audioPath}.`)); + return; + } console.log( c.success( `✓ ${result.beatTimes.length} beats in ${audioPath} (bpm ${result.bpm ?? "?"}, ${result.bpmConfidence})`, @@ -153,9 +164,7 @@ export default defineCommand({ const result = await analyzeMusicFromSignal(await decode(audioPath)); // The Studio ignores a 0-beat file (treats it as a stale seed), so don't write one. - if (result.beatTimes.length === 0) { - fail(`No beats detected in ${rel} — nothing written. (Track may be silent/ambient.)`); - } + if (result.beatTimes.length === 0) return answerNoBeats(rel, Boolean(args.json)); const outPath = join(project.dir, "beats", `${rel}.json`); if (!isSafePath(project.dir, outPath))