local cache_yarn = import 'github/workflows/_cache-yarn.libsonnet';
local utils = import 'utils.libjsonnet';

function(settings)
  {
    jobs: {
      test: {
        'runs-on': settings.tests_run_on,
        steps: [
          {
            uses: 'actions/checkout@' + utils.githubLatestActionTag('actions', 'checkout'),
          },
        ] + (if std.length(settings.github.workflows.tests.apt_packages) > 0 then [{
               name: 'Install dependencies',
               run: 'sudo apt-get update && sudo apt-get install -y ' + std.join(' ', settings.github.workflows.tests.apt_packages),
             }] else []) + [
          cache_yarn,
          {
            name: 'Install dependencies (Yarn)',
            run: 'yarn',
          },
          {
            name: 'Tests',
            run: 'yarn jest --coverage',
          },
        ] + (if settings.want_coveralls then [{
               name: 'Coveralls',
               uses: 'coverallsapp/github-action@' + utils.githubLatestActionTag('coverallsapp', 'github-action'),
             }] else []),
      },
    },
    name: 'Tests',
    on: {
      pull_request: {
        branches: [
          settings.default_branch,
        ],
      },
      push: {
        branches: [
          settings.default_branch,
        ],
      },
    },
    permissions: {
      contents: 'read',
    },
  }
