Files
KomediruzeckiandGitHub 5b4a4d913c test(contract): one CRUD suite for cloud and local-first, and the 35 bugs it found (#606)
Every route both runtimes serve now has a scenario, written once and run against the Worker on D1 and against local-first on IndexedDB: create from the body the form sends, read it back to the cent, edit, delete, and check that another profile can neither read nor change it. Guards fail when a served route is on no list or has no passing scenario.

- shared/contract/ holds 120 scenarios in 24 entity files and the route lists: 171 routes served by both runtimes, 69 by the Worker only, 25 by local-first only. One-runtime write routes are tested in their own runtime.
- 52 places where the runtimes still answer differently are pinned at the check (// DIFFERENCE <id>) and described in shared/contract/differences.ts, each with the slice that settles it.
- 35 bugs are fixed, each in its own commit with a test that fails without it. Among them: Copy last month deleted the month's budgets before copying, in both runtimes (it now fills only the categories with no budget yet, and says how many already had one); a cloud month summary counted every later month; a local-first bill lost its account, so paying it moved no balance; deleting a cloud loan left its rate periods and extra payments in D1.
- Local-first's PUT /loans/:id/rate, which answered 404 for every loan and had no caller, is gone with api.updateLoanRate.
2026-10-08 16:14:52 +02:00

53 lines
2.2 KiB
TypeScript

/**
* The services the runtimes call out to, answered here so that no contract scenario reaches the
* network. Each runner routes its runtime's own `fetch` through `outbound`. A request to anything
* not listed fails the scenario.
*/
/** Quotes the price service knows: anything else is a ticker it has never heard of. */
export const QUOTES: Readonly<
Record<string, { price: number; previousClose: number; currency: string; name: string }>
> = {
ACME: { price: 12.5, previousClose: 12, currency: 'USD', name: 'Acme Corp' },
};
/** Spreadsheets shared as "anyone with the link", by id, as their CSV export reads. */
export const SHEETS: Readonly<Record<string, string>> = {
'contract-sheet':
'Date,Description,Amount,Category\n2026-03-01,Salary,2500,Salary\n2026-03-02,Groceries,-45.50,Food\n',
};
/** The answer to one outbound request a runtime makes, by its URL. */
export async function outbound(url: string): Promise<Response> {
// A Google Sheet's CSV export (worker/src/routes/imports.ts, fetchGoogleSheetRows, and the
// strategies of local-first's importGoogleSheet). Its other endpoints answer 404 here.
const sheet =
/^https:\/\/docs\.google\.com\/spreadsheets\/d\/([^/]+)\/(export\?format=csv)?/.exec(url);
if (sheet) {
const csv = SHEETS[sheet[1]];
if (csv && sheet[2]) return new Response(csv, { status: 200 });
return new Response('<!DOCTYPE html><html>Not found</html>', { status: 404 });
}
// Yahoo's chart endpoint (worker/src/routes/portfolio.ts, fetchYahooQuote).
const chart = /^https:\/\/query[12]\.finance\.yahoo\.com\/v8\/finance\/chart\/([^?]+)/.exec(url);
if (chart) {
const symbol = decodeURIComponent(chart[1]);
const quote = QUOTES[symbol];
if (!quote) {
return new Response(JSON.stringify({ chart: { result: null, error: 'Not Found' } }), {
status: 404,
});
}
const meta = {
symbol,
regularMarketPrice: quote.price,
chartPreviousClose: quote.previousClose,
currency: quote.currency,
shortName: quote.name,
};
return new Response(JSON.stringify({ chart: { result: [{ meta }] } }), { status: 200 });
}
throw new Error(`a contract scenario reached the network: ${url}`);
}