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
47 changes: 46 additions & 1 deletion fe/src/lib/api/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { getAdvances, getTransactions, usingMockApi } from "./client";

const BORROWER = "GBORROWER";
Expand Down Expand Up @@ -41,3 +41,48 @@ describe("the fixture client's forepay.mock.ledger switch", () => {
expect(await getAdvances(BORROWER)).toHaveLength(1);
});
});

describe("the #16 API's advances", () => {
afterEach(() => {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
vi.resetModules();
});

/** `getAdvances` against an API that answers `rows`, read with the API set at import time. */
async function advancesFrom(rows: object[]) {
vi.stubEnv("NEXT_PUBLIC_API_URL", "https://api.test");
vi.stubGlobal("fetch", async () => new Response(JSON.stringify(rows)));
vi.resetModules();
const client = await import("./client");
return client.getAdvances(BORROWER);
}

const row = {
nullifier: "aa",
borrower: BORROWER,
windowStart: "2026-07-01",
windowEnd: "2026-09-30",
revenueCents: "36000",
principal: "900000000",
repaid: "0",
openedAt: "1791806400",
txHash: "adv",
};

it("keeps AdMob's source (#107)", async () => {
const [advance] = await advancesFrom([{ ...row, source: "AdMob" }]);
expect(advance.source).toBe("AdMob");
expect(advance.revenueCents).toBe(36_000n);
});

it("reads a row without a source, from an API before #107, as AdSense", async () => {
const [advance] = await advancesFrom([row]);
expect(advance.source).toBe("AdSense");
});

it("reads a source it does not know as AdSense", async () => {
const [advance] = await advancesFrom([{ ...row, source: "YouTube" }]);
expect(advance.source).toBe("AdSense");
});
});
11 changes: 9 additions & 2 deletions fe/src/lib/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ async function fetchJson<T>(path: string): Promise<T> {
/** Every advance this wallet has taken, newest first. */
export async function getAdvances(borrower: string): Promise<AdvanceSummary[]> {
if (usingMockApi) return mock.advances(borrower);
const rows = await fetchJson<Wire<AdvanceSummary>[]>(`/advances?borrower=${encodeURIComponent(borrower)}`);
return rows.map((r) => fromWire<AdvanceSummary>(r, ["revenueCents", "principal", "repaid", "openedAt"]));
const rows = await fetchJson<(Wire<AdvanceSummary> & { source?: string })[]>(
`/advances?borrower=${encodeURIComponent(borrower)}`,
);
return rows.map((r) => ({
...fromWire<AdvanceSummary>(r, ["revenueCents", "principal", "repaid", "openedAt"]),
// Missing before #107, and those advances are all AdSense.
source: r.source === "AdMob" ? "AdMob" : "AdSense",
}));
}

/** Every advance and repayment for this wallet, newest first. */
Expand Down Expand Up @@ -76,6 +82,7 @@ const mock = {
return [
{
nullifier: NULLIFIER,
source: "AdSense",
borrower,
windowStart: "2026-07-01",
windowEnd: "2026-09-30",
Expand Down
5 changes: 5 additions & 0 deletions fe/src/lib/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
export type AdvanceSummary = {
/** 32 bytes as hex. `repay` takes it (ARCHITECTURE.md §10, question 1). */
nullifier: string;
/**
* Where the revenue was earned (#97), read by #107 from the `request_advance` call. An API from before
* #107 sends none, and every advance it lists is AdSense's.
*/
source: "AdSense" | "AdMob";
borrower: string;
/** The attested three-month window, `YYYY-MM-DD`. */
windowStart: string;
Expand Down
1 change: 1 addition & 0 deletions fe/src/lib/ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const OPENED = 1_791_806_400n; // 12 Oct 2026
function advance(over: Partial<AdvanceSummary> = {}): AdvanceSummary {
return {
nullifier: "aa",
source: "AdSense",
borrower: "G",
windowStart: "2026-07-01",
windowEnd: "2026-09-30",
Expand Down
19 changes: 13 additions & 6 deletions fe/src/modules/repayment/components/AdvanceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ export function AdvanceDetails({
}) {
const sized: ReactNode =
terms === "loading" ? <SkeletonBlock className="h-4 w-28 rounded-[6px]" /> : sizingLabel(advance, terms);
const rows: { label: string; value: ReactNode }[] = [
{ label: "Advanced", value: formatUsdc(advance.principal) },
const rows: { key: string; label: ReactNode; value: ReactNode }[] = [
{ key: "advanced", label: "Advanced", value: formatUsdc(advance.principal) },
{
label: `AdSense revenue, ${formatWindow(advance.windowStart, advance.windowEnd).short}`,
key: "revenue",
// When the label has to wrap, the window moves to the next line whole, never the year alone.
label: (
<>
{advance.source} revenue,{" "}
<span className="whitespace-nowrap">{formatWindow(advance.windowStart, advance.windowEnd).short}</span>
</>
),
value: formatUsdCents(advance.revenueCents),
},
...(sized ? [{ label: "How it was sized", value: sized }] : []),
{ label: "Received on", value: formatLedgerTime(advance.openedAt) },
...(sized ? [{ key: "sized", label: "How it was sized", value: sized }] : []),
{ key: "received", label: "Received on", value: formatLedgerTime(advance.openedAt) },
];
return (
<section className={cn("flex w-full flex-col gap-5 rounded-card bg-surface p-6", className)}>
<h2 className="text-card-title text-fg">Advance details</h2>
<dl className="flex flex-col gap-3.5">
{rows.map((r) => (
<div key={r.label} className="flex items-center justify-between gap-4 text-body">
<div key={r.key} className="flex items-center justify-between gap-4 text-body">
<dt className="text-fg-muted">{r.label}</dt>
<dd className="whitespace-nowrap font-medium text-fg">{r.value}</dd>
</div>
Expand Down
1 change: 1 addition & 0 deletions fe/tests/e2e/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function frameLedger(): Ledger {
advances: [
{
nullifier: NULLIFIER,
source: "AdSense",
borrower: E2E_ADDRESS,
windowStart: "2026-07-01",
windowEnd: "2026-09-30",
Expand Down
19 changes: 19 additions & 0 deletions fe/tests/e2e/repayment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,25 @@ test("advance details say how it was sized, from the contract's own rate", async
await expect(details.getByText("How it was sized").locator("..")).toContainText("25% of $360.00");
});

test("advance details name where the revenue was earned, from the API's source", async ({ page, isMobile }) => {
test.skip(isMobile, "advance details are the desktop layout's");
const ledger = frameLedger();
ledger.advances[0].source = "AdMob";
await open(page, "/repayment", ledger);
const details = page.getByRole("heading", { name: "Advance details" }).locator("..");
await expect(details.getByText("AdMob revenue, Jul to Sep 2026")).toBeVisible();
await expect(details).not.toContainText("AdSense");
});

test("advance details read an advance without a source, from an API before #107, as AdSense", async ({ page, isMobile }) => {
test.skip(isMobile, "advance details are the desktop layout's");
const ledger = frameLedger();
delete (ledger.advances[0] as Partial<(typeof ledger.advances)[0]>).source;
await open(page, "/repayment", ledger);
const details = page.getByRole("heading", { name: "Advance details" }).locator("..");
await expect(details.getByText("AdSense revenue, Jul to Sep 2026")).toBeVisible();
});

test("the sizing row holds its place while the contract's sizing loads", async ({ page, isMobile }) => {
test.skip(isMobile, "advance details are the desktop layout's");
await open(page, "/repayment", frameLedger(), { advance: { sizingDelayMs: 1500 } });
Expand Down