test {
  parallel = true
}

provider "github" {
  owner = "theorchard"
}

variables {
  application_family = "devops"
}

run "repo_with_all_defaults" {
  state_key = "repo_with_all_defaults"

  variables {
    repository_name    = "ows-success-test"
    description        = "Test repository"
    default_code_owner = "devops-squad"
  }

  assert {
    condition     = github_repository.orchard_repository.full_name == "theorchard/ows-success-test"
    error_message = "repo full_name mismatch"
  }

  assert {
    condition     = github_repository.orchard_repository.name == "ows-success-test"
    error_message = "repo name mismatch"
  }

  assert {
    condition     = github_repository.orchard_repository.visibility == "private"
    error_message = "repo not private"
  }

  assert {
    condition     = contains(split("\n", github_repository_file.codeowners.content), "* @theorchard/devops-squad")
    error_message = "CODEOWNERS file does not contain the expected default code owner"
  }

  assert {
    condition     = github_branch_default.default.branch == "master"
    error_message = "default branch is not 'master'"
  }

  # Test branch protection
  assert {
    condition     = length(keys(github_branch_protection.branch_protection)) == 1
    error_message = "unexpected number of branch protection rules"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].pattern == "master"
    error_message = "branch protection pattern mismatch for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].repository_id == "ows-success-test"
    error_message = "branch protection repository_id mismatch for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_pull_request_reviews[0].dismiss_stale_reviews == true
    error_message = "branch protection dismiss_stale_reviews not enabled for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_pull_request_reviews[0].require_last_push_approval == true
    error_message = "branch protection require_last_push_approval not enabled for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_pull_request_reviews[0].required_approving_review_count >= 1
    error_message = "branch protection required_approving_review_count not at least 1 for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_status_checks[0].strict == true
    error_message = "branch protection 'require branches to be up to date' setting is not as expected for master branch"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_status_checks[0].contexts == toset(["Jenkins"])
    error_message = "branch protection required status checks are not as expected for master branch"
  }

  assert {
    condition     = github_repository_custom_property.repo_custom_property["application_family"].property_name == "application_family"
    error_message = "repo custom property name mismatch"
  }
}

run "repo_with_custom_branch_protection" {
  state_key = "repo_with_custom_branch_protection"

  variables {
    repository_name                   = "ows-success-test-develop"
    description                       = "Test repository"
    default_code_owner                = "devops-squad"
    branch_protection_patterns        = ["master", "main", "develop"]
    require_branches_to_be_up_to_date = false
    required_status_checks            = ["custom-status-check-1", "custom-status-check-2"]
  }

  assert {
    condition     = github_repository.orchard_repository.full_name == "theorchard/ows-success-test-develop"
    error_message = "repo full_name mismatch"
  }

  assert {
    condition     = github_repository.orchard_repository.name == "ows-success-test-develop"
    error_message = "repo name mismatch"
  }

  assert {
    condition     = github_repository.orchard_repository.visibility == "private"
    error_message = "repo not private"
  }

  assert {
    condition     = contains(split("\n", github_repository_file.codeowners.content), "* @theorchard/devops-squad")
    error_message = "CODEOWNERS file does not contain the expected default code owner"
  }

  assert {
    condition     = length(keys(github_branch_protection.branch_protection)) == 3
    error_message = "unexpected number of branch protection rules"
  }

  # Test branch protection for "master" pattern
  assert {
    condition     = github_branch_protection.branch_protection["master"].pattern == "master"
    error_message = "branch protection pattern mismatch for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].repository_id == "ows-success-test-develop"
    error_message = "branch protection repository_id mismatch for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_status_checks[0].strict == false
    error_message = "branch protection 'require branches to be up to date' setting is not as expected for master branch"
  }

  assert {
    condition     = github_branch_protection.branch_protection["master"].required_status_checks[0].contexts == toset(["custom-status-check-1", "custom-status-check-2"])
    error_message = "branch protection required status checks are not as expected for master branch"
  }

  # Test branch protection for "main" pattern
  assert {
    condition     = github_branch_protection.branch_protection["main"].pattern == "main"
    error_message = "branch protection pattern mismatch for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].repository_id == "ows-success-test-develop"
    error_message = "branch protection repository_id mismatch for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_status_checks[0].strict == false
    error_message = "branch protection 'require branches to be up to date' setting is not as expected for main branch"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_status_checks[0].contexts == toset(["custom-status-check-1", "custom-status-check-2"])
    error_message = "branch protection required status checks are not as expected for main branch"
  }

  # Test branch protection for "develop" pattern
  assert {
    condition     = github_branch_protection.branch_protection["develop"].pattern == "develop"
    error_message = "branch protection pattern mismatch for develop"
  }

  assert {
    condition     = github_branch_protection.branch_protection["develop"].repository_id == "ows-success-test-develop"
    error_message = "branch protection repository_id mismatch for develop"
  }

  assert {
    condition     = github_branch_protection.branch_protection["develop"].required_status_checks[0].strict == false
    error_message = "branch protection 'require branches to be up to date' setting is not as expected for develop branch"
  }

  assert {
    condition     = github_branch_protection.branch_protection["develop"].required_status_checks[0].contexts == toset(["custom-status-check-1", "custom-status-check-2"])
    error_message = "branch protection required status checks are not as expected for develop branch"
  }

  # Test that all three patterns are correctly protected with consistent settings
  assert {
    condition     = github_branch_protection.branch_protection["master"].required_pull_request_reviews[0].dismiss_stale_reviews == true
    error_message = "branch protection dismiss_stale_reviews not enabled for master"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_pull_request_reviews[0].require_last_push_approval == true
    error_message = "branch protection require_last_push_approval not enabled for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["develop"].required_pull_request_reviews[0].required_approving_review_count >= 1
    error_message = "branch protection required_approving_review_count not at least 1 for develop"
  }

  assert {
    condition     = github_repository_custom_property.repo_custom_property["application_family"].property_name == "application_family"
    error_message = "repo custom property name mismatch"
  }
}


run "repo_with_custom_default_branch" {
  state_key = "repo_with_custom_default_branch"

  variables {
    repository_name     = "ows-success-test-custom-default-branch"
    description         = "Test repository"
    default_code_owner  = "devops-squad"
    default_branch_name = "main"
  }

  assert {
    condition     = github_branch_default.default.branch == "main"
    error_message = "default branch is not 'main'"
  }

  # Test branch protection
  assert {
    condition     = length(keys(github_branch_protection.branch_protection)) == 1
    error_message = "unexpected number of branch protection rules"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].pattern == "main"
    error_message = "branch protection pattern mismatch for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].repository_id == "ows-success-test-custom-default-branch"
    error_message = "branch protection repository_id mismatch for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_pull_request_reviews[0].dismiss_stale_reviews == true
    error_message = "branch protection dismiss_stale_reviews not enabled for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_pull_request_reviews[0].require_last_push_approval == true
    error_message = "branch protection require_last_push_approval not enabled for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_pull_request_reviews[0].required_approving_review_count >= 1
    error_message = "branch protection required_approving_review_count not at least 1 for main"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_status_checks[0].strict == true
    error_message = "branch protection 'require branches to be up to date' setting is not as expected for main branch"
  }

  assert {
    condition     = github_branch_protection.branch_protection["main"].required_status_checks[0].contexts == toset(["Jenkins"])
    error_message = "branch protection required status checks are not as expected for main branch"
  }

  assert {
    condition     = github_repository_custom_property.repo_custom_property["application_family"].property_name == "application_family"
    error_message = "repo custom property name mismatch"
  }
}

run "repo_with_autolink_references" {
  state_key = "repo_with_autolink_references"

  variables {
    repository_name    = "ows-success-test-autolink"
    description        = "Test repository with autolink references"
    default_code_owner = "devops-squad"
    autolink_references = [
      {
        key_prefix          = "CCM-"
        target_url_template = "https://jira.sonymusic.com/browse/CCM-<num>"
      },
      {
        key_prefix          = "SYS-"
        target_url_template = "https://jira.sonymusic.com/browse/SYS-<num>"
        is_alphanumeric     = false
      }
    ]
  }

  assert {
    condition     = github_repository.orchard_repository.full_name == "theorchard/ows-success-test-autolink"
    error_message = "repo full_name mismatch"
  }

  assert {
    condition     = length(keys(github_repository_autolink_reference.autolink)) == 2
    error_message = "unexpected number of autolink references"
  }

  assert {
    condition     = github_repository_autolink_reference.autolink["CCM-"].key_prefix == "CCM-"
    error_message = "autolink key_prefix mismatch for CCM-"
  }

  assert {
    condition     = github_repository_autolink_reference.autolink["CCM-"].target_url_template == "https://jira.sonymusic.com/browse/CCM-<num>"
    error_message = "autolink target_url_template mismatch for CCM-"
  }

  assert {
    condition     = github_repository_autolink_reference.autolink["CCM-"].is_alphanumeric == true
    error_message = "autolink is_alphanumeric should default to true for CCM-"
  }

  assert {
    condition     = github_repository_autolink_reference.autolink["SYS-"].key_prefix == "SYS-"
    error_message = "autolink key_prefix mismatch for SYS-"
  }

  assert {
    condition     = github_repository_autolink_reference.autolink["SYS-"].target_url_template == "https://jira.sonymusic.com/browse/SYS-<num>"
    error_message = "autolink target_url_template mismatch for SYS-"
  }

  assert {
    condition     = github_repository_autolink_reference.autolink["SYS-"].is_alphanumeric == false
    error_message = "autolink is_alphanumeric should be false for SYS-"
  }

  assert {
    condition     = github_repository_custom_property.repo_custom_property["application_family"].property_name == "application_family"
    error_message = "repo custom property name mismatch"
  }
}
