# frozen_string_literal: true

# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE
require 'rails_helper'

Rails.application.load_tasks

describe 'i18n_js.rake' do # rubocop:disable RSpec/DescribeClass
	def system!(*args)
		system(*args) || abort("\n== Command #{args} failed ==")
	end

	let(:locales_dir) { Rails.root.join('app/javascript/i18n/locales') }

	def wrap_task(rake_taskname)
		task = Rake::Task[rake_taskname]
		Rake.application.tasks.each(&:reenable)
		task.invoke
		yield
		Rake.application.tasks.each(&:reenable)
	end

	def verify_cleaned(rake_taskname)
		wrap_task(rake_taskname) do
			expect(!Dir.exist?(locales_dir) || Dir[locales_dir].none?).to(
				be(true),
				"#{locales_dir} wasn't cleaned by bin/rails #{rake_taskname}"
			)
		end
	end

	def verify_task_generate(rake_taskname)
		wrap_task(rake_taskname) do
			expect(Dir.exist?(locales_dir) && Dir[locales_dir].any?).to(
				be(true),
				"#{locales_dir} wasn't generated by bin/rails #{rake_taskname}"
			)
		end
	end

	def verify_generate(command_line)
		system!(command_line)
		expect(Dir.exist?(locales_dir) && Dir[locales_dir].any?).to(
			be(true),
			"#{locales_dir} wasn't generated by #{command_line}"
		)
	end

	it 'export and clean' do # rubocop:disable RSpec/NoExpectationExample
		verify_task_generate('i18n:js:export')
		verify_cleaned('webpacker:clean')
		verify_task_generate('webpacker:compile')
		verify_cleaned('assets:clobber')
		verify_task_generate('i18n:js:export')
		verify_cleaned('i18n:js:clean')
		verify_generate('bin/webpack')
	end
end
