mirror of
https://github.com/whekin/household-bot.git
synced 2026-04-01 00:04:02 +00:00
feat(bot): implement household status summary
This commit is contained in:
286
apps/bot/src/finance-commands.test.ts
Normal file
286
apps/bot/src/finance-commands.test.ts
Normal file
@@ -0,0 +1,286 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import type { FinanceCommandService } from '@household/application'
|
||||
import { Money, instantFromIso } from '@household/domain'
|
||||
import type { HouseholdConfigurationRepository } from '@household/ports'
|
||||
|
||||
import { createTelegramBot } from './bot'
|
||||
import { createFinanceCommandsService } from './finance-commands'
|
||||
|
||||
function householdStatusUpdate(languageCode: string) {
|
||||
return {
|
||||
update_id: 9100,
|
||||
message: {
|
||||
message_id: 10,
|
||||
date: Math.floor(Date.now() / 1000),
|
||||
chat: {
|
||||
id: -100123456,
|
||||
type: 'supergroup',
|
||||
title: 'Kojori'
|
||||
},
|
||||
from: {
|
||||
id: 123456,
|
||||
is_bot: false,
|
||||
first_name: 'Stan',
|
||||
language_code: languageCode
|
||||
},
|
||||
text: '/household_status',
|
||||
entities: [
|
||||
{
|
||||
offset: 0,
|
||||
length: 17,
|
||||
type: 'bot_command'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createRepository(): HouseholdConfigurationRepository {
|
||||
return {
|
||||
registerTelegramHouseholdChat: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
getTelegramHouseholdChat: async () => ({
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
telegramChatId: '-100123456',
|
||||
telegramChatType: 'supergroup',
|
||||
title: 'Kojori',
|
||||
defaultLocale: 'ru'
|
||||
}),
|
||||
getHouseholdChatByHouseholdId: async () => null,
|
||||
bindHouseholdTopic: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
getHouseholdTopicBinding: async () => null,
|
||||
findHouseholdTopicByTelegramContext: async () => null,
|
||||
listHouseholdTopicBindings: async () => [],
|
||||
listReminderTargets: async () => [],
|
||||
upsertHouseholdJoinToken: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
getHouseholdJoinToken: async () => null,
|
||||
getHouseholdByJoinToken: async () => null,
|
||||
upsertPendingHouseholdMember: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
getPendingHouseholdMember: async () => null,
|
||||
findPendingHouseholdMemberByTelegramUserId: async () => null,
|
||||
ensureHouseholdMember: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
getHouseholdMember: async () => null,
|
||||
listHouseholdMembers: async () => [],
|
||||
listHouseholdMembersByTelegramUserId: async () => [
|
||||
{
|
||||
id: 'member-1',
|
||||
householdId: 'household-1',
|
||||
telegramUserId: '123456',
|
||||
displayName: 'Stan',
|
||||
preferredLocale: 'ru',
|
||||
householdDefaultLocale: 'ru',
|
||||
rentShareWeight: 1,
|
||||
isAdmin: true
|
||||
}
|
||||
],
|
||||
getHouseholdBillingSettings: async () => ({
|
||||
householdId: 'household-1',
|
||||
settlementCurrency: 'GEL',
|
||||
rentAmountMinor: 70000n,
|
||||
rentCurrency: 'USD',
|
||||
rentDueDay: 20,
|
||||
rentWarningDay: 17,
|
||||
utilitiesDueDay: 4,
|
||||
utilitiesReminderDay: 3,
|
||||
timezone: 'Asia/Tbilisi'
|
||||
}),
|
||||
updateHouseholdBillingSettings: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
listHouseholdUtilityCategories: async () => [],
|
||||
upsertHouseholdUtilityCategory: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
listPendingHouseholdMembers: async () => [],
|
||||
approvePendingHouseholdMember: async () => null,
|
||||
updateHouseholdDefaultLocale: async () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
updateMemberPreferredLocale: async () => null,
|
||||
promoteHouseholdAdmin: async () => null,
|
||||
updateHouseholdMemberRentShareWeight: async () => null
|
||||
}
|
||||
}
|
||||
|
||||
function createDashboard(): NonNullable<
|
||||
Awaited<ReturnType<FinanceCommandService['generateDashboard']>>
|
||||
> {
|
||||
return {
|
||||
period: '2026-03',
|
||||
currency: 'GEL',
|
||||
totalDue: Money.fromMajor('400', 'GEL'),
|
||||
totalPaid: Money.fromMajor('150', 'GEL'),
|
||||
totalRemaining: Money.fromMajor('250', 'GEL'),
|
||||
rentSourceAmount: Money.fromMajor('700', 'USD'),
|
||||
rentDisplayAmount: Money.fromMajor('1890', 'GEL'),
|
||||
rentFxRateMicros: 2_700_000n,
|
||||
rentFxEffectiveDate: '2026-03-17',
|
||||
members: [
|
||||
{
|
||||
memberId: 'member-1',
|
||||
displayName: 'Стас',
|
||||
rentShare: Money.fromMajor('200', 'GEL'),
|
||||
utilityShare: Money.fromMajor('20', 'GEL'),
|
||||
purchaseOffset: Money.fromMajor('-10', 'GEL'),
|
||||
netDue: Money.fromMajor('210', 'GEL'),
|
||||
paid: Money.fromMajor('100', 'GEL'),
|
||||
remaining: Money.fromMajor('110', 'GEL'),
|
||||
explanations: []
|
||||
},
|
||||
{
|
||||
memberId: 'member-2',
|
||||
displayName: 'Ион',
|
||||
rentShare: Money.fromMajor('200', 'GEL'),
|
||||
utilityShare: Money.fromMajor('20', 'GEL'),
|
||||
purchaseOffset: Money.fromMajor('10', 'GEL'),
|
||||
netDue: Money.fromMajor('190', 'GEL'),
|
||||
paid: Money.fromMajor('50', 'GEL'),
|
||||
remaining: Money.fromMajor('140', 'GEL'),
|
||||
explanations: []
|
||||
}
|
||||
],
|
||||
ledger: [
|
||||
{
|
||||
id: 'utility-1',
|
||||
kind: 'utility',
|
||||
title: 'Electricity',
|
||||
memberId: 'member-1',
|
||||
amount: Money.fromMajor('82', 'GEL'),
|
||||
currency: 'GEL',
|
||||
displayAmount: Money.fromMajor('82', 'GEL'),
|
||||
displayCurrency: 'GEL',
|
||||
fxRateMicros: null,
|
||||
fxEffectiveDate: null,
|
||||
actorDisplayName: 'Стас',
|
||||
occurredAt: instantFromIso('2026-03-10T12:00:00.000Z').toString(),
|
||||
paymentKind: null
|
||||
},
|
||||
{
|
||||
id: 'purchase-1',
|
||||
kind: 'purchase',
|
||||
title: 'Туалетная бумага',
|
||||
memberId: 'member-1',
|
||||
amount: Money.fromMajor('30', 'GEL'),
|
||||
currency: 'GEL',
|
||||
displayAmount: Money.fromMajor('30', 'GEL'),
|
||||
displayCurrency: 'GEL',
|
||||
fxRateMicros: null,
|
||||
fxEffectiveDate: null,
|
||||
actorDisplayName: 'Стас',
|
||||
occurredAt: instantFromIso('2026-03-09T12:00:00.000Z').toString(),
|
||||
paymentKind: null
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
function createFinanceService(): FinanceCommandService {
|
||||
return {
|
||||
getMemberByTelegramUserId: async (telegramUserId) =>
|
||||
telegramUserId === '123456'
|
||||
? {
|
||||
id: 'member-1',
|
||||
telegramUserId,
|
||||
displayName: 'Стас',
|
||||
rentShareWeight: 1,
|
||||
isAdmin: true
|
||||
}
|
||||
: null,
|
||||
getOpenCycle: async () => ({
|
||||
id: 'cycle-1',
|
||||
period: '2026-03',
|
||||
currency: 'GEL'
|
||||
}),
|
||||
ensureExpectedCycle: async () => ({
|
||||
id: 'cycle-1',
|
||||
period: '2026-03',
|
||||
currency: 'GEL'
|
||||
}),
|
||||
getAdminCycleState: async () => ({
|
||||
cycle: null,
|
||||
rentRule: null,
|
||||
utilityBills: []
|
||||
}),
|
||||
openCycle: async () => ({
|
||||
id: 'cycle-1',
|
||||
period: '2026-03',
|
||||
currency: 'GEL'
|
||||
}),
|
||||
closeCycle: async () => null,
|
||||
setRent: async () => null,
|
||||
addUtilityBill: async () => null,
|
||||
updateUtilityBill: async () => null,
|
||||
deleteUtilityBill: async () => false,
|
||||
updatePurchase: async () => null,
|
||||
deletePurchase: async () => false,
|
||||
addPayment: async () => null,
|
||||
updatePayment: async () => null,
|
||||
deletePayment: async () => false,
|
||||
generateDashboard: async () => createDashboard(),
|
||||
generateStatement: async () => null
|
||||
}
|
||||
}
|
||||
|
||||
describe('createFinanceCommandsService', () => {
|
||||
test('replies with a compact localized household status summary', async () => {
|
||||
const repository = createRepository()
|
||||
const financeService = createFinanceService()
|
||||
const bot = createTelegramBot('000000:test-token', undefined, repository)
|
||||
createFinanceCommandsService({
|
||||
householdConfigurationRepository: repository,
|
||||
financeServiceForHousehold: () => financeService
|
||||
}).register(bot)
|
||||
|
||||
bot.botInfo = {
|
||||
id: 999000,
|
||||
is_bot: true,
|
||||
first_name: 'Household Test Bot',
|
||||
username: 'household_test_bot',
|
||||
can_join_groups: true,
|
||||
can_read_all_group_messages: false,
|
||||
supports_inline_queries: false,
|
||||
can_connect_to_business: false,
|
||||
has_main_web_app: false,
|
||||
has_topics_enabled: true,
|
||||
allows_users_to_create_topics: false
|
||||
}
|
||||
|
||||
const calls: Array<{ method: string; payload: unknown }> = []
|
||||
bot.api.config.use(async (_prev, method, payload) => {
|
||||
calls.push({ method, payload })
|
||||
return {
|
||||
ok: true,
|
||||
result: {
|
||||
message_id: calls.length,
|
||||
date: Math.floor(Date.now() / 1000),
|
||||
chat: {
|
||||
id: -100123456,
|
||||
type: 'supergroup'
|
||||
},
|
||||
text: 'ok'
|
||||
}
|
||||
} as never
|
||||
})
|
||||
|
||||
await bot.handleUpdate(householdStatusUpdate('ru') as never)
|
||||
|
||||
const payload = calls[0]?.payload as { text?: string } | undefined
|
||||
expect(payload?.text).toContain('Статус дома за 2026-03')
|
||||
expect(payload?.text).toContain('Аренда: 700.00 USD (~1890.00 GEL)')
|
||||
expect(payload?.text).toContain('Коммуналка: 82.00 GEL')
|
||||
expect(payload?.text).toContain('Общие покупки: 30.00 GEL')
|
||||
expect(payload?.text).toContain(
|
||||
'- Стас: должен 210.00 GEL, оплачено 100.00 GEL, осталось 110.00 GEL'
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user