palico-bot/test/libraryCovers.test.js
Shinuwa 11ee972636
All checks were successful
Deploy Palico Bot / check (push) Successful in 9s
Deploy Palico Bot / deploy (push) Successful in 7s
Add library sections and track covers
Sections:
- The library can be split into named, ordered sections, stored in
  storage/runtime/sections.json; each track keeps its sectionId.
- Sections are created, renamed, reordered and deleted from the page
  (/api/library/sections). Deleting one keeps its tracks, without a
  section.
- A section, or the tracks without one, can be queued in one go,
  shuffled or not (enqueue-all takes a sectionId).
- The upload form (file or link) picks the section of a kept track.
- Without any section, the library stays a plain list.

Covers:
- A kept track takes the cover embedded in its file (MP3, FLAC...),
  from the web page or /play garder:. Older tracks are looked at once
  on startup; a track without one keeps cover: null.
- "Renommer" becomes "Modifier": a dialog to change the title, the
  section and the cover (upload with preview, or removal).
- ffmpeg turns covers into JPEGs of at most 512 px, stored in
  storage/covers and deleted with their track. An uploaded image is
  only passed to ffmpeg after its signature is checked (PNG, JPEG,
  WebP, GIF), with that decoder forced, so ffmpeg never guesses the
  format of an unknown file.
- Covers are served under /covers without authentication, since an
  <img> cannot send the key; their random names are only given by the
  API and never change content (one-year cache).
- The current track and the queue show the cover of their library
  track.

Docs: README lists the new data to back up; storage/covers is ignored.

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

118 lines
4.8 KiB
JavaScript

/**
* @file Vérifie le stockage des pochettes sans ffmpeg : pochette intégrée reprise à l'ajout,
* remplacement, refus d'un fichier qui n'est pas une image, suppression avec le morceau, et
* adresse donnée par l'API.
*/
import test from "node:test";
import assert from "node:assert/strict";
import { EventEmitter } from "node:events";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// storageManager.js fixe ses dossiers au chargement, d'après le répertoire de travail.
const originalDirectory = process.cwd();
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-covers-"));
process.chdir(temporaryDirectory);
const storage = await import("../src/web/storage/storageManager.js");
const covers = await import("../src/web/storage/libraryCovers.js");
const { ingestAudioFile } = await import("../src/music/ingest.js");
const { backfillLibraryMetadata } = await import("../src/web/storage/libraryMetadata.js");
const { serializeTrack } = await import("../src/web/serializers.js");
storage.ensureStorageLayout();
test.after(() => {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
});
// Remplace ffmpeg : écrit un faux JPEG, ou échoue comme pour un fichier sans pochette.
const fakeCreate = async (_input, output) => fs.writeFileSync(output, "jpeg");
const noCover = async () => {
throw new Error("Output file does not contain any stream");
};
const probe = async () => ({ duration: 10, title: null, artist: null });
function tempFile(name, content = "audio") {
const filePath = path.join(storage.TEMP_DIR, `upload-${name}`);
fs.writeFileSync(filePath, Buffer.from(content, "latin1"));
return { path: filePath, originalname: name, size: content.length, mimetype: "audio/mpeg" };
}
function fakeQueue() {
const queue = new EventEmitter();
queue.enqueue = () => {};
return queue;
}
const coverPath = (name) => path.join(storage.COVERS_DIR, name);
test("a kept track takes the cover embedded in its file", async () => {
const track = await ingestAudioFile(
tempFile("with.mp3"),
{ permanent: true },
{ queue: fakeQueue(), probe, createCover: fakeCreate }
);
const entry = storage.findPermanentTrack(track.libraryTrackId);
assert.match(entry.cover, /^[0-9a-f-]{36}\.jpg$/);
assert.equal(fs.existsSync(coverPath(entry.cover)), true);
assert.equal(serializeTrack(entry).coverUrl, `/covers/${entry.cover}`);
assert.equal("cover" in serializeTrack(entry), false);
const without = await ingestAudioFile(
tempFile("without.mp3"),
{ permanent: true },
{ queue: fakeQueue(), probe, createCover: noCover }
);
const withoutEntry = storage.findPermanentTrack(without.libraryTrackId);
// Examiné une fois : null, pour ne pas être retenté à chaque démarrage.
assert.equal(withoutEntry.cover, null);
assert.equal("coverUrl" in serializeTrack(withoutEntry), false);
});
test("a new cover replaces the old file, and only images are accepted", async () => {
const id = (
await ingestAudioFile(tempFile("c.mp3"), { permanent: true }, {
queue: fakeQueue(), probe, createCover: fakeCreate,
})
).libraryTrackId;
const oldCover = storage.findPermanentTrack(id).cover;
const png = tempFile("cover.png", "\x89PNG\r\n\x1a\n0000");
const newCover = await covers.saveCoverFromImage(png.path, { create: fakeCreate });
covers.setTrackCover(id, newCover);
assert.equal(storage.findPermanentTrack(id).cover, newCover);
assert.equal(fs.existsSync(coverPath(oldCover)), false);
const text = tempFile("cover.txt", "#EXTM3U\nfile:///etc/passwd\n");
await assert.rejects(covers.saveCoverFromImage(text.path, { create: fakeCreate }), {
status: 400,
});
covers.setTrackCover(id, null);
assert.equal(storage.findPermanentTrack(id).cover, null);
assert.equal(fs.existsSync(coverPath(newCover)), false);
});
test("deleting a track deletes its cover", async () => {
const { libraryTrackId } = await ingestAudioFile(tempFile("d.mp3"), { permanent: true }, {
queue: fakeQueue(), probe, createCover: fakeCreate,
});
const { cover } = storage.findPermanentTrack(libraryTrackId);
storage.deletePermanentTrack(libraryTrackId);
assert.equal(fs.existsSync(coverPath(cover)), false);
});
test("the startup backfill looks once for the covers of older tracks", async () => {
const older = storage.registerUploadedFile(tempFile("old.mp3"), {
permanent: true,
duration: 10,
}).libraryTrackId;
assert.equal(storage.findPermanentTrack(older).cover, undefined);
const updated = await backfillLibraryMetadata({ probe, createCover: fakeCreate });
assert.deepEqual(updated.map((entry) => entry.id), [older]);
assert.ok(storage.findPermanentTrack(older).cover);
assert.deepEqual(await backfillLibraryMetadata({ probe, createCover: fakeCreate }), []);
});