-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
196 lines (164 loc) · 6.04 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* eslint-disable no-unused-vars */
'use strict';
process.env["NTBA_FIX_350"] = 1;////https://github.com/yagop/node-telegram-bot-api/issues/482
process.env["NTBA_FIX_319"] = 1;
import * as util from './utils.js';
import path from 'path';
import exitHook from 'exit-hook';
const privateConfig = util.GetJsonObj(path.join(util.GetAppDirPath(), 'etc/hidden_config.json'));
const TG_AwHappyBdToken = privateConfig[0].TG_Token;
const AWgroup = privateConfig[0].TG_privateGroup;
const GoogleSheetID = privateConfig[0].GoogleSheetID;
const GoogleServiceAcc = privateConfig[0].GoogleServiceAcc;
const GoogleKey = privateConfig[0].GoogleKey;
const TG_UnitTestBot = privateConfig[1].TG_Token; //
const TestGroup = privateConfig[1].TG_privateGroup;
await util.ResetPublicLog_HTML();
const _Tests = {
FullAppTest: 1, //Do not run With other test. run it alone
ScheduleTest: 2, ServerTest: 3, TelegramModuleTest: 4, GspreadSheetTest: 5,
AnimeHandlerTest: 6, SheetHandlerTest: 7, StartBotTest: 8
};
//runtest(_Tests.FullAppTest);
//runtest(_Tests.StartBotTest);
exitHook(() => { util.AddCounter(true) });
async function runtest(id) {
switch (id) {
case 1:
FullAppTest(TG_UnitTestBot, TestGroup);
break;
case 2:
ScheduleTest(() => { console.log("2--------2") });
break;
case 3:
ServerTest();
break;
case 4:
TelegramModuleTest(TG_UnitTestBot, TestGroup);
break;
case 5:
GspreadSheetTest();
break;
case 6: {
let anime_data = await AnimeHandlerTest();
console.log(anime_data);
}
break;
case 7:
SheetHandlerTest();
break;
case 8:
StartBotTest(TG_UnitTestBot);
break;
case 9:
break;
default:
break;
}
}
import { AwSheetHandler } from './Classes/AwGSheetHandler.js';
import { AwBdDbModel } from './Classes/AwBdDbModel.js';
function SheetHandlerTest() {
let dbModel = new AwBdDbModel(GoogleSheetID, 0, 946533461);
let SheetHandler = new AwSheetHandler(GoogleServiceAcc, GoogleKey, dbModel);
return SheetHandler;
}
import { HappyBot } from './Classes/bot.js';
async function StartBotTest(token) {
let h = SheetHandlerTest();
let bot = new HappyBot(token, h);
await bot.Init();
return bot;
}
import schedule from 'node-schedule';
async function FullAppTest(token, group) {
try {
ServerTest();
let counter = 0;
let bot = await StartBotTest(token);
const rule = new schedule.RecurrenceRule();
rule.hour = new schedule.Range(0, 23, 2); //every x hour
rule.minute = 0 // needed for every
let ran = false;
let runner = schedule.scheduleJob(rule, () => {
let date = new Date();
ran = false;
if (date.getHours() < 19 & date.getHours() > 1) { //in case server is utc, so i can send msg 5am in tehran
bot.SendHBD(group);
ran = true;
}
console.log(`${date.toUTCString()} - ${++counter}. SendHBD Called: '${ran}'.\n` + `\t next run at: ${rule.nextInvocationDate().toUTCString()}`);
util.LogToPublic(`${counter}. SendHBD Called: '${ran}'.\n` + `\t next run at: ${rule.nextInvocationDate().toUTCString()}`);
});
console.log(`0 - First Run at: ${runner.nextInvocation()}`); util.LogToPublic(`0 - First Run at: ${runner.nextInvocation()}`);
}
catch (err) { console.log(`Main Entry Err: ${util.ShortError(err, 200)}`); util.LogToPublic(`Main Entry Err: ${util.ShortError(err, 200)}`); }
}
/**
* @param {function} delegate give me somting to do while running
*/
function ScheduleTest(delegate) {
try {
let counter = 0
const rule = new schedule.RecurrenceRule();
rule.second = new schedule.Range(0, 60, 5); //every second
//rule.minute = 0 // needed for every
let ran = false;
schedule.scheduleJob(rule, () => {
let date = new Date();
ran = false;
if (date.getHours() < 19 & date.getHours() > 1) { //in case server is utc, so i can send msg 5am in tehran
delegate();
ran = true;
}
console.log(
`${++counter}. SendHBD Called: '${ran}' at (${date.toUTCString()}).\n` +
`\t Next run at: (${rule.nextInvocationDate().toUTCString()})\n`
);
});
console.log(`0 - First Run at: (${rule.nextInvocationDate().toUTCString()})`);
} catch (err) {
console.log(`Main Entry Err: ${util.ShortError(err, 200)}`);
}
}
import http from 'http';
import serveStatic from 'serve-static';
import finalhandler from 'finalhandler';
import { existsSync } from 'fs';
async function ServerTest() {
let htmlindex = `./public_log/index.html`;
if (!existsSync(htmlindex)) {
await util.ResetPublicLog_HTML();
}
var h = util.GetAppDirPath();
let publicPath = path.join(h, './public_log/');
var serve = serveStatic(publicPath, { index: ['index.html', 'index.htm'] })
const server = http.createServer((rq, rs) => {
serve(rq, rs, finalhandler(rq, rs));
});
server.listen(process.env.PORT || 5000);
}
import TelegramBot from 'node-telegram-bot-api';
function TelegramModuleTest(t, g) {
let b = new TelegramBot(t, { polling: true });
b.sendMessage(g, "test").catch(x => handleSentErro(x));
function handleSentErro(error) {
console.log(error.message)
}
}
import { GoogleSpreadsheet } from 'google-spreadsheet';
async function GspreadSheetTest() {
const doc = new GoogleSpreadsheet(GoogleSheetID);
await doc.useServiceAccountAuth({
client_email: GoogleServiceAcc,
private_key: GoogleKey
});
await doc.loadInfo();
let sheet = doc.sheetsById[946533461];
}
import { AnimeHandler } from './Classes/AnimeHandler.js';
async function AnimeHandlerTest() {
var ah = new AnimeHandler();
let anime = await ah.RandomAnimeAsync();
return anime;
}