|
1 | 1 | /**
|
2 |
| - * @file Unit test for the AbstractSubsys class. |
| 2 | + * @file Unit test for the AbstractSubsys and AbstractSubsysConfigurable classes. |
3 | 3 | * @author Felix Kopp <sandtler@sandtler.club>
|
4 | 4 | *
|
5 | 5 | * @license
|
|
22 | 22 | import { expect } from 'chai';
|
23 | 23 | import { describe } from 'mocha';
|
24 | 24 |
|
25 |
| -import { Videu } from '../../src/core/videu'; |
26 |
| -import { InvalidConfigError } from '../../src/error/invalid-config-error'; |
| 25 | +import { IllegalAccessError } from '../../src/error/illegal-access-error'; |
27 | 26 |
|
28 |
| -import { StubExtAbstractSubsys, StubExtAbstractSubsysFromEnv } from '../dummy/core/subsys'; |
| 27 | +import { IStubSubsysConfig, StubSubsys, StubSubsysConfigurable } from '../dummy/core/subsys'; |
| 28 | + |
| 29 | +before(() => { |
| 30 | + if (typeof global.videu !== 'object') { |
| 31 | + (global.videu as any) = { logLevel: 0 }; |
| 32 | + } else { |
| 33 | + global.videu.logLevel = 0; |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +/** Test result data returned by a {@link FStubSubsysTester} callback. */ |
| 38 | +interface IStubSubsysTestResult<InitParams extends any[] = []> { |
| 39 | + id: string; |
| 40 | + initParams: InitParams | null; |
| 41 | + isInitialized: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +/** Test callback function for testing the {@link AbstractSubsys} class. */ |
| 45 | +type FStubSubsysTester<InitParams extends any[] = []> = |
| 46 | + () => Promise<IStubSubsysTestResult<InitParams>>; |
29 | 47 |
|
30 | 48 | describe('core/abstract-subsys:AbstractSubsys', () => {
|
31 |
| - let dummy: StubExtAbstractSubsys; |
32 |
| - |
33 |
| - it('should instantiate normally', () => { |
34 |
| - const id = 'normal'; |
35 |
| - dummy = new StubExtAbstractSubsys(id, []); |
36 |
| - expect(dummy.config).to.deep.eq(StubExtAbstractSubsys.CONFIG); |
37 |
| - expect(dummy.isInitialized).to.eq(false); |
38 |
| - expect(dummy.id).to.eq(id); |
39 |
| - expect(dummy.getApp()).to.eq(null); |
| 49 | + it('should initialize normally without init params', () => { |
| 50 | + const fn: FStubSubsysTester<[]> = async () => { |
| 51 | + const dummy = new StubSubsys<[]>('stub', false, false); |
| 52 | + await dummy.init(); |
| 53 | + return { |
| 54 | + id: dummy.id, |
| 55 | + initParams: dummy.initParams, |
| 56 | + isInitialized: dummy.isInitialized, |
| 57 | + }; |
| 58 | + }; |
| 59 | + |
| 60 | + return expect(fn()).to.eventually.deep.equal({ |
| 61 | + id: 'stub', |
| 62 | + initParams: [], |
| 63 | + isInitialized: true, |
| 64 | + }, 'Subsys state invalid'); |
40 | 65 | });
|
41 | 66 |
|
42 |
| - it('should instantiate normally with config from env', () => { |
43 |
| - const id = 'config-from-env'; |
44 |
| - dummy = new StubExtAbstractSubsysFromEnv(id, [], false, false, false, true); |
45 |
| - expect(dummy.config).to.deep.eq(StubExtAbstractSubsys.CONFIG); |
46 |
| - expect(dummy.isInitialized).to.eq(false); |
47 |
| - expect(dummy.id).to.eq(id); |
48 |
| - expect(dummy.getApp()).to.eq(null); |
| 67 | + it('should initialize normally with one init parameter', () => { |
| 68 | + const fn: FStubSubsysTester<[number]> = async () => { |
| 69 | + const dummy = new StubSubsys<[number]>('stub', false, false); |
| 70 | + await dummy.init(420); |
| 71 | + return { |
| 72 | + id: dummy.id, |
| 73 | + initParams: dummy.initParams, |
| 74 | + isInitialized: dummy.isInitialized, |
| 75 | + }; |
| 76 | + }; |
| 77 | + |
| 78 | + return expect(fn()).to.eventually.deep.equal({ |
| 79 | + id: 'stub', |
| 80 | + initParams: [ 420 ], |
| 81 | + isInitialized: true, |
| 82 | + }, 'Subsys state invalid'); |
49 | 83 | });
|
50 | 84 |
|
51 |
| - let videu: Videu; |
| 85 | + it('should initialize normally with two init parameters', () => { |
| 86 | + const fn: FStubSubsysTester<[string, number]> = async () => { |
| 87 | + const dummy = new StubSubsys<[string, number]>('stub', false, false); |
| 88 | + await dummy.init('param', 420); |
| 89 | + return { |
| 90 | + id: dummy.id, |
| 91 | + initParams: dummy.initParams, |
| 92 | + isInitialized: dummy.isInitialized, |
| 93 | + }; |
| 94 | + }; |
52 | 95 |
|
53 |
| - it('should initialize normally', () => { |
54 |
| - videu = new Videu([dummy]); |
55 |
| - const fn = async () => await videu.init(); |
56 |
| - expect(videu.init()).to.eventually.become(undefined); |
57 |
| - expect(dummy.isInitialized).to.eq(true); |
| 96 | + return expect(fn()).to.eventually.deep.equal({ |
| 97 | + id: 'stub', |
| 98 | + initParams: [ 'param', 420 ], |
| 99 | + isInitialized: true, |
| 100 | + }, 'Subsys state invalid'); |
58 | 101 | });
|
59 | 102 |
|
60 | 103 | it('should de-initialize normally', () => {
|
61 |
| - expect(videu.exit()).to.eq(undefined); |
62 |
| - expect(dummy.isInitialized).to.eq(false); |
| 104 | + const fn: FStubSubsysTester<[]> = async () => { |
| 105 | + const dummy = new StubSubsys<[]>('stub', false, false); |
| 106 | + await dummy.init(); |
| 107 | + dummy.exit(); |
| 108 | + return { |
| 109 | + id: dummy.id, |
| 110 | + initParams: dummy.initParams, |
| 111 | + isInitialized: dummy.isInitialized, |
| 112 | + }; |
| 113 | + }; |
| 114 | + |
| 115 | + return expect(fn()).to.eventually.deep.eq({ |
| 116 | + id: 'stub', |
| 117 | + initParams: [], |
| 118 | + isInitialized: false, |
| 119 | + }, 'Subsys state invalid'); |
63 | 120 | });
|
64 | 121 |
|
65 |
| - it('should throw an error with invalid ids', () => { |
66 |
| - const fn = () => new StubExtAbstractSubsys('INVALID_ID', []); |
| 122 | + it('should reject invalid subsystem id', () => { |
| 123 | + const fn = () => new StubSubsys<[]>('INVALID NAME', false, false); |
67 | 124 | return expect(fn).to.throw(Error);
|
68 | 125 | });
|
| 126 | +}); |
| 127 | + |
| 128 | +/** |
| 129 | + * Return value of a {@link FStubSubsysConfigurableTester} callback function |
| 130 | + * used in the individual test cases for {@link AbstractSubsysConfigurable}. |
| 131 | + */ |
| 132 | +interface IStubSubsysConfigurableTestResult<InitParams extends any[] = []> |
| 133 | +extends IStubSubsysTestResult<InitParams> { |
| 134 | + config: IStubSubsysConfig; |
| 135 | +} |
| 136 | + |
| 137 | +/** Test callback for testing the {@link AbstractSubsysConfigurable} class */ |
| 138 | +type FStubSubsysConfigurableTester<InitParams extends any[] = []> = |
| 139 | + () => Promise<IStubSubsysConfigurableTestResult<InitParams>>; |
| 140 | + |
| 141 | +describe('core/abstract-subsys:AbstractSubsysConfigurable', () => { |
| 142 | + it('should initialize with config from constructor', () => { |
| 143 | + const fn: FStubSubsysConfigurableTester<[]> = async () => { |
| 144 | + const dummy = new StubSubsysConfigurable<[]>('stub'); |
| 145 | + await dummy.init(); |
| 146 | + return { |
| 147 | + id: dummy.id, |
| 148 | + initParams: dummy.initParams, |
| 149 | + config: dummy.config, |
| 150 | + isInitialized: dummy.isInitialized, |
| 151 | + }; |
| 152 | + }; |
| 153 | + |
| 154 | + return expect(fn()).to.eventually.deep.equal({ |
| 155 | + id: 'stub', |
| 156 | + initParams: [], |
| 157 | + config: StubSubsysConfigurable.CONFIG, |
| 158 | + isInitialized: true, |
| 159 | + }, 'Subsys state invalid'); |
| 160 | + }); |
69 | 161 |
|
70 |
| - it('should throw an error with invalid config object', () => { |
71 |
| - const fn = () => new StubExtAbstractSubsys('a', [], false, false, true); |
72 |
| - return expect(fn).to.throw(InvalidConfigError); |
| 162 | + it('should initialize with config from env', () => { |
| 163 | + const fn: FStubSubsysConfigurableTester<[]> = async () => { |
| 164 | + const dummy = new StubSubsysConfigurable<[]>('stub', false, false, false, true); |
| 165 | + await dummy.init(); |
| 166 | + return { |
| 167 | + id: dummy.id, |
| 168 | + initParams: dummy.initParams, |
| 169 | + config: dummy.config, |
| 170 | + isInitialized: dummy.isInitialized, |
| 171 | + }; |
| 172 | + }; |
| 173 | + |
| 174 | + return expect(fn()).to.eventually.deep.equal({ |
| 175 | + id: 'stub', |
| 176 | + initParams: [], |
| 177 | + config: StubSubsysConfigurable.CONFIG, |
| 178 | + isInitialized: true, |
| 179 | + }, 'Subsys state invalid'); |
73 | 180 | });
|
74 | 181 |
|
75 |
| - it('should throw an error with no config specified', () => { |
76 |
| - const fn = () => new StubExtAbstractSubsys('a', [], false, false, false, true); |
77 |
| - return expect(fn).to.throw(InvalidConfigError); |
| 182 | + it('should throw an error with no config', () => { |
| 183 | + const fn = async () => { |
| 184 | + const dummy = new StubSubsysConfigurable<[]>('stub', false, false, false, false, true); |
| 185 | + await dummy.init(); |
| 186 | + }; |
| 187 | + |
| 188 | + return expect(fn()).to.be.rejectedWith(Error); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should reject accessing the config before init', () => { |
| 192 | + const fn = () => { |
| 193 | + const dummy = new StubSubsysConfigurable<[]>('stub'); |
| 194 | + return dummy.config; |
| 195 | + }; |
| 196 | + |
| 197 | + return expect(fn).to.throw(IllegalAccessError); |
78 | 198 | });
|
79 | 199 | });
|
0 commit comments