taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 6c69e44c31b9d1597c9b83e1dfa9880dfbd1943a
parent ab13290253adfc66cd2884fab6141062a6863e80
Author: Florian Dold <dold@taler.net>
Date:   Tue, 17 Nov 2026 23:20:42 +0100

web wallet: show renewal notices for the selected balance

Keep the last report on screen if updates stop, and let the user retry.

Diffstat:
Mpackages/taler-util/src/types-taler-wallet.ts | 2--
Mpackages/wallet-webui/src/api/notifications.ts | 4++--
Mpackages/wallet-webui/src/api/observations.ts | 177++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mpackages/wallet-webui/src/platform/extension-background.ts | 6++++++
Mpackages/wallet-webui/src/platform/observation-owner.ts | 16++++++++++++++--
Mpackages/wallet-webui/src/routes/App.tsx | 15+++++++++++++--
Mpackages/wallet-webui/src/routes/balance-model.ts | 23++++++++++++++++++-----
Mpackages/wallet-webui/src/screens/BalanceScreen.tsx | 11+++++++++++
Mpackages/wallet-webui/src/testing/demo-wallet.ts | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mpackages/wallet-webui/test/notifications.test.ts | 4++--
Mpackages/wallet-webui/test/observations-hook.test.tsx | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 372 insertions(+), 79 deletions(-)

diff --git a/packages/taler-util/src/types-taler-wallet.ts b/packages/taler-util/src/types-taler-wallet.ts @@ -476,8 +476,6 @@ export interface WalletRefreshInfo { export interface WalletBalance { /** True when this scope contains funds at risk of expiration. */ hasExpirationRisk?: boolean; - /** Kept for clients that also support earlier core versions. */ - refreshInfo?: WalletRefreshInfo; scopeInfo: ScopeInfo; available: AmountString; pendingIncoming: AmountString; diff --git a/packages/wallet-webui/src/api/notifications.ts b/packages/wallet-webui/src/api/notifications.ts @@ -50,9 +50,9 @@ export function notificationFamilies( case NotificationType.BalanceChange: return ["balances", "transactions", "tokens"]; case NotificationType.TransactionStateTransition: - return ["balances", "transactions", "tokens"]; + return ["transactions", "tokens"]; case NotificationType.ExchangeStateTransition: - return ["balances", "transactions", "exchanges"]; + return ["transactions", "exchanges"]; case NotificationType.BankAccountChange: return ["accounts"]; case NotificationType.ContactAdded: diff --git a/packages/wallet-webui/src/api/observations.ts b/packages/wallet-webui/src/api/observations.ts @@ -7,6 +7,10 @@ import { NotificationType, type TransactionSnapshot, + type ObservationSnapshotNotification, + type ScopeInfo, + type WalletRefreshInfo, + stringifyScopeInfo, TransactionType, type ObservedPaymentTransaction, type MerchantContractTerms, @@ -17,16 +21,16 @@ import { useEffect, useRef, useState } from "preact/hooks"; import type { WalletConnection } from "./contracts.js"; import { i18n } from "../i18n/runtime.js"; -export type TransactionObservationState = - | { status: "loading"; snapshot?: TransactionSnapshot } - | { status: "ready"; snapshot: TransactionSnapshot } - | { status: "disconnected"; snapshot?: TransactionSnapshot } - | { status: "failed"; error: Error; snapshot?: TransactionSnapshot } +type ObservationState<T> = + | { status: "loading"; snapshot?: T } + | { status: "ready"; snapshot: T } + | { status: "disconnected"; snapshot?: T } + | { status: "failed"; error: Error; snapshot?: T } | { status: "deleted" }; /** Owns one UI subscription; registration acknowledgements never carry state. */ -export class TransactionObservation { - state: TransactionObservationState = { status: "loading" }; +class WalletObservation<T> { + state: ObservationState<T> = { status: "loading" }; private id?: string; private revision = 0; private connected = false; @@ -36,8 +40,10 @@ export class TransactionObservation { constructor( private connection: WalletConnection, - private transactionId: TransactionIdStr, - private changed: (state: TransactionObservationState) => void, + private register: (id: string) => Promise<unknown>, + select: (n: ObservationSnapshotNotification) => T | null | undefined, + fallbackError: string, + private changed: (state: ObservationState<T>) => void, private newId: () => string = () => crypto.randomUUID(), ) { this.unsubscribeNotifications = connection.subscribeNotifications((n) => { @@ -48,32 +54,23 @@ export class TransactionObservation { n.observationId !== this.id ) return; - if ( - n.type === NotificationType.ObservationSnapshot && - n.observationType === "transaction" - ) { - if ( - n.transactionId !== this.transactionId || - n.revision <= this.revision - ) - return; + if (n.type === NotificationType.ObservationSnapshot) { + if (n.revision <= this.revision) return; + const snapshot = select(n); + if (snapshot === undefined) return; this.revision = n.revision; + if (snapshot === null) this.id = undefined; this.update( - n.snapshot === null + snapshot === null ? { status: "deleted" } - : { status: "ready", snapshot: n.snapshot }, + : { status: "ready", snapshot }, ); - if (n.snapshot === null) this.id = undefined; } else if (n.type === NotificationType.ObservationError) { this.id = undefined; this.update({ status: "failed", snapshot: this.snapshot(), - error: new Error( - n.error.hint ?? - n.error.message ?? - i18n.str`Transaction updates stopped.`, - ), + error: new Error(n.error.hint ?? n.error.message ?? fallbackError), }); } }); @@ -94,7 +91,7 @@ export class TransactionObservation { return this.state.status === "deleted" ? undefined : this.state.snapshot; } - private update(state: TransactionObservationState) { + private update(state: ObservationState<T>) { this.state = state; if (!this.disposed) this.changed(state); } @@ -115,11 +112,7 @@ export class TransactionObservation { const id = (this.id = this.newId()); this.revision = 0; this.update({ status: "loading", snapshot: this.snapshot() }); - void this.connection.client - .call(WalletApiOperation.ObserveTransaction, { - transactionId: this.transactionId, - observationId: id, - }) + void this.register(id) .then(() => { // stop may have arrived before a delayed registration was installed. if (this.disposed || this.id !== id) { @@ -147,6 +140,33 @@ export class TransactionObservation { } } +export type TransactionObservationState = ObservationState<TransactionSnapshot>; + +export class TransactionObservation extends WalletObservation<TransactionSnapshot> { + constructor( + connection: WalletConnection, + transactionId: TransactionIdStr, + changed: (state: TransactionObservationState) => void, + newId?: () => string, + ) { + super( + connection, + (observationId) => + connection.client.call(WalletApiOperation.ObserveTransaction, { + transactionId, + observationId, + }), + (n) => + n.observationType === "transaction" && n.transactionId === transactionId + ? n.snapshot + : undefined, + i18n.str`Transaction updates stopped.`, + changed, + newId, + ); + } +} + export interface TransactionObservationView { state: TransactionObservationState; snapshot?: TransactionSnapshot; @@ -161,29 +181,12 @@ export function useTransactionObservation( connection: WalletConnection, transactionId: TransactionIdStr | undefined, ): TransactionObservationView { - const [value, setValue] = useState<{ - connection: WalletConnection; - transactionId: TransactionIdStr; - state: TransactionObservationState; - }>(); - const controller = useRef<TransactionObservation>(); - useEffect(() => { - if (!transactionId) return; - const instance = new TransactionObservation( - connection, - transactionId, - (state) => setValue({ connection, transactionId, state }), - ); - controller.current = instance; - return () => { - instance.dispose(); - controller.current = undefined; - }; - }, [connection, transactionId]); - const state: TransactionObservationState = - value?.connection === connection && value.transactionId === transactionId - ? value.state - : { status: "loading" }; + const { state, retry } = useObservation<TransactionSnapshot>( + connection, + transactionId, + (changed) => + new TransactionObservation(connection, transactionId!, changed), + ); const snapshot = "snapshot" in state ? state.snapshot : undefined; const error = state.status === "failed" @@ -200,7 +203,7 @@ export function useTransactionObservation( ready: state.status === "ready", loading: state.status === "loading" && !snapshot, canRetry: state.status === "failed", - retry: () => controller.current?.retry(), + retry, }; } @@ -308,3 +311,65 @@ export function usePaymentContract( retry: () => setDocumentAttempt((n) => n + 1), }; } + +/** The key identifies the registration parameters across renders. */ +function useObservation<T>( + connection: WalletConnection, + key: string | undefined, + create: ( + changed: (state: ObservationState<T>) => void, + ) => WalletObservation<T>, +) { + const [value, setValue] = useState<{ + connection: WalletConnection; + key: string; + state: ObservationState<T>; + }>(); + const controller = useRef<WalletObservation<T>>(); + useEffect(() => { + if (!key) return; + const instance = create((state) => setValue({ connection, key, state })); + controller.current = instance; + return () => { + instance.dispose(); + controller.current = undefined; + }; + }, [connection, key]); + const state: ObservationState<T> = + value?.connection === connection && value.key === key + ? value.state + : { status: "loading" }; + return { state, retry: () => controller.current?.retry() }; +} + +export function useBalanceRefreshInfo( + connection: WalletConnection, + scope: ScopeInfo | undefined, +) { + const key = scope && stringifyScopeInfo(scope); + const { state, retry } = useObservation<WalletRefreshInfo>( + connection, + key, + (changed) => + new WalletObservation( + connection, + (observationId) => + connection.client.call(WalletApiOperation.ObserveBalanceRefreshInfo, { + observationId, + scopeInfo: scope!, + }), + (n) => + n.observationType === "balance-refresh-info" && + stringifyScopeInfo(n.scopeInfo) === key + ? n.snapshot + : undefined, + i18n.str`Balances could not be loaded.`, + changed, + ), + ); + return { + snapshot: "snapshot" in state ? state.snapshot : undefined, + error: state.status === "failed" ? state.error : undefined, + retry, + }; +} diff --git a/packages/wallet-webui/src/platform/extension-background.ts b/packages/wallet-webui/src/platform/extension-background.ts @@ -65,6 +65,12 @@ function sendToPort( function broadcast(message: BrowserRpcMessage): void { for (const port of ports) { + if ( + message.kind === "notification" && + "observationId" in message.notification && + !observationOwners.get(port)?.owns(message.notification.observationId) + ) + continue; sendToPort(port, message); } } diff --git a/packages/wallet-webui/src/platform/observation-owner.ts b/packages/wallet-webui/src/platform/observation-owner.ts @@ -14,6 +14,11 @@ import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; export class ObservationOwner { private ids = new Set<string>(); private closed = false; + private registering = new Set<string>(); + + owns(id: string): boolean { + return this.ids.has(id) || this.registering.has(id); + } constructor( private dispatch: ( request: CoreApiRequestEnvelope, @@ -25,9 +30,16 @@ export class ObservationOwner { const id = typeof args?.observationId === "string" ? args.observationId : undefined; const registering = - request.operation === WalletApiOperation.ObserveTransaction && + (request.operation === WalletApiOperation.ObserveTransaction || + request.operation === WalletApiOperation.ObserveBalanceRefreshInfo) && id !== undefined; - const response = await this.dispatch(request); + if (registering) this.registering.add(id); + let response: CoreApiResponse; + try { + response = await this.dispatch(request); + } finally { + if (registering) this.registering.delete(id); + } if (registering && response.type !== "error") { if (this.closed) this.stop(id); else this.ids.add(id); diff --git a/packages/wallet-webui/src/routes/App.tsx b/packages/wallet-webui/src/routes/App.tsx @@ -2,6 +2,7 @@ import { usePaymentActions, type PaymentActions } from "./payment-actions.js"; import { peerCreditConfirmationView } from "./peer-credit-review-model.js"; import { useTransactionObservation, + useBalanceRefreshInfo, usePaymentContract, type TransactionObservationView, type PaymentContractView, @@ -721,6 +722,14 @@ function BalanceRoute() { ); } }; + const report = useBalanceRefreshInfo(connection, selectedBalance?.scopeInfo); + const displayedBalances = + selectedBalance && report.snapshot + ? balancesToView(query.data, { + scopeId: selectedBalance.scopeId, + info: report.snapshot, + }).balances + : view.balances; const activitySupported = selectedBalance?.scopeInfo.type === ScopeType.Global || selectedBalance?.scopeInfo.type === ScopeType.Exchange || @@ -803,14 +812,16 @@ function BalanceRoute() { }; return ( <BalanceScreen + renewalError={report.error} + onRetryRenewal={report.retry} onDismissRenewal={(warningId) => { void callMutation( WalletApiOperation.DismissWalletWarning, { warningId }, - ["balances"], + [], ); }} - balances={view.balances} + balances={displayedBalances} selectedScopeId={selectedBalance?.scopeId} trend={balanceTrendView( selectedBalance, diff --git a/packages/wallet-webui/src/routes/balance-model.ts b/packages/wallet-webui/src/routes/balance-model.ts @@ -3,6 +3,7 @@ import type { DonauSummaryItem, Transaction, WalletBalance, + WalletRefreshInfo, CashExpirationRisk, } from "@gnu-taler/taler-util"; import { @@ -83,7 +84,10 @@ function refreshDate(stamp: { t_s: number | "never" }): string { }).format(new Date(stamp.t_s * 1000)); } -function balanceView(balance: WalletBalance): BalanceView { +function balanceView( + balance: WalletBalance, + refresh?: WalletRefreshInfo, +): BalanceView { const amount = Amounts.parseOrThrow(balance.available); let provider: string | undefined; switch (balance.scopeInfo.type) { @@ -110,8 +114,7 @@ function balanceView(balance: WalletBalance): BalanceView { if (balance.flags.includes(BalanceFlag.OutgoingKyc)) { warnings.push(i18n.str`Outgoing funds need identity verification`); } - const refresh = balance.refreshInfo; - if (refresh?.risks.length) + if (balance.hasExpirationRisk) warnings.unshift(i18n.str`Some coins are close to expiration`); return { ...(refresh @@ -337,12 +340,22 @@ export function balanceTrendView( return points; } -export function balancesToView(response: BalancesResponse | undefined): { +export function balancesToView( + response: BalancesResponse | undefined, + details?: { scopeId: string; info: WalletRefreshInfo }, +): { balances?: BalanceView[]; donauSummaries: DonauSummaryView[]; } { return { - balances: response?.balances.map(balanceView), + balances: response?.balances.map((b) => + balanceView( + b, + details && scopeIdentity(b.scopeInfo) === details.scopeId + ? details.info + : undefined, + ), + ), donauSummaries: (response?.donauSummary ?? []) .map(donauSummaryView) .sort( diff --git a/packages/wallet-webui/src/screens/BalanceScreen.tsx b/packages/wallet-webui/src/screens/BalanceScreen.tsx @@ -697,6 +697,8 @@ export function BalanceScreen(props: { activityLoading?: boolean; error?: ErrorPresentation; activityError?: ErrorPresentation; + renewalError?: Error; + onRetryRenewal?: () => void; onDismissRenewal?: (id: string) => void; onSelectScope?: (scopeId: string) => void; onTrendPeriod?: (period: BalanceHistoryPeriod) => void; @@ -782,6 +784,15 @@ export function BalanceScreen(props: { return ( <div class="mx-auto max-w-6xl text-onBackground"> <h1 class="sr-only">{i18n.str`Balances`}</h1> + {props.renewalError && ( + <ErrorCard + class="mb-5" + title={i18n.str`Digital cash renewal`} + error={props.renewalError} + > + <Button onClick={props.onRetryRenewal}>{i18n.str`Retry`}</Button> + </ErrorCard> + )} {props.activityError && ( <ErrorCard class="mb-5" error={props.activityError} /> )} diff --git a/packages/wallet-webui/src/testing/demo-wallet.ts b/packages/wallet-webui/src/testing/demo-wallet.ts @@ -17,6 +17,7 @@ import { TransactionType, WithdrawalType, stringifyYaml, + stringifyScopeInfo, type AmountString, type ScopeInfo, type Transaction, @@ -449,6 +450,10 @@ class MemoryActionInbox implements ActionInbox { export class DemoWalletConnection implements WalletConnection { readonly client: WalletCoreApiClient; + private refreshObservers = new Map< + string, + { scope: ScopeInfo; revision: number; value?: string } + >(); private observations = new Map< string, { transactionId: string; revision: number; value?: string } @@ -474,7 +479,6 @@ export class DemoWalletConnection implements WalletConnection { this.state = seedState(this.now()); this.emit({ type: NotificationType.BalanceChange, - hintTransactionId: "demo-reset", }); this.emitTransaction(this.state.transactions[0]); this.notice( @@ -499,7 +503,38 @@ export class DemoWalletConnection implements WalletConnection { this.publishObservations(); } + private refreshInfo(scope: ScopeInfo): WalletRefreshInfo | null { + if (stringifyScopeInfo(scope) === stringifyScopeInfo(chfScope)) + return this.state.refreshInfo ?? null; + if (stringifyScopeInfo(scope) === stringifyScopeInfo(eurScope)) + return { + risks: [], + recoveries: [], + annualCostBound: { + status: "available", + horizonDays: 365, + projection: "stable-current-offerings", + amount: "EUR:0", + }, + }; + return null; + } private publishObservations() { + for (const [id, entry] of this.refreshObservers) { + const snapshot = this.refreshInfo(entry.scope); + const value = JSON.stringify(snapshot); + if (entry.value === value) continue; + entry.value = value; + this.emit({ + type: NotificationType.ObservationSnapshot, + observationType: ObservationType.BalanceRefreshInfo, + observationId: id, + scopeInfo: entry.scope, + revision: ++entry.revision, + snapshot, + }); + if (!snapshot) this.refreshObservers.delete(id); + } for (const [observationId, entry] of this.observations) { const t = this.state.transactions.find( (t) => t.transactionId === entry.transactionId, @@ -741,16 +776,13 @@ export class DemoWalletConnection implements WalletConnection { this.state.refreshInfo.recoveries.filter( (r) => r.warningId !== request.warningId, ); - this.emit({ - type: NotificationType.BalanceChange, - hintTransactionId: "demo-renewal", - }); + this.publishObservations(); return {} as never; case WalletApiOperation.GetBalances: return { balances: [ { - refreshInfo: this.state.refreshInfo, + hasExpirationRisk: !!this.state.refreshInfo?.risks.length, scopeInfo: chfScope, available: amount("CHF", this.state.balances.CHF), pendingIncoming: amount("CHF", this.state.pendingIncoming.CHF), @@ -761,6 +793,7 @@ export class DemoWalletConnection implements WalletConnection { disableDirectDeposits: false, }, { + hasExpirationRisk: false, scopeInfo: eurScope, available: amount("EUR", this.state.balances.EUR), pendingIncoming: amount("EUR", 0), @@ -1215,9 +1248,22 @@ export class DemoWalletConnection implements WalletConnection { transactionId: id, } as never); } + case WalletApiOperation.GetBalanceRefreshInfo: + return this.refreshInfo(request.scopeInfo as ScopeInfo) as never; + case WalletApiOperation.ObserveBalanceRefreshInfo: { + const id = String(request.observationId); + if (this.observations.has(id) || this.refreshObservers.has(id)) + throw Error("Observation ID already active"); + this.refreshObservers.set(id, { + scope: request.scopeInfo as ScopeInfo, + revision: 0, + }); + this.publishObservations(); + return {} as never; + } case WalletApiOperation.ObserveTransaction: { const id = String(request.observationId); - if (this.observations.has(id)) + if (this.observations.has(id) || this.refreshObservers.has(id)) throw Error("Observation ID already active"); const t = this.findTransaction(String(request.transactionId)); @@ -1229,6 +1275,7 @@ export class DemoWalletConnection implements WalletConnection { return {} as never; } case WalletApiOperation.StopObservation: + this.refreshObservers.delete(String(request.observationId)); this.observations.delete(String(request.observationId)); return {} as never; case WalletApiOperation.PreparePeerPushCredit: { @@ -1397,7 +1444,7 @@ export class DemoWalletConnection implements WalletConnection { walletCoreVersion: { implementationSemver: "demo", implementationGitHash: "demo", - version: "10:0:0", + version: "11:0:0", }, frontendInfo: request.frontendInfo, database: { backend: "indexeddb", recordCounts: {} }, @@ -1443,6 +1490,7 @@ export class DemoWalletConnection implements WalletConnection { close(): void { this.notifications.clear(); this.observations.clear(); + this.refreshObservers.clear(); this.statuses.clear(); } diff --git a/packages/wallet-webui/test/notifications.test.ts b/packages/wallet-webui/test/notifications.test.ts @@ -25,7 +25,7 @@ test("idle does not cause broad revalidation", () => { ); }); -test("transaction transitions refresh lists and balances without reloading observed details", () => { +test("transaction transitions refresh lists without reloading observed details", () => { const operations = operationsForNotification({ type: NotificationType.TransactionStateTransition, transactionId: "txn:withdrawal:test", @@ -35,5 +35,5 @@ test("transaction transitions refresh lists and balances without reloading obser newStId: 1, }); assert.equal(operations.has(WalletApiOperation.GetTransactionById), false); - assert(operations.has(WalletApiOperation.GetBalances)); + assert(!operations.has(WalletApiOperation.GetBalances)); }); diff --git a/packages/wallet-webui/test/observations-hook.test.tsx b/packages/wallet-webui/test/observations-hook.test.tsx @@ -10,11 +10,15 @@ import { Window } from "happy-dom"; import { TransactionType, NotificationType, + ObservationType, + type WalletNotification, type TransactionIdStr, + type ScopeInfo, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { useTransactionObservation, + useBalanceRefreshInfo, usePaymentContract, type PaymentContractView, } from "../src/api/observations.js"; @@ -107,6 +111,11 @@ function installDom() { writable: true, value, }); + return dom; +} + +test("the hook resets on connection and target changes and rejects late notifications", async () => { + const dom = installDom(); const { render, cleanup, act } = await import("@testing-library/preact"); const first = await fixture("first"); const second = await fixture("second"); @@ -356,3 +365,123 @@ test("contract reads discard late failures and never reuse another payment's doc await dom.happyDOM.abort(); } }); + +test("the balance screen observes only its selected scope and updates dismissed recoveries", async () => { + const dom = installDom(); + const { render, cleanup, act } = await import("@testing-library/preact"); + const connection = new DemoWalletConnection(); + const { balances } = await connection.client.call( + WalletApiOperation.GetBalances, + {}, + ); + const calls: WalletApiOperation[] = []; + const call = connection.client.call.bind(connection.client); + connection.client.call = (op, args) => { + calls.push(op); + return call(op, args); + }; + function Report({ scope }: { scope: ScopeInfo }) { + const { snapshot: info } = useBalanceRefreshInfo(connection, scope); + return ( + <div> + {info + ? info.recoveries.map((n) => n.warningId).join() || "none" + : "loading"} + </div> + ); + } + try { + const view = render(<Report scope={balances[0].scopeInfo} />); + await view.findByText("demo-renewal"); + await act(async () => { + await connection.client.call(WalletApiOperation.DismissWalletWarning, { + warningId: "demo-renewal", + }); + }); + await view.findByText("none"); + await act(async () => + view.rerender(<Report scope={balances[1].scopeInfo} />), + ); + await view.findByText("none"); + view.unmount(); + assert.deepEqual(calls, [ + WalletApiOperation.ObserveBalanceRefreshInfo, + WalletApiOperation.DismissWalletWarning, + WalletApiOperation.StopObservation, + WalletApiOperation.ObserveBalanceRefreshInfo, + WalletApiOperation.StopObservation, + ]); + } finally { + cleanup(); + connection.close(); + await dom.happyDOM.abort(); + } +}); + +test("a failed balance observation keeps the last warning and retries with a fresh ID", async () => { + const dom = installDom(); + const { render, cleanup, act } = await import("@testing-library/preact"); + const connection = new DemoWalletConnection(); + const { balances } = await connection.client.call( + WalletApiOperation.GetBalances, + {}, + ); + let deliver!: (n: WalletNotification) => void; + const subscribe = connection.subscribeNotifications.bind(connection); + connection.subscribeNotifications = (listener) => { + deliver = listener; + return subscribe(listener); + }; + const ids: string[] = []; + const call = connection.client.call.bind(connection.client); + connection.client.call = (op, args) => { + if ( + op === WalletApiOperation.ObserveBalanceRefreshInfo && + args && + typeof args === "object" && + "observationId" in args + ) { + ids.push(String(args.observationId)); + if (ids.length === 2) return Promise.reject(Error("registration failed")); + } + return call(op, args); + }; + function Report() { + const { snapshot, error, retry } = useBalanceRefreshInfo( + connection, + balances[0].scopeInfo, + ); + return ( + <div> + <span>{snapshot?.recoveries[0]?.warningId}</span> + {error && <button onClick={retry}>{error.message}</button>} + </div> + ); + } + try { + const view = render(<Report />); + await view.findByText("demo-renewal"); + const failure = { + type: NotificationType.ObservationError, + observationType: ObservationType.BalanceRefreshInfo, + observationId: ids[0], + error: { code: 1, hint: "updates stopped" }, + } as const; + await act(async () => deliver(failure)); + const retry = await view.findByText("updates stopped"); + assert.ok(view.getByText("demo-renewal")); + await act(async () => retry.click()); + const again = await view.findByText("registration failed"); + assert.ok(view.getByText("demo-renewal")); + await act(async () => again.click()); + await act(async () => deliver(failure)); + assert.equal(ids.length, 3); + assert.equal(new Set(ids).size, 3); + assert.ok(view.getByText("demo-renewal")); + assert.equal(view.queryByRole("button"), null); + } finally { + cleanup(); + connection.close(); + await dom.happyDOM.abort(); + } +});