fix: look for new mail when a push names changes it can't read #34

The native push router and the push task decided separately whether a
push may carry mail, and disagreed. For an account entry that is not
an object, or a map that names no type, the router started the mail
task as possible mail, but the task's guard read the same payload as
"no mail" and returned, so new mail it announced was never looked for.
A push without a kind was judged by its map in the router but taken
for mail by the task.

Both now apply the same rules: anything but a StateChange, a map that
is missing, unreadable or names no type, an unreadable account entry,
or a mail type means the mail task runs. Only a readable StateChange
naming contact, calendar or other non-mail types skips it.

Checked with one table of payloads run against the router's decision
and the task's guard alike.
This commit is contained in:
Linus Rath
2026-09-27 10:20:55 +02:00
parent 41e2bdf922
commit eebb00238c
5 changed files with 128 additions and 15 deletions
@@ -108,6 +108,56 @@ class PushRoutingTest {
assertEquals(setOf("alice@example.org" to contacts), decision.syncs)
}
@Test
fun `reads a push the same way as the push task`() {
// kind (null: none), changed (null: none) → whether the mail task starts. The
// same table as MAIL_CASES in src/lib/__tests__/push-background-task.test.ts,
// whose carriesNoMail must agree.
val state = "jmap-state-change"
val emailPush = "jmap-email-push"
val cases: List<Triple<String?, String?, Boolean>> = listOf(
// No readable map: as before device sync.
Triple(state, null, true),
Triple(state, "", true),
Triple(state, "not json", true),
Triple(state, "[1]", true),
Triple(state, "{}", true),
// A map that names no type is read like a missing one.
Triple(state, """{"c":{}}""", true),
Triple(state, """{"c":{},"team":{}}""", true),
// An account entry that can't be read may be mail.
Triple(state, """{"c":null}""", true),
Triple(state, """{"c":"garbled","team":{"ContactCard":"s"}}""", true),
Triple(state, """{"c":[],"team":{"ContactCard":"s"}}""", true),
Triple(state, """{"c":5}""", true),
// Mail types anywhere in the map.
Triple(state, """{"c":{"EmailDelivery":"s"}}""", true),
Triple(state, """{"c":{"Email":"s"}}""", true),
Triple(state, """{"c":{"Mailbox":"s"}}""", true),
Triple(state, """{"c":{"ContactCard":"s1"},"team":{"EmailDelivery":"s2"}}""", true),
// Only a StateChange can say it carries no mail.
Triple(null, """{"c":{"ContactCard":"s"}}""", true),
Triple(emailPush, """{"c":{"EmailDelivery":"s"}}""", true),
Triple(emailPush, null, true),
// Contact and calendar changes, and types that say nothing about mail.
Triple(state, """{"c":{"ContactCard":"s1","AddressBook":"s2"}}""", false),
Triple(state, """{"c":{"CalendarEvent":"e1"},"team":{"Calendar":"c1"}}""", false),
Triple(state, """{"c":{"ContactCard":"s"},"team":{}}""", false),
Triple(state, """{"elsewhere":{"ContactCard":"s"}}""", false),
Triple(state, """{"c":{"Thread":"t1","ContactCard":"s1"}}""", false),
)
for ((kind, changed, mail) in cases) {
val data = buildMap<String, String> {
if (kind != null) put("kind", kind)
put("accountLabel", "alice")
put("accountId", "c")
put("emailIds", "[]")
if (changed != null) put("changed", changed)
}
assertEquals("kind=$kind changed=$changed", mail, PushRouting.decide(data, routes).startMailTask)
}
}
@Test
fun `accounts without routes are ignored`() {
val decision = PushRouting.decide(stateChange("""{"elsewhere":{"ContactCard":"s"}}"""), routes)