# Copyright (C) 2018  The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information

import json
import os

from hypothesis import settings, HealthCheck

# Register some hypothesis profiles

settings.register_profile('default', settings())

settings.register_profile(
    'swh-web', settings(deadline=None,
                        suppress_health_check=[HealthCheck.too_slow]))

settings.register_profile(
    'swh-web-fast', settings(deadline=None, max_examples=1))


def pytest_configure(config):
    # Small hack in order to be able to run the unit tests
    # without static assets generated by webpack.
    # Those assets are not really needed for the Python tests
    # but the django templates will fail to load due to missing
    # generated file webpack-stats.json describing the js and css
    # files to include.
    # So generate a dummy webpack-stats.json file to overcome
    # that issue.
    test_dir = os.path.dirname(__file__)
    static_dir = os.path.join(test_dir, '../static')
    webpack_stats = os.path.join(static_dir, 'webpack-stats.json')
    if os.path.exists(webpack_stats):
        return
    bundles_dir = os.path.join(test_dir, '../assets/src/bundles')
    _, dirs, _ = next(os.walk(bundles_dir))
    mock_webpack_stats = {
        'status': 'done',
        'publicPath': '/static',
        'chunks': {}
    }
    for bundle in dirs:
        asset = 'js/%s.js' % bundle
        mock_webpack_stats['chunks'][bundle] = [{
            'name': asset,
            'publicPath': '/static/%s' % asset,
            'path': os.path.join(static_dir, asset)
        }]

    with open(webpack_stats, 'w') as outfile:
        json.dump(mock_webpack_stats, outfile)
