Feature: Backup hooks

  Keld can run backup-specific shell commands before and after restic backup.
  These hooks let users prepare generated backup inputs, such as database
  dumps, and clean up generated files after restic has attempted the backup.

  Background:
    Given the selected preset supplies backup paths

  Rule: Backup hooks are resolved by the most-specific matching configuration

    Scenario: Most-specific hook keys override inherited hook keys
      Given backup hooks are configured in multiple matching sections
      When the backup configuration is resolved
      Then the pre-hooks come from the most-specific section that defines pre-hooks
      And the post-hooks come from the most-specific section that defines post-hooks
      And the hooks are not passed to restic as flags

    Scenario: Omitted hook keys inherit less-specific hooks
      Given backup hooks are configured in a less-specific matching section
      And the most-specific matching section omits hook keys
      When the backup configuration is resolved
      Then the inherited pre-hooks are used
      And the inherited post-hooks are used

    Scenario: Empty hook lists clear inherited hooks
      Given backup hooks are configured in a less-specific matching section
      And the most-specific matching section clears hook keys
      When the backup configuration is resolved
      Then no pre-hooks are configured
      And no post-hooks are configured

  Rule: Pre-hooks run before restic backup

    Scenario: Pre-hooks complete successfully
      Given the backup configuration includes pre-hooks
      When keld runs the backup
      Then each pre-hook runs before restic backup
      And restic backup is attempted

    Scenario: A pre-hook fails
      Given the backup configuration includes a failing pre-hook
      When keld runs the backup
      Then restic backup is not attempted
      And post-hooks are not run
      And keld reports the pre-hook failure

  Rule: Post-hooks run after restic backup is attempted

    Scenario Outline: Post-hooks receive the restic outcome
      Given the backup configuration includes post-hooks
      And restic backup exits with code <code>
      When keld runs the backup
      Then each post-hook runs after restic backup
      And each post-hook receives KELD_RESTIC_EXIT_CODE set to "<code>"
      And each post-hook receives KELD_RESTIC_STATUS set to "<status>"
      And keld exits with code <code>

      Examples:
        | code | status             |
        | 0    | success            |
        | 1    | fatal              |
        | 2    | runtime_error      |
        | 3    | partial            |
        | 10   | repository_missing |
        | 11   | locked             |
        | 12   | wrong_password     |
        | 130  | interrupted        |
        | 143  | terminated         |
        | 99   | unknown_failure    |

    Scenario: A post-hook fails after restic fails
      Given the backup configuration includes a failing post-hook
      And restic backup exits with code 3
      When keld runs the backup
      Then keld exits with code 3
      And keld reports the post-hook failure

    Scenario: Restic fails to start after pre-hooks complete
      Given the backup configuration includes pre-hooks and post-hooks
      And restic backup fails to start
      When keld runs the backup
      Then each post-hook runs after the failed restic start
      And each post-hook receives KELD_RESTIC_STATUS set to "start_failed"
      And each post-hook receives no KELD_RESTIC_EXIT_CODE
      And keld reports the restic start failure

  Rule: Command previews show hooks without runtime-only outcome values

    Scenario: Showing the command for a backup with hooks
      Given the backup configuration includes pre-hooks and post-hooks
      When keld shows the resolved backup command
      Then the output includes the configured pre-hooks
      And the output includes the configured post-hooks
      And the output does not include KELD_RESTIC_EXIT_CODE
      And the output does not include KELD_RESTIC_STATUS
