mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 21:31:46 +00:00
5bcadb3cc6
- Fix SEC-005: rewrite path traversal test to upload a real file, inject traversal filename into DB, and assert the download does not succeed - Fix SEC-007: rename misleading test description to reflect it tests rejection of an invalid token, not acceptance of a valid one - Delete health.test.ts: all 3 tests were exact duplicates of auth.test.ts and misc.test.ts - Remove duplicate describe blocks from misc.test.ts: Categories endpoint (duplicate of categories.test.ts) and App config (duplicate of auth.test.ts) - Remove TRIP-016 from trips.test.ts: weaker duplicate of TRIP-007 (no body assertion) - Remove API Keys describe block from profile.test.ts: canonical copy lives in security.test.ts where it belongs - Remove avatarUrl describe block from budgetService.test.ts: identical tests already exist in authService.test.ts; drop now-unused import - Add DB verification to ASSIGN-007 and PACK-006 reorder tests: query day_assignments / packing_items after PUT reorder to confirm order changed - Strengthen BUDGET-007/008/009: add member/payer setup and assert concrete values (total_paid, per-user balance, flow direction and amount) - Remove 6 pointless Map-semantics tests from inAppNotificationActions.test.ts; keep only the two built-in registration checks - Remove 5 passthrough tests from queryHelpers.test.ts; keep the 4 tests that cover actual flat-to-nested transformation logic
192 lines
7.1 KiB
TypeScript
192 lines
7.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
// ── DB mock setup ────────────────────────────────────────────────────────────
|
|
|
|
interface MockPrepared {
|
|
all: ReturnType<typeof vi.fn>;
|
|
get: ReturnType<typeof vi.fn>;
|
|
run: ReturnType<typeof vi.fn>;
|
|
}
|
|
|
|
const preparedMap: Record<string, MockPrepared> = {};
|
|
let defaultAll: ReturnType<typeof vi.fn>;
|
|
let defaultGet: ReturnType<typeof vi.fn>;
|
|
|
|
const mockDb = vi.hoisted(() => {
|
|
return {
|
|
db: {
|
|
prepare: vi.fn((sql: string) => {
|
|
return {
|
|
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 } from '../../../src/types';
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
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, paid: boolean | 0 | 1, username: string): BudgetItemMember & { budget_item_id: number } {
|
|
return {
|
|
budget_item_id,
|
|
user_id,
|
|
paid: paid ? 1 : 0,
|
|
username,
|
|
avatar: null,
|
|
} as BudgetItemMember & { budget_item_id: number };
|
|
}
|
|
|
|
function setupDb(items: BudgetItem[], members: (BudgetItemMember & { 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() };
|
|
}
|
|
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)], []);
|
|
const result = calculateSettlement(1);
|
|
expect(result.flows).toEqual([]);
|
|
});
|
|
|
|
it('returns no flows when no one is marked as paid', () => {
|
|
setupDb(
|
|
[makeItem(1, 100)],
|
|
[makeMember(1, 1, 0, 'alice'), makeMember(1, 2, 0, '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, Bob did not. Each owes $50. Alice net: +$50. Bob net: -$50.
|
|
setupDb(
|
|
[makeItem(1, 100)],
|
|
[makeMember(1, 1, 1, 'alice'), makeMember(1, 2, 0, '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(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, 1, 'alice'), makeMember(1, 2, 0, 'bob'), makeMember(1, 3, 0, 'carol')],
|
|
);
|
|
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, all paid equally (each paid $20, each owes $20). Net: 0.
|
|
// Actually with "paid" flag it means: paidPerPayer = item.total / numPayers.
|
|
// If all 3 paid: each gets +20 credit, each owes -20 = net 0 for everyone.
|
|
setupDb(
|
|
[makeItem(1, 60)],
|
|
[makeMember(1, 1, 1, 'alice'), makeMember(1, 2, 1, 'bob'), makeMember(1, 3, 1, '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, 1, 'alice'), makeMember(1, 2, 0, 'bob')],
|
|
);
|
|
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, 1, 'alice'), makeMember(1, 2, 0, 'bob'), makeMember(1, 3, 0, 'carol')],
|
|
);
|
|
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, 1, 'alice'), makeMember(1, 2, 0, 'bob'),
|
|
makeMember(2, 1, 0, 'alice'), makeMember(2, 2, 1, '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);
|
|
});
|
|
});
|