taler-typescript-core

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

commit 461a0d17a143a88c2f432718fe0bdf21952f6ff6
parent 6c9d46b4b0bb17c68f045029a95a78c56c55cae9
Author: Florian Dold <dold@taler.net>
Date:   Tue, 10 Nov 2026 16:58:55 +0100

wallet web UI: support peer-credit observations in the demo wallet

Emit complete snapshots for preparation, confirmation and deletion. Exercise
the observation hook across connection and transaction changes, including
late notifications from the previous target.

Diffstat:
Mpackages/wallet-webui/src/testing/demo-wallet.ts | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apackages/wallet-webui/test/observations-hook.test.tsx | 144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 235 insertions(+), 0 deletions(-)

diff --git a/packages/wallet-webui/src/testing/demo-wallet.ts b/packages/wallet-webui/src/testing/demo-wallet.ts @@ -4,6 +4,8 @@ import { ExchangeTosStatus, ExchangeUpdateStatus, NotificationType, + ObservationType, + type PeerPushCreditSnapshot, Paytos, PaytoType, Result, @@ -445,6 +447,10 @@ class MemoryActionInbox implements ActionInbox { export class DemoWalletConnection implements WalletConnection { readonly client: WalletCoreApiClient; + private observations = new Map< + string, + { transactionId: string; revision: number; value?: string } + >(); private readonly notifications = new Set<(n: WalletNotification) => void>(); private readonly statuses = new Set<(s: ConnectionStatus) => void>(); private state: DemoState; @@ -484,6 +490,73 @@ export class DemoWalletConnection implements WalletConnection { private emit(notification: WalletNotification): void { for (const listener of this.notifications) listener(notification); + if ( + notification.type !== NotificationType.ObservationSnapshot && + notification.type !== NotificationType.ObservationError + ) + this.publishObservations(); + } + + private publishObservations() { + for (const [observationId, entry] of this.observations) { + const t = this.state.transactions.find( + (t) => t.transactionId === entry.transactionId, + ); + const snapshot: PeerPushCreditSnapshot | null = + t?.type === TransactionType.PeerPushCredit + ? { + transaction: { + type: t.type, + transactionId: t.transactionId, + timestamp: t.timestamp, + scopes: t.scopes, + txState: t.txState, + txActions: t.txActions, + amountRaw: t.amountRaw, + amountEffective: t.amountEffective, + amountEffectiveFinal: t.amountEffectiveFinal, + info: t.info, + exchangeBaseUrl: t.exchangeBaseUrl, + kycUrl: t.kycUrl, + kycAmlReview: t.kycAmlReview, + error: t.error, + }, + exchange: { + exchangeBaseUrl: t.exchangeBaseUrl, + tosStatus: + this.state.exchanges.find( + (e) => e.exchangeBaseUrl === t.exchangeBaseUrl, + )?.tosStatus ?? ExchangeTosStatus.Accepted, + exchangeUpdateStatus: ExchangeUpdateStatus.Ready, + currencySpec: { + name: "CHF", + alt_unit_names: {}, + num_fractional_input_digits: 2, + num_fractional_normal_digits: 2, + num_fractional_trailing_zero_digits: 2, + }, + }, + confirmation: { + unavailableReasons: + t.txState.major === TransactionMajorState.Dialog + ? [] + : ["not-proposed"], + }, + } + : null; + const value = JSON.stringify(snapshot); + if (value === entry.value) continue; + entry.value = value; + this.emit({ + type: NotificationType.ObservationSnapshot, + observationType: ObservationType.PeerPushCredit, + observationId, + transactionId: entry.transactionId as TransactionIdStr, + revision: ++entry.revision, + snapshot, + }); + if (!snapshot) this.observations.delete(observationId); + } } private emitTransaction( @@ -1100,6 +1173,23 @@ export class DemoWalletConnection implements WalletConnection { transactionId: id, } as never); } + case WalletApiOperation.ObservePeerPushCredit: { + const id = String(request.observationId); + if (this.observations.has(id)) + throw Error("Observation ID already active"); + const t = this.findTransaction(String(request.transactionId)); + if (t.type !== TransactionType.PeerPushCredit) + throw Error("Expected peer push credit"); + this.observations.set(id, { + transactionId: t.transactionId, + revision: 0, + }); + this.publishObservations(); + return {} as never; + } + case WalletApiOperation.StopObservation: + this.observations.delete(String(request.observationId)); + return {} as never; case WalletApiOperation.PreparePeerPushCredit: { if (request.talerUri !== demoActionUris["peer-receive"]) throw Error( @@ -1311,6 +1401,7 @@ export class DemoWalletConnection implements WalletConnection { close(): void { this.notifications.clear(); + this.observations.clear(); this.statuses.clear(); } diff --git a/packages/wallet-webui/test/observations-hook.test.tsx b/packages/wallet-webui/test/observations-hook.test.tsx @@ -0,0 +1,144 @@ +/* + * This file is part of GNU Taler + * (C) 2026 Taler Systems S.A. + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import assert from "node:assert/strict"; +import test from "node:test"; +import { Window } from "happy-dom"; +import { NotificationType, type TransactionIdStr } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; +import { usePeerCreditObservation } from "../src/api/observations.js"; +import type { WalletConnection } from "../src/api/contracts.js"; +import { + DemoWalletConnection, + demoActionUris, +} from "../src/testing/demo-wallet.js"; + +function Probe(props: { + connection: WalletConnection; + transactionId: TransactionIdStr; +}) { + const { state } = usePeerCreditObservation( + props.connection, + props.transactionId, + ); + return ( + <div> + {state.status === "ready" + ? state.snapshot.transaction.info.summary + : state.status} + </div> + ); +} + +async function fixture(summary: string) { + const connection = new DemoWalletConnection(); + const prepare = () => + connection.client.call(WalletApiOperation.PreparePeerPushCredit, { + talerUri: demoActionUris["peer-receive"], + }); + const transactionId = (await prepare()).transactionId; + const secondId = (await prepare()).transactionId; + const pending: (() => void)[] = []; + const subscribe = connection.subscribeNotifications.bind(connection); + connection.subscribeNotifications = (listener) => + subscribe((n) => { + if (n.type === NotificationType.ObservationSnapshot && n.snapshot) + pending.push(() => + listener({ + ...n, + snapshot: { + ...n.snapshot!, + transaction: { + ...n.snapshot!.transaction, + info: { + ...n.snapshot!.transaction.info, + summary: `${summary}:${n.transactionId}`, + }, + }, + }, + }), + ); + else listener(n); + }); + return { + connection, + transactionId, + secondId, + pending, + flush: () => { + for (const deliver of pending.splice(0)) deliver(); + }, + }; +} + +test("the hook resets on connection and target changes and rejects late notifications", async () => { + const dom = new Window({ url: "https://wallet.example/" }); + for (const [key, value] of Object.entries({ + window: dom, + document: dom.document, + navigator: dom.navigator, + HTMLElement: dom.HTMLElement, + MutationObserver: dom.MutationObserver, + })) + Object.defineProperty(globalThis, key, { + configurable: true, + writable: true, + value, + }); + const { render, cleanup, act } = await import("@testing-library/preact"); + const first = await fixture("first"); + const second = await fixture("second"); + const view = render( + <Probe connection={first.connection} transactionId={first.transactionId} />, + ); + const wait = async (check: () => void) => { + for (let i = 0; ; i++) { + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 10)); + }); + try { + check(); + return; + } catch (e) { + if (i > 100) throw e; + } + } + }; + try { + await wait(() => assert(first.pending.length > 0)); + await act(async () => first.flush()); + await wait(() => assert(view.getByText(`first:${first.transactionId}`))); + await act(async () => + view.rerender( + <Probe + connection={second.connection} + transactionId={first.transactionId} + />, + ), + ); + await wait(() => assert(second.pending.length > 0)); + assert.equal(view.container.textContent, "loading"); + // Keep the old-target delivery queued while switching to a different ID. + await act(async () => + view.rerender( + <Probe + connection={second.connection} + transactionId={second.secondId} + />, + ), + ); + await wait(() => assert(second.pending.length > 1)); + await act(async () => second.pending.shift()!()); + assert.equal(view.container.textContent, "loading"); + await act(async () => second.flush()); + await wait(() => assert(view.getByText(`second:${second.secondId}`))); + } finally { + cleanup(); + first.connection.close(); + second.connection.close(); + await dom.happyDOM.abort(); + } +});