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
67 changes: 67 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"recharts": "^2.12.7",
"remark-gfm": "^4.0.1",
"swr": "^2.4.2",
"tesseract.js": "^7.0.0",
"web-push": "^3.6.7",
"zod": "^3.25.76",
"zustand": "^5.0.14"
Expand Down
21 changes: 20 additions & 1 deletion src/__tests__/earningsForecast.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forecastEarnings, totalForecast } from "@/lib/earningsForecast";
import { forecastEarnings, forecastEarningsDetailed, totalForecast } from "@/lib/earningsForecast";

describe("forecastEarnings", () => {
it("projects a linear trend", () => {
Expand All @@ -23,4 +23,23 @@ describe("forecastEarnings", () => {
expect(forecastEarnings([], 3)).toEqual([]);
expect(forecastEarnings([{ label: "x", value: 1 }], 0)).toEqual([{ label: "x", actual: 1 }]);
});

it("evaluates candidate models using walk-forward error metrics", () => {
const history = [10, 20, 30, 40, 50, 60, 70, 80].map((value, index) => ({ label: `m${index}`, value }));
const result = forecastEarningsDetailed(history, 2);
expect(result.model).toBe("linear");
expect(result.metrics).toMatchObject({ mae: 0, rmse: 0, mape: 0, sampleCount: 5 });
expect(result.points[8].low).toBe(result.points[8].forecast);
expect(result.points[8].high).toBe(result.points[8].forecast);
expect(Number.isNaN(Date.parse(result.trainedAt))).toBe(false);
});

it("selects the model with lower walk-forward error for noisy history", () => {
const history = [10, 11, 10, 30, 10, 11, 10, 30, 10, 11, 10, 30].map((value, index) => ({ label: `m${index}`, value }));
const result = forecastEarningsDetailed(history, 2);
expect(result.model).toBe("moving-average");
expect(result.metrics.sampleCount).toBe(9);
expect(result.points.slice(-2).every((point) => point.low! <= point.forecast! && point.high! >= point.forecast!)).toBe(true);
expect(result.points[13].high! - result.points[13].forecast!).toBeGreaterThan(result.points[12].high! - result.points[12].forecast!);
});
});
30 changes: 30 additions & 0 deletions src/__tests__/exportHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
addExportHistoryEntry,
completeScheduledInvoiceExport,
getExportHistory,
getScheduledInvoiceExports,
scheduleInvoiceExport,
} from '@/lib/exportHistory';

describe('invoice export history and scheduling', () => {
beforeEach(() => window.localStorage.clear());

it('records completed exports with their format and invoice filters', () => {
addExportHistoryEntry({ format: 'xml', filters: { invoiceId: 'invoice-1' }, recordCount: 1, status: 'completed' });
expect(getExportHistory()).toMatchObject([{ format: 'xml', filters: { invoiceId: 'invoice-1' }, recordCount: 1, status: 'completed' }]);
});

it('persists a snapshot and marks a scheduled export complete', () => {
const job = scheduleInvoiceExport({
invoiceId: 'invoice-1',
format: 'json',
scheduledAt: new Date(Date.now() + 60_000).toISOString(),
content: '{"id":"invoice-1"}',
mimeType: 'application/json',
extension: 'json',
});
expect(getScheduledInvoiceExports()).toContainEqual(job);
completeScheduledInvoiceExport(job.id);
expect(getScheduledInvoiceExports()[0].status).toBe('completed');
});
});
32 changes: 32 additions & 0 deletions src/__tests__/invoiceFormatExport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { generateInvoiceExport } from '@/lib/invoiceFormatExport';
import type { Invoice } from '@stellar-split/sdk';

const invoice: Invoice = {
id: 'invoice-1',
creator: 'G<creator>',
recipients: [{ address: 'G&recipient', amount: 12500000n }],
payments: [{ payer: 'G"payer', amount: 2500000n }],
status: 'Pending',
funded: 2500000n,
token: 'USDC',
deadline: 0,
};

describe('generateInvoiceExport', () => {
it('preserves amount precision in JSON', () => {
const result = generateInvoiceExport(invoice, 12500000n, 'json');
expect(result.extension).toBe('json');
expect(JSON.parse(result.content)).toMatchObject({ total: '1.2500000', funded: '0.2500000' });
});

it('escapes XML and HTML values', () => {
expect(generateInvoiceExport(invoice, 12500000n, 'xml').content).toContain('G&lt;creator&gt;');
expect(generateInvoiceExport(invoice, 12500000n, 'html').content).toContain('G&amp;recipient');
});

it('renders values into a custom template', () => {
const result = generateInvoiceExport(invoice, 12500000n, 'custom', '{{id}}: {{total}} {{token}}');
expect(result.content).toBe('invoice-1: 1.2500000 USDC');
expect(result.extension).toBe('txt');
});
});
28 changes: 28 additions & 0 deletions src/__tests__/invoiceOcr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getInvoiceOcrAnalytics, recognizeInvoiceFields, recordInvoiceOcrReview } from '@/lib/invoiceOcr';

describe('recognizeInvoiceFields', () => {
it('extracts invoice fields and scores recognized values', () => {
const address = `G${'A'.repeat(55)}`;
const result = recognizeInvoiceFields(`Recipient ${address}. Total due: $1,234.50 USDC. Due in 14 days.`, 90);
expect(result.fields).toEqual({ recipient_address: address, amount: '1234.50', deadline_days: '14', token: 'USDC' });
expect(result.confidence.recipient_address).toBe(90);
expect(result.confidence.amount).toBeGreaterThan(70);
});

it('leaves unrecognized values empty with zero confidence', () => {
const result = recognizeInvoiceFields('An invoice without recognized fields', 75);
expect(result.fields.amount).toBe('');
expect(result.confidence.amount).toBe(0);
});
});

describe('OCR correction analytics', () => {
beforeEach(() => window.localStorage.clear());

it('tracks reviewed and corrected fields without storing invoice contents', () => {
const original = { recipient_address: 'G1', amount: '1', deadline_days: '5', token: 'USDC' };
recordInvoiceOcrReview(original, { ...original, amount: '2' });
expect(getInvoiceOcrAnalytics()).toEqual({ extractions: 1, reviewedFields: 4, correctedFields: 1 });
expect(window.localStorage.getItem('invoice_ocr_analytics_v1')).not.toContain('G1');
});
});
Loading