The dashboard, the deadline engine, the three sweeps and the notification channels. Confirm a comprobante and the IVA and IRP figures move; every headline number opens the documents behind it. The dashboard does no arithmetic of its own: it picks inputs and calls packages/rules. The acceptance test asserts the API's numbers equal what computeF120 and computeF515 produce over the same rows, before and after the bandeja is confirmed, so a drift in either direction fails. Deadlines come from the calendario perpetuo, skipping any period earlier than the date the taxpayer took the obligation on. Without that guard a brand new account opens on a red overdue card for a period that predates it. Sweeps are idempotent by dedupe key rather than bookkeeping: a restarted poller, a second replica and a crash mid-sweep all converge on one run, and one reminder per user per period per milestone. They queue notifications rather than sending them, so a channel being down retries on the job schedule. The T-10 test derives the date from dueDateFor rather than restating the calendar. Auto-confirm only touches what the rules were confident about and never a decision the user already made. An unconfigured channel is absent rather than broken: the fan-out skips it, the UI hides it, and a frozen account receives nothing. Web: the dashboard's three zones with swipeable position cards and traceable numbers, the deadlines timeline, and the app shell with the tab bar and the persistent scan button FLOWS.md asks for. Two additions to the specs, both marked: hasDocuments on DashboardDto, without which a position of all zeros is indistinguishable from a real one and the first-run state never shows; and an insight_dismissals table, which FLOWS.md requires and SPEC.md has nowhere to put. Also: pnpm db:reset, because the e2e suite changes the seed it runs against and a suite that is not repeatable is not a suite. 280 vitest tests, 51 Playwright tests, rules coverage still 100%, typecheck and lint clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
109 lines
3.9 KiB
TypeScript
109 lines
3.9 KiB
TypeScript
import { mkdtempSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { createAuth } from '../auth/options';
|
|
import { createDb } from '../db/index';
|
|
import { migrateToLatest } from '../db/migrator';
|
|
import { seed } from '../db/seed';
|
|
import { createApp, type AppHandle } from '../http/app';
|
|
import type { AppDeps } from '../http/context';
|
|
import { type Env, parseEnv } from '../lib/env';
|
|
import { createHandlers, createPoller, type Poller } from '../modules/jobs';
|
|
import type { OcrProvider } from '../modules/documents/ocr';
|
|
import { createChannels, type Channels } from '../modules/notifications';
|
|
import { createStorage, type StorageDriver } from '../modules/storage';
|
|
|
|
export const TEST_ENV: Record<string, string> = {
|
|
NODE_ENV: 'test',
|
|
DATABASE_URL: 'sqlite::memory:',
|
|
BETTER_AUTH_SECRET: 'test-secret-that-is-long-enough-32chars',
|
|
BETTER_AUTH_URL: 'http://localhost:3000',
|
|
APP_PUBLIC_URL: 'http://localhost:3000',
|
|
};
|
|
|
|
export interface HarnessOptions {
|
|
env?: Record<string, string>;
|
|
/** Stand in for the Anthropic call, so OCR paths are testable without a key. */
|
|
ocr?: OcrProvider | null;
|
|
storage?: StorageDriver;
|
|
channels?: Channels;
|
|
/** Freezes the clock, so seeded dates and dashboard figures are deterministic. */
|
|
now?: Date;
|
|
}
|
|
|
|
export interface Harness extends AppHandle {
|
|
deps: AppDeps;
|
|
env: Env;
|
|
storage: StorageDriver;
|
|
channels: Channels;
|
|
/** The clock the harness was built with, frozen when one was supplied. */
|
|
now: () => Date;
|
|
/** Drains the job queue. Tests call this instead of waiting on the interval. */
|
|
runJobs: () => Promise<number>;
|
|
poller: Poller;
|
|
close: () => Promise<void>;
|
|
/** Signs in a seeded account and returns the cookie header for later requests. */
|
|
signIn: (email: string, password: string) => Promise<string>;
|
|
}
|
|
|
|
export async function createHarness(options: HarnessOptions = {}): Promise<Harness> {
|
|
const parsed = parseEnv({ ...TEST_ENV, ...(options.env ?? {}) });
|
|
if (!parsed.ok || !parsed.env) throw new Error(parsed.message);
|
|
const env = parsed.env;
|
|
|
|
const handle = createDb(env.DATABASE_URL);
|
|
await migrateToLatest(handle, env);
|
|
|
|
const auth = createAuth({ db: handle.db, dialect: handle.dialect, env, sendOtp: async () => undefined });
|
|
await seed(handle, auth, options.now ? { now: options.now } : {});
|
|
|
|
const channels = options.channels ?? createChannels(env);
|
|
const now = options.now ? () => options.now as Date : () => new Date();
|
|
|
|
const storage =
|
|
options.storage ??
|
|
createStorage({ ...env, STORAGE_DRIVER: 'local', STORAGE_LOCAL_PATH: mkdtempSync(join(tmpdir(), 'impuestos-test-')) });
|
|
const ocr = options.ocr ?? null;
|
|
const ingest = { db: handle.db, storage, ocrEnabled: ocr !== null };
|
|
|
|
const deps: AppDeps = { env, handle, auth, storage, ingest };
|
|
const appHandle = createApp(deps);
|
|
|
|
const poller = createPoller({
|
|
db: handle.db,
|
|
dialect: handle.dialect,
|
|
handlers: createHandlers({ db: handle.db, storage, ocr, channels, now }),
|
|
intervalMs: env.JOBS_POLL_INTERVAL_MS,
|
|
staleMinutes: env.JOBS_STALE_MINUTES,
|
|
// Tests queue exactly the work they mean to exercise.
|
|
scheduleSweeps: false,
|
|
now,
|
|
});
|
|
|
|
return {
|
|
...appHandle,
|
|
deps,
|
|
env,
|
|
storage,
|
|
channels,
|
|
now,
|
|
poller,
|
|
runJobs: () => poller.runOnce(),
|
|
close: async () => {
|
|
await poller.stop();
|
|
await handle.close();
|
|
},
|
|
signIn: async (email, password) => {
|
|
const response = await appHandle.app.request('/api/auth/sign-in/email', {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
if (!response.ok) throw new Error(`sign in failed: ${response.status} ${await response.text()}`);
|
|
const cookie = response.headers.get('set-cookie');
|
|
if (!cookie) throw new Error('sign in returned no cookie');
|
|
return cookie.split(';')[0] ?? '';
|
|
},
|
|
};
|
|
}
|