File size: 5,868 Bytes
16cdcb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package patch_test

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/openmeterio/openmeter/openmeter/subscription"
	"github.com/openmeterio/openmeter/openmeter/subscription/patch"
	"github.com/openmeterio/openmeter/openmeter/testutils"
	"github.com/openmeterio/openmeter/pkg/clock"
	"github.com/openmeterio/openmeter/pkg/datetime"
)

func TestStretchPhase(t *testing.T) {
	now := testutils.GetRFC3339Time(t, "2021-01-01T00:00:01Z")
	clock.SetTime(now)

	s, p := getDefaultSpec(t, now)

	getSpec := func(_ *testing.T) subscription.SubscriptionSpec {
		phases := make(map[string]*subscription.SubscriptionPhaseSpec)

		for k, v := range s.Phases {
			vCopy := *v
			phases[k] = &vCopy
		}

		s2 := *s
		s2.Phases = phases

		return s2
	}

	tests := testsuite[patch.PatchStretchPhase]{
		SystemTime: now,
		TT: []testcase[patch.PatchStretchPhase]{
			{
				Name: "Should extend first phase by 1 Month",
				Patch: patch.PatchStretchPhase{
					PhaseKey: "test_phase_1",
					Duration: datetime.MustParseDuration(t, "P1M"),
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s := getSpec(t)

					// Let's validate the spec looks as we expect
					require.GreaterOrEqual(t, len(s.Phases), 2)
					require.Equal(t, "test_phase_1", p.Phases[0].Key)

					_, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					p2, ok := s.Phases["test_phase_2"]
					require.True(t, ok)

					// The second phase should start after 1 month
					require.Equal(t, p2.StartAfter, datetime.NewISODuration(0, 1, 0, 0, 0, 0, 0))

					return &s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now,
				},
				GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec {
					s := getSpec(t)

					for k := range s.Phases {
						if k != "test_phase_1" {
							nSA, err := s.Phases[k].StartAfter.Add(datetime.MustParseDuration(t, "P1M"))
							require.NoError(t, err)

							s.Phases[k].StartAfter = nSA
						}
					}

					return s
				},
			},
			{
				Name: "Should shrink first phase by 2 weeks",
				Patch: patch.PatchStretchPhase{
					PhaseKey: "test_phase_1",
					Duration: datetime.MustParseDuration(t, "-P2W"),
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s := getSpec(t)

					// Let's validate the spec looks as we expect
					require.GreaterOrEqual(t, len(s.Phases), 2)
					require.Equal(t, "test_phase_1", p.Phases[0].Key)

					_, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					p2, ok := s.Phases["test_phase_2"]
					require.True(t, ok)

					// The second phase should start after 1 month
					require.Equal(t, p2.StartAfter, datetime.NewISODuration(0, 1, 0, 0, 0, 0, 0))

					return &s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now,
				},
				GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec {
					s := getSpec(t)

					for k := range s.Phases {
						if k != "test_phase_1" {
							nSA, err := s.Phases[k].StartAfter.Subtract(datetime.MustParseDuration(t, "P2W"))
							require.NoError(t, err)

							s.Phases[k].StartAfter = nSA
						}
					}

					return s
				},
			},
			{
				Name: "Should not allow stretching if there's a single phase",
				Patch: patch.PatchStretchPhase{
					PhaseKey: "test_phase_1",
					Duration: datetime.MustParseDuration(t, "P1M"),
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s := getSpec(t)

					// Let's validate the spec looks as we expect
					require.GreaterOrEqual(t, len(s.Phases), 2)
					require.Equal(t, "test_phase_1", p.Phases[0].Key)

					_, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					delete(s.Phases, "test_phase_2")
					delete(s.Phases, "test_phase_3")

					return &s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now,
				},
				ExpectedError: &subscription.PatchConflictError{Msg: "cannot stretch a single phase"},
			},
			{
				Name: "Should not allow stretching more than phase length",
				Patch: patch.PatchStretchPhase{
					PhaseKey: "test_phase_1",
					Duration: datetime.MustParseDuration(t, "-P1M"),
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s := getSpec(t)

					// Let's validate the spec looks as we expect
					require.GreaterOrEqual(t, len(s.Phases), 2)
					require.Equal(t, "test_phase_1", p.Phases[0].Key)

					_, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					return &s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now,
				},
				ExpectedError: &subscription.PatchConflictError{Msg: "phase test_phase_1 would disappear due to stretching"},
			},
			{
				Name: "Should work when stretching past next phase",
				Patch: patch.PatchStretchPhase{
					PhaseKey: "test_phase_1",
					Duration: datetime.MustParseDuration(t, "P5M"),
				},
				GetSpec: func(t *testing.T) *subscription.SubscriptionSpec {
					s := getSpec(t)

					// Let's validate the spec looks as we expect
					require.GreaterOrEqual(t, len(s.Phases), 2)
					require.Equal(t, "test_phase_1", p.Phases[0].Key)

					_, ok := s.Phases["test_phase_1"]
					require.True(t, ok)

					return &s
				},
				GetExpectedSpec: func(t *testing.T) subscription.SubscriptionSpec {
					s := getSpec(t)

					for k := range s.Phases {
						if k != "test_phase_1" {
							nSA, err := s.Phases[k].StartAfter.Add(datetime.MustParseDuration(t, "P5M"))
							require.NoError(t, err)

							s.Phases[k].StartAfter = nSA
						}
					}

					return s
				},
				Ctx: subscription.ApplyContext{
					CurrentTime: now,
				},
			},
		},
	}

	tests.Run(t)
}

func TestASD(t *testing.T) {
	p1 := datetime.MustParseDuration(t, "P1M")
	p2 := datetime.MustParseDuration(t, "P2W")
	p3 := datetime.MustParseDuration(t, "-P2W")

	r1, err := p1.Subtract(p2)
	require.Nil(t, err)

	r2, err := p1.Add(p3)
	require.Nil(t, err)

	require.Equal(t, r1, r2)
}