mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 21:31:46 +00:00
247433fb2a
* fix(journey): authorize reads of the journey share link GET /api/journeys/:id/share-link now requires journey access (canAccessJourney), matching the create/delete share-link routes and the get_journey_share_link MCP tool. Returns no link when the caller lacks access to the journey. * feat(costs): rework Budget into Costs — Splitwise-style, multi-currency, mobile Renames the Budget addon to "Costs" (UI only) and reworks it into a Tricount/ Splitwise-style cost tracker: multiple payers per expense, equal split across chosen members, settle-up with persisted history + undo, 12 fixed categories, per-expense currency with live FX conversion to a user-set display currency (Settings -> Display), and locale-correct money formatting. Adds a desktop and a dedicated mobile layout. A migration backfills existing budget items (single payer, split members, currency). Closes #551 (per-expense currency). Also switches the app font to self-hosted Poppins (Geist for secondary subtext), replacing the Google Fonts CDN dependency. * fix(costs): neutral dashboard dark palette + liquid glass, full page width, entry-count badge - Dark mode used a warm oklch palette that read brownish; switch to the neutral zinc tokens used by the dashboard (#121215 bg, #f4f4f5 ink) and add a subtle backdrop-blur glass on cards. - Costs now uses the full available page width on desktop instead of a 1280px cap. - Render the expense count next to the Expenses title as a badge. - Adapt budget/journey unit tests to the new payer-based settlement model and the Costs rename (category default 'other', Costs tab/CostsPanel). * fix(costs): drop the entry-count badge, always show row edit/delete actions Removes the count badge next to the Expenses title and makes the per-row edit/delete actions permanently visible (no longer hover-only) on desktop too. * feat(costs): currency-native money formatting, custom select/date, rename addon to Costs - Format every amount in its own currency convention (symbol position, grouping and decimal separators) regardless of app language, via a currency->locale map (EUR -> '12,00 €', USD -> '$12.00', JPY -> '¥12', ...). Previously Intl used the app locale, so EUR showed the symbol in front under an English UI. - Use TREK's CustomSelect (searchable, with symbols) and CustomDatePicker in the add/edit expense modal instead of the native <select>/<input type=date>. - Rename the 'Budget Planner' add-on to 'Costs' in the admin list (display only; id/tables/permissions/MCP stay 'budget') via seed + a migration for existing DBs. * feat(auth): configurable session duration via SESSION_DURATION Adds a SESSION_DURATION env var (ms-style strings: 1h, 7d, 30d, ...) controlling how long a session stays valid before re-login. It drives both the trek_session JWT exp claim and the cookie maxAge from one source, so they never drift. Invalid values warn at startup and fall back to the default (24h — unchanged). The MFA challenge token and MCP OAuth tokens keep their own TTL. Implements the request from discussion #946. Documented in the env-var wiki page, .env.example and docker-compose.yml.
193 lines
7.5 KiB
TypeScript
193 lines
7.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
// ── DB mock setup ────────────────────────────────────────────────────────────
|
|
|
|
const mockDb = vi.hoisted(() => {
|
|
return {
|
|
db: {
|
|
prepare: vi.fn(() => ({
|
|
all: vi.fn(() => []),
|
|
get: vi.fn(() => undefined),
|
|
run: vi.fn(),
|
|
})),
|
|
},
|
|
canAccessTrip: vi.fn(() => true),
|
|
};
|
|
});
|
|
|
|
vi.mock('../../../src/db/database', () => mockDb);
|
|
|
|
import { calculateSettlement } from '../../../src/services/budgetService';
|
|
import type { BudgetItem, BudgetItemMember, BudgetItemPayer } from '../../../src/types';
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
// Who actually paid is recorded as explicit payers (budget_item_payers); members
|
|
// are only the equal-split participants.
|
|
|
|
function makeItem(id: number, total_price: number, trip_id = 1): BudgetItem {
|
|
return { id, trip_id, name: `Item ${id}`, total_price, category: 'other' } as BudgetItem;
|
|
}
|
|
|
|
function makeMember(budget_item_id: number, user_id: number, username: string): BudgetItemMember & { budget_item_id: number } {
|
|
return { budget_item_id, user_id, paid: 0, username, avatar: null } as BudgetItemMember & { budget_item_id: number };
|
|
}
|
|
|
|
function makePayer(budget_item_id: number, user_id: number, amount: number, username: string): BudgetItemPayer & { budget_item_id: number } {
|
|
return { budget_item_id, user_id, amount, username, avatar: null } as BudgetItemPayer & { budget_item_id: number };
|
|
}
|
|
|
|
function setupDb(
|
|
items: BudgetItem[],
|
|
members: (BudgetItemMember & { budget_item_id: number })[],
|
|
payers: (BudgetItemPayer & { budget_item_id: number })[] = [],
|
|
) {
|
|
mockDb.db.prepare.mockImplementation((sql: string) => {
|
|
if (sql.includes('SELECT * FROM budget_items')) {
|
|
return { all: vi.fn(() => items), get: vi.fn(), run: vi.fn() };
|
|
}
|
|
if (sql.includes('budget_item_members')) {
|
|
return { all: vi.fn(() => members), get: vi.fn(), run: vi.fn() };
|
|
}
|
|
if (sql.includes('budget_item_payers')) {
|
|
return { all: vi.fn(() => payers), get: vi.fn(), run: vi.fn() };
|
|
}
|
|
// budget_settlements and anything else → empty
|
|
return { all: vi.fn(() => []), get: vi.fn(), run: vi.fn() };
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
setupDb([], [], []);
|
|
});
|
|
|
|
// ── calculateSettlement ──────────────────────────────────────────────────────
|
|
|
|
describe('calculateSettlement', () => {
|
|
it('returns empty balances and flows when trip has no items', () => {
|
|
setupDb([], [], []);
|
|
const result = calculateSettlement(1);
|
|
expect(result.balances).toEqual([]);
|
|
expect(result.flows).toEqual([]);
|
|
});
|
|
|
|
it('returns no flows when there are items but no members', () => {
|
|
setupDb([makeItem(1, 100)], [], [makePayer(1, 1, 100, 'alice')]);
|
|
const result = calculateSettlement(1);
|
|
expect(result.flows).toEqual([]);
|
|
});
|
|
|
|
it('returns no flows when no one has paid', () => {
|
|
setupDb(
|
|
[makeItem(1, 100)],
|
|
[makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob')],
|
|
[],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
expect(result.flows).toEqual([]);
|
|
});
|
|
|
|
it('2 members, 1 payer: payer is owed half, non-payer owes half', () => {
|
|
// Item: $100. Alice paid all, [Alice, Bob] split. Each owes $50. Alice net: +$50. Bob: -$50.
|
|
setupDb(
|
|
[makeItem(1, 100)],
|
|
[makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob')],
|
|
[makePayer(1, 1, 100, 'alice')],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
const alice = result.balances.find(b => b.user_id === 1)!;
|
|
const bob = result.balances.find(b => b.user_id === 2)!;
|
|
expect(alice.balance).toBe(50);
|
|
expect(bob.balance).toBe(-50);
|
|
expect(result.flows).toHaveLength(1);
|
|
expect(result.flows[0].from.user_id).toBe(2); // Bob owes
|
|
expect(result.flows[0].to.user_id).toBe(1); // Alice is owed
|
|
expect(result.flows[0].amount).toBe(50);
|
|
});
|
|
|
|
it('3 members, 1 payer: correct 3-way split', () => {
|
|
// Item: $90. Alice paid. Each of 3 owes $30. Alice net: +$60. Bob: -$30. Carol: -$30.
|
|
setupDb(
|
|
[makeItem(1, 90)],
|
|
[makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob'), makeMember(1, 3, 'carol')],
|
|
[makePayer(1, 1, 90, 'alice')],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
const alice = result.balances.find(b => b.user_id === 1)!;
|
|
const bob = result.balances.find(b => b.user_id === 2)!;
|
|
const carol = result.balances.find(b => b.user_id === 3)!;
|
|
expect(alice.balance).toBe(60);
|
|
expect(bob.balance).toBe(-30);
|
|
expect(carol.balance).toBe(-30);
|
|
expect(result.flows).toHaveLength(2);
|
|
});
|
|
|
|
it('all paid equally: all balances are zero, no flows', () => {
|
|
// Item: $60. 3 members, each paid $20 and owes $20. Net: 0 for everyone.
|
|
setupDb(
|
|
[makeItem(1, 60)],
|
|
[makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob'), makeMember(1, 3, 'carol')],
|
|
[makePayer(1, 1, 20, 'alice'), makePayer(1, 2, 20, 'bob'), makePayer(1, 3, 20, 'carol')],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
for (const b of result.balances) {
|
|
expect(Math.abs(b.balance)).toBeLessThanOrEqual(0.01);
|
|
}
|
|
expect(result.flows).toHaveLength(0);
|
|
});
|
|
|
|
it('flow direction: from is debtor (owes), to is creditor (is owed)', () => {
|
|
// Alice paid $100 for 2 people. Bob owes Alice $50.
|
|
setupDb(
|
|
[makeItem(1, 100)],
|
|
[makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob')],
|
|
[makePayer(1, 1, 100, 'alice')],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
const flow = result.flows[0];
|
|
expect(flow.from.username).toBe('bob'); // debtor
|
|
expect(flow.to.username).toBe('alice'); // creditor
|
|
});
|
|
|
|
it('amounts are rounded to 2 decimal places', () => {
|
|
// Item: $10. 3 members, 1 payer. Share = 3.333... Each rounded to 3.33.
|
|
setupDb(
|
|
[makeItem(1, 10)],
|
|
[makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob'), makeMember(1, 3, 'carol')],
|
|
[makePayer(1, 1, 10, 'alice')],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
for (const b of result.balances) {
|
|
const str = b.balance.toString();
|
|
const decimals = str.includes('.') ? str.split('.')[1].length : 0;
|
|
expect(decimals).toBeLessThanOrEqual(2);
|
|
}
|
|
for (const flow of result.flows) {
|
|
const str = flow.amount.toString();
|
|
const decimals = str.includes('.') ? str.split('.')[1].length : 0;
|
|
expect(decimals).toBeLessThanOrEqual(2);
|
|
}
|
|
});
|
|
|
|
it('2 items with different payers: aggregates balances correctly', () => {
|
|
// Item 1: $100, Alice paid, [Alice, Bob] (Alice net: +50, Bob: -50)
|
|
// Item 2: $60, Bob paid, [Alice, Bob] (Bob net: +30, Alice: -30)
|
|
// Final: Alice: +50 - 30 = +20, Bob: -50 + 30 = -20
|
|
setupDb(
|
|
[makeItem(1, 100), makeItem(2, 60)],
|
|
[
|
|
makeMember(1, 1, 'alice'), makeMember(1, 2, 'bob'),
|
|
makeMember(2, 1, 'alice'), makeMember(2, 2, 'bob'),
|
|
],
|
|
[makePayer(1, 1, 100, 'alice'), makePayer(2, 2, 60, 'bob')],
|
|
);
|
|
const result = calculateSettlement(1);
|
|
const alice = result.balances.find(b => b.user_id === 1)!;
|
|
const bob = result.balances.find(b => b.user_id === 2)!;
|
|
expect(alice.balance).toBe(20);
|
|
expect(bob.balance).toBe(-20);
|
|
expect(result.flows).toHaveLength(1);
|
|
expect(result.flows[0].amount).toBe(20);
|
|
});
|
|
});
|