diff --git a/AGENTS.md b/AGENTS.md index 34ebedfa..c3ee27d3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -978,6 +978,15 @@ Two more rules that came out of the same failures: boxes and the bundle passcode groups both advance when full, and Espresso's per-character typing fails the moment the field it started on stops being focused. It is also the case people actually hit: a code is pasted far more often than typed. +- **Never seed a timestamp as an offset from now.** `AMapWithTagsOnIt` seeded every tag at "two + hours ago", with a comment saying that was comfortably inside today. It is, between 02:00 and + 22:00. Outside that it is yesterday, the history screen asks for one local day at a time, and + sees nothing: `DebugModeReachesEveryScreenTest` failed at 00:14 UTC with *"no history row was + built at all"* while the other 858 tests passed. That is a 22-minute rerun with no fault to + find, and it has happened on `main` and on a contributor's PR. **`TimesToday.at(i)`** anchors + seeded times to local midnight, and any timestamp a screen filters by wants the same + treatment. `TimesTodayTest` pins the midnight, end-of-day, first-minute and foreign-zone + cases, and 5 of its 6 go red if the offset comes back. #### `animationsDisabled` does not disable animations diff --git a/app/src/androidTest/java/dev/wander/android/opentagviewer/TheWholeAppJourneyTest.java b/app/src/androidTest/java/dev/wander/android/opentagviewer/TheWholeAppJourneyTest.java index 7ad3f18b..dc5a6d84 100644 --- a/app/src/androidTest/java/dev/wander/android/opentagviewer/TheWholeAppJourneyTest.java +++ b/app/src/androidTest/java/dev/wander/android/opentagviewer/TheWholeAppJourneyTest.java @@ -40,8 +40,7 @@ import org.junit.Test; import org.junit.runner.RunWith; -import java.time.Instant; -import java.time.ZoneId; +import dev.wander.android.opentagviewer.util.TimesToday; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; @@ -213,7 +212,7 @@ public void signOutAndReplaceTheOutsideWorld() { *

Absolute times, not offsets. The history screen asks for one local day at * a time, so "two hours ago" falls on yesterday for anybody running the suite shortly after * midnight - a test that is green all day and red at 00:30 for reasons that have nothing to - * do with the app. The day boundary is computed here the same way the screen computes it. + * do with the app. {@link TimesToday} anchors them to the day boundary the screen uses. */ private void givenTheAccountHasReportedToday() { if (!Python.isStarted()) { @@ -224,7 +223,6 @@ private void givenTheAccountHasReportedToday() { this.appleDouble.callAttr("install"); this.appleDouble.callAttr("clearReports"); - final long startOfToday = startOfTodayLocal(); for (int i = 0; i < REPORTS_TODAY; i++) { // A minute apart, and only minutes into the day, so every one of them is in the // past however early the suite runs. @@ -235,21 +233,11 @@ private void givenTheAccountHasReportedToday() { // could have been drawn through any of them, or one of them, and looked the same. // A few hundred metres apart makes the drawing say what it did. this.appleDouble.callAttr("reportAt", - startOfToday + ((i + 1) * 60_000L), + TimesToday.at(i), LATITUDE + (i * STEP_LATITUDE), LONGITUDE + (i * STEP_LONGITUDE)); } } - /** Midnight this morning, by the same clock {@code HistoryViewActivity} uses. */ - private static long startOfTodayLocal() { - return Instant.ofEpochMilli(System.currentTimeMillis()) - .atZone(ZoneId.systemDefault()) - .toLocalDate() - .atStartOfDay(ZoneId.systemDefault()) - .toInstant() - .toEpochMilli(); - } - @After public void putEverythingBack() { if (this.scenario != null) { diff --git a/app/src/androidTest/java/dev/wander/android/opentagviewer/ui/maps/AMapWithTagsOnIt.java b/app/src/androidTest/java/dev/wander/android/opentagviewer/ui/maps/AMapWithTagsOnIt.java index fea197ce..0a1c9a72 100644 --- a/app/src/androidTest/java/dev/wander/android/opentagviewer/ui/maps/AMapWithTagsOnIt.java +++ b/app/src/androidTest/java/dev/wander/android/opentagviewer/ui/maps/AMapWithTagsOnIt.java @@ -37,6 +37,7 @@ import dev.wander.android.opentagviewer.db.room.entity.OwnedBeacon; import dev.wander.android.opentagviewer.anisette.FakeAnisetteSource; import dev.wander.android.opentagviewer.python.AppDependencies; +import dev.wander.android.opentagviewer.util.TimesToday; import dev.wander.android.opentagviewer.util.android.AppCryptographyUtil; import dev.wander.android.opentagviewer.util.rx.RefreshPolicy; @@ -179,14 +180,13 @@ public AMapWithTagsOnIt seed(final String... names) { + "") .build()); - // **Reported a couple of hours ago, not on a fixed date in 2023.** The timestamp - // decides which day the report belongs to, and the history screen asks for one - // local day at a time - so a fixed past date gives every screen built on this - // fixture an empty "today", which reads as the history being broken. - // - // Two hours rather than minutes, so it is comfortably inside today whatever hour - // the suite runs at, and inside every window the app asks for. - final long reportedAt = System.currentTimeMillis() - (2L * 60 * 60 * 1000); + // **On today's local date, not a fixed date in 2023 and not "two hours ago".** + // The timestamp decides which day the report belongs to, and the history screen + // asks for one local day at a time - so a fixed past date gives every screen built + // on this fixture an empty "today", which reads as the history being broken. Two + // hours ago did the same thing between 00:00 and 02:00, and this fixture was red on + // CI twice that way. See TimesToday. + final long reportedAt = TimesToday.at(i); // Spread apart, so "which card is centred" has a different answer per tag. this.db.locationReportDao().insertAll(LocationReport.builder() diff --git a/app/src/main/java/dev/wander/android/opentagviewer/util/TimesToday.java b/app/src/main/java/dev/wander/android/opentagviewer/util/TimesToday.java new file mode 100644 index 00000000..de9052f5 --- /dev/null +++ b/app/src/main/java/dev/wander/android/opentagviewer/util/TimesToday.java @@ -0,0 +1,53 @@ +package dev.wander.android.opentagviewer.util; + +import java.time.Instant; +import java.time.ZoneId; + +/** + * A timestamp on today's local date, a few minutes in, for seeded test data. + * + *

Anchored to local midnight rather than counted back from now. The history screen + * asks for one local day at a time, so a report seeded at "two hours ago" belongs to yesterday + * for anybody running the suite between 00:00 and 02:00 - green all day, and empty-handed at + * half past midnight for reasons that have nothing to do with the app under test. That happened + * on CI, twice, and cost a 22-minute rerun each time. + * + *

Minutes rather than hours, so a report seeded this way is always on today. And clamped to + * {@code now}, because in the first minutes of the day today's clock has not got as far as the + * offset yet and a report dated in the future is its own kind of wrong. + * + *

Plain Java with no Android types, so the JVM suite can pin the boundary cases (rule 13). It + * lives in the app's source set rather than beside the instrumentation tests because the JVM + * suite cannot compile against that one. + */ +public final class TimesToday { + + private static final long MINUTE = 60_000L; + + private TimesToday() { + } + + /** + * A distinct timestamp on today's local date, for item {@code index} of a seeded batch. + * + * @param index which item; each one is a minute later than the one before + */ + public static long at(final int index) { + return at(index, System.currentTimeMillis(), ZoneId.systemDefault()); + } + + /** @see #at(int) */ + public static long at(final int index, final long nowMillis, final ZoneId zone) { + return Math.min(nowMillis, startOfToday(nowMillis, zone) + ((index + 1) * MINUTE)); + } + + /** Midnight this morning, by the clock the history screen uses. */ + public static long startOfToday(final long nowMillis, final ZoneId zone) { + return Instant.ofEpochMilli(nowMillis) + .atZone(zone) + .toLocalDate() + .atStartOfDay(zone) + .toInstant() + .toEpochMilli(); + } +} diff --git a/app/src/test/java/dev/wander/android/opentagviewer/util/TimesTodayTest.java b/app/src/test/java/dev/wander/android/opentagviewer/util/TimesTodayTest.java new file mode 100644 index 00000000..91be2d99 --- /dev/null +++ b/app/src/test/java/dev/wander/android/opentagviewer/util/TimesTodayTest.java @@ -0,0 +1,97 @@ +package dev.wander.android.opentagviewer.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * Seeded report times must land on today, whatever hour the suite runs at. + * + *

They used to be "now minus two hours", which is yesterday for anybody running between 00:00 + * and 02:00 - and the history screen asks for one local day at a time, so it showed nothing. + * Green all day, red at midnight, twice on CI. Pure arithmetic with the clock passed in, so JVM + * (rule 13). + */ +public class TimesTodayTest { + + private static final ZoneId UTC = ZoneId.of("UTC"); + + private static long at(final LocalDateTime local, final ZoneId zone) { + return local.atZone(zone).toInstant().toEpochMilli(); + } + + private static LocalDate day(final long millis, final ZoneId zone) { + return Instant.ofEpochMilli(millis).atZone(zone).toLocalDate(); + } + + @Test + public void aMidafternoonRunGetsAReportFromEarlierToday() { + final long now = at(LocalDateTime.of(2026, 11, 24, 15, 30), UTC); + + final long report = TimesToday.at(0, now, UTC); + + assertEquals(LocalDate.of(2026, 11, 24), day(report, UTC)); + assertTrue("not in the past", report < now); + assertEquals("a minute into the day", TimesToday.startOfToday(now, UTC) + 60_000L, report); + } + + /** The regression: this is the window the fixture was red in. */ + @Test + public void aRunJustAfterMidnightStillGetsTodaysDate() { + final long now = at(LocalDateTime.of(2026, 11, 24, 0, 30), UTC); + + final long report = TimesToday.at(0, now, UTC); + + assertEquals(LocalDate.of(2026, 11, 24), day(report, UTC)); + assertTrue("not in the past", report < now); + } + + @Test + public void aRunJustBeforeMidnightGetsTheDayThatIsEnding() { + final long now = at(LocalDateTime.of(2026, 11, 24, 23, 59, 59), UTC); + + final long report = TimesToday.at(0, now, UTC); + + assertEquals(LocalDate.of(2026, 11, 24), day(report, UTC)); + assertTrue("not in the past", report < now); + } + + /** In the first minute of the day the offset has not happened yet, so now is all there is. */ + @Test + public void inTheFirstMinuteItDoesNotDateAnythingInTheFuture() { + final long now = at(LocalDateTime.of(2026, 11, 24, 0, 0, 10), UTC); + + final long report = TimesToday.at(0, now, UTC); + + assertEquals(now, report); + assertEquals(LocalDate.of(2026, 11, 24), day(report, UTC)); + } + + @Test + public void eachItemInABatchIsASeparateMoment() { + final long now = at(LocalDateTime.of(2026, 11, 24, 9, 0), UTC); + + assertEquals(TimesToday.at(0, now, UTC) + 60_000L, TimesToday.at(1, now, UTC)); + assertEquals(TimesToday.at(1, now, UTC) + 60_000L, TimesToday.at(2, now, UTC)); + } + + /** The day boundary is the local one, not UTC's, whatever the device is set to. */ + @Test + public void itUsesTheZoneItIsGivenNotUtc() { + final ZoneId berlin = ZoneId.of("Europe/Berlin"); + // 23:30 UTC is already the 25th in Berlin, and its midnight is 23:00 UTC. + final long now = at(LocalDateTime.of(2026, 11, 24, 23, 30), UTC); + + final long report = TimesToday.at(0, now, berlin); + + assertEquals(LocalDate.of(2026, 11, 25), day(report, berlin)); + assertEquals(day(now, berlin), day(report, berlin)); + assertTrue("not in the past", report < now); + } +}