From 429750572ea151a80bf32b7c237e28e8286ecdba Mon Sep 17 00:00:00 2001 From: j Date: Sun, 7 Apr 2024 22:02:58 -0400 Subject: [PATCH] Inital Commit of Testing script for Gameplay --- tests/publish/testComment.ts | 109 +++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tests/publish/testComment.ts diff --git a/tests/publish/testComment.ts b/tests/publish/testComment.ts new file mode 100644 index 0000000..136f4cf --- /dev/null +++ b/tests/publish/testComment.ts @@ -0,0 +1,109 @@ +import { Comment } from "../../src/rdrama/models/Comment"; +import * as readline from 'readline'; +import RedisCommentService from "../../src/redis/services/CommentService" +import RedisSessionManager from '../../src/redis/session/SessionManager'; + + +const comment: Comment = { + "author_id": 9978, + "author_name": "J", + "body": "!!oregon 3", + "body_html": "

!!oregon 3

", + "created_utc": 1712512562, + "deleted_utc": 0, + "distinguished": false, + "downvotes": 0, + "edited_utc": 0, + "id": 6227144, + "is_banned": false, + "is_bot": false, + "is_nsfw": false, + "level": 1, + "permalink": "/comment/6227144#context", + "pinned": null, + "post_id": 0, + "replies": [], + "reports": {}, + "score": 1, + "upvotes": 1 +} + + + +async function main(): Promise { + console.log('Redis Start'); + const redisManager = RedisSessionManager.getInstance(); + continuouslyPrompt(redisManager); + setupProcessListeners(redisManager); +} + +// Create an interface for input and output +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +// Function to continuously prompt for commands, update, and publish comments +const continuouslyPrompt = async (redisManager: RedisSessionManager) => { + rl.setPrompt('Enter command (or type "exit" to quit): '); + rl.prompt(); + + rl.on('line', async (line) => { + if (line.trim().toLowerCase() === 'exit') { + console.log('Exiting...'); + rl.close(); + } else { + // Update the comment body and body_html with the provided command + comment.body = line.trim(); + comment.body_html = `

${line.trim()}

`; + + // Publish the updated comment to the Redis server + try { + const publishResult = await RedisCommentService.publishComment(comment); + console.log(`Comment published with result: ${publishResult}`); + } catch (error) { + console.error('Failed to publish comment:', error); + } + + rl.prompt(); + } + }).on('close', async () => { + console.log('Goodbye!'); + await redisManager.shutdown(); + process.exit(0); + }); +}; + + + +function setupProcessListeners(redisManager: RedisSessionManager) { + process.on('SIGINT', async () => { + console.log('SIGINT received. Shutting down gracefully...'); + await shutdownServices(redisManager); + }); + + process.on('SIGTERM', async () => { + console.log('SIGTERM received. Shutting down gracefully...'); + await shutdownServices(redisManager); + }); + + process.on('uncaughtException', async (error) => { + console.error('Uncaught Exception:', error); + await shutdownServices(redisManager); + process.exit(1); // Exit with error + }); + + process.on('unhandledRejection', async (reason, promise) => { + console.error('Unhandled Rejection at:', promise, 'reason:', reason); + await shutdownServices(redisManager); + process.exit(1); // Exit with error + }); +} + +async function shutdownServices(redisManager: RedisSessionManager) { + await redisManager.shutdown(); + console.log('Services shut down successfully.'); + process.exit(0); // Exit cleanly +} + +main(); \ No newline at end of file