package com.example.aiapp import java.time.Duration import kotlin.test.Test import kotlin.test.assertEquals /** * The two ways a span of time is written here, and the rule each of them follows. * * Both are read off a screen to make a decision -- how long a tool call may take, how long a quota * has left -- so what matters is that the shortest form that answers the question is what appears. */ class DurationsTest { @Test fun `under a minute is the largest unit alone`() { assertEquals("30ms", formatMillis(30)) assertEquals("999ms", formatMillis(999)) assertEquals("1s", formatMillis(1000)) assertEquals("2.5s", formatMillis(2500)) // One decimal, rounded rather than cut: 2.46s is nearer two and a half than two and four. assertEquals("2.5s", formatMillis(2460)) assertEquals("59.9s", formatMillis(59_900)) } @Test fun `a minute or more is every unit that has something in it`() { // The figure this rule was written for: a tool timeout, which arrives as milliseconds and // is unreadable as 480000. assertEquals("8m", formatMillis(480_000)) assertEquals("1m", formatMillis(60_000)) assertEquals("1m 30s", formatMillis(90_000)) assertEquals("5d 12h 4m", formatMillis(475_440_000)) // Empty units are left out rather than written as zero: the labels say which is which, // and "5d 0h 4m" is only longer. assertEquals("5d 4m", formatMillis(432_240_000)) } @Test fun `only a whole number of milliseconds is rewritten`() { assertEquals("8m", formatMillisText(" 480000 ")) // A timeout a tool expressed some other way is its own words, passed through rather than // guessed at. assertEquals("2 minutes", formatMillisText("2 minutes")) assertEquals("", formatMillisText("")) } @Test fun `a countdown rounds up, so it never reports a minute already spent`() { assertEquals("3h 13m", formatSpan(Duration.ofMinutes(192).plusSeconds(50))) // Exactly on a minute is already the answer and is not pushed past it. assertEquals("3h 12m", formatSpan(Duration.ofMinutes(192))) assertEquals("12m", formatSpan(Duration.ofMinutes(12))) // Rounding up carries, so a day's worth of minutes reads as a day. assertEquals("1d 0h", formatSpan(Duration.ofHours(23).plusMinutes(59).plusSeconds(30))) } }