import { test } from "node:test"; import assert from "node:assert/strict"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/ttsTuning.test.ts // // An Orpheus tuning and a reading (lane orpheus-tuning, S1): every field's // range, at its edges and past them, refused in a sentence naming the field. import { CHARACTER_TUNING_FIELDS, ORPHEUS_DEFAULT_TAKES, ORPHEUS_TUNING_DEFAULTS, ORPHEUS_TUNING_NAMED, ORPHEUS_TUNING_NAMES, TTS_READING_MAX, TTS_TUNING_FIELDS, TTS_TUNING_RANGES, normalizeTtsTuning, parseTtsTuning, quantizeTtsTuningValue, ttsReadingError, ttsReadingSeedBase, ttsTuningNameOf, ttsTuningValueError, } from "./ttsTuning.mjs"; test("the fields and their ranges", () => { assert.deepEqual([...TTS_TUNING_FIELDS], ["temperature", "top_p", "top_k", "min_p", "repeat_penalty", "takes"]); assert.deepEqual( Object.fromEntries(Object.entries(TTS_TUNING_RANGES).map(([k, r]) => [k, [r.min, r.max, r.integer]])), { temperature: [0.1, 1.5, false], top_p: [0.05, 1, false], top_k: [0, 200, true], min_p: [0, 0.5, false], repeat_penalty: [1, 2, false], takes: [1, 6, true], }, ); assert.equal(TTS_READING_MAX, 99_999); }); test("each field is taken at its edges and refused past them, naming the field and its range", () => { for (const f of TTS_TUNING_FIELDS) { const r = TTS_TUNING_RANGES[f]; assert.equal(ttsTuningValueError(f, r.min), null, `${f} ${r.min}`); assert.equal(ttsTuningValueError(f, r.max), null, `${f} ${r.max}`); const low = parseTtsTuning({ [f]: r.min - (r.integer ? 1 : 0.01) }); assert.ok(!low.ok, f); if (!low.ok) { assert.match(low.error, new RegExp(`^"tuning\\.${f}" must be a (whole )?number from ${r.min} to ${r.max} \\(got `), low.error); } const high = parseTtsTuning({ [f]: r.max + 1 }); assert.ok(!high.ok, f); } assert.equal( ttsTuningValueError("temperature", 3), '"tuning.temperature" must be a number from 0.1 to 1.5 (got 3)', ); assert.equal(ttsTuningValueError("takes", 2.5), '"tuning.takes" must be a whole number from 1 to 6 (got 2.5)'); assert.equal(ttsTuningValueError("top_k", "50"), '"tuning.top_k" must be a whole number from 0 to 200 (got "50")'); assert.equal(ttsTuningValueError("top_p", Number.NaN, "--top-p"), "--top-p must be a number from 0.05 to 1 (got NaN)"); }); test("a tuning: an object of the known fields; nulls left out; anything else refused", () => { assert.deepEqual(parseTtsTuning({ temperature: 0.7, top_k: 40, min_p: null }), { ok: true, value: { temperature: 0.7, top_k: 40 } }); assert.deepEqual(parseTtsTuning({}), { ok: true, value: {} }); for (const [raw, re] of [ [[], /"tuning" must be an object of temperature, top_p, top_k, min_p, repeat_penalty, takes/], ["warm", /"tuning" must be an object/], [{ seed: 4 }, /"tuning\.seed" is not a tuning field \(temperature, top_p/], [{ repeat_last_n: 64 }, /"tuning\.repeat_last_n" is not a tuning field/], ] as const) { const r = parseTtsTuning(raw); assert.ok(!r.ok, JSON.stringify(raw)); if (!r.ok) assert.match(r.error, re); } }); test("a reading: a whole number from 0 to 99999; its seed base steps by 9973", () => { for (const ok of [0, 1, 99_999]) assert.equal(ttsReadingError(ok), null, String(ok)); for (const bad of [-1, 1.5, 100_000, "2", null]) { assert.match(ttsReadingError(bad) ?? "", /^"reading" must be a whole number from 0 to 99999 \(0 = the first reading; got /, String(bad)); } assert.match(ttsReadingError(-1, "--reading") ?? "", /^--reading must be/); assert.equal(ttsReadingSeedBase(1234), 1234); assert.equal(ttsReadingSeedBase(1234, 0), 1234); assert.equal(ttsReadingSeedBase(1234, 1), 11_207); assert.equal(ttsReadingSeedBase(1234, 2), 21_180); // The highest one's seeds stay a signed 32-bit integer (llama-server's seed). assert.ok(ttsReadingSeedBase(1234, TTS_READING_MAX) + 101 * 6 + 5000 < 2 ** 31); }); test("decimals are quantised to 3 places before the range check, and -0 is 0", () => { assert.equal(quantizeTtsTuningValue("temperature", 0.550000001), 0.55); assert.equal(quantizeTtsTuningValue("temperature", 0.7000000000000001), 0.7); assert.equal(quantizeTtsTuningValue("top_p", 0.12345), 0.123); assert.ok(Object.is(quantizeTtsTuningValue("min_p", -0), 0)); assert.ok(Object.is(quantizeTtsTuningValue("top_k", -0), 0)); assert.equal(quantizeTtsTuningValue("repeat_penalty", 1.0004), 1); // Whole-number fields are not rounded (2.5 is still refused). assert.equal(quantizeTtsTuningValue("takes", 2.5), 2.5); assert.deepEqual(parseTtsTuning({ temperature: 0.7000000000000001, min_p: -0 }), { ok: true, value: { temperature: 0.7, min_p: 0 } }); assert.ok(Object.is((parseTtsTuning({ min_p: -0 }) as { value: { min_p: number } }).value.min_p, 0)); // Rounded first, then checked: 1.5004 is 1.5, in range; 1.5006 is 1.501, out. assert.deepEqual(parseTtsTuning({ temperature: 1.5004 }), { ok: true, value: { temperature: 1.5 } }); assert.ok(!parseTtsTuning({ temperature: 1.5006 }).ok); }); test("the normaliser drops what equals its default, quantised; takes only against the takes it is given", () => { assert.deepEqual(ORPHEUS_TUNING_DEFAULTS, { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2 }); assert.equal(ORPHEUS_DEFAULT_TAKES, 3); assert.deepEqual(normalizeTtsTuning({ temperature: 0.550000001, top_p: 0.85, min_p: -0, top_k: 50, repeat_penalty: 1.2 }), {}); assert.deepEqual(normalizeTtsTuning({ temperature: 0.7000000000000001, takes: 3 }), { temperature: 0.7, takes: 3 }); assert.deepEqual(normalizeTtsTuning({ temperature: 0.7, takes: 3 }, { takes: 3 }), { temperature: 0.7 }); assert.deepEqual(normalizeTtsTuning({ takes: 3 }, { takes: 5 }), { takes: 3 }); // In the registry's order, whatever order it was written in; unknown fields gone. assert.deepEqual(Object.keys(normalizeTtsTuning({ top_k: 40, temperature: 0.7, seed: 4 })), ["temperature", "top_k"]); assert.deepEqual(normalizeTtsTuning(null), {}); }); test("a character's tuning: its fields, and the three named starting points (lane character-tuning)", () => { assert.deepEqual([...CHARACTER_TUNING_FIELDS], ["temperature", "top_p", "repeat_penalty", "takes"]); assert.deepEqual([...ORPHEUS_TUNING_NAMES], ["steady", "default", "lively"]); // Default IS the sampler's defaults; every named value is in range and quantised. assert.deepEqual({ ...ORPHEUS_TUNING_NAMED.default }, { temperature: ORPHEUS_TUNING_DEFAULTS.temperature, top_p: ORPHEUS_TUNING_DEFAULTS.top_p, repeat_penalty: ORPHEUS_TUNING_DEFAULTS.repeat_penalty }); assert.deepEqual(normalizeTtsTuning(ORPHEUS_TUNING_NAMED.default), {}); for (const name of ORPHEUS_TUNING_NAMES) { const parsed = parseTtsTuning({ ...ORPHEUS_TUNING_NAMED[name] }); assert.deepEqual(parsed, { ok: true, value: { ...ORPHEUS_TUNING_NAMED[name] } }, name); } // Steady is cooler than Default, Lively warmer. assert.ok(ORPHEUS_TUNING_NAMED.steady.temperature < ORPHEUS_TUNING_NAMED.default.temperature); assert.ok(ORPHEUS_TUNING_NAMED.lively.temperature > ORPHEUS_TUNING_NAMED.default.temperature); }); test("ttsTuningNameOf: a tuning's sampler named, else custom; takes is not looked at", () => { assert.equal(ttsTuningNameOf(null), "default"); assert.equal(ttsTuningNameOf({}), "default"); assert.equal(ttsTuningNameOf({ takes: 5 }), "default"); assert.equal(ttsTuningNameOf({ ...ORPHEUS_TUNING_NAMED.lively }), "lively"); assert.equal(ttsTuningNameOf({ ...ORPHEUS_TUNING_NAMED.steady, takes: 2 }), "steady"); assert.equal(ttsTuningNameOf({ temperature: 0.4, top_p: 0.8 }), "steady", "a missing field is its default (repeat_penalty 1.2)"); assert.equal(ttsTuningNameOf({ ...ORPHEUS_TUNING_NAMED.lively, temperature: 0.8 }), "custom"); assert.equal(ttsTuningNameOf({ top_k: 40 }), "custom"); assert.equal(ttsTuningNameOf({ temperature: 0.7500000001, top_p: 0.92, repeat_penalty: 1.25 }), "lively", "quantised first"); });