All files / sportident-testbench-shell/src/commands test.ts

4.7% Statements 4/85
0% Branches 0/14
0% Functions 0/23
4.87% Lines 4/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 2271x           1x   1x                                                                                                                                                                                                                                                                                                                                                                                                             1x                                      
import si from 'sportident/lib';
import {SiStationMode} from 'sportident/lib/SiStation/ISiStation';
import {ISiCard} from 'sportident/lib/SiCard/ISiCard';
import {SiMainStationSiCardInsertedEvent, SiMainStationSiCardRemovedEvent} from 'sportident/lib/SiStation/ISiMainStation';
import * as utils from 'sportident/lib/utils';
import {ShellCommandContext} from '../Shell';
import {BaseCommand, ArgType} from './BaseCommand';
 
const tests: {[name: string]: (context: ShellCommandContext) => Promise<void>} = {
    'card': (context: ShellCommandContext) => {
        const device = context.env.device;
        Iif (!device) {
            return Promise.reject(new Error('No device.'));
        }
        const mainStation = si.SiMainStation.fromSiDevice(device);
        let fixedSiNumber: number|undefined;
        const samples: {[key: string]: unknown} = {};
 
        const resetCardCallbacks = () => {
            mainStation.removeAllEventListeners();
        };
        let currentReject: (() => void)|undefined = undefined;
        context.waitChar().then((char: number) => {
            Iif (char === 27 || char === 3) { // Escape || Ctrl-C
                resetCardCallbacks();
                Iif (currentReject !== undefined) {
                    currentReject();
                }
            }
        });
 
        let cardState = '';
        const simulateStation = (
            mode: keyof typeof SiStationMode,
            code: number,
            actionName: string,
        ) => () => mainStation.atomically(() => {
            mainStation.setInfo('code', code);
            mainStation.setInfo('mode', mode);
            mainStation.setInfo('autoSend', true);
        })
            .then(() => {
                context.putString(`Insert card to ${actionName}...\n`);
                return new Promise((resolve, reject) => {
                    currentReject = reject;
                    resetCardCallbacks();
                    mainStation.addEventListener('siCardObserved', (cardEvent) => {
                        const card = cardEvent.siCard;
                        Iif (fixedSiNumber === undefined) {
                            fixedSiNumber = card.cardNumber;
                        }
                        Iif (fixedSiNumber !== card.cardNumber) {
                            context.putString(`Other ${card.constructor.name}: ${card.cardNumber} (not ${fixedSiNumber})\n`);
                            return;
                        }
                        resetCardCallbacks();
                        context.putString(`${actionName} ${card.constructor.name} succeeded: ${card.cardNumber}\n`);
                        if (mode === 'Clear') {
                            cardState = '';
                        } else {
                            cardState += `${cardState === '' ? '' : '-'}${actionName}`;
                        }
                        currentReject = undefined;
                        setTimeout(resolve, 1);
                    });
                });
            });
        const readoutCard = () => () => mainStation.atomically(() => {
            mainStation.setInfo('autoSend', false);
            mainStation.setInfo('mode', 'Readout');
            mainStation.setInfo('code', 10);
        })
            .then(() => {
                context.putString('Insert card to read...\n');
                return new Promise((resolve, reject) => {
                    currentReject = reject;
                    resetCardCallbacks();
                    const handleCardRemoved: utils.EventCallback<SiMainStationSiCardRemovedEvent> =
                        (removeEvent) => {
                            const removedCard = removeEvent.siCard;
                            Iif (fixedSiNumber === removedCard.cardNumber) {
                                resetCardCallbacks();
                                currentReject = undefined;
                                setTimeout(resolve, 1);
                            }
                        };
                    const handleCardRead = (card: ISiCard) => {
                        context.putString(`${card.constructor.name} ${card.cardNumber} read.\n`);
                        samples[cardState] = card.toDict();
                        context.putString(`${cardState}\n`);
                        card.toString().split('\n').forEach((line: string) => {
                            context.putString(`${line}\n`);
                        });
                        card.confirm();
                        context.putString('Remove card...\n');
                        mainStation.addEventListener('siCardRemoved', handleCardRemoved);
                    };
                    const handleCardInserted: utils.EventCallback<SiMainStationSiCardInsertedEvent> = (cardEvent) => {
                        const card = cardEvent.siCard;
                        Iif (fixedSiNumber === null) {
                            fixedSiNumber = card.cardNumber;
                        }
                        Iif (fixedSiNumber !== card.cardNumber) {
                            context.putString(`Other ${card.constructor.name}: ${card.cardNumber} (not ${fixedSiNumber})\n`);
                            return;
                        }
                        context.putString(`Reading ${card.constructor.name} ${card.cardNumber}...\n`);
                        card.read()
                            .then(handleCardRead);
                    };
                    mainStation.addEventListener('siCardInserted', handleCardInserted);
                });
            });
        return simulateStation('Clear', 1, 'Clear')()
            .then(readoutCard())
            .then(simulateStation('Check', 2, 'Check'))
            .then(readoutCard())
            .then(simulateStation('Start', 3, 'Start'))
            .then(readoutCard())
            .then(simulateStation('Control', 31, 'Punch 31'))
            .then(readoutCard())
            .then(simulateStation('Control', 32, 'Punch 32'))
            .then(readoutCard())
            .then(simulateStation('Control', 33, 'Punch 33'))
            .then(readoutCard())
            .then(simulateStation('Control', 34, 'Punch 34'))
            .then(readoutCard())
            .then(simulateStation('Control', 35, 'Punch 35'))
            .then(readoutCard())
            .then(simulateStation('Control', 36, 'Punch 36'))
            .then(readoutCard())
            .then(simulateStation('Control', 37, 'Punch 37'))
            .then(readoutCard())
            .then(simulateStation('Control', 38, 'Punch 38'))
            .then(readoutCard())
            .then(simulateStation('Control', 39, 'Punch 39'))
            .then(readoutCard())
            .then(simulateStation('Control', 40, 'Punch 40'))
            .then(readoutCard())
            .then(simulateStation('Control', 41, 'Punch 41'))
            .then(readoutCard())
            .then(simulateStation('Control', 42, 'Punch 42'))
            .then(readoutCard())
            .then(simulateStation('Control', 43, 'Punch 43'))
            .then(readoutCard())
            .then(simulateStation('Control', 44, 'Punch 44'))
            .then(readoutCard())
            .then(simulateStation('Control', 45, 'Punch 45'))
            .then(readoutCard())
            .then(simulateStation('Control', 46, 'Punch 46'))
            .then(readoutCard())
            .then(simulateStation('Control', 47, 'Punch 47'))
            .then(readoutCard())
            .then(simulateStation('Control', 48, 'Punch 48'))
            .then(readoutCard())
            .then(simulateStation('Control', 49, 'Punch 49'))
            .then(readoutCard())
            .then(simulateStation('Control', 50, 'Punch 50'))
            .then(readoutCard())
            .then(simulateStation('Control', 51, 'Punch 51'))
            .then(readoutCard())
            .then(simulateStation('Control', 52, 'Punch 52'))
            .then(readoutCard())
            .then(simulateStation('Control', 53, 'Punch 53'))
            .then(readoutCard())
            .then(simulateStation('Control', 54, 'Punch 54'))
            .then(readoutCard())
            .then(simulateStation('Control', 55, 'Punch 55'))
            .then(readoutCard())
            .then(simulateStation('Control', 56, 'Punch 56'))
            .then(readoutCard())
            .then(simulateStation('Control', 57, 'Punch 57'))
            .then(readoutCard())
            .then(simulateStation('Control', 58, 'Punch 58'))
            .then(readoutCard())
            .then(simulateStation('Control', 59, 'Punch 59'))
            .then(readoutCard())
            .then(simulateStation('Control', 60, 'Punch 60'))
            .then(readoutCard())
            .then(simulateStation('Control', 61, 'Punch 61'))
            .then(readoutCard())
            .then(simulateStation('Control', 62, 'Punch 62'))
            .then(readoutCard())
            .then(simulateStation('Control', 63, 'Punch 63'))
            .then(readoutCard())
            .then(simulateStation('Control', 64, 'Punch 64'))
            .then(readoutCard())
            .then(simulateStation('Control', 65, 'Punch 65'))
            .then(readoutCard())
            .then(simulateStation('Control', 66, 'Punch 66'))
            .then(readoutCard())
            .then(simulateStation('Control', 67, 'Punch 67'))
            .then(readoutCard())
            .then(simulateStation('Control', 68, 'Punch 68'))
            .then(readoutCard())
            .then(simulateStation('Control', 69, 'Punch 69'))
            .then(readoutCard())
            .then(simulateStation('Control', 70, 'Punch 70'))
            .then(readoutCard())
            .then(() => {
                context.putString('Finished!\n');
                console.log('SAMPLES', samples);
            })
            .catch(() => undefined);
    },
};
 
export class TestCommand extends BaseCommand {
    getArgTypes(): ArgType[] {
        return [
            {
                name: 'testName',
                choices: Object.keys(tests),
            },
        ];
    }
 
    printUsage(context: ShellCommandContext): void {
        super.printUsage(context);
    }
 
    run(context: ShellCommandContext): Promise<void> {
        const what = context.args[1];
        return tests[what](context);
    }
}