mirror of
https://github.com/mudabbir-ahmad/Media-Aggregation-and-Sorting-System.git
synced 2026-10-07 19:40:20 +00:00
129 lines
4.4 KiB
JavaScript
129 lines
4.4 KiB
JavaScript
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3001';
|
|
|
|
async function main() {
|
|
console.log('\n--- NAS REST backend test ---');
|
|
console.log(`Backend: ${BACKEND_URL}\n`);
|
|
|
|
console.log('[1] Health check...');
|
|
try {
|
|
const res = await fetch(`${BACKEND_URL}/api/health`);
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
const data = await res.json();
|
|
console.log(` Status: ${data.status}, activeSessions: ${data.activeSessions}, mode: ${data.nasMode}\n`);
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}`);
|
|
console.log(' Start the backend first: cd ServerFiles && npm start\n');
|
|
process.exit(1);
|
|
}
|
|
|
|
const host = process.env.NAS_HOST || '127.0.0.1';
|
|
const port = parseInt(process.env.NAS_PORT || '22', 10);
|
|
const username = process.env.NAS_USER || 'demo';
|
|
const password = process.env.NAS_PASS || 'demo';
|
|
|
|
let connectionToken = null;
|
|
console.log(`[2] POST /api/nas/sessions (${host}, ${username})...`);
|
|
try {
|
|
const res = await fetch(`${BACKEND_URL}/api/nas/sessions`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ host, port, username, password }),
|
|
});
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
const data = await res.json();
|
|
if (!data.success) throw new Error(data.error || 'Session failed');
|
|
connectionToken = data.connectionToken;
|
|
console.log(' Session OK.\n');
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}\n`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const authHeaders = {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${connectionToken}`,
|
|
};
|
|
|
|
console.log('[3] GET /api/nas/media (list)...');
|
|
try {
|
|
const res = await fetch(`${BACKEND_URL}/api/nas/media?limit=10&include=metadata`, {
|
|
headers: { Authorization: `Bearer ${connectionToken}` },
|
|
});
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
const data = await res.json();
|
|
console.log(` total=${data.total}, returned=${(data.items || []).length}\n`);
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}\n`);
|
|
}
|
|
|
|
console.log('[4] POST /api/nas/media (tiny PNG create)...');
|
|
let createdId = null;
|
|
try {
|
|
const onePxPng =
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';
|
|
const res = await fetch(`${BACKEND_URL}/api/nas/media`, {
|
|
method: 'POST',
|
|
headers: authHeaders,
|
|
body: JSON.stringify({
|
|
filename: 'test-pixel.png',
|
|
mimeType: 'image/png',
|
|
base64: onePxPng,
|
|
metadata: { DateTimeOriginal: '2020:01:01 12:00:00', test: true },
|
|
}),
|
|
});
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
|
const data = await res.json();
|
|
createdId = data.id;
|
|
console.log(` Created id=${createdId}\n`);
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}\n`);
|
|
}
|
|
|
|
if (createdId) {
|
|
console.log('[5] GET /api/nas/media/:id ...');
|
|
try {
|
|
const res = await fetch(
|
|
`${BACKEND_URL}/api/nas/media/${encodeURIComponent(createdId)}?include=metadata`,
|
|
{ headers: { Authorization: `Bearer ${connectionToken}` } }
|
|
);
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
const data = await res.json();
|
|
console.log(` filename=${data.filename}, byteLength=${data.byteLength}\n`);
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}\n`);
|
|
}
|
|
|
|
console.log('[6] DELETE /api/nas/media/:id ...');
|
|
try {
|
|
const res = await fetch(`${BACKEND_URL}/api/nas/media/${encodeURIComponent(createdId)}`, {
|
|
method: 'DELETE',
|
|
headers: { Authorization: `Bearer ${connectionToken}` },
|
|
});
|
|
if (!res.ok && res.status !== 204) throw new Error(`HTTP ${res.status}`);
|
|
console.log(' Deleted.\n');
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}\n`);
|
|
}
|
|
}
|
|
|
|
console.log('[7] DELETE /api/nas/sessions/current ...');
|
|
try {
|
|
const res = await fetch(`${BACKEND_URL}/api/nas/sessions/current`, {
|
|
method: 'DELETE',
|
|
headers: { Authorization: `Bearer ${connectionToken}` },
|
|
});
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
const data = await res.json();
|
|
console.log(` ${data.message}\n`);
|
|
} catch (err) {
|
|
console.log(` Failed: ${err.message}\n`);
|
|
}
|
|
|
|
console.log('--- Tests complete ---\n');
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('Unhandled error:', err);
|
|
process.exit(1);
|
|
});
|