import Quagga from '../../src/quagga';
import TestExternalCode128Reader from '../../src/reader/test_external_code_128_reader';
import { runDecoderTestBothHalfSample, generateConfig } from './helpers';

describe('External Reader Test, using test external code_128 reader', () => {
    // NOTE: This test demonstrates the external reader plugin API.
    // There is a known issue where external readers may fail intermittently in TypeScript
    // test environments. The .allowFail mechanism handles this by skipping failing tests
    // rather than failing the build. This issue does not occur in production (compiled code).
    // External readers work correctly in production (compiled code).
    describe('works', () => {
        before(() => {
            Quagga.registerReader('external_code_128_reader', TestExternalCode128Reader);
        });
        // Note: FNC1 characters are represented as ASCII 29 (Group Separator, \x1D or \u001d)
        // These are used in GS1-128 barcodes as field separators
        const FNC1 = String.fromCharCode(29);
        const externalCode128TestSet = [
            { 'name': 'image-001.jpg', 'result': '0001285112001000040801', format: 'code_128' },
            { 'name': 'image-002.jpg', 'result': 'FANAVF14617104', format: 'code_128' },
            { 'name': 'image-003.jpg', 'result': '673023', format: 'code_128' },
            { 'name': 'image-004.jpg', 'result': '010210150301625334', format: 'code_128', allowFailInNode: true, allowFailInBrowser: true },
            { 'name': 'image-005.jpg', 'result': '419055603900009001012999', format: 'code_128' },
            { 'name': 'image-006.jpg', 'result': '419055603900009001012999', format: 'code_128' },
            // GS1-128 barcode with FNC1 characters as field separators
            { 'name': 'image-007.jpg', 'result': `${FNC1}42095747${FNC1}9499907123456123456781`, format: 'code_128' },
            { 'name': 'image-008.jpg', 'result': '1020185021797280784055', format: 'code_128' },
            { 'name': 'image-009.jpg', 'result': '0001285112001000040801', format: 'code_128' },
            { 'name': 'image-010.jpg', 'result': '673023', format: 'code_128' },
            // TODO: need to implement having different inputStream parameters to be able to
            // read this one -- it works only with inputStream size set to 1600 presently, but
            // other samples break at that high a size.
            // { name: 'image-011.png', result: '33c64780-a9c0-e92a-820c-fae7011c11e2' },
        ];
        runDecoderTestBothHalfSample('code_128_external', (halfSample) => generateConfig({
            inputStream: {
                size: 800,
                singleChannel: false,
            },
            locator: {
                halfSample,
            },
            decoder: {
                readers: ['external_code_128_reader'],
            },
        }), externalCode128TestSet, 'code_128'); // Use code_128 fixture path
    });
});
