#The MIT License (MIT) # #Copyright (c) 2026 rick barrette # #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require File.expand_path('../test_helper', __dir__) class RunnerTest < ActiveSupport::TestCase include QboBonusTestHelper # Stands in for QuickBooks class FakeWriter attr_reader :created def initialize(fail_for: []) @created = [] @fail_for = fail_for end def create(line, customer:, email: nil) raise 'QuickBooks is down' if @fail_for.include?(line.technician.user_id) @created << [line.technician.user_id, line.amount, email] memo = Quickbooks::Model::CreditMemo.new memo.id = (700 + @created.size).to_s memo.doc_number = (1000 + @created.size).to_s memo end end def setup User.current = User.find(1) RedmineQboBonus::Settings.stubs(:item).returns(Struct.new(:id, :name).new('31', 'Tech Bonus')) Customer.any_instance.stubs(:email).returns('tech@example.com') RedmineQboBonus::CreditMemoWriter.stubs(:send_email).returns(true) @tech = create_technician(2) @foreman = create_technician(3, role: 'foreman') log_time(2, 60) log_time(3, 50) log_time(1, 16) # 126 hours: 1 tier end def run_bonus(attended: [2, 3], writer: FakeWriter.new) runner = RedmineQboBonus::Runner.new(bonus_week, attended_user_ids: attended, writer: writer) [runner.run, writer] end def test_credits_and_emails_each_technician week, writer = run_bonus assert_equal [[2, BigDecimal('200'), 'tech@example.com'], [3, BigDecimal('300'), 'tech@example.com']], writer.created.sort_by(&:first) assert_equal BigDecimal('126'), week.total_hours assert_equal 1, week.tiers assert_equal BigDecimal('16'), week.hours_by_user[1] credit = week.credits.find_by(user_id: 3) assert_equal 'emailed', credit.status assert_equal 'tech@example.com', credit.emailed_to assert credit.credited? assert_equal BigDecimal('500'), week.credited_total end def test_missed_shift_is_skipped week, writer = run_bonus(attended: [2]) assert_equal [2], writer.created.map(&:first) credit = week.credits.find_by(user_id: 3) assert_equal 'skipped', credit.status assert_equal BigDecimal('0'), credit.amount end def test_running_again_never_credits_twice run_bonus(attended: [2]) week, writer = run_bonus(attended: [2, 3]) assert_equal [3], writer.created.map(&:first) assert_equal 2, week.credits.count(&:credited?) end def test_a_failure_is_recorded_and_retried week, = run_bonus(writer: FakeWriter.new(fail_for: [3])) credit = week.credits.find_by(user_id: 3) assert_equal 'failed', credit.status assert_equal 'QuickBooks is down', credit.message week, writer = run_bonus assert_equal [3], writer.created.map(&:first) assert_equal 'emailed', week.credits.find_by(user_id: 3).status end def test_no_email_keeps_it_created Customer.any_instance.stubs(:email).returns(nil) week, = run_bonus credit = week.credits.find_by(user_id: 2) assert_equal 'created', credit.status assert_match(/no email/, credit.message) end def test_not_emailed_when_turned_off RedmineQboBonus::CreditMemoWriter.expects(:send_email).never with_bonus_settings('email_credits' => '0') do week, = run_bonus assert_equal %w[created created], week.credits.map(&:status) end end def test_goal_not_met_records_the_week_only TimeEntry.where(user_id: 3).delete_all # 76 hours week, writer = run_bonus assert_empty writer.created assert_not week.goal_met? assert_empty week.credits end def test_needs_a_product_or_service RedmineQboBonus::Settings.stubs(:item).returns(nil) assert_raises(RedmineQboBonus::Runner::Error) { run_bonus } assert_equal 0, TechBonusWeek.count end def test_only_finished_weeks runner = RedmineQboBonus::Runner.new(RedmineQboBonus::Week.containing(Date.current), attended_user_ids: [2], writer: FakeWriter.new) assert_raises(RedmineQboBonus::Runner::Error) { runner.run } end end