# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Wombat::Services::Base do
  subject(:service_a) do
    Wombat::Services::ServiceA.new(
      config: Wombat::Service::Config::Pack.new(
        definition: {
          version: '0.1.1',
          dependencies: [{ name: 'dummy', version: '0.3.0' }]
        },
        builder: OpenStruct.new(docker: {}),
        settings: {}
      ),
      ui: spec_ui,
      registry: registry,
      dependency_handler: dependency_handler
    ).start
  end

  let(:dependency_handler) { Wombat::Utils::DependencyHandler.new }
  let(:registry) { Wombat::Service::Manager::Registry.new }

  before(:all) do
    Wombat::Services::Dummy = Class.new(described_class)
    load fixtures_path.join('services', 'service_a', 'joey', 'main.rb')
  end

  after(:all) do
    Wombat::Services.send(:remove_const, :Dummy)
    Wombat::Services.send(:remove_const, :ServiceA)
  end

  example '#service_name' do
    expect(service_a.send(:service_name)).to eq 'service_a'
  end

  example '#service_config' do
    expect(service_a.definition[:version]).to eq '0.1.1'
  end

  example '#settings' do
    service_a.config.settings[:test] = 'lorem ipsum'
    expect(service_a.settings).to eq(test: 'lorem ipsum')
  end

  example '#builder.docker' do
    service_a.config.builder.docker[:test] = 'lorem ipsum'
    expect(service_a.builder.docker[:test]).to eq('lorem ipsum')
  end

  example '#definition' do
    service_a.config.definition[:test] = 'lorem ipsum'
    expect(service_a.definition[:test]).to eq('lorem ipsum')
  end

  example '#joey' do
    service_a.config.settings[:test] = 'lorem ipsum joey'
    expect(service_a.service(:service_a).settings[:test]).to eq 'lorem ipsum joey'
  end

  context '#build_files' do
    before do
      service_a.settings[:test] = 'I live... again!'
      service_a.send(:build_files)
    end

    example 'Dockerfile' do
      expect(File.read(service_a.tmp_path.join('Dockerfile'))).to include('RUN echo "I live... again!"')
    end

    example 'templates/config' do
      expect(File.read(service_a.tmp_path.join('templates', 'config'))).to include('test = "I live... again!"')
    end
  end
end
