SPB Git

Repository Public

next-web-audio.test.ts 7.6 KB 97 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/next-web-audio.test.ts9 * Description      : pure helpers of the audio block (src/lib/editor/audio.ts):10 *                    time formatting, transcript stamps, waveform peaks, the11 *                    active transcript line and the choice of a recording12 *                    container.13 * Module           : tests14 * Repository       : https://git.spboucher.ai/note-spboucher-ai15 * Project Website  : https://note.spboucher.ai16 * Created          : 2026-11-1917 * Last Modified    : 2026-11-1918 *19 * Author           : Simon-Pierre Boucher20 * Position         : Professor of Finance21 * Institution      : Université du Québec en Outaouais (UQO)22 * Organization     : SP Boucher23 * Website          : https://spboucher.ai24 * Git              : https://git.spboucher.ai25 *26 * Preferred Contact: contact@spboucher.ai27 * Development      : dev@spboucher.ai28 * Support          : support@spboucher.ai29 * Security/Admin   : admin@spboucher.ai30 *31 * Copyright (c) 202632 * Simon-Pierre Boucher33 * All rights reserved where applicable. This file is governed by the34 * applicable project license, copyright notices and relevant law.35 * Existing third-party rights and notices remain unaffected.36 *37 * License          : None specified (no license file in this repository)38 *39 * Do not embed credentials, private keys, access tokens,40 * certificates or confidential personal information.41 *42 * =============================================================================43 * END OF SP BOUCHER SOURCE HEADER44 * =============================================================================45 */4647import { test } from "node:test";48import assert from "node:assert/strict";49import { activeLineIndex, computePeaks, extensionForMime, formatTime, pickRecordingMime, transcriptLines, transcriptStamp } from "../../src/lib/editor/audio";5051test("formatTime: m:ss under an hour, h:mm:ss from an hour", () => {52  assert.equal(formatTime(0), "0:00"); assert.equal(formatTime(5), "0:05"); assert.equal(formatTime(59.9), "0:59"); assert.equal(formatTime(60), "1:00"); assert.equal(formatTime(75.4), "1:15");53  assert.equal(formatTime(600), "10:00"); assert.equal(formatTime(3599), "59:59"); assert.equal(formatTime(3600), "1:00:00"); assert.equal(formatTime(3661), "1:01:01"); assert.equal(formatTime(36000 + 62), "10:01:02");54  for (const bad of [-3, NaN, Infinity, -Infinity, undefined as any, null as any, "x" as any]) assert.equal(formatTime(bad), "0:00");55  assert.equal(formatTime("90" as any), "1:30");56});57test("transcriptStamp: [mm:ss], minutes never folded into hours", () => {58  assert.equal(transcriptStamp(0), "[00:00]"); assert.equal(transcriptStamp(7.9), "[00:07]"); assert.equal(transcriptStamp(65), "[01:05]"); assert.equal(transcriptStamp(600), "[10:00]");59  assert.equal(transcriptStamp(3723), "[62:03]"); assert.equal(transcriptStamp(6000), "[100:00]"); assert.equal(transcriptStamp(-1), "[00:00]"); assert.equal(transcriptStamp(NaN), "[00:00]");60});61test("computePeaks: one max-abs value per bucket, normalised to the loudest", () => {62  assert.deepEqual(computePeaks([0.1, -0.5, 0.2, 0.25, -1, 0.3, 0, 0], 4), [0.5, 0.25, 1, 0]);63  assert.deepEqual(computePeaks(new Float32Array([0.5, -0.25, 0.125, 0]), 2), [1, 0.25]);64  assert.deepEqual(computePeaks([0.2, 0.2, 0.2, 0.2], 2), [1, 1], "normalised: a quiet but even recording fills the height");65  assert.deepEqual(computePeaks([-2, 1], 2), [1, 0.5], "out-of-range samples still normalise to 1");66  const samples = new Float32Array(48000); for (let i = 0; i < samples.length; i++) samples[i] = Math.sin(i / 50) * (i / samples.length);67  const peaks = computePeaks(samples, 120); assert.equal(peaks.length, 120); assert.equal(Math.max(...peaks), 1); assert.ok(peaks.every((p) => p >= 0 && p <= 1)); assert.ok(peaks[119] > peaks[10], "the envelope follows the signal");68  assert.equal(computePeaks(samples, 7).length, 7); assert.equal(computePeaks(samples, 1).length, 1); assert.deepEqual(computePeaks(samples, 1), [1]);69});70test("computePeaks: empty, silent, short and degenerate inputs", () => {71  assert.deepEqual(computePeaks([], 4), [0, 0, 0, 0]); assert.deepEqual(computePeaks(new Float32Array(0), 3), [0, 0, 0]); assert.deepEqual(computePeaks([0, 0, 0, 0], 2), [0, 0], "silence does not divide by zero");72  assert.deepEqual(computePeaks([1, 2, 3], 0), []); assert.deepEqual(computePeaks([1, 2, 3], -2), []); assert.deepEqual(computePeaks([1, 2, 3], NaN), []); assert.equal(computePeaks([1, 2, 3], 2.9).length, 2);73  assert.deepEqual(computePeaks([0.5, 1], 4), [0.5, 0.5, 1, 1], "fewer samples than buckets: nearest sample");74  assert.deepEqual(computePeaks([NaN, 0.5, 1, NaN], 2), [0.5, 1]); assert.deepEqual(computePeaks(null as any, 2), [0, 0]);75  const input = Object.freeze([0.3, -0.6]); assert.deepEqual(computePeaks(input as any, 2), [0.5, 1]);76});77test("activeLineIndex: the line with the latest start ≤ t", () => {78  const lines = [{ t: 0, text: "a" }, { t: 4.5, text: "b" }, { t: 10, text: "c" }];79  assert.equal(activeLineIndex(lines, 0), 0); assert.equal(activeLineIndex(lines, 4.49), 0); assert.equal(activeLineIndex(lines, 4.5), 1); assert.equal(activeLineIndex(lines, 9.99), 1); assert.equal(activeLineIndex(lines, 10), 2); assert.equal(activeLineIndex(lines, 9999), 2);80  assert.equal(activeLineIndex([{ t: 3, text: "late start" }], 1), -1); assert.equal(activeLineIndex([], 5), -1); assert.equal(activeLineIndex(lines, NaN), -1); assert.equal(activeLineIndex(null as any, 1), -1);81  assert.equal(activeLineIndex([{ t: 10, text: "c" }, { t: 0, text: "a" }, { t: 4.5, text: "b" }], 5), 2, "unsorted transcripts still resolve");82  assert.equal(activeLineIndex([{ t: 2, text: "x" }, { t: 2, text: "y" }], 2), 0, "first of equal stamps");83  assert.equal(activeLineIndex([{ t: 0, text: "a" }, { t: "x" as any, text: "bad" }, null as any, { t: 3, text: "b" }], 4), 3);84  assert.deepEqual(transcriptLines([{ t: 0, text: "a" }, { t: "1", text: "b" }, null, { text: "c" }, { t: 2, text: "d", speaker: "S1" }]), [{ t: 0, text: "a" }, { t: 2, text: "d", speaker: "S1" }]); assert.deepEqual(transcriptLines(undefined), []);85});86test("pickRecordingMime: audio/mp4 first, then WebM/Opus, then WebM", () => {87  assert.deepEqual(pickRecordingMime(() => true), { mime: "audio/mp4", ext: "m4a" });88  assert.deepEqual(pickRecordingMime((m) => m.startsWith("audio/webm")), { mime: "audio/webm;codecs=opus", ext: "webm" });89  assert.deepEqual(pickRecordingMime((m) => m === "audio/webm"), { mime: "audio/webm", ext: "webm" });90  assert.deepEqual(pickRecordingMime((m) => m.startsWith("audio/ogg")), { mime: "audio/ogg;codecs=opus", ext: "ogg" });91  assert.deepEqual(pickRecordingMime(() => false), { mime: "", ext: "webm" }, "nothing declared: the browser default is used");92  assert.deepEqual(pickRecordingMime(() => { throw new Error("probe failed"); }), { mime: "", ext: "webm" });93  const asked: string[] = []; pickRecordingMime((m) => { asked.push(m); return false; }); assert.deepEqual(asked.slice(0, 3), ["audio/mp4", "audio/webm;codecs=opus", "audio/webm"]);94  const first = pickRecordingMime(() => true); first.ext = "changed"; assert.equal(pickRecordingMime(() => true).ext, "m4a", "the result is a copy");95  assert.equal(extensionForMime("audio/mp4;codecs=mp4a.40.2"), "m4a"); assert.equal(extensionForMime("audio/webm;codecs=opus"), "webm"); assert.equal(extensionForMime("audio/ogg"), "ogg"); assert.equal(extensionForMime("", "m4a"), "m4a"); assert.equal(extensionForMime("audio/flac"), "webm");96});97
Files main
Menu

Theme