# Test a loss

> Show how a loss of a given size runs up a tower and who pays.

Draw a test loss on a tower to see which Layers it reaches. Use the Engine to estimate how much each Layer pays.

Drag the slider to move the loss line.

## Draw the loss

Pass an amount as `loss`. The tower is washed up to that level and flagged. A loss larger than the tower says how far above it runs.

```tsx
<Towers data={response} loss={6200000} />
```

Pass `null`, or leave the prop out, for no loss.

## Estimate recoveries with the Engine

[POST /recoveries](https://towers.vinsa.dev/docs/api/recoveries.md) returns the ceded amount per Layer after applying inuring order and annual terms. The amounts are **Estimates for illustration and what-if analysis. Never use them for booking.**

In the browser, call a route on your own server that forwards the request body to the Engine's `POST /api/v1/recoveries` with your secret API key. This example lists ceded Estimates for one Loss on the selected Risk.

```js
async function showRecoveryEstimates(program, riskId, currency, date, amount) {
  const response = await fetch('/api/recovery-estimates', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      program,
      losses: [{ lossId: 'test-loss', riskId, currency, date, amount }],
    }),
  });
  if (!response.ok) throw new Error('Could not load recovery Estimates');

  const { losses } = await response.json();
  console.info(`Ceded Estimates in ${currency} (never for booking)`);
  console.table(losses[0].layers.map(({ contractRef, layerRef, ceded }) => ({
    contractRef, layerRef, ceded,
  })));
}
```

## One tower at a time

Pass the loss to a [`<TowerChart />`](https://towers.vinsa.dev/docs/react/tower-chart.md) when you want to mark it on a single tower. The line shows where the gross amount falls on that tower's vertical scale. Use the Engine to estimate recoveries across the Risk's Layers.

## A loss input of your own

Keep the amount in state and pass it down. `towerTop` gives a sensible upper bound for a slider:

```tsx
import { useState } from 'react';
import { Towers, towerTop } from '@vinsa/towers';

function TowersWithLoss({ response }) {
  const [loss, setLoss] = useState(0);
  const top = Math.max(...response.risks.flatMap((r) => r.towers.map(towerTop)));

  return (
    <>
      <input type="range" min={0} max={top * 1.25} value={loss} onChange={(e) => setLoss(Number(e.target.value))} />
      <Towers data={response} loss={loss > 0 ? loss : null} />
    </>
  );
}
```
