mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 10:24:02 +00:00
Fix topic message history persistence
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { afterAll, describe, expect, test } from 'bun:test'
|
||||
import { inArray } from 'drizzle-orm'
|
||||
|
||||
import { instantFromIso } from '@household/domain'
|
||||
import { createDbClient, schema } from '@household/db'
|
||||
|
||||
import { createDbHouseholdConfigurationRepository } from './household-config-repository'
|
||||
import { createDbTopicMessageHistoryRepository } from './topic-message-history-repository'
|
||||
|
||||
const databaseUrl = process.env.DATABASE_URL
|
||||
const testIfDatabase = databaseUrl ? test : test.skip
|
||||
|
||||
describe('createDbTopicMessageHistoryRepository', () => {
|
||||
const createdHouseholdIds: string[] = []
|
||||
|
||||
afterAll(async () => {
|
||||
if (!databaseUrl || createdHouseholdIds.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const { db, queryClient } = createDbClient(databaseUrl, {
|
||||
max: 1,
|
||||
prepare: false
|
||||
})
|
||||
|
||||
await db.delete(schema.households).where(inArray(schema.households.id, createdHouseholdIds))
|
||||
await queryClient.end({ timeout: 5 })
|
||||
})
|
||||
|
||||
testIfDatabase('lists the latest same-day chat messages in chronological order', async () => {
|
||||
const householdClient = createDbHouseholdConfigurationRepository(databaseUrl!)
|
||||
const historyClient = createDbTopicMessageHistoryRepository(databaseUrl!)
|
||||
const telegramChatId = `-100${Date.now()}`
|
||||
const registered = await householdClient.repository.registerTelegramHouseholdChat({
|
||||
householdName: `History Household ${randomUUID()}`,
|
||||
telegramChatId,
|
||||
telegramChatType: 'supergroup',
|
||||
title: 'History Household'
|
||||
})
|
||||
|
||||
createdHouseholdIds.push(registered.household.householdId)
|
||||
|
||||
const baseMessage = {
|
||||
householdId: registered.household.householdId,
|
||||
telegramChatId,
|
||||
telegramThreadId: '777',
|
||||
senderTelegramUserId: '10002',
|
||||
senderDisplayName: 'Mia',
|
||||
isBot: false
|
||||
} as const
|
||||
|
||||
await historyClient.repository.saveMessage({
|
||||
...baseMessage,
|
||||
telegramMessageId: 'msg-1',
|
||||
telegramUpdateId: 'upd-1',
|
||||
rawText: '08:00',
|
||||
messageSentAt: instantFromIso('2026-03-05T08:00:00.000Z')
|
||||
})
|
||||
await historyClient.repository.saveMessage({
|
||||
...baseMessage,
|
||||
telegramMessageId: 'msg-2',
|
||||
telegramUpdateId: 'upd-2',
|
||||
rawText: '10:00',
|
||||
messageSentAt: instantFromIso('2026-03-05T10:00:00.000Z')
|
||||
})
|
||||
await historyClient.repository.saveMessage({
|
||||
...baseMessage,
|
||||
telegramMessageId: 'msg-3',
|
||||
telegramUpdateId: 'upd-3',
|
||||
rawText: '11:00',
|
||||
messageSentAt: instantFromIso('2026-03-05T11:00:00.000Z')
|
||||
})
|
||||
await historyClient.repository.saveMessage({
|
||||
...baseMessage,
|
||||
telegramMessageId: 'msg-4',
|
||||
telegramUpdateId: 'upd-4',
|
||||
rawText: '12:00',
|
||||
messageSentAt: instantFromIso('2026-03-05T12:00:00.000Z')
|
||||
})
|
||||
|
||||
const rows = await historyClient.repository.listRecentChatMessages({
|
||||
householdId: registered.household.householdId,
|
||||
telegramChatId,
|
||||
sentAtOrAfter: instantFromIso('2026-03-05T00:00:00.000Z'),
|
||||
limit: 2
|
||||
})
|
||||
|
||||
expect(rows.map((row) => row.rawText)).toEqual(['11:00', '12:00'])
|
||||
|
||||
await householdClient.close()
|
||||
await historyClient.close()
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { and, asc, desc, eq, gte, isNotNull } from 'drizzle-orm'
|
||||
import { and, desc, eq, gte, isNotNull } from 'drizzle-orm'
|
||||
|
||||
import { instantFromDatabaseValue, instantToDate } from '@household/domain'
|
||||
import { createDbClient, schema } from '@household/db'
|
||||
@@ -72,10 +72,10 @@ export function createDbTopicMessageHistoryRepository(databaseUrl: string): {
|
||||
gte(schema.topicMessages.messageSentAt, instantToDate(input.sentAtOrAfter))
|
||||
)
|
||||
)
|
||||
.orderBy(asc(schema.topicMessages.messageSentAt), asc(schema.topicMessages.createdAt))
|
||||
.orderBy(desc(schema.topicMessages.messageSentAt), desc(schema.topicMessages.createdAt))
|
||||
.limit(input.limit)
|
||||
|
||||
return rows.map((row) => ({
|
||||
return rows.reverse().map((row) => ({
|
||||
householdId: row.householdId,
|
||||
telegramChatId: row.telegramChatId,
|
||||
telegramThreadId: row.telegramThreadId,
|
||||
|
||||
Reference in New Issue
Block a user