diff --git a/package.json b/package.json index 9dec01c..3138526 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "scripts": { "lint": "eslint \"src/**\"", + "test": "ts-node --log-error --files --project ./tests/tsconfig.json ./tests/lib.ts", "prebuild": "rimraf dist && node ./node_modules/ts-node/dist/bin.js ./build/copyfiles.ts", "build": "node ./node_modules/typescript/bin/tsc ", "prepublishOnly": "npm run build", diff --git a/tests/lib.ts b/tests/lib.ts new file mode 100644 index 0000000..afe5979 --- /dev/null +++ b/tests/lib.ts @@ -0,0 +1,42 @@ +import 'dotenv/config'; +import XDCC from '../src' +import { env } from 'process' + +const xdccJS = new XDCC({ + host: env.SERVER, +}) + + +function msToReadableTime(ms: number) { + const seconds = Math.floor(ms / 1000); + const minutes = Math.floor(seconds / 60); + const hours = Math.floor(minutes / 60); + const days = Math.floor(hours / 24); + + const readableTime = { + days, + hours: hours % 24, + minutes: minutes % 60, + seconds: seconds % 60, + }; + + // return a human readable string + const readableString = Object.entries(readableTime) + .map(([unit, value]) => `${value} ${unit}${value > 1 ? 's' : ''}`) + .join(', '); + + return readableString; +} + + +xdccJS.on('ready', ()=> { + xdccJS.download(env.BOT, env.PACK) + let lastTime = Date.now(); + xdccJS.on('downloading', (fileinfo, received, percentage, eta) => { + if(lastTime + 500 < Date.now()) { + lastTime = Date.now() + console.log(`Downloading ${fileinfo.file} - ${received}/${fileinfo.length} - (${percentage}%) @ETA (${msToReadableTime(eta)})`) + } + }) + xdccJS.on('done', () => console.log('done')); +}) \ No newline at end of file diff --git a/tests/test-env.d.ts b/tests/test-env.d.ts new file mode 100644 index 0000000..313495c --- /dev/null +++ b/tests/test-env.d.ts @@ -0,0 +1,8 @@ +declare namespace NodeJS { + export interface ProcessEnv { + SERVER: string; + BOT: string; + PACK: string; + PORT: string, + } +} diff --git a/tests/tsconfig.json b/tests/tsconfig.json new file mode 100644 index 0000000..a76642c --- /dev/null +++ b/tests/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": ["node", "./test-env"] + }, + "include": [ + "**/*.ts" + ], + "ts-node": { + "files": true + }, + "files": [ + "./lib.ts", "./test-env.d.ts" + ] +}