|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class MissionControl::Jobs::InterfaceHelperTest < ActionView::TestCase |
| 4 | + include MissionControl::Jobs::InterfaceHelper |
| 5 | + |
| 6 | + test "jobs_count_for returns formatted count for finite numbers" do |
| 7 | + mock_with_status = Minitest::Mock.new |
| 8 | + mock_count = Minitest::Mock.new |
| 9 | + |
| 10 | + ActiveJob.stub :jobs, mock_with_status do |
| 11 | + mock_with_status.expect :with_status, mock_count, [ :failed ] |
| 12 | + mock_count.expect :count, 1000 |
| 13 | + |
| 14 | + result = jobs_count_for(:failed) |
| 15 | + assert_equal "1K", result |
| 16 | + |
| 17 | + mock_with_status.verify |
| 18 | + mock_count.verify |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + test "jobs_count_for returns formatted count with plus suffix for infinite count" do |
| 23 | + mock_with_status = Minitest::Mock.new |
| 24 | + mock_count = Minitest::Mock.new |
| 25 | + |
| 26 | + ActiveJob.stub :jobs, mock_with_status do |
| 27 | + mock_with_status.expect :with_status, mock_count, [ :failed ] |
| 28 | + mock_count.expect :count, Float::INFINITY |
| 29 | + |
| 30 | + result = jobs_count_for(:failed) |
| 31 | + expected = number_to_human(MissionControl::Jobs.count_limit, |
| 32 | + format: "%n%u", |
| 33 | + units: { thousand: "K", million: "M", billion: "B", trillion: "T", quadrillion: "Q" }) + "+" |
| 34 | + |
| 35 | + assert_equal expected, result |
| 36 | + |
| 37 | + mock_with_status.verify |
| 38 | + mock_count.verify |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + test "jobs_count_for works with different statuses" do |
| 43 | + statuses = [ :pending, :finished, :failed, :blocked, :scheduled, :in_progress ] |
| 44 | + |
| 45 | + statuses.each do |status| |
| 46 | + mock_with_status = Minitest::Mock.new |
| 47 | + mock_count = Minitest::Mock.new |
| 48 | + |
| 49 | + ActiveJob.stub :jobs, mock_with_status do |
| 50 | + mock_with_status.expect :with_status, mock_count, [ status ] |
| 51 | + mock_count.expect :count, 1000 |
| 52 | + |
| 53 | + result = jobs_count_for(status) |
| 54 | + assert_equal "1K", result |
| 55 | + |
| 56 | + mock_with_status.verify |
| 57 | + mock_count.verify |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + test "jobs_count_for handles zero count" do |
| 63 | + mock_with_status = Minitest::Mock.new |
| 64 | + mock_count = Minitest::Mock.new |
| 65 | + |
| 66 | + ActiveJob.stub :jobs, mock_with_status do |
| 67 | + mock_with_status.expect :with_status, mock_count, [ :pending ] |
| 68 | + mock_count.expect :count, 0 |
| 69 | + |
| 70 | + result = jobs_count_for(:pending) |
| 71 | + assert_equal "0", result |
| 72 | + |
| 73 | + mock_with_status.verify |
| 74 | + mock_count.verify |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + test "jobs_count_for handles small numbers without abbreviation" do |
| 79 | + mock_with_status = Minitest::Mock.new |
| 80 | + mock_count = Minitest::Mock.new |
| 81 | + |
| 82 | + ActiveJob.stub :jobs, mock_with_status do |
| 83 | + mock_with_status.expect :with_status, mock_count, [ :pending ] |
| 84 | + mock_count.expect :count, 500 |
| 85 | + |
| 86 | + result = jobs_count_for(:pending) |
| 87 | + assert_equal "500", result |
| 88 | + |
| 89 | + mock_with_status.verify |
| 90 | + mock_count.verify |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + test "jobs_count_for handles large numbers with proper formatting" do |
| 95 | + test_cases = [ |
| 96 | + [ 1500, "1.5K" ], |
| 97 | + [ 2500000, "2.5M" ], |
| 98 | + [ 1000000000, "1B" ], |
| 99 | + [ 1000000000000, "1T" ], |
| 100 | + [ 1000000000000000, "1Q" ] |
| 101 | + ] |
| 102 | + |
| 103 | + test_cases.each do |count, expected| |
| 104 | + mock_with_status = Minitest::Mock.new |
| 105 | + mock_count = Minitest::Mock.new |
| 106 | + |
| 107 | + ActiveJob.stub :jobs, mock_with_status do |
| 108 | + mock_with_status.expect :with_status, mock_count, [ :finished ] |
| 109 | + mock_count.expect :count, count |
| 110 | + |
| 111 | + result = jobs_count_for(:finished) |
| 112 | + assert_equal expected, result |
| 113 | + |
| 114 | + mock_with_status.verify |
| 115 | + mock_count.verify |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments