# frozen_string_literal: true

require 'spec_helper'
require 'tempfile'
require_relative '../lib/nav_generator'

RSpec.describe 'When generating a nav' do
  let(:nav_file) { Tempfile.new }

  def create_nav(nav)
    html = '<ul>' + nav.map { |href, name| "<li><a href='#{href}'>#{name}</a></li>" }.join('') + '</ul>'
    File.write(nav_file, html)
    Docs::NavGenerator.new(path: nav_file).to_hash
  end

  it 'converts html to md' do
    nav = create_nav([
                       ['doc1.html', 'Document exists'],
                       ['doc2.html', 'Document does not exist']
                     ])

    expect(nav).to eq([
                        { 'Document exists' => 'doc1.md' },
                        { 'Document does not exist' => 'doc2.md' }
                      ])
  end

  xit 'handles navigation with sub-sub-menus' do
    File.write(nav_file, File.read(File.join(__dir__, 'fixtures/deepnav.html')))
    nav = Docs::NavGenerator.new(path: nav_file).to_hash

    expect(nav).to eq([
                        { 'Header' => [{ 'Appears Under header' => 'under.md' }] },
                        { 'API' => [
                          { 'Home' => 'api/index.md' },
                          { 'Config' => 'api/config.md' },
                          { 'Auth' => 'api/auth.md' }
                        ] },
                        { 'Release Notes' => 'release-notes.md' },
                        { 'Another Nav' => [
                          { 'Something' => 'another-nav/something.md' }
                        ] },
                        { 'Home' => 'index.md' },
                        { 'Using' => 'using.md' }
                      ])
  end

  it 'does something complex' do
    File.write(nav_file, File.read(File.join(__dir__, 'fixtures/complexnav.html')))
    nav = Docs::NavGenerator.new(path: nav_file).to_hash

    expect(nav).to eq([{ 'General Information' => [{ 'Contributing Documentation' => '/concepts/contribute.md' },
                                                   { 'Cloud Foundry Concepts' => [{ 'Cloud Foundry Overview' => '/concepts/overview.md' },
                                                                                  { 'Cloud Foundry Components' => '/concepts/architecture/' },
                                                                                  { 'Diego Components and Architecture' => '/concepts/diego/diego-architecture.md' },
                                                                                  { 'How the Diego Auction Allocates Jobs' => '/concepts/diego/diego-auction.md' },
                                                                                  { 'How Applications Are Staged' => '/concepts/how-applications-are-staged.md' },
                                                                                  { 'Understanding Application SSH' => '/concepts/diego/ssh-conceptual.md' },
                                                                                  { 'Orgs, Spaces, Roles, and Permissions' => '/concepts/roles.md' },
                                                                                  { 'Cloud Foundry Security' => '/concepts/security.md' },
                                                                                  { 'Understanding Container Security' => '/concepts/container-security.md' },
                                                                                  { 'Understanding Container-to-Container Networking' => '/concepts/understand-cf-networking.md' },
                                                                                  { 'Understanding GrootFS Disk Usage' => '/concepts/grootfs-disk.md' },
                                                                                  { 'User Account and Authentication (UAA) Server' => '/concepts/architecture/uaa.md' },
                                                                                  { 'Cloud Foundry Routing Architecture' => '/concepts/cf-routing-architecture.md' },
                                                                                  { 'HTTP Routing' => '/concepts/http-routing.md' },
                                                                                  { 'Stacks' => '/devguide/deploy-apps/stacks.md' }] },
                                                   { 'Cloud Foundry Command Line Interface (cf CLI)' => [{ 'Installing the cf CLI' => '/cf-cli/install-go-cli.md' },
                                                                                                         { 'Getting Started with the cf CLI' => '/cf-cli/getting-started.md' },
                                                                                                         { 'Using the cf CLI with a Proxy Server' => '/cf-cli/http-proxy.md' },
                                                                                                         { 'Using the cf CLI with a Self-Signed Certificate' => '/cf-cli/self-signed.md' },
                                                                                                         { 'Using cf CLI Plugins' => '/cf-cli/use-cli-plugins.md' },
                                                                                                         { 'Developing cf CLI Plugins' => '/cf-cli/develop-cli-plugins.md' },
                                                                                                         { 'cf CLI Reference Guide' => '/cf-cli/cf-help.md' }] }] },
                       { 'Information for Operators' => [{ 'Deploying Cloud Foundry' => [{ 'Home' => '/deploying/index.md' },
                                                                                         { 'Setting Up DNS for Your Environment' => '/deploying/common/dns_prereqs.md' },
                                                                                         { 'Deploying Cloud Foundry with cf-deployment' => [{ 'Home' => '/deploying/cf-deployment/index.md' },
                                                                                                                                            { 'Deploying BOSH on AWS' => '/deploying/common/aws.md' },
                                                                                                                                            { 'Deploying BOSH on GCP' => '/deploying/cf-deployment/gcp.md' },
                                                                                                                                            { 'Deploying Cloud Foundry' => '/deploying/cf-deployment/deploy-cf.md' }] },
                                                                                         { 'Migrating from cf-release to cf-deployment' => '/deploying/migrating.md' },
                                                                                         { 'Configuring your Cloud Foundry for BOSH Backup and Restore' => [{ 'Home' => '/bbr/cf-backup.md' },
                                                                                                                                                            { 'Backup and Restore for External Blobstores' => '/bbr/external-blobstores.md' }] },
                                                                                         { 'Additional Configuration' => [{ 'High Availability in Cloud Foundry' => '/concepts/high-availability.md' },
                                                                                                                          { 'Log Drain Blacklist Configuration' => '/deploying/common/log_drain_blacklists.md' },
                                                                                                                          { 'Cloud Controller Blobstore Configuration' => '/deploying/common/cc-blobstore-config.md' }] }] },
                                                         { 'Administering Cloud Foundry' => [{ 'Home' => '/adminguide/index.md' },
                                                                                             { 'Managing Custom Buildpacks' => '/adminguide/buildpacks.md' },
                                                                                             { 'Using Docker in Cloud Foundry' => '/adminguide/docker.md' },
                                                                                             { 'Creating and Managing Users with the cf CLI' => '/adminguide/cli-user-management.md' },
                                                                                             { 'Creating and Managing Users with the UAA CLI (UAAC)' => '/uaa/uaa-user-management.md' },
                                                                                             { 'Creating and Modifying Quota Plans' => '/adminguide/quota-plans.md' },
                                                                                             { 'Getting Started with the Notifications Service' => '/adminguide/notifications.md' },
                                                                                             { 'Application Security Groups' => '/concepts/asg.md' },
                                                                                             { 'Administering Container-to-Container Networking' => '/devguide/deploy-apps/cf-networking.md' },
                                                                                             { 'Managing Isolation Segments' => '/adminguide/isolation-segments.md' },
                                                                                             { 'Routing for Isolation Segments' => '/adminguide/routing-is.md' },
                                                                                             { 'Feature Flags' => '/adminguide/listing-feature-flags.md' },
                                                                                             { 'Managing Diego Cell Limits During Upgrade' => '/adminguide/diego-cell-upgrade.md' },
                                                                                             { 'Examining GrootFS Disk Usage' => '/adminguide/examining_grootfs_disk.md' },
                                                                                             { 'Setting a Maximum Number of Started Containers' => '/adminguide/max-container-starts.md' },
                                                                                             { 'Bulletin Board System (BBS) Data Store Encryption' => '/adminguide/bbs-encryption-keys.md' },
                                                                                             { 'Enabling IPv6 for Hosted Applications' => '/adminguide/enabling_ipv6.md' },
                                                                                             { 'Securing Traffic into Cloud Foundry' => '/adminguide/securing-traffic.md' },
                                                                                             { 'Configuring Trusted System Certificates for Applications' => '/running/trusted-system-certificates.md' },
                                                                                             { 'Enabling TCP Routing' => '/adminguide/enabling-tcp-routing.md' },
                                                                                             { 'Troubleshooting TCP Routing' => '/adminguide/troubleshooting_tcp_routing.md' },
                                                                                             { 'Enabling Zipkin Tracing' => '/adminguide/zipkin_tracing.md' },
                                                                                             { 'Supporting WebSockets' => '/adminguide/supporting-websockets.md' },
                                                                                             { 'Configuring Load Balancer Healthchecks for Cloud Foundry Routers' => '/adminguide/configure-lb-healthcheck.md' },
                                                                                             { 'Troubleshooting Slow Requests in Cloud Foundry' => '/adminguide/troubleshooting_slow_requests.md' },
                                                                                             { 'Troubleshooting Router Error Responses' => '/adminguide/troubleshooting-router-error-responses.md' },
                                                                                             { 'Setting the Rate Limit for the Cloud Controller API' => '/running/setting-rate-limit-cloud-api.md' },
                                                                                             { 'Adding Volume Services to your Deployment' => '/running/deploy-vol-services.md' },
                                                                                             { 'Router Back-End Keepalive Connections' => '/adminguide/routing-keepalive.md' }] },
                                                         { 'Running and Troubleshooting Cloud Foundry' => [{ 'Home' => '/running/' },
                                                                                                           { 'Cloud Foundry Logging' => '/running/managing-cf/logging.md' },
                                                                                                           { 'Configuring System Logging' => '/running/managing-cf/logging-config.md' },
                                                                                                           { 'Audit Events' => '/running/managing-cf/audit-events.md' },
                                                                                                           { 'Usage Events and Billing' => '/running/managing-cf/usage-events.md' },
                                                                                                           { 'Configuring SSH Access for Cloud Foundry' => '/running/config-ssh.md' },
                                                                                                           { 'Configuring Cell Disk Cleanup Scheduling' => '/running/config-cell-cleanup.md' },
                                                                                                           { 'Configuring Health Monitor Notifications' => '/running/hm-notifications.md' },
                                                                                                           { 'Monitoring and Testing Diego Components' => '/running/monitoring-test.md' },
                                                                                                           { 'Troubleshooting Cloud Foundry' => '/running/troubleshooting.md' },
                                                                                                           { 'UAA Performance' => '/running/uaa-performance.md' },
                                                                                                           { 'UAA Performance Metrics' => '/uaa/uaa-metrics.md' }] },
                                                         { 'Logging and Metrics in Cloud Foundry' => [{ 'Overview of the Loggregator System' => '/loggregator/architecture.md' },
                                                                                                      { 'Installing the Loggregator Plugin for CF CLI' => '/loggregator/cli-plugin.md' },
                                                                                                      { 'Security Event Logging' => '/loggregator/cc-uaa-logging.md' },
                                                                                                      { 'Cloud Foundry Component Metrics' => '/running/all_metrics.md' },
                                                                                                      { 'Loggregator Guide for Cloud Foundry Operators' => '/loggregator/log-ops-guide.md' },
                                                                                                      { 'Cloud Foundry Data Sources' => '/loggregator/data-sources.md' },
                                                                                                      { 'Deploying a Nozzle to the Loggregator Firehose' => '/loggregator/nozzle-tutorial.md' }] },
                                                         { 'BOSH Documentation' => 'http://bosh.io/docs/' },
                                                         { 'BOSH Backup and Restore' => [{ 'Home' => '/bbr/index.md' },
                                                                                         { 'Installing BBR' => '/bbr/installing.md' },
                                                                                         { 'Release Notes for BBR' => '/bbr/bbr-rn.md' },
                                                                                         { 'Backing Up with BBR' => '/bbr/backup.md' },
                                                                                         { 'Restoring with BBR' => '/bbr/restore.md' },
                                                                                         { 'BBR Logging' => '/bbr/logging.md' },
                                                                                         { "BBR Developer's Guide" => '/bbr/bbr-devguide.md' }] }] },
                       { 'Information for Developers' => [{ 'Developing and Managing Applications' => [{ 'Considerations for Designing and Running an Application in the Cloud' => '/devguide/deploy-apps/prepare-to-deploy.md' },
                                                                                                       { 'Deploy an Application' => '/devguide/deploy-apps/deploy-app.md' },
                                                                                                       { 'Deploy a Large Application' => '/devguide/deploy-apps/large-app-deploy.md' },
                                                                                                       { 'Deploy an App with Docker' => '/devguide/deploy-apps/push-docker.md' },
                                                                                                       { 'Starting, Restarting, and Restaging Applications' => '/devguide/deploy-apps/start-restart-restage.md' },
                                                                                                       { 'Deploying with Application Manifests' => '/devguide/deploy-apps/manifest.md' },
                                                                                                       { 'Application Container Lifecycle' => '/devguide/deploy-apps/app-lifecycle.md' },
                                                                                                       { 'Using Application Health Checks' => '/devguide/deploy-apps/healthchecks.md' },
                                                                                                       { 'Scaling an Application Using cf scale' => '/devguide/deploy-apps/cf-scale.md' },
                                                                                                       { 'Running Tasks' => '/devguide/using-tasks.md' },
                                                                                                       { 'Routes and Domains' => '/devguide/deploy-apps/routes-domains.md' },
                                                                                                       { 'Changing Stacks' => '/devguide/deploy-apps/stacks.md' },
                                                                                                       { 'Cloud Foundry Environment Variables' => '/devguide/deploy-apps/environment-variable.md' },
                                                                                                       { 'Using Blue-Green Deployment to Reduce Downtime and Risk' => '/devguide/deploy-apps/blue-green.md' },
                                                                                                       { 'Application Logging in Cloud Foundry' => '/devguide/deploy-apps/streaming-logs.md' },
                                                                                                       { 'Troubleshooting Application Deployment and Health' => '/devguide/deploy-apps/troubleshoot-app-health.md' },
                                                                                                       { 'Application SSH Overview' => '/devguide/deploy-apps/app-ssh-overview.md' },
                                                                                                       { 'Accessing Apps with Diego SSH' => '/devguide/deploy-apps/ssh-apps.md' },
                                                                                                       { 'Accessing Services with Diego SSH' => '/devguide/deploy-apps/ssh-services.md' },
                                                                                                       { 'Trusted System Certificates' => '/devguide/deploy-apps/trusted-system-certificates.md' },
                                                                                                       { 'Identifying your Cloud Foundry API Endpoint and Version' => '/running/cf-api-endpoint.md' },
                                                                                                       { 'Using Experimental cf CLI Commands' => '/devguide/v3-commands.md' },
                                                                                                       { 'Integrating Service Instances with Applications' => [{ 'Services Overview' => '/devguide/services/' },
                                                                                                                                                               { 'Delivering Service Credentials to an Application' => '/devguide/services/application-binding.md' },
                                                                                                                                                               { 'Managing Service Instances with the CLI' => '/devguide/services/managing-services.md' },
                                                                                                                                                               { 'Managing Service Keys' => '/devguide/services/service-keys.md' },
                                                                                                                                                               { 'Sharing Service Instances' => '/devguide/services/sharing-instances.md' },
                                                                                                                                                               { 'User-Provided Service Instances' => '/devguide/services/user-provided.md' },
                                                                                                                                                               { 'Manage Application Requests with Route Services' => '/devguide/services/route-binding.md' },
                                                                                                                                                               { 'Using an External File System (Volume Services)' => '/devguide/services/using-vol-services.md' },
                                                                                                                                                               { 'Configuring Play Framework Service Connections' => '/devguide/services/play-service-bindings.md' },
                                                                                                                                                               { 'Migrating a Database in Cloud Foundry' => '/devguide/services/migrate-db.md' },
                                                                                                                                                               { 'Streaming Application Logs to Log Management Services' => '/devguide/services/log-management.md' },
                                                                                                                                                               { 'Service-Specific Instructions for Streaming Application Logs' => '/devguide/services/log-management-thirdparty-svc.md' },
                                                                                                                                                               { 'Integrating Cloud Foundry with Splunk' => '/devguide/services/integrate-splunk.md' }] }] },
                                                          { 'Buildpacks' => [{ 'Home' => '/buildpacks/' },
                                                                             { 'About Buildpacks' => [{ 'Home' => '/buildpacks/using-buildpacks.md' },
                                                                                                      { 'Understanding Buildpacks' => '/buildpacks/understand-buildpacks.md' },
                                                                                                      { 'Stack Association' => '/buildpacks/stack-association.md' },
                                                                                                      { 'Pushing an Application with Multiple Buildpacks' => '/buildpacks/use-multiple-buildpacks.md' },
                                                                                                      { 'Using a Proxy' => '/buildpacks/proxy-usage.md' },
                                                                                                      { 'Supported Binary Dependencies' => '/buildpacks/supported-binary-dependencies.md' },
                                                                                                      { 'Production Server Configuration' => '/buildpacks/prod-server.md' }] },
                                                                             { 'Binary' => '/buildpacks/binary/index.md' },
                                                                             { 'Go' => '/buildpacks/go/index.md' },
                                                                             { 'HWC' => '/buildpacks/hwc/index.md' },
                                                                             { 'Java' => [{ 'Home' => '/buildpacks/java/index.md' },
                                                                                          { 'Tips for Java Developers' => '/buildpacks/java/java-tips.md' },
                                                                                          { 'Getting Started Deploying Apps' => [{ 'Home' => '/buildpacks/java/getting-started-deploying-apps/index.md' },
                                                                                                                                 { 'Grails' => '/buildpacks/java/getting-started-deploying-apps/gsg-grails.md' },
                                                                                                                                 { 'Ratpack' => '/buildpacks/java/getting-started-deploying-apps/gsg-ratpack.md' },
                                                                                                                                 { 'Spring' => '/buildpacks/java/getting-started-deploying-apps/gsg-spring.md' }] },
                                                                                          { 'Configuring Service Connections' => [{ 'Home' => '/buildpacks/java/configuring-service-connections/index.md' },
                                                                                                                                  { 'Grails' => '/buildpacks/java/configuring-service-connections/grails-service-bindings.md' },
                                                                                                                                  { 'Play' => '/buildpacks/java/configuring-service-connections/play-service-bindings.md' },
                                                                                                                                  { 'Spring' => '/buildpacks/java/configuring-service-connections/spring-service-bindings.md' }] },
                                                                                          { 'Cloud Foundry Java Client Library' => '/buildpacks/java/java-client.md' }] },
                                                                             { '.NET Core' => '/buildpacks/dotnet-core/index.md' },
                                                                             { 'NGINX Buildpack' => '/buildpacks/nginx/index.md' },
                                                                             { 'Node.js' => [{ 'Home' => '/buildpacks/node/index.md' },
                                                                                             { 'Tips for Node.js Applications' => '/buildpacks/node/node-tips.md' },
                                                                                             { 'Environment Variables Defined by the Node Buildpack' => '/buildpacks/node/node-environment.md' },
                                                                                             { 'Configure Service Connections for Node.js' => '/buildpacks/node/node-service-bindings.md' }] },
                                                                             { 'PHP' => [{ 'Home' => '/buildpacks/php/index.md' },
                                                                                         { 'Tips for PHP Developers' => '/buildpacks/php/gsg-php-tips.md' },
                                                                                         { 'Getting Started Deploying PHP Apps' => '/buildpacks/php/gsg-php-usage.md' },
                                                                                         { 'PHP Buildpack Configuration' => '/buildpacks/php/gsg-php-config.md' },
                                                                                         { 'Composer' => '/buildpacks/php/gsg-php-composer.md' },
                                                                                         { 'Sessions' => '/buildpacks/php/gsg-php-sessions.md' },
                                                                                         { 'New Relic' => '/buildpacks/php/gsg-php-newrelic.md' }] },
                                                                             { 'Python' => '/buildpacks/python/index.md' },
                                                                             { 'R' => '/buildpacks/r/index.md' },
                                                                             { 'Ruby' => [{ 'Home' => '/buildpacks/ruby/index.md' },
                                                                                          { 'Tips for Ruby Developers' => '/buildpacks/ruby/ruby-tips.md' },
                                                                                          { 'Getting Started Deploying Apps' => [{ 'Home' => '/buildpacks/ruby/deploying-apps-index.md' },
                                                                                                                                 { 'Ruby' => '/buildpacks/ruby/gsg-ruby.md' },
                                                                                                                                 { 'Ruby on Rails' => '/buildpacks/ruby/gsg-ror.md' }] },
                                                                                          { 'Configure Rake Tasks for Deployed Apps' => '/buildpacks/ruby/rake-config.md' },
                                                                                          { 'Environment Variables Defined by the Ruby Buildpack' => '/buildpacks/ruby/ruby-environment.md' },
                                                                                          { 'Configure Service Connections for Ruby' => '/buildpacks/ruby/ruby-service-bindings.md' },
                                                                                          { 'Support for Windows Gemfiles' => '/buildpacks/ruby/windows.md' }] },
                                                                             { 'Staticfile' => '/buildpacks/staticfile/index.md' },
                                                                             { 'Customizing and Developing Buildpacks' => [{ 'Home' => '/buildpacks/developing-buildpacks.md' },
                                                                                                                           { 'Creating Custom Buildpacks' => '/buildpacks/custom.md' },
                                                                                                                           { 'Packaging Dependencies for Offline Buildpacks' => '/buildpacks/depend-pkg-offline.md' },
                                                                                                                           { 'Merging from Upstream Buildpacks' => '/buildpacks/merging_upstream.md' },
                                                                                                                           { 'Upgrading Dependency Versions' => '/buildpacks/upgrading_dependency_versions.md' },
                                                                                                                           { 'Using CI for Buildpacks' => [{ 'Home' => '/buildpacks/buildpack-ci-index.md' },
                                                                                                                                                           { 'Releasing a New Buildpack Version' => '/buildpacks/releasing_a_new_buildpack_version.md' },
                                                                                                                                                           { 'Updating Buildpack-Related Gems' => '/buildpacks/updating-buildpack-related-gems.md' }] }] }] }] },
                       { 'Information for Managed Service Authors' => [{ 'Custom Services' => '/services/' },
                                                                       { 'Overview' => '/services/overview.md' },
                                                                       { 'Service Broker API' => [{ 'Open Service Broker API' => 'https://github.com/openservicebrokerapi/servicebroker/blob/v2.13/spec.md' },
                                                                                                  { 'Platform Profiles' => 'https://github.com/openservicebrokerapi/servicebroker/blob/v2.13/profile.md' },
                                                                                                  { 'Catalog Metadata' => 'https://github.com/openservicebrokerapi/servicebroker/blob/v2.13/profile.md#service-metadata' },
                                                                                                  { 'Volume Services' => 'https://github.com/openservicebrokerapi/servicebroker/blob/v2.13/spec.md#volume-mounts-object' },
                                                                                                  { 'Release Notes' => 'https://github.com/openservicebrokerapi/servicebroker/blob/v2.13/release-notes.md' }] },
                                                                       { 'Managing Service Brokers' => '/services/managing-service-brokers.md' },
                                                                       { 'Access Control' => '/services/access-control.md' },
                                                                       { 'Binding Credentials' => '/services/binding-credentials.md' },
                                                                       { 'CredHub' => [{ 'Home' => '/credhub/index.md' },
                                                                                       { 'Setting Up and Deploying CredHub with BOSH' => '/credhub/setup-credhub-bosh.md' },
                                                                                       { 'Configuring a Hardware Security Module' => '/credhub/hsm-config.md' },
                                                                                       { 'CredHub Credential Types' => '/credhub/credential-types.md' },
                                                                                       { 'Backing Up and Restoring CredHub Instances' => '/credhub/backup-restore.md' },
                                                                                       { 'Troubleshooting CredHub' => '/credhub/troubleshooting-credhub.md' }] },
                                                                       { 'Dashboard Single Sign-On' => '/services/dashboard-sso.md' },
                                                                       { 'Enabling Service Instance Sharing' => '/services/enable-sharing.md' },
                                                                       { 'Example Service Brokers' => '/services/examples.md' },
                                                                       { 'Application Log Streaming' => '/services/app-log-streaming.md' },
                                                                       { 'Route Services' => '/services/route-services.md' },
                                                                       { 'Supporting Multiple Cloud Foundry Instances' => '/services/supporting-multiple-cf-instances.md' }] },
                       { 'API Reference' => [{ 'UAA API' => 'https://docs.cloudfoundry.org/api/uaa/index.md' },
                                             { 'CAPI API' => [{ 'Client Libraries' => '/devguide/capi/client-libraries.md' },
                                                              { 'Rate Limit Information Returned by the Cloud Controller API' => '/running/rate-limit-cloud-controller-api.md' },
                                                              { 'CAPI V2' => 'http://apidocs.cloudfoundry.org' },
                                                              { 'CAPI V3' => 'http://v3-apidocs.cloudfoundry.org/index.md' }] }] }])
  end
end
