From 116925c2c72fe6ad3814201e55dba58a9a110776 Mon Sep 17 00:00:00 2001 From: whonion Date: Sat, 23 Dec 2023 16:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20update=20test.yml=20|=20add=20test.?= =?UTF-8?q?ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yml | 2 +- package.json | 3 ++- test.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d4ee5b3..bae7c80 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -59,4 +59,4 @@ jobs: run: npm i - name: Test Running - run: npm run start + run: npm run test diff --git a/package.json b/package.json index 7257ba9..637875a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "start": "js/main.js", "author": "https://github.com/whonion", "scripts": { - "start": "ts-node main.ts" + "start": "ts-node main.ts", + "test": "ts-node test.ts" }, "dependencies": { diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..64eed23 --- /dev/null +++ b/test.ts @@ -0,0 +1,36 @@ +import { spawn } from 'child_process'; + +const MAX_TEST_DURATION_MS = 120000; // Test duration in milliseconds (e.g., 120 seconds) +const SCRIPT_FILE = 'main.ts'; // Change this to match your main.ts filename + +// Function to run the main.ts script for a specific duration +function runMainScriptForDuration() { + return new Promise((resolve, reject) => { + const process = spawn('ts-node', [SCRIPT_FILE], { + stdio: 'inherit', // Inherit stdio from the parent process + }); + + setTimeout(() => { + process.kill(); // Terminate the script after the specified duration + resolve(); + }, MAX_TEST_DURATION_MS); + + process.on('close', (code) => { + if (code === 0) { + resolve(); + } else { + reject(`main.ts exited with code ${code}`); + } + }); + }); +} + +// Run the test +runMainScriptForDuration() + .then(() => { + console.log('Test completed successfully.'); + }) + .catch((error) => { + console.error('Test failed:', error); + process.exit(1); + });