Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/development/QUALITY_GATES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions scripts/run-loan-dialog-regressions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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],
Expand Down
21 changes: 20 additions & 1 deletion ui/src/lib/loan_data_source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
11 changes: 7 additions & 4 deletions ui/src/lib/loan_data_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 8 additions & 9 deletions ui/src/pages/loans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand Down
Loading