107 lines
3.7 KiB
JavaScript
107 lines
3.7 KiB
JavaScript
import db from "../src/inc/sql.mjs";
|
|
import http from "http";
|
|
import crypto from "crypto";
|
|
|
|
const HOST = "localhost";
|
|
const PORT = 3000;
|
|
import { readFile } from "fs/promises";
|
|
const cfg = JSON.parse(await readFile("../config.json", "utf8"));
|
|
const serverPort = cfg.websrv.port;
|
|
|
|
const runTest = async () => {
|
|
// 1. Setup Data
|
|
console.log("Setting up test data...");
|
|
const user = await db`SELECT id FROM "user" LIMIT 1`;
|
|
const item = await db`SELECT id FROM "items" LIMIT 1`;
|
|
|
|
if (!user.length || !item.length) {
|
|
console.error("No user or item found.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const userId = user[0].id;
|
|
const itemId = item[0].id;
|
|
|
|
// Create session
|
|
const sessionKey = "testsession_" + Date.now();
|
|
const sessionHash = crypto.createHash('md5').update(sessionKey).digest("hex");
|
|
|
|
await db`DELETE FROM user_sessions WHERE user_id = ${userId}`; // Clear old sessions for clean test
|
|
await db`INSERT INTO user_sessions (user_id, session, browser, created_at, last_used, last_action)
|
|
VALUES (${userId}, ${sessionHash}, 'test-bot', ${Math.floor(Date.now() / 1000)}, ${Math.floor(Date.now() / 1000)}, 'test')`;
|
|
|
|
console.log(`User: ${userId}, Item: ${itemId}, Session: ${sessionKey}`);
|
|
|
|
// Helper for requests
|
|
const request = (method, path, body = null, cookie = null) => {
|
|
return new Promise((resolve, reject) => {
|
|
const options = {
|
|
hostname: HOST,
|
|
port: serverPort,
|
|
path: path,
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Cookie': cookie ? `session=${cookie}` : ''
|
|
}
|
|
};
|
|
|
|
const req = http.request(options, (res) => {
|
|
let data = '';
|
|
res.on('data', chunk => data += chunk);
|
|
res.on('end', () => resolve({ statusCode: res.statusCode, body: data }));
|
|
});
|
|
|
|
req.on('error', reject);
|
|
if (body) req.write(JSON.stringify(body));
|
|
req.end();
|
|
});
|
|
};
|
|
|
|
// 2. Test GET (Empty)
|
|
console.log("Testing GET /api/comments/" + itemId);
|
|
let res = await request('GET', `/api/comments/${itemId}`);
|
|
console.log("GET Response:", res.body);
|
|
let json = JSON.parse(res.body);
|
|
if (!json.success) throw new Error("GET failed");
|
|
|
|
// 3. Test POST
|
|
console.log("Testing POST /api/comments");
|
|
res = await request('POST', '/api/comments', {
|
|
item_id: itemId,
|
|
content: "Hello World from Test Bot"
|
|
}, sessionKey);
|
|
console.log("POST Response:", res.body);
|
|
json = JSON.parse(res.body);
|
|
if (!json.success) throw new Error("POST failed");
|
|
const commentId = json.comment.id;
|
|
|
|
// 4. Test GET (With comment)
|
|
console.log("Testing GET /api/comments/" + itemId);
|
|
res = await request('GET', `/api/comments/${itemId}`);
|
|
json = JSON.parse(res.body);
|
|
if (json.comments.length === 0) throw new Error("Comment not found");
|
|
console.log("Found comments:", json.comments.length);
|
|
|
|
// 5. Test Subscribe
|
|
console.log("Testing POST /api/subscribe/" + itemId);
|
|
res = await request('POST', `/api/subscribe/${itemId}`, {}, sessionKey);
|
|
console.log("Subscribe Response:", res.body);
|
|
json = JSON.parse(res.body);
|
|
if (!json.success) throw new Error("Subscribe failed");
|
|
if (!json.subscribed) throw new Error("Expected subscribed=true");
|
|
|
|
console.log("Testing Unsubscribe...");
|
|
res = await request('POST', `/api/subscribe/${itemId}`, {}, sessionKey);
|
|
json = JSON.parse(res.body);
|
|
if (json.subscribed) throw new Error("Expected subscribed=false");
|
|
|
|
console.log("ALL TESTS PASSED");
|
|
process.exit(0);
|
|
};
|
|
|
|
runTest().catch(e => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|