From 200e7a6ed9e7453bd0e1f49745e089b3aaa46f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Tore=20Liatun?= <2yscrx692f@privaterelay.appleid.com> Date: Wed, 2 Dec 2026 09:52:50 +0100 Subject: [PATCH] Reconcile paired loan views and snapshot exports --- docs/development/QUALITY_GATES.md | 4 +++- scripts/run-loan-dialog-regressions.mjs | 32 +++++++++++++++++++++++++ ui/src/lib/loan_data_source.test.ts | 21 +++++++++++++++- ui/src/lib/loan_data_source.ts | 11 +++++---- ui/src/pages/loans.tsx | 17 +++++++------ 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/docs/development/QUALITY_GATES.md b/docs/development/QUALITY_GATES.md index 2a6dbec5..4dfa8894 100644 --- a/docs/development/QUALITY_GATES.md +++ b/docs/development/QUALITY_GATES.md @@ -192,7 +192,9 @@ these boundaries, including a local–Client–local cycle. Paired loan-history CSV uses the protected Host export when supported. A current Host returns all matching loans from one database snapshot. Older Hosts -and cached history export the displayed history as a snapshot CSV. Tests cover +and cached history export the rows available in the All view as a snapshot CSV. +When the active-loan read fails, live history supplies current details for its +own rows and the active cache supplies older loans outside that page. Tests cover empty and 10,000-row histories, both directions, and stale replies. Settings full-backup export serializes portable rows from one SQLite snapshot diff --git a/scripts/run-loan-dialog-regressions.mjs b/scripts/run-loan-dialog-regressions.mjs index 3e43512b..f212a42d 100644 --- a/scripts/run-loan-dialog-regressions.mjs +++ b/scripts/run-loan-dialog-regressions.mjs @@ -178,6 +178,8 @@ async function checkPairedLoanExport({ page, state, fixture, fallback, openWorks switch (command) { case "get_library_sync_page_status": return host; case "fetch_library_sync_loans": + assert.equal(payload.expectedGeneration, host.target_generation); + return { captured_at: "2026-04-02 10:00:00", rows: [fixture.loanRows[0]] }; case "fetch_library_sync_active_loans": assert.equal(payload.expectedGeneration, host.target_generation); return { captured_at: "2026-04-02 10:00:00", rows: fixture.loanRows }; @@ -217,6 +219,35 @@ async function checkPairedLoanExport({ page, state, fixture, fallback, openWorks assert.equal(full.text, "loan_id\nrow-outside-visible-history\n"); assert.match(snapshot.name, /-snapshot-/); assert.match(snapshot.text, /dialog-loan-OUTBOUND/); + assert.match(snapshot.text, /dialog-loan-INBOUND/); +} + +async function checkPartialLoanRefresh({ page, state, fixture, fallback, openWorkspace }) { + const [outbound, inbound] = fixture.loanRows; + const returned = { ...inbound, loan: { ...inbound.loan, loan_status: "RETURNED", returned_at: "2026-04-02 09:00:00" } }; + const older = { ...inbound, loan: { ...inbound.loan, id: "older-loan", spool_id: "older-spool", + counterparty_name: "Older lender", lent_at: "2024-01-01 00:00:00" } }; + state.invoke = (command, payload) => { + switch (command) { + case "get_library_sync_page_status": + return { mode: "CLIENT", library_id: "library-a", host_base_url: "https://loans.invalid", + client_auth_paired: true, target_generation: 5 }; + case "fetch_library_sync_loans": return { captured_at: "2026-04-02 10:00:00", rows: [outbound, returned] }; + case "fetch_library_sync_active_loans": throw new Error("[loan-regression] Active read failed"); + case "fetch_cached_library_sync_active_loans": return { captured_at: "2026-04-01 10:00:00", rows: [inbound, older] }; + default: return fallback(command, payload); + } + }; + await openWorkspace(); + await page.getByText(/Showing the last cached loan snapshot/).waitFor(); + await page.getByRole("button", { name: /^Return ·/ }).waitFor(); + await page.getByRole("button", { name: /^Hand back ·/ }).waitFor(); + const status = page.getByRole("group", { name: "Status" }); + await status.getByRole("button", { name: "All", exact: true }).click(); + await page.getByText("3 loans", { exact: true }).waitFor(); + await status.getByRole("button", { name: "Returned", exact: true }).click(); + await page.getByText("1 loan", { exact: true }).waitFor(); + assert.equal(await page.getByRole("button", { name: /^Hand back ·/ }).count(), 0); } async function checkWorkspaceOffline({ page, state, fixture, calls, fallback, openWorkspace }) { @@ -1322,6 +1353,7 @@ async function main() { ["Stock pages search beyond previews, discard stale results and filter the offline copy", checkStockCards, true], ["An older Host supplies the complete stock for the selected location on demand", checkLegacyLocation, true], ["offline loan rows and badges reject a late A–B–A read", checkWorkspaceOffline, true], + ["live history and a cached active list agree across loan status filters", checkPartialLoanRefresh, true], ["paired loan CSV downloads the full Host export or names the older Host snapshot", checkPairedLoanExport, true], ["loan CSV uses the reviewed library and only downloads in the open workspace", checkLoanExport], ["inventory selection, paged search, changing stock and interrupted reads", checkPagedLoanPicker], diff --git a/ui/src/lib/loan_data_source.test.ts b/ui/src/lib/loan_data_source.test.ts index 3b3f026c..43711b10 100644 --- a/ui/src/lib/loan_data_source.test.ts +++ b/ui/src/lib/loan_data_source.test.ts @@ -252,7 +252,26 @@ test("active snapshot joins history without duplicates or stale closed loans", ( const beforeReturn = normalizeLoanDetailsRow(loanDetailsRow("recent-returned")); const history = { rows: historyRows, source: "LIVE" as const, updatedAt: null, usedFallback: false }; const active = { rows: [beforeReturn, old], source: "CACHED" as const, updatedAt: null, usedFallback: true }; - assert.deepEqual(activeLoanRowsForDisplay(active, history), [old]); + assert.deepEqual(activeLoanRowsForDisplay(active, history), [stale, old]); +}); + +test("partial Host failure keeps recent active loans and older loans outside the history window", () => { + const loan = (id: string) => normalizeLoanDetailsRow(loanDetailsRow(id)); + const old = loan("outside-history"); + const changed = loan("changed"); + const liveChanged = { ...changed, spool_remaining_g: 123 }; + const inbound = loan("new-inbound"); + inbound.loan.loan_direction = "INBOUND"; + const returned = loan("returned"); + const closed = { ...returned, loan: { ...returned.loan, returned_at: "2026-04-02 10:00:00" } }; + const removed = loan("deleted"); + const history = { source: "LIVE" as const, rows: [inbound, liveChanged, closed, { ...removed, spool_status: "DELETED" }], + usedFallback: false, updatedAt: "2026-04-02 10:00:00" }; + const active = { source: "CACHED" as const, rows: [old, changed, returned, removed], + usedFallback: true, updatedAt: "2026-04-01 10:00:00" }; + assert.deepEqual(activeLoanRowsForDisplay(active, history), [inbound, liveChanged, old]); + assert.deepEqual(activeLoanRowsForDisplay({ ...active, rows: [] }, history), [inbound, liveChanged]); + assert.deepEqual(activeLoanRowsForDisplay({ ...active, source: "LIVE", rows: [] }, history), []); }); test("loadLoanRowsPage uses rows and timestamp from a single host snapshot in client mode", async () => { diff --git a/ui/src/lib/loan_data_source.ts b/ui/src/lib/loan_data_source.ts index d697ff85..9955c393 100644 --- a/ui/src/lib/loan_data_source.ts +++ b/ui/src/lib/loan_data_source.ts @@ -163,10 +163,13 @@ export function activeLoanRowsForDisplay( if (activeSnapshot?.source !== "CACHED" || historySnapshot?.source !== "LIVE") { return activeRows; } - const closedIds = new Set(historySnapshot.rows - .filter((row) => !isLoanCurrentlyActive(row)) - .map((row) => row.loan.id)); - return activeRows.filter((row) => !closedIds.has(row.loan.id)); + // Live history describes every loan on its page. The dedicated cache can + // also contain older active loans beyond that page. + const historyIds = new Set(historySnapshot.rows.map((row) => row.loan.id)); + return [ + ...historySnapshot.rows.filter(isLoanCurrentlyActive), + ...activeRows.filter((row) => !historyIds.has(row.loan.id)), + ].sort((left, right) => right.loan.lent_at.localeCompare(left.loan.lent_at)); } export function mergeLoanHistoryAndActiveRows( diff --git a/ui/src/pages/loans.tsx b/ui/src/pages/loans.tsx index f5711da9..d66d8596 100644 --- a/ui/src/pages/loans.tsx +++ b/ui/src/pages/loans.tsx @@ -211,15 +211,14 @@ function LoanWorkspace({ embedded = false, onCountsChange, onOpenSpool, library () => activeLoanRowsForDisplay(activeLoanSnapshot, loanHistory), [activeLoanSnapshot, loanHistory], ); + const getAllRows = useCallback( + () => mergeLoanHistoryAndActiveRows(loanHistory?.rows ?? [], activeRows), + [loanHistory, activeRows], + ); const filteredLoans = useMemo(() => { - const historyRows = loanHistory?.rows ?? []; - const rows = filter === "ACTIVE" - ? activeRows - : filter === "ALL" - ? mergeLoanHistoryAndActiveRows(historyRows, activeRows) - : historyRows; + const rows = filter === "ACTIVE" ? activeRows : filter === "ALL" ? getAllRows() : loanHistory?.rows ?? []; return filterLoans(rows, directionFilter, filter, deferredSearch); - }, [activeRows, deferredSearch, directionFilter, filter, loanHistory]); + }, [activeRows, getAllRows, deferredSearch, directionFilter, filter, loanHistory]); const displayedSnapshot = filter === "ACTIVE" || ( filter === "ALL" && loanHistory?.source === "LIVE" && activeLoanSnapshot?.source !== "LIVE" ) ? activeLoanSnapshot : loanHistory; @@ -248,7 +247,7 @@ function LoanWorkspace({ embedded = false, onCountsChange, onOpenSpool, library target_generation: clientTargetGeneration, }, directionFilter) : null; snapshot = csv === null; - content = csv ?? buildLoansCsv(loanHistory.rows, directionFilter); + content = csv ?? buildLoansCsv(getAllRows(), directionFilter); const current = await loadLibrarySyncPageState(); if (!current.clientReadOnly || current.clientLibraryId !== clientLibraryId || current.clientTargetGeneration !== clientTargetGeneration || current.clientHostBaseUrl !== clientHostBaseUrl) { @@ -268,7 +267,7 @@ function LoanWorkspace({ embedded = false, onCountsChange, onOpenSpool, library "text/csv;charset=utf-8", ); const success = t("loans.csvExported", "Loan CSV exported."); - if (snapshot) { + if (snapshot && loanHistory?.source === activeLoanSnapshot?.source) { const source = loanHistory?.source === "CACHED" ? t("dashboard.clientSnapshotSyncedCached", "Cached host snapshot") : t("dashboard.clientSnapshotSyncedLive", "Live host snapshot");