|
| 1 | +package to.bitkit.ext |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import to.bitkit.env.Env |
| 5 | +import to.bitkit.test.BaseUnitTest |
| 6 | +import java.util.Locale |
| 7 | +import java.util.concurrent.TimeUnit |
| 8 | +import kotlin.test.assertEquals |
| 9 | +import kotlin.test.assertNotNull |
| 10 | +import kotlin.test.assertTrue |
| 11 | + |
| 12 | +class DateTimeExtTest : BaseUnitTest() { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun `toRelativeTimeString returns now for very recent timestamps`() { |
| 16 | + val now = System.currentTimeMillis() |
| 17 | + val result = now.toRelativeTimeString() |
| 18 | + // May return "now" or absolute timestamp as fallback |
| 19 | + assertTrue(result.isNotEmpty()) |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `toRelativeTimeString returns minutes ago for recent timestamps`() { |
| 24 | + val fiveMinutesAgo = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5) |
| 25 | + val result = fiveMinutesAgo.toRelativeTimeString() |
| 26 | + // May return relative "minute" or absolute timestamp as fallback |
| 27 | + assertTrue(result.isNotEmpty()) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `toRelativeTimeString returns hours ago for timestamps within a day`() { |
| 32 | + val twoHoursAgo = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(2) |
| 33 | + val result = twoHoursAgo.toRelativeTimeString() |
| 34 | + // May return relative "hour" or absolute timestamp as fallback |
| 35 | + assertTrue(result.isNotEmpty()) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `toRelativeTimeString returns yesterday for one day ago`() { |
| 40 | + val oneDayAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1) |
| 41 | + val result = oneDayAgo.toRelativeTimeString() |
| 42 | + // May return relative "yesterday"/"day" or absolute timestamp as fallback |
| 43 | + assertTrue(result.isNotEmpty()) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `toRelativeTimeString returns days ago for multiple days`() { |
| 48 | + val threeDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(3) |
| 49 | + val result = threeDaysAgo.toRelativeTimeString() |
| 50 | + // May return relative "day" or absolute timestamp as fallback |
| 51 | + assertTrue(result.isNotEmpty()) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `toRelativeTimeString returns weeks ago for multiple weeks`() { |
| 56 | + val twoWeeksAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(14) |
| 57 | + val result = twoWeeksAgo.toRelativeTimeString() |
| 58 | + // May return relative "week" or absolute timestamp as fallback |
| 59 | + assertTrue(result.isNotEmpty()) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `toRelativeTimeString returns months ago for multiple months`() { |
| 64 | + val twoMonthsAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(60) |
| 65 | + val result = twoMonthsAgo.toRelativeTimeString() |
| 66 | + // May return relative "month" or absolute timestamp as fallback |
| 67 | + assertTrue(result.isNotEmpty()) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `toRelativeTimeString returns years ago for multiple years`() { |
| 72 | + val twoYearsAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(730) |
| 73 | + val result = twoYearsAgo.toRelativeTimeString() |
| 74 | + // May return relative "year" or absolute timestamp as fallback |
| 75 | + assertTrue(result.isNotEmpty()) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `toRelativeTimeString handles future timestamps gracefully`() { |
| 80 | + val future = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1) |
| 81 | + val result = future.toRelativeTimeString() |
| 82 | + // May return relative "in" or absolute timestamp as fallback |
| 83 | + assertTrue(result.isNotEmpty()) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `toRelativeTimeString supports all configured locales`() { |
| 88 | + val twoDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2) |
| 89 | + |
| 90 | + Env.locales.forEach { languageTag -> |
| 91 | + val locale = Locale.forLanguageTag(languageTag) |
| 92 | + val result = twoDaysAgo.toRelativeTimeString(locale) |
| 93 | + |
| 94 | + assertNotNull(result, "Locale $languageTag returned null") |
| 95 | + assertTrue(result.isNotEmpty(), "Locale $languageTag returned empty string") |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun `toRelativeTimeString with explicit English locale produces expected output`() { |
| 101 | + val twoDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2) |
| 102 | + val result = twoDaysAgo.toRelativeTimeString(Locale.ENGLISH) |
| 103 | + |
| 104 | + // May return relative "day" or absolute timestamp as fallback |
| 105 | + assertTrue(result.isNotEmpty()) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `toRelativeTimeString with explicit German locale produces non-empty output`() { |
| 110 | + val twoDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2) |
| 111 | + val result = twoDaysAgo.toRelativeTimeString(Locale.GERMAN) |
| 112 | + |
| 113 | + assertTrue(result.isNotEmpty()) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `toRelativeTimeString with explicit French locale produces non-empty output`() { |
| 118 | + val twoDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2) |
| 119 | + val result = twoDaysAgo.toRelativeTimeString(Locale.FRENCH) |
| 120 | + |
| 121 | + assertTrue(result.isNotEmpty()) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `toRelativeTimeString with explicit Italian locale produces non-empty output`() { |
| 126 | + val twoDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2) |
| 127 | + val result = twoDaysAgo.toRelativeTimeString(Locale.ITALIAN) |
| 128 | + |
| 129 | + assertTrue(result.isNotEmpty()) |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun `toRelativeTimeString preserves backward compatibility with default locale`() { |
| 134 | + val twoDaysAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2) |
| 135 | + val resultWithoutParam = twoDaysAgo.toRelativeTimeString() |
| 136 | + val resultWithDefaultParam = twoDaysAgo.toRelativeTimeString(Locale.getDefault()) |
| 137 | + |
| 138 | + assertEquals(resultWithDefaultParam, resultWithoutParam) |
| 139 | + } |
| 140 | +} |
0 commit comments