Report a rate-limited bank sync as a wait, and name real failures

Two of three banks were only pacing us, yet the dashboard demanded attention,
printed four nested wrappers and a nanosecond UTC deadline, and the scheduler
retried hourly into a refusal whose end time the bank had already given.

A rate limit now carries its retry time as data: Status.SyncRetryAt is set when
every failure is self-clearing, the connection reports rate_limited with that
deadline, the dashboard says synchronization resumes by itself and renders the
time in the browser's zone, and the scheduler sleeps until the deadline instead
of spending hourly session checks. Sync now still tries immediately.

The third bank's "transaction retrieval failed" hid its cause. Provider
failures Finance Duck determines itself are typed as banking.ProviderError,
so an unreachable provider, a timeout or an unusable response, such as a booked
transaction without a booking date, is reported instead of the opaque fallback.
Provider response text still never reaches the message.
This commit is contained in:
Lars Nolden
2026-09-11 18:41:35 +02:00
parent dece0d5b79
commit b3e1c65a82
12 changed files with 305 additions and 52 deletions
+83 -1
View File
@@ -273,7 +273,7 @@ func TestSyncMissingMembershipStillRejectsAccount(t *testing.T) {
}
func TestSyncTransactionFailuresPreserveProgressAndSafeErrors(t *testing.T) {
for _, scenario := range []string{"rate limit", "background rate limit", "reconnect", "private response"} {
for _, scenario := range []string{"rate limit", "background rate limit", "reconnect", "private response", "unusable response"} {
t.Run(scenario, func(t *testing.T) {
a, s, b := backfillApp(t)
s = seed(t, a, s)
@@ -292,6 +292,9 @@ func TestSyncTransactionFailuresPreserveProgressAndSafeErrors(t *testing.T) {
b.fetchErr = fmt.Errorf("private provider response: %w", banking.ErrReconnect)
case "private response":
b.fetchErr = errors.New("private provider response")
case "unusable response":
unusable := &banking.ProviderError{Detail: "a booked transaction has no valid booking date"}
b.fetchErr = fmt.Errorf("private provider response: %w", unusable)
}
failed, err := a.Sync(context.Background())
if err != nil {
@@ -313,6 +316,43 @@ func TestSyncTransactionFailuresPreserveProgressAndSafeErrors(t *testing.T) {
if !reflect.DeepEqual(s.Data, failed.Data) || !reflect.DeepEqual(cursors, a.ops.AccountSync) || a.ops.LastSync != old {
t.Fatal("failed retrieval imported partial data or advanced synchronization")
}
rateLimited := strings.Contains(scenario, "rate limit")
// A bank that named its own retry time is a wait, not a fault: the
// UI and the scheduler both rely on this distinction.
if (failed.Status.SyncRetryAt != "") != rateLimited {
t.Fatalf("waiting state is wrong for %s: retry at %q", scenario, failed.Status.SyncRetryAt)
}
for _, connection := range failed.Connections {
if connection.Status == "local" {
continue
}
want := "error"
switch {
case rateLimited:
want = "rate_limited"
case scenario == "reconnect":
want = "reconnect_required"
}
if connection.Status != want || (connection.RetryAt != "") != rateLimited {
t.Fatalf("connection reported %q with retry %q, want %q", connection.Status, connection.RetryAt, want)
}
}
if rateLimited {
at, e := time.Parse(time.RFC3339, failed.Status.SyncRetryAt)
if e != nil || !at.After(time.Now()) {
t.Fatalf("unusable retry deadline %q: %v", failed.Status.SyncRetryAt, e)
}
// Whole seconds, once: operators read this message.
if !strings.Contains(meta.Error, at.UTC().Format(time.RFC3339)) || strings.Count(meta.Error, "429") != 1 {
t.Fatalf("rate limit message is not legible: %q", meta.Error)
}
}
if scenario == "unusable response" && (!strings.Contains(meta.Error, "cannot use") || !strings.Contains(meta.Error, "booking date")) {
t.Fatalf("unusable provider response was reduced to an opaque failure: %q", meta.Error)
}
if scenario == "private response" && !strings.Contains(meta.Error, "transaction retrieval failed") {
t.Fatalf("unrecognized failure lost its fallback: %q", meta.Error)
}
b.fetchErr = nil
recovered, err := a.Sync(context.Background())
if err != nil || recovered.Status.SyncError != "" || a.ops.Consents["current"].Error != "" || a.ops.Consents["current"].NeedsReconnect {
@@ -321,3 +361,45 @@ func TestSyncTransactionFailuresPreserveProgressAndSafeErrors(t *testing.T) {
})
}
}
// The scheduler must not spend session-status calls on a refusal the bank has
// already scheduled, and must not sit on a deadline that has passed.
func TestSyncSchedulingRespectsTheBanksOwnRetryTime(t *testing.T) {
// Persisted deadlines carry whole seconds; compare against the same grid.
now := time.Now().UTC().Truncate(time.Second)
stale := now.Add(-48 * time.Hour).Format(time.RFC3339)
waiting := operational{LastSync: stale, SyncError: "N26: rate limited", SyncRetryAt: now.Add(3 * time.Hour).Format(time.RFC3339)}
broken := operational{LastSync: stale, SyncError: "Trade Republic: retrieval failed"}
healthy := operational{LastSync: now.Format(time.RFC3339)}
elapsed := operational{LastSync: stale, SyncError: waiting.SyncError, SyncRetryAt: now.Add(-time.Minute).Format(time.RFC3339)}
cases := []struct {
name string
ops operational
force bool
wait time.Duration
due bool
}{
{"waiting for the bank", waiting, false, time.Hour, false},
{"manual sync during a wait", waiting, true, 0, true},
{"deadline elapsed", elapsed, false, 0, true},
{"failure without a deadline", broken, false, 0, true},
{"recent success", healthy, false, time.Minute, false},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
wait, due := syncSchedule(now, tt.ops, tt.force)
if wait != tt.wait || due != tt.due {
t.Fatalf("schedule = (%s, %t), want (%s, %t)", wait, due, tt.wait, tt.due)
}
})
}
if backoff := syncBackoff(now, waiting); backoff != 3*time.Hour+time.Minute {
t.Fatalf("rate-limited backoff = %s, want the bank's own deadline", backoff)
}
if backoff := syncBackoff(now, broken); backoff != time.Hour {
t.Fatalf("failure backoff = %s, want hourly retries", backoff)
}
if backoff := syncBackoff(now, healthy); backoff != 24*time.Hour {
t.Fatalf("successful backoff = %s, want daily synchronization", backoff)
}
}