Harden bot, storage and web API
This commit is contained in:
parent
e974bc6660
commit
20624a5c9c
43 changed files with 1265 additions and 1012 deletions
83
test/storageManager.test.js
Normal file
83
test/storageManager.test.js
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
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 });
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue