# Unit tests for variable defaults and output validation
# These tests validate module interface and defaults

variables {
  chef_role         = "test_role"
  aws_iam_role_id   = "test-iam-role"
  aws_instance_name = "test-instance"
}

# Test: Default chef_version is set
run "default_chef_version" {
  command = plan

  assert {
    condition     = can(regex("cinc/18\\.8\\.11/", output.user_data_output))
    error_message = "Default chef_version should be 18.8.11"
  }
}

# Test: Default chef_environment is _default
run "default_chef_environment" {
  command = plan

  assert {
    condition     = can(regex("environment.*_default", output.user_data_output))
    error_message = "Default chef_environment should be _default"
  }
}

# Test: Default ssl_verify_mode is verify_peer
run "default_ssl_verify_mode" {
  command = plan

  assert {
    condition     = can(regex("ssl_verify_mode.*:verify_peer", output.user_data_output))
    error_message = "Default ssl_verify_mode should be verify_peer"
  }
}

# Test: Default filesystem type is ext4
run "default_filesystem_type" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("mkfs\\.ext4", output.user_data_output))
    error_message = "Default block_device_filesystem_type should be ext4"
  }
}

# Test: Default log file path
run "default_log_file_path" {
  command = plan

  assert {
    condition     = can(regex("/var/log/cinc/bootstrap\\.log", output.user_data_output))
    error_message = "Default log_file should be /var/log/cinc/bootstrap.log"
  }
}

# Test: user_data_output is not empty
run "user_data_output_not_empty" {
  command = plan

  assert {
    condition     = length(output.user_data_output) > 1000
    error_message = "user_data_output should contain substantial script content"
  }
}

# Test: s3_chef_bucket_output matches variable
run "s3_bucket_output_matches" {
  command = plan

  variables {
    s3_chef_bucket = "test-bucket"
  }

  assert {
    condition     = output.s3_chef_bucket_output == "test-bucket"
    error_message = "s3_chef_bucket_output should match input variable"
  }
}

# Test: Empty mount points don't break the script
run "empty_mount_points_valid" {
  command = plan

  variables {
    ebs_data_mount_point       = ""
    instance_store_mount_point = ""
  }

  assert {
    condition     = can(regex("#!/bin/bash", output.user_data_output))
    error_message = "Script should be valid even with empty mount points"
  }
}

# Test: Architecture detection for amd64
run "architecture_detection_amd64" {
  command = plan

  assert {
    condition     = can(regex("x86_64\\)", output.user_data_output))
    error_message = "Script should detect x86_64 architecture"
  }

  assert {
    condition     = can(regex("cpu_arch=\"amd64\"", output.user_data_output))
    error_message = "Script should map x86_64 to amd64"
  }
}

# Test: Architecture detection for arm64
run "architecture_detection_arm64" {
  command = plan

  assert {
    condition     = can(regex("aarch64\\)", output.user_data_output))
    error_message = "Script should detect aarch64 architecture"
  }

  assert {
    condition     = can(regex("cpu_arch=\"arm64\"", output.user_data_output))
    error_message = "Script should map aarch64 to arm64"
  }
}

# Test: Platform detection for Debian
run "platform_detection_debian" {
  command = plan

  assert {
    condition     = can(regex("\\$ID.*==.*debian", output.user_data_output))
    error_message = "Script should detect Debian platform"
  }

  assert {
    condition     = can(regex("PLATFORM=\"debian\"", output.user_data_output))
    error_message = "Script should set PLATFORM to debian"
  }
}

# Test: Script includes reboot at the end
run "script_includes_reboot" {
  command = plan

  assert {
    condition     = can(regex("reboot\\s*$", output.user_data_output))
    error_message = "Script should end with reboot command"
  }
}

# Test: Cinc download uses correct Debian paths
run "cinc_download_debian_paths" {
  command = plan

  assert {
    condition     = can(regex("downloads\\.cinc\\.sh/files/stable/cinc/.*/debian/", output.user_data_output))
    error_message = "Cinc download URL should use Debian paths"
  }
}
