|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require_relative "shared_examples" |
| 5 | + |
| 6 | +module SOF |
| 7 | + RSpec.describe Cycles::Interval, type: :value do |
| 8 | + subject(:cycle) { Cycle.for(notation) } |
| 9 | + |
| 10 | + let(:notation) { "V1I24MF#{from_date}" } |
| 11 | + let(:anchor) { nil } |
| 12 | + |
| 13 | + let(:end_date) { from_date + 24.months } |
| 14 | + let(:from_date) { "2026-03-31".to_date } |
| 15 | + |
| 16 | + let(:completed_dates) { [] } |
| 17 | + |
| 18 | + it_behaves_like "#kind returns", :interval |
| 19 | + it_behaves_like "#valid_periods are", %w[D W M Y] |
| 20 | + |
| 21 | + describe "#recurring?" do |
| 22 | + it "repeats" do |
| 23 | + expect(cycle).to be_recurring |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + @from = "2026-03-31".to_date.to_fs(:american) |
| 28 | + it_behaves_like "#to_s returns", |
| 29 | + "1x every 24 months from #{@from}" |
| 30 | + |
| 31 | + context "when the cycle is dormant" do |
| 32 | + before { allow(cycle.parser).to receive(:dormant?).and_return(true) } |
| 33 | + |
| 34 | + it_behaves_like "#to_s returns", |
| 35 | + "1x every 24 months" |
| 36 | + end |
| 37 | + |
| 38 | + it_behaves_like "#volume returns the volume" |
| 39 | + it_behaves_like "#notation returns the notation" |
| 40 | + it_behaves_like "#as_json returns the notation" |
| 41 | + it_behaves_like "it computes #final_date(given)", |
| 42 | + given: nil, returns: "2026-03-31".to_date + 24.months |
| 43 | + it_behaves_like "it cannot be extended" |
| 44 | + |
| 45 | + describe "#last_completed" do |
| 46 | + context "with an activated cycle" do |
| 47 | + it_behaves_like "last_completed is", :from_date |
| 48 | + end |
| 49 | + |
| 50 | + context "with a dormant cycle" do |
| 51 | + let(:notation) { "V1I24M" } |
| 52 | + |
| 53 | + it "returns nil" do |
| 54 | + expect(cycle.last_completed).to be_nil |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe "#final_date" do |
| 60 | + it "returns from_date + period without end-of-month rounding" do |
| 61 | + expect(cycle.final_date).to eq "2028-03-31".to_date |
| 62 | + end |
| 63 | + |
| 64 | + context "with a mid-month from_date" do |
| 65 | + let(:from_date) { "2026-06-15".to_date } |
| 66 | + |
| 67 | + it "preserves the exact day" do |
| 68 | + expect(cycle.final_date).to eq "2028-06-15".to_date |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "#covered_dates" do |
| 74 | + let(:completed_dates) do |
| 75 | + [ |
| 76 | + within_window, |
| 77 | + just_before_end, |
| 78 | + too_early_date, |
| 79 | + too_late_date |
| 80 | + ] |
| 81 | + end |
| 82 | + let(:within_window) { from_date + 6.months } |
| 83 | + let(:just_before_end) { end_date - 1.day } |
| 84 | + let(:too_early_date) { from_date - 1.day } |
| 85 | + let(:too_late_date) { end_date + 1.day } |
| 86 | + |
| 87 | + let(:anchor) { from_date + 1.year } |
| 88 | + |
| 89 | + it "returns dates that fall within the window" do |
| 90 | + expect(cycle.covered_dates(completed_dates, anchor:)).to eq([ |
| 91 | + within_window, |
| 92 | + just_before_end |
| 93 | + ]) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe "#satisfied_by?(anchor:)" do |
| 98 | + context "when the anchor date is < the final date" do |
| 99 | + let(:anchor) { "2028-03-30".to_date } |
| 100 | + |
| 101 | + it "returns true" do |
| 102 | + expect(cycle).to be_satisfied_by(anchor:) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context "when the anchor date is = the final date" do |
| 107 | + let(:anchor) { "2028-03-31".to_date } |
| 108 | + |
| 109 | + it "returns true" do |
| 110 | + expect(cycle).to be_satisfied_by(anchor:) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context "when the anchor date is > the final date" do |
| 115 | + let(:anchor) { "2028-04-01".to_date } |
| 116 | + |
| 117 | + it "returns false" do |
| 118 | + expect(cycle).not_to be_satisfied_by(completed_dates, anchor:) |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + describe "#expiration_of" do |
| 124 | + it "returns the final date" do |
| 125 | + expect(cycle.expiration_of).to eq "2028-03-31".to_date |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + describe "dormant behavior" do |
| 130 | + let(:notation) { "V1I24M" } |
| 131 | + |
| 132 | + it "is dormant without a from_date" do |
| 133 | + expect(cycle).to be_dormant |
| 134 | + end |
| 135 | + |
| 136 | + it "returns nil for final_date" do |
| 137 | + expect(cycle.final_date).to be_nil |
| 138 | + end |
| 139 | + |
| 140 | + it "returns nil for expiration_of" do |
| 141 | + expect(cycle.expiration_of).to be_nil |
| 142 | + end |
| 143 | + |
| 144 | + it "returns false for satisfied_by?" do |
| 145 | + expect(cycle).not_to be_satisfied_by(anchor: Date.current) |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe "activation" do |
| 150 | + let(:notation) { "V1I24M" } |
| 151 | + |
| 152 | + it "can be activated with a date" do |
| 153 | + activated = Cycle.for(cycle.parser.activated_notation("2026-03-31".to_date)) |
| 154 | + expect(activated.notation).to eq "V1I24MF2026-03-31" |
| 155 | + expect(activated).not_to be_dormant |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments