# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Gql::Mutations::System::Setup::Lock, :aggregate_failures, type: :graphql do
  context 'when locking system setup' do
    let(:mutation) do
      <<~MUTATION
        mutation systemSetupLock($ttl: Int) {
          systemSetupLock(ttl: $ttl) {
            resource
            value
            errors {
              message
              field
            }
          }
        }
      MUTATION
    end

    let(:resource)  { 'Zammad::System::Setup' }
    let(:value)     { SecureRandom.uuid }
    let(:ttl)       { 1 }
    let(:lock_info) { { resource: resource, value: value } }
    let(:variables) { { ttl: ttl } }

    it 'returns lock info' do
      allow_any_instance_of(Redlock::Client).to receive(:lock).with(resource, ttl).and_return(lock_info)

      gql.execute(mutation, variables: variables)
      expect(gql.result.data).to include({ 'resource' => resource, 'value' => value })
    end

    context 'when system setup is already done' do
      before do
        Setting.set('system_init_done', true)
        create(:admin)
      end

      it 'raises error' do
        gql.execute(mutation, variables: variables)
        expect(gql.result.error_type).to eq(Service::System::CheckSetup::SystemSetupError)
        expect(gql.result.error_message).to eq('The system setup cannot be started, because there is another one running or it was completed before.')
      end
    end

    context 'when system setup is already locked' do
      it 'raises error' do
        allow_any_instance_of(Redlock::Client).to receive(:locked?).with(resource).and_return(true)

        gql.execute(mutation, variables: variables)
        expect(gql.result.error_type).to eq(Service::System::CheckSetup::SystemSetupError)
        expect(gql.result.error_message).to eq('The system setup cannot be started, because there is another one running or it was completed before.')
      end
    end
  end
end
