Skip to content

Commit e33d709

Browse files
committed
Better tests + Add "Authenticated" test
1 parent 0f70936 commit e33d709

12 files changed

+110
-23
lines changed

.github/workflows/node.js.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ jobs:
2626
run: npm ci
2727
- name: Run tests
2828
run: npm test
29+
env:
30+
SESSION: ${{ secrets.TW_SESSION }}

test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@ function success() {
4747
}
4848

4949
console.info('§90§30§104 ==== ALL TESTS DONE ==== §0\n');
50+
51+
let errored = false;
52+
5053
Object.keys(TESTS).forEach((t) => {
5154
let color = '2';
5255
if (TESTS[t].warnings) color = '3';
53-
if (TESTS[t].errors) color = '1';
56+
if (TESTS[t].errors) {
57+
color = '1';
58+
errored = true;
59+
}
5460

5561
console.info(`§90§30§10${color} ${t} §0 §91E§0: ${TESTS[t].errors}§0 §93W§0: ${TESTS[t].warnings}`);
56-
// console.info(` - §91 Errors:§0 ${TESTS[t].errors}`);
57-
// console.info(` - §93 Warnings:§0 ${TESTS[t].warnings}`);
5862
});
5963

60-
process.exit(0);
64+
process.exit(errored ? 1 : 0);
6165
})();
6266

6367
setTimeout(() => {

tests/1. Search.js renamed to tests/01. Search.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = async (log, success, warn, err, cb) => {
1111

1212
if (markets.length < 10 || indicators.length < 10) {
1313
err('Not enough results');
14-
throw new Error('Not enough results');
1514
}
1615

1716
cb();

tests/2. QuoteSession.js renamed to tests/02. QuoteSession.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', ...error);
8-
throw new Error('Client error');
98
});
109

1110
const quoteSession = new client.Session.Quote({
@@ -38,7 +37,6 @@ module.exports = (log, success, warn, err, cb) => {
3837
keys.forEach((k) => {
3938
if (!rsKeys.includes(k)) {
4039
err(`Missing '${k}' key in`, rsKeys);
41-
throw new Error('Missing key');
4240
}
4341
});
4442

@@ -49,6 +47,5 @@ module.exports = (log, success, warn, err, cb) => {
4947

5048
BTC.onError((...error) => {
5149
err('BTCEUR ERROR:', error);
52-
throw new Error('Missing key');
5350
});
5451
};

tests/3. SimpleChart.js renamed to tests/03. SimpleChart.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', error);
8-
throw new Error('Client error');
98
});
109

1110
const chart = new client.Session.Chart();
@@ -16,7 +15,6 @@ module.exports = async (log, success, warn, err, cb) => {
1615

1716
chart.onError((...error) => {
1817
err('Chart error', error);
19-
throw new Error('Chart error');
2018
});
2119

2220
chart.onSymbolLoaded(() => { // When the symbol is successfully loaded

tests/4. Indicators.js renamed to tests/04. Indicators.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', error);
8-
throw new Error('Client error');
98
});
109

1110
const chart = new client.Session.Chart();
@@ -15,7 +14,6 @@ module.exports = async (log, success, warn, err, cb) => {
1514

1615
chart.onError((...error) => {
1716
err('Chart error', error);
18-
throw new Error('Chart error');
1917
});
2018

2119
TradingView.getIndicator('STD;Supertrend%Strategy').then((indicator) => {
@@ -31,7 +29,6 @@ module.exports = async (log, success, warn, err, cb) => {
3129

3230
SuperTrend.onError((...error) => {
3331
err('SuperTrend error', error[0]);
34-
throw new Error('SuperTrend error');
3532
});
3633

3734
let QTY = 10;
@@ -81,7 +78,6 @@ module.exports = async (log, success, warn, err, cb) => {
8178

8279
CipherB.onError((...error) => {
8380
err('MarketCipher B error:', error[0]);
84-
throw new Error('MarketCipher B error');
8581
});
8682

8783
CipherB.onUpdate(() => {

tests/5. BuiltInIndicator.js renamed to tests/05. BuiltInIndicator.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', error);
8-
throw new Error('Client error');
98
});
109

1110
const chart = new client.Session.Chart();
@@ -15,7 +14,6 @@ module.exports = async (log, success, warn, err, cb) => {
1514

1615
chart.onError((...error) => {
1716
err('Chart error', error);
18-
throw new Error('Chart error');
1917
});
2018

2119
const volumeProfile = new TradingView.BuiltInIndicator('VbPFixed@tv-basicstudies-139!');

tests/6. FakeReplayMode.js renamed to tests/06. FakeReplayMode.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', error);
8-
throw new Error('Client error');
98
});
109

1110
const chart = new client.Session.Chart();
@@ -17,7 +16,6 @@ module.exports = async (log, success, warn, err, cb) => {
1716

1817
chart.onError((...error) => {
1918
err('Chart error', error);
20-
throw new Error('Chart error');
2119
});
2220

2321
let interval = NaN;
@@ -43,7 +41,7 @@ module.exports = async (log, success, warn, err, cb) => {
4341
if (Number.isNaN(interval) && times.length >= 2) interval = intrval;
4442

4543
if (!Number.isNaN(interval) && interval !== intrval) {
46-
throw new Error(`Wrong interval: ${intrval} (should be ${interval})`);
44+
err(`Wrong interval: ${intrval} (should be ${interval})`);
4745
}
4846

4947
log('Next ->', times[0]);

tests/7. ReplayMode.js renamed to tests/07. ReplayMode.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', error);
8-
throw new Error('Client error');
98
});
109

1110
const chart = new client.Session.Chart();
@@ -17,7 +16,6 @@ module.exports = async (log, success, warn, err, cb) => {
1716

1817
chart.onError((...error) => {
1918
err('Chart error', error);
20-
throw new Error('Chart error');
2119
});
2220

2321
let finished = false;

tests/8. CustomChartTypes.js renamed to tests/08. CustomChartTypes.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ module.exports = async (log, success, warn, err, cb) => {
55

66
client.onError((...error) => {
77
err('Client error', error);
8-
throw new Error('Client error');
98
});
109

1110
const chart = new client.Session.Chart();
1211

1312
chart.onError((...error) => {
1413
err('Chart error', error);
15-
throw new Error('Chart error');
1614
});
1715

1816
/* (0s) Heikin Ashi chart */
File renamed without changes.

tests/10. Authenticated.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-disable no-restricted-syntax */
2+
/* eslint-disable no-await-in-loop */
3+
const TradingView = require('../main');
4+
5+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
6+
7+
module.exports = async (log, success, warn, err, cb) => {
8+
log('Getting user info');
9+
10+
const userInfos = await TradingView.getUser(process.env.SESSION);
11+
if (userInfos && userInfos.id) {
12+
success('User info:', {
13+
id: userInfos.id,
14+
username: userInfos.username,
15+
firstName: userInfos.firstName,
16+
lastName: userInfos.lastName,
17+
following: userInfos.following,
18+
followers: userInfos.followers,
19+
notifications: userInfos.notifications,
20+
joinDate: userInfos.joinDate,
21+
});
22+
} else err('Get user error. Result:', userInfos);
23+
24+
await wait(1000);
25+
26+
log('Getting user indicators');
27+
28+
const userIndicators = await TradingView.getPrivateIndicators(process.env.SESSION);
29+
if (userIndicators) {
30+
if (userIndicators.length === 0) warn('No private indicator found');
31+
else success('User indicators:', userIndicators.map((i) => i.name));
32+
} else err('Get indicators error. Result:', userIndicators);
33+
34+
await wait(1000);
35+
36+
log('Creating logged client');
37+
const client = new TradingView.Client({
38+
token: process.env.SESSION,
39+
});
40+
client.onError((...error) => {
41+
err('Client error', error);
42+
});
43+
44+
const chart = new client.Session.Chart();
45+
chart.onError((...error) => {
46+
err('Chart error', error);
47+
});
48+
49+
log('Setting market to BINANCE:BTCEUR...');
50+
chart.setMarket('BINANCE:BTCEUR', { timeframe: 'D' });
51+
52+
const checked = new Set();
53+
async function check(item) {
54+
await wait(1000);
55+
56+
checked.add(item);
57+
if (checked.size < userIndicators.length + 1) {
58+
log('Waiting for', userIndicators.length + 1 - checked.size, 'item(s)...');
59+
return;
60+
}
61+
62+
log('Closing client...');
63+
chart.delete();
64+
await client.end();
65+
success('Client closed');
66+
cb();
67+
}
68+
69+
chart.onUpdate(async () => {
70+
success('Market data:', {
71+
name: chart.infos.pro_name,
72+
description: chart.infos.short_description,
73+
exchange: chart.infos.exchange,
74+
price: chart.periods[0].close,
75+
});
76+
77+
await check(Symbol('PRICE'));
78+
});
79+
80+
await wait(1000);
81+
82+
log('Loading indicators...');
83+
84+
for (const indic of userIndicators) {
85+
const privateIndic = await indic.get();
86+
log(`[${indic.name}] Loading indicator...`);
87+
88+
const indicator = new chart.Study(privateIndic);
89+
90+
indicator.onReady(() => {
91+
success(`[${indic.name}] Indicator loaded !`);
92+
});
93+
94+
indicator.onUpdate(async () => {
95+
success(`[${indic.name}] Last plot:`, indicator.periods[0]);
96+
await check(indic.id);
97+
});
98+
}
99+
};

0 commit comments

Comments
 (0)