Added games cache: skip bad records, expire names on read, retry failed saves

A record whose fields could not be decoded threw while the cache loaded,
and since every scan loads it first, one bad record blocked every scan;
each record is now decoded on its own and skipped if it is malformed.

name() returned names older than a week that were still in memory; it now
applies the same age limit as rank().

save() cleared its dirty flag before writing and called startWrite outside
its try, so a write that failed was never retried and could replace the
scan's result with an exception; a failed write now leaves the cache dirty
and is tried again on the next save.
This commit is contained in:
K11MCH1 authored and Kurt Himebauch committed 2026-11-05 13:59:59 -05:00
1 parent 93923e125d
commit df60fdb194
2 files changed
+49 -10

No files matched your search

@@ -74,6 +74,43 @@ class AddedGamesCacheTest {
assertNull(kept.name("unknown"))
}
@Test fun aMalformedRecordIsSkippedAndTheOthersStillLoad() {
val (folder, exe) = game()
val (other, _) = game("Other")
val now = System.currentTimeMillis()
val good = JSONObject().put("modified", folder.lastModified()).put("at", now)
.put("exes", JSONArray(listOf(exe.path))).put("scores", JSONArray(listOf(72)))
val bad = JSONObject().put("modified", other.lastModified()).put("at", now)
.put("exes", JSONArray(listOf("bin/Other.exe"))).put("scores", JSONArray(listOf("bad")))
store().writeText(JSONObject()
.put("ranks", JSONObject().put(folder.path, good).put(other.path, bad))
.put("names", JSONObject().put("broken", "not a record").put("named", JSONObject().put("name", "Bastion").put("at", now)))
.toString())
val kept = AddedGamesCache(store())
assertEquals(listOf(exe), kept.rank(folder)?.exes)
assertNull(kept.rank(other))
assertEquals("Bastion", kept.name("named"))
assertNull(kept.name("broken"))
}
@Test fun aNameOlderThanAWeekIsIdentifiedAgain() {
store().writeText(JSONObject().put("names", JSONObject().put("named", JSONObject().put("name", "Bastion")
.put("at", System.currentTimeMillis() - AddedGamesCache.KEEP_MS - 1))).toString())
assertNull(AddedGamesCache(store()).name("named"))
}
@Test fun aSaveThatCannotBeWrittenIsTriedAgainNextTime() {
val blocked = File(tmp.root, "blocked").apply { writeText("a file where the cache folder should be") }
val file = File(blocked, "scan.json")
val cache = AddedGamesCache(file).apply { keepName("named", "Bastion") }
cache.save()
assertFalse(file.exists())
blocked.delete()
blocked.mkdirs()
cache.save()
assertEquals("Bastion", AddedGamesCache(file).name("named"))
}
@Test fun aScanKeepsEachGamesRankingForTheNextLaunch() {
val (folder, exe) = game("Card Game")
AddedExes.put(app, AddedExes.Entry(folder.path, exe.path, picked = false))