File size: 2,633 Bytes
5bab120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { env } from "../config.js";
import { runTest } from "../misc/run-test.js";
import { loadJSON } from "../misc/load-from-fs.js";
import { Red, Bright } from "../misc/console-text.js";
import { randomizeCiphers } from "../misc/randomize-ciphers.js";

import { services } from "../processing/service-config.js";

const tests = loadJSON('./src/util/tests.json');

// services that are known to frequently fail due to external
// factors (e.g. rate limiting)
const finnicky = new Set(['bilibili', 'instagram', 'facebook', 'youtube'])

const action = process.argv[2];
switch (action) {
    case "get-services":
        const fromConfig = Object.keys(services);

        const missingTests = fromConfig.filter(
            service => !tests[service] || tests[service].length === 0
        );

        if (missingTests.length) {
            console.error('services have no tests:', missingTests);
            console.log('[]');
            process.exitCode = 1;
            break;
        }

        console.log(JSON.stringify(fromConfig));
        break;

    case "run-tests-for":
        const service = process.argv[3];
        let failed = false;

        if (!tests[service]) {
            console.error('no such service:', service);
        }

        env.streamLifespan = 10000;
        env.apiURL = 'http://x';
        randomizeCiphers();

        for (const test of tests[service]) {
            const { name, url, params, expected } = test;
            const canFail = test.canFail || finnicky.has(service);

            try {
                await runTest(url, params, expected);
                console.log(`${service}/${name}: ok`);

            } catch(e) {
                failed = !canFail;

                let failText = canFail ? `${Red('FAIL')} (ignored)` : Bright(Red('FAIL'));
                if (canFail && process.env.GITHUB_ACTION) {
                    console.log(`::warning title=${service}/${name.replace(/,/g, ';')}::failed and was ignored`);
                }

                console.error(`${service}/${name}: ${failText}`);
                const errorString = e.toString().split('\n');
                let c = '┃';
                errorString.forEach((line, index) => {
                    line = line.replace('!=', Red('!='));

                    if (index === errorString.length - 1) {
                        c = '┗';
                    }

                    console.error(`   ${c}`, line);
                });
            }
        }

        process.exitCode = Number(failed);
        break;
    default:
        console.error('invalid action:', action);
        process.exitCode = 1;
}