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
63 changes: 62 additions & 1 deletion e2e/mockBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ export interface MockOptions {
/** Delay every response (ms) — for observing loading states. */
delayMs?: number;
positions?: unknown[];
/** Vault fixtures — override the defaults for capacity/waitlist/epoch states. */
vaults?: unknown[];
vaultDetail?: Record<string, unknown>;
epochLedger?: unknown[];
/** Force a specific vault route to fail with the given status/body. */
vaultErrors?: Record<string, { status: number; body: unknown }>;
}

export interface MockBackend {
requests: { mode: Mode; method: string; path: string; auth: string | null }[];
opened: unknown[];
deposits: unknown[];
withdrawals: unknown[];
redemptions: unknown[];
}

const SPOT: Record<string, number> = { XLM: 0.1182, BTC: 67420.5, ETH: 3512.8, SOL: 182.45 };
Expand Down Expand Up @@ -50,10 +59,33 @@ export const samplePosition = {
close_premium: null, close_spot: null, realized_pnl: null, opened_at: "2026-09-01T10:00:00Z", closed_at: null, strategy_id: null,
};

/** Default vault list fixture — derived from the frozen REST contract. */
export const sampleVaults = [
{ id: "vault-xlm-1", name: "XLM Covered Call", asset: "XLM", status: "open", capacity: 100000, total_deposits: 42000, apy: 0.124, share_price: 1.042, epoch: 12, epoch_ends_at: "2026-10-01T00:00:00Z" },
{ id: "vault-btc-1", name: "BTC Put Seller", asset: "BTC", status: "waitlist", capacity: 50000, total_deposits: 50000, apy: 0.081, share_price: 1.011, epoch: 4, epoch_ends_at: "2026-10-02T00:00:00Z" },
{ id: "vault-eth-1", name: "ETH Straddle", asset: "ETH", status: "closed", capacity: 80000, total_deposits: 12000, apy: 0.153, share_price: 0.998, epoch: 7, epoch_ends_at: "2026-09-28T00:00:00Z" },
];

export const sampleVaultDetail = {
...sampleVaults[0],
description: "Sells covered calls on XLM to earn premium.",
min_deposit: 10,
lockup_days: 7,
share_price: 1.042,
pending_withdrawals: 0,
};

export const sampleEpochLedger = [
{ epoch: 12, started_at: "2026-09-24T00:00:00Z", ended_at: null, deposits: 42000, withdrawals: 0, share_price: 1.042, status: "active" },
{ epoch: 11, started_at: "2026-09-17T00:00:00Z", ended_at: "2026-09-24T00:00:00Z", deposits: 38000, withdrawals: 2000, share_price: 1.031, status: "settled" },
];

/** Installs a fake Zenith backend for all three modes and (optionally) a signed-in session. */
export async function mockBackend(page: Page, opts: MockOptions = {}): Promise<MockBackend> {
const state: MockBackend = { requests: [], opened: [] };
const state: MockBackend = { requests: [], opened: [], deposits: [], withdrawals: [], redemptions: [] };
const positions: unknown[] = [...(opts.positions ?? [])];
const vaults: unknown[] = [...(opts.vaults ?? sampleVaults)];
const epochLedger: unknown[] = [...(opts.epochLedger ?? sampleEpochLedger)];

for (const mode of opts.signedIn ?? []) {
await page.addInitScript(([m]) => {
Expand Down Expand Up @@ -105,6 +137,35 @@ export async function mockBackend(page: Page, opts: MockOptions = {}): Promise<M
if (p === "/watchlist") return json([]);
if (p === "/alerts") return json([]);
if (p === "/history") return json({ trades: [], stats: { trade_count: 0, win_count: 0, loss_count: 0, total_realized_pnl: 0 } });

// Vault routes — derived from the frozen REST contract.
const vaultErr = opts.vaultErrors?.[p];
if (vaultErr) return json(vaultErr.body, vaultErr.status);
if (p === "/vaults" && req.method() === "GET") return json(vaults);
if (p === "/vaults/deposit" && req.method() === "POST") {
const body = req.postDataJSON();
state.deposits.push(body);
const shares = Number(body.amount) / 1.042;
return json({ id: `dep-${state.deposits.length}`, vault_id: body.vault_id, amount: body.amount, shares, share_price: 1.042, status: "confirmed" }, 201);
}
if (p === "/vaults/withdraw" && req.method() === "POST") {
const body = req.postDataJSON();
state.withdrawals.push(body);
return json({ id: `wd-${state.withdrawals.length}`, vault_id: body.vault_id, shares: body.shares, status: "queued", epoch: 12, queued_at: "2026-09-25T00:00:00Z" }, 202);
}
if (p === "/vaults/redeem" && req.method() === "POST") {
const body = req.postDataJSON();
state.redemptions.push(body);
return json({ id: `rd-${state.redemptions.length}`, vault_id: body.vault_id, shares: body.shares, amount: Number(body.shares) * 1.042, status: "redeemed" }, 200);
}
if (p === "/vaults/epochs" && req.method() === "GET") return json(epochLedger);
const detailMatch = p.match(/^\/vaults\/([^/]+)$/);
if (detailMatch && req.method() === "GET") {
const id = decodeURIComponent(detailMatch[1]);
const found = (vaults as { id: string }[]).find(v => v.id === id);
if (!found) return json({ error: "vault not found" }, 404);
return json(opts.vaultDetail ?? { ...sampleVaultDetail, ...found });
}
return json({ error: `unmocked ${p}` }, 404);
});
}
Expand Down
Loading