palico-bot/test/ingest.test.js
Shinuwa 39d2b6acfb Add tracks from a direct link and from Discord with /play, /skip, /queue
- Download direct audio links server side (web page and /play), refusing
  internal and special addresses at connection time and after every
  redirect, with size and time limits.
- /play takes an attachment, a direct link or a library track
  (autocomplete), and can keep the file in the library; /skip and /queue
  control and show the shared queue.
- Share one ingest path between uploads, links and Discord attachments.
- Answer 413 instead of 500 when the library is full.

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

67 lines
2.4 KiB
JavaScript

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-ingest-"));
process.chdir(temporaryDirectory);
const storage = await import("../src/web/storage/storageManager.js");
const { defaultTitle, ingestAudioFile } = await import("../src/music/ingest.js");
storage.ensureStorageLayout();
test.after(() => {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
});
function fakeQueue() {
const queue = new EventEmitter();
queue.enqueued = [];
queue.enqueue = (track) => queue.enqueued.push(track);
return queue;
}
function tempFile(name) {
const filePath = path.join(storage.TEMP_DIR, `upload-${name}`);
fs.writeFileSync(filePath, "audio");
return { path: filePath, originalname: name, size: 5, mimetype: "audio/mpeg" };
}
test("default titles come from the tags, else from the file name", () => {
assert.equal(defaultTitle({ title: "Song", artist: "Band" }, "x.mp3"), "Band - Song");
assert.equal(defaultTitle({ title: "Song", artist: null }, "x.mp3"), "Song");
assert.equal(defaultTitle({ title: null, artist: null }, "Mon morceau.flac"), "Mon morceau");
});
test("an ingested file is queued with its metadata", async () => {
const queue = fakeQueue();
let libraryUpdates = 0;
queue.on("libraryUpdate", () => libraryUpdates++);
const track = await ingestAudioFile(
tempFile("a.mp3"),
{ permanent: true, uploader: "Shinuwa\u0007" },
{ queue, probe: async () => ({ duration: 61, title: "Titre", artist: "Artiste" }) }
);
assert.equal(track.title, "Artiste - Titre");
assert.equal(track.duration, 61);
assert.equal(track.uploader, "Shinuwa");
assert.deepEqual(queue.enqueued, [track]);
assert.equal(libraryUpdates, 1);
assert.equal(storage.getPermanentLibrary()[0].duration, 61);
});
test("a file that is not audio is deleted", async () => {
const file = tempFile("b.mp3");
const invalid = Object.assign(new Error("pas de l'audio"), { status: 400 });
await assert.rejects(
ingestAudioFile(file, {}, { queue: fakeQueue(), probe: async () => { throw invalid; } }),
invalid
);
assert.equal(fs.existsSync(file.path), false);
});