File size: 3,439 Bytes
61d39e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*

 * Copyright (C) 2024-present Puter Technologies Inc.

 *

 * This file is part of Puter.

 *

 * Puter is free software: you can redistribute it and/or modify

 * it under the terms of the GNU Affero General Public License as published

 * by the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU Affero General Public License for more details.

 *

 * You should have received a copy of the GNU Affero General Public License

 * along with this program.  If not, see <https://www.gnu.org/licenses/>.

 */
const { expect } = require('chai');
const { BasicBase } = require('../src/bases/BasicBase');
const { AdvancedBase } = require('../src/AdvancedBase');
const { Invoker } = require('../src/libs/invoker');

class ClassA extends BasicBase {
    static STATIC_OBJ = {
        a: 1,
        b: 2,
    };
    static STATIC_ARR = ['a', 'b'];
}

class ClassB extends ClassA {
    static STATIC_OBJ = {
        c: 3,
        d: 4,
    };
    static STATIC_ARR = ['c', 'd'];
}

describe('testing', () => {
    it('does a thing', () => {
        const b = new ClassB();

        console.log(b._get_inheritance_chain());
        console.log([ClassA, ClassB]);
        expect(b._get_inheritance_chain()).deep.equal([ClassA, ClassB]);
        expect(b._get_merged_static_array('STATIC_ARR'))
            .deep.equal(['a', 'b', 'c', 'd']);
        expect(b._get_merged_static_object('STATIC_OBJ'))
            .deep.equal({ a: 1, b: 2, c: 3, d: 4 });
    });
});

class ClassWithModule extends AdvancedBase {
    static MODULES = {
        axios: 'axios',
    };
}

describe('AdvancedBase', () => {
    it('passes DI modules to instance', () => {
        const c1 = new ClassWithModule();
        expect(c1.modules.axios).to.equal('axios');

        const c2 = new ClassWithModule({
            modules: {
                axios: 'my-axios',
            },
        });
        expect(c2.modules.axios).to.equal('my-axios');
    });
});

describe('lib:invoker', () => {
    it('works', async () => {
        const invoker = Invoker.create({
            decorators: [
                {
                    name: 'uphill both ways',
                    on_call: (args) => {
                        return {
                            ...args,
                            n: args.n + 1,
                        };
                    },
                    on_return: (result) => {
                        return {
                            n: result.n + 1,
                        };
                    },
                },
                {
                    name: 'error number five',
                    on_error: a => {
                        a.cancel_error();
                        return { n: 5 };
                    },
                },
            ],
            async delegate (args) {
                const { n } = args;
                if ( n === 3 ) {
                    throw new Error('test error');
                }
                return { n: 'oops' };
            },
        });
        expect(await invoker.run({ n: 2 })).to.deep.equal({ n: 6 });
    });
});