// Test gate for the Hermes code-repair demo.
//
// Tuned for a demo, where the wait between pushing a defect and seeing the
// build go red is watched by people. The tests themselves run in under a
// second; everything else was overhead:
//
//   agent pod scheduling + image pull   ~54s
//   full clone of a five-commit repo    ~25s
//   pip install pytest                  ~55s
//   the actual tests                    ~0.6s
//
// A named template with idleMinutes keeps the pod alive between builds, so a
// second run reuses it: no scheduling, no image pull, and the pip cache in the
// workspace volume is already warm. The first run after an idle period still
// pays the full cost, which is why a throwaway run before a demo is worth it.
podTemplate(
  cloud: 'kubernetes',
  // A fixed label is what makes reuse possible at all. The default inline
  // template mints a random POD_LABEL per build, so every build necessarily
  // got a fresh pod.
  label: 'hermes-code-demo-agent',
  idleMinutes: 60,
  yaml: """
apiVersion: v1
kind: Pod
spec:
  serviceAccountName: jenkins
  securityContext:
    runAsUser: 1000
    runAsGroup: 1000
    fsGroup: 1000
    fsGroupChangePolicy: "OnRootMismatch"
  nodeSelector:
    kubernetes.io/arch: arm64
    node-role.kubernetes.io/worker: "true"
  containers:
    - name: python
      image: python:3.12-slim
      command: ["sleep"]
      args: ["3600"]
      env:
        - name: HOME
          value: /home/jenkins/agent
        - name: PIP_CACHE_DIR
          value: /home/jenkins/agent/.pip-cache
      resources:
        requests:
          cpu: 100m
          memory: 128Mi
        limits:
          cpu: 1000m
          memory: 512Mi
""") {
  node('hermes-code-demo-agent') {
    container('python') {
      stage('Checkout') {
        // Depth 1 and no tags: the build only ever needs the commit under
        // test, and fetching five commits of history to run one test file is
        // time spent on nothing anyone will look at.
        checkout([
          $class: 'GitSCM',
          branches: scm.branches,
          userRemoteConfigs: scm.userRemoteConfigs,
          extensions: [
            [$class: 'CloneOption', shallow: true, depth: 1, noTags: true, timeout: 5],
          ],
        ])
      }
      stage('tests') {
        // Idempotent and near-instant once the pod's pip cache is warm; the
        // cost is only paid on a cold agent.
        sh 'pip install --quiet --disable-pip-version-check pytest'
        sh 'python -m pytest -v --junitxml=build/junit.xml'
      }
    }
  }
}
