palico-bot/test/librarySections.test.js
Shinuwa bda95ca946
All checks were successful
Deploy Palico Bot / check (push) Successful in 10s
Deploy Palico Bot / deploy (push) Successful in 6s
Add section images, larger covers and track tags
Section images:
- A section gets an optional image, set from its new "Modifier" dialog
  (name and image), which replaces "Renommer". It goes through the same
  conversion as track covers and is deleted with its section.
- Track and section uploads share routes/coverUpload.js. The cover
  folder is created again if it went missing, instead of failing.

Larger images: 72 px for tracks (was 48), 96 px for section headers,
160 px in the player and the edit dialog (was 96); smaller on phones.

Tags:
- Library tracks take free tags, edited in the "Modifier" dialog, which
  suggests the tags already in use. Case is ignored ("Épique" and
  "épique" are one tag); at most 15 tags of 30 characters.
- A tag bar above the library filters it, and the "Tout ajouter"
  buttons (global and per section) then only queue its tracks
  (enqueue-all takes a tag). The search also matches tags.

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

91 lines
3.9 KiB
JavaScript

/**
* @file Vérifie les sections de la bibliothèque : création, renommage, déplacement, image, et
* suppression qui remet ses morceaux sans section.
*/
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";
// 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-sections-"));
process.chdir(temporaryDirectory);
const storage = await import("../src/web/storage/storageManager.js");
const sections = await import("../src/web/storage/librarySections.js");
const { serializeSection } = await import("../src/web/serializers.js");
storage.ensureStorageLayout();
test.after(() => {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
});
function addTrack(name, sectionId) {
const filePath = path.join(storage.TEMP_DIR, `upload-${name}`);
fs.writeFileSync(filePath, "audio");
return storage.registerUploadedFile(
{ path: filePath, originalname: name, size: 5, mimetype: "audio/mpeg" },
{ permanent: true, uploader: "test", sectionId }
).libraryTrackId;
}
test("sections are created, renamed and reordered", () => {
assert.deepEqual(sections.getSections(), []);
const chill = sections.createSection(" Chill\u0007 ");
const boss = sections.createSection("Combats de boss");
const menu = sections.createSection("Menus");
assert.equal(chill.name, "Chill");
sections.updateSection(menu.id, { name: "Écrans titres", position: 0 });
assert.deepEqual(
sections.getSections().map((section) => section.name),
["Écrans titres", "Chill", "Combats de boss"]
);
assert.equal(sections.findSection(boss.id).name, "Combats de boss");
assert.equal(sections.updateSection("unknown", { name: "x" }), null);
});
test("invalid names and positions are refused", () => {
const [first] = sections.getSections();
assert.throws(() => sections.createSection(" "), { status: 400 });
assert.throws(() => sections.updateSection(first.id, { position: 99 }), { status: 400 });
assert.throws(() => sections.updateSection(first.id, { position: "1" }), { status: 400 });
});
test("deleting a section keeps its tracks, without a section", () => {
const doomed = sections.createSection("À supprimer");
const kept = sections.createSection("Gardée");
const inDoomed = addTrack("a.mp3", doomed.id);
const inKept = addTrack("b.mp3", kept.id);
assert.equal(sections.deleteSection(doomed.id).id, doomed.id);
assert.equal(sections.findSection(doomed.id), undefined);
assert.equal(storage.findPermanentTrack(inDoomed).sectionId, null);
assert.equal(storage.findPermanentTrack(inKept).sectionId, kept.id);
assert.equal(sections.deleteSection(doomed.id), null);
});
test("a section image is replaced, removed, and deleted with its section", () => {
const section = sections.createSection("Avec image");
const coverFile = (name) => path.join(storage.COVERS_DIR, name);
fs.writeFileSync(coverFile("first.jpg"), "jpeg");
fs.writeFileSync(coverFile("second.jpg"), "jpeg");
sections.setSectionCover(section.id, "first.jpg");
assert.equal(serializeSection(sections.findSection(section.id)).coverUrl, "/covers/first.jpg");
assert.equal("cover" in serializeSection(sections.findSection(section.id)), false);
sections.setSectionCover(section.id, "second.jpg");
assert.equal(fs.existsSync(coverFile("first.jpg")), false);
// Section disparue : la nouvelle image est supprimée au lieu de rester orpheline.
fs.writeFileSync(coverFile("orphan.jpg"), "jpeg");
assert.equal(sections.setSectionCover("unknown", "orphan.jpg"), null);
assert.equal(fs.existsSync(coverFile("orphan.jpg")), false);
sections.deleteSection(section.id);
assert.equal(fs.existsSync(coverFile("second.jpg")), false);
});