Skip to content

Refactor getBillingPeriodInDays to support all values #6169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ import dagger.SingleInstanceIn
import java.io.IOException
import java.time.Duration
import java.time.Instant
import java.time.Period
import java.time.format.DateTimeParseException
import javax.inject.Inject
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.CoroutineScope
Expand All @@ -95,6 +97,7 @@ import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import logcat.LogPriority.ERROR
import logcat.asLog
import logcat.logcat
import retrofit2.HttpException

Expand Down Expand Up @@ -1105,13 +1108,15 @@ data class PricingPhase(
val billingPeriod: String,

) {
internal fun getBillingPeriodInDays(): Int? =
when (billingPeriod) {
"P1W" -> 7
"P1M" -> 30
"P1Y" -> 365
else -> null
internal fun getBillingPeriodInDays(): Int? {
return try {
val period = Period.parse(billingPeriod)
return period.days + period.months * 30 + period.years * 365
} catch (e: DateTimeParseException) {
logcat { "Subs: Failed to parse billing period \"$billingPeriod\": ${e.asLog()}" }
null
}
}
}

data class ValidatedTokenPair(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2025 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.subscriptions.impl.billing

import com.duckduckgo.subscriptions.impl.PricingPhase
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNull
import org.junit.Test

class PricingPhaseTest {
@Test
fun billingPeriodParsesDaysCorrectly() {
val phase = PricingPhase(formattedPrice = "Free", billingPeriod = "P10D")
assertEquals(10, phase.getBillingPeriodInDays())
}

@Test
fun billingPeriodParsesWeeksCorrectly() {
val phase = PricingPhase(formattedPrice = "Free", billingPeriod = "P2W")
assertEquals(14, phase.getBillingPeriodInDays())
}

@Test
fun billingPeriodParsesMonthsCorrectly() {
val phase = PricingPhase(formattedPrice = "Free", billingPeriod = "P2M")
assertEquals(60, phase.getBillingPeriodInDays())
}

@Test
fun billingPeriodParsesYearsCorrectly() {
val phase = PricingPhase(formattedPrice = "Free", billingPeriod = "P1Y")
assertEquals(365, phase.getBillingPeriodInDays())
}

@Test
fun billingPeriodParsesMixedPeriodCorrectly() {
val phase = PricingPhase(formattedPrice = "Free", billingPeriod = "P1Y2M10D")
val expectedDays = 1 * 365 + 2 * 30 + 10 // 365 + 60 + 10 = 435
assertEquals(expectedDays, phase.getBillingPeriodInDays())
}

@Test
fun billingPeriodEmptyReturnsNull() {
val phase = PricingPhase(formattedPrice = "$0", billingPeriod = "")
assertNull(phase.getBillingPeriodInDays())
}

@Test
fun billingPeriodReturnsNullForInvalidFormat() {
val phase = PricingPhase(formattedPrice = "Free", billingPeriod = "INVALID")
assertNull(phase.getBillingPeriodInDays())
}
}
Loading