palico-bot/test/storageManager.test.js
Shinuwa 98f4d29ac7 Encode jukebox audio with native ffmpeg, normalise loudness, read metadata
- Play every track through an ffmpeg process that normalises loudness
  (EBU R128) and outputs Ogg Opus, handed to Discord as is: no more Opus
  encoding in JavaScript with opusscript. Measured at over 40x real time,
  about 2% of a core while playing. Closing the stream kills ffmpeg.
- Encoding at upload instead was ruled out: over two minutes for an 85-minute
  mix, during which the upload would hang.
- Read duration, title and artist with ffprobe at upload; default the title
  to "Artist - Title" or the file name without extension.
- Fill the duration of existing library entries at startup.
- Expose the position of the current track in queue snapshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:10:49 +01:00

116 lines
3.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
test("the library persists portable entries and restores absolute runtime paths", async () => {
const originalDirectory = process.cwd();
const temporaryDirectory = fs.mkdtempSync(
path.join(os.tmpdir(), "palico-storage-")
);
try {
process.chdir(temporaryDirectory);
const storage = await import(
`../src/web/storage/storageManager.js?test=${Date.now()}`
);
storage.ensureStorageLayout();
const uploadedPath = path.join(storage.TEMP_DIR, "upload");
fs.writeFileSync(uploadedPath, "fake audio content");
const track = storage.registerUploadedFile(
{
path: uploadedPath,
originalname: "sample.mp3",
size: 18,
mimetype: "audio/mpeg",
},
{ permanent: true, uploader: "test", title: "Sample" }
);
const persisted = JSON.parse(
fs.readFileSync(
path.join(temporaryDirectory, "storage/runtime/library.json"),
"utf8"
)
);
assert.equal(persisted.length, 1);
assert.equal("path" in persisted[0], false);
const restored = storage.findPermanentTrack(track.libraryTrackId);
assert.equal(path.dirname(restored.path), storage.PERMANENT_DIR);
assert.equal(fs.existsSync(restored.path), true);
} finally {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
}
});
test("legacy library entries migrate without their absolute paths", async () => {
const originalDirectory = process.cwd();
const temporaryDirectory = fs.mkdtempSync(
path.join(os.tmpdir(), "palico-migration-")
);
try {
process.chdir(temporaryDirectory);
fs.mkdirSync("storage", { recursive: true });
fs.writeFileSync(
"storage/library.json",
JSON.stringify([
{
id: "legacy",
storedName: "legacy.mp3",
path: "/old/server/storage/permanent/legacy.mp3",
},
])
);
const storage = await import(
`../src/web/storage/storageManager.js?migration=${Date.now()}`
);
storage.ensureStorageLayout();
const migrated = JSON.parse(
fs.readFileSync("storage/runtime/library.json", "utf8")
);
assert.deepEqual(migrated, [{ id: "legacy", storedName: "legacy.mp3" }]);
} finally {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
}
});
test("library metadata backfill fills the missing durations", async () => {
const originalDirectory = process.cwd();
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-storage-"));
try {
process.chdir(temporaryDirectory);
const storage = await import(`../src/web/storage/storageManager.js?backfill=${Date.now()}`);
storage.ensureStorageLayout();
const uploadedPath = path.join(storage.TEMP_DIR, "upload");
fs.writeFileSync(uploadedPath, "fake audio content");
storage.registerUploadedFile(
{ path: uploadedPath, originalname: "old.mp3", size: 18, mimetype: "audio/mpeg" },
{ permanent: true, uploader: "test", title: "Old" }
);
// libraryMetadata.js importe storageManager.js sans paramètre : on passe par la même
// instance de module que lui, avec le répertoire de travail temporaire.
const { backfillLibraryMetadata } = await import("../src/web/storage/libraryMetadata.js");
const shared = await import("../src/web/storage/storageManager.js");
shared.ensureStorageLayout();
const updated = await backfillLibraryMetadata({
probe: async () => ({ duration: 42, artist: "Palico", title: null }),
});
assert.equal(updated.length, 1);
assert.equal(shared.getPermanentLibrary()[0].duration, 42);
assert.equal(shared.getPermanentLibrary()[0].artist, "Palico");
} finally {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
}
});