mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
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.
150 lines
5.2 KiB
TypeScript
150 lines
5.2 KiB
TypeScript
/**
|
|
* The CRUD contract: scenarios written once and run against both runtimes, the Worker on a real
|
|
* D1 (worker/test/contract.test.ts) and local-first on IndexedDB
|
|
* (frontend/src/core/storage/__tests__/contract.test.ts). A scenario writes through the API and
|
|
* reads the result back, so a route that answers 200 and stores nothing fails here, in the runtime
|
|
* that does it.
|
|
*
|
|
* Test-only: nothing in the app imports shared/contract. It cannot import vitest either (pnpm puts
|
|
* no vitest at the repository root), so each runner hands its own `expect` in.
|
|
*/
|
|
|
|
export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
export type Runtime = 'worker' | 'local';
|
|
|
|
/** An answer's parsed JSON. Scenarios read whatever came back, so it is not typed further. */
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export type Json = any;
|
|
|
|
export interface Reply {
|
|
status: number;
|
|
/** The answer's JSON, or its text when it is not JSON. */
|
|
body: Json;
|
|
/** The answer's media type, as its Content-Type header names it ('' when it names none). */
|
|
type: string;
|
|
}
|
|
|
|
/**
|
|
* The API as one profile of one person sees it. A body goes as JSON, except a FormData, which goes
|
|
* as the multipart form a file upload sends.
|
|
*/
|
|
export interface ContractApi {
|
|
readonly runtime: Runtime;
|
|
/** The id of the profile this API acts as. */
|
|
readonly profile: number;
|
|
get(path: string): Promise<Reply>;
|
|
post(path: string, body?: unknown): Promise<Reply>;
|
|
put(path: string, body?: unknown): Promise<Reply>;
|
|
patch(path: string, body?: unknown): Promise<Reply>;
|
|
delete(path: string): Promise<Reply>;
|
|
/** The same person, acting as their second profile. */
|
|
readonly other: ContractApi;
|
|
/** The same person, acting as another profile of theirs: one a scenario made, or a restore gave. */
|
|
as(profile: number): ContractApi;
|
|
/** This profile sending no profile header, as the app sends a whole-account backup or restore. */
|
|
readonly unscoped: ContractApi;
|
|
/**
|
|
* This profile with the second one selected too, reading across both as the household switcher
|
|
* has the app read (`apiHouseholdGet`).
|
|
*/
|
|
readonly household: ContractApi;
|
|
/**
|
|
* What storage holds for a profile, read below the API, so a deleted profile can still be looked
|
|
* at: the rows of each kind that carry its id, and the rows that hang off the loans, accounts and
|
|
* transactions given (an extra payment belongs to a loan, not to a profile). A row that hangs off
|
|
* one now in another profile is not counted (a local-first restore gives rows their old ids
|
|
* back); one whose loan, account or transaction is gone is: it was left behind.
|
|
*/
|
|
stored(profile: number, owned?: Owned): Promise<Record<StoredKind, number>>;
|
|
}
|
|
|
|
/** The kinds of row `ContractApi.stored` counts. */
|
|
export const STORED_KINDS = [
|
|
'accounts',
|
|
'balance history',
|
|
'bills',
|
|
'budgets',
|
|
'categories',
|
|
'category mappings',
|
|
'goals',
|
|
'holdings',
|
|
'housing',
|
|
'import logs',
|
|
'import sources',
|
|
'loan extra payments',
|
|
'loan rate periods',
|
|
'loans',
|
|
'receipts',
|
|
'recurring',
|
|
'retirement goals',
|
|
'retirement settings',
|
|
'tag rules',
|
|
'tags',
|
|
'transaction tags',
|
|
'transactions',
|
|
] as const;
|
|
export type StoredKind = (typeof STORED_KINDS)[number];
|
|
|
|
/** The rows a profile's own rows own in turn, by their ids. */
|
|
export interface Owned {
|
|
loans?: readonly number[];
|
|
accounts?: readonly number[];
|
|
transactions?: readonly number[];
|
|
}
|
|
|
|
/** The part of vitest's `expect` the scenarios use. Each runner passes vitest's own. */
|
|
export interface Matchers {
|
|
toBe(expected: unknown): void;
|
|
toEqual(expected: unknown): void;
|
|
toMatchObject(expected: object): void;
|
|
toContainEqual(expected: unknown): void;
|
|
toContain(expected: unknown): void;
|
|
toHaveLength(length: number): void;
|
|
toBeNull(): void;
|
|
toBeUndefined(): void;
|
|
toBeDefined(): void;
|
|
toBeGreaterThan(n: number): void;
|
|
toBeGreaterThanOrEqual(n: number): void;
|
|
toBeLessThan(n: number): void;
|
|
toBeCloseTo(n: number, digits?: number): void;
|
|
readonly not: Matchers;
|
|
}
|
|
|
|
export interface Expect {
|
|
(actual: unknown, message?: string): Matchers;
|
|
objectContaining(expected: Record<string, unknown>): Json;
|
|
arrayContaining(expected: unknown[]): Json;
|
|
any(constructor: unknown): Json;
|
|
}
|
|
|
|
export interface Scenario {
|
|
name: string;
|
|
run(api: ContractApi, expect: Expect): Promise<void>;
|
|
}
|
|
|
|
export function scenario(name: string, run: Scenario['run']): Scenario {
|
|
return { name, run };
|
|
}
|
|
|
|
/** A write that worked: any 2xx. The message carries the answer, so a refusal says why. */
|
|
export function expectOk(expect: Expect, reply: Reply, what: string): void {
|
|
const said = `${what} answered ${reply.status}: ${JSON.stringify(reply.body)}`;
|
|
expect(reply.status, said).toBeGreaterThanOrEqual(200);
|
|
expect(reply.status, said).toBeLessThan(300);
|
|
}
|
|
|
|
/** Adds a row and answers its id, which both runtimes return as `id`. */
|
|
export async function added(
|
|
api: ContractApi,
|
|
expect: Expect,
|
|
path: string,
|
|
body: unknown
|
|
): Promise<number> {
|
|
const reply = await api.post(path, body);
|
|
expectOk(expect, reply, `POST ${path}`);
|
|
expect(reply.body?.id, `POST ${path} answered ${JSON.stringify(reply.body)}`).toEqual(
|
|
expect.any(Number)
|
|
);
|
|
return reply.body.id as number;
|
|
}
|