import { testDatabase } from '../../test/database'; import { FakeContext, message } from '../../test/fakes'; import { BotStore } from '../../core/db/bot-store'; import { pluginScope } from '../../core/plugin/room-runtime'; import { CheckinPlugin, checkinConfig } from './checkin.plugin'; import { CommandRouter } from '../../core/command/command-router'; import { migrateCheckin } from '../../core/plugin/migrate-checkin'; import { rootConfigSchema } from '../../config/schema'; let database: Awaited> | undefined; beforeAll(async () => { database = await testDatabase(); }); afterAll(async () => { await database?.close(); }); afterEach(async () => { await database?.prisma.botState.deleteMany(); }); const roomId = '!control:example.org'; const user = '@member:example.org'; const config = () => rootConfigSchema.parse({ accounts: { test: { userId: '@bot:example.org', password: 'fixture', homeServer: 'https://example.org' } }, rooms: [{ account: 'test', roomId, plugins: { checkin: { enabled: true } } }], legacyCheckinTarget: { account: 'test', roomId } }); function plugin(room = roomId, account = 'test') { const ctx = new FakeContext(new BotStore(database!.prisma, pluginScope(account, room, 'checkin'))); const bot = new CheckinPlugin(ctx, checkinConfig.parse({})); const router = new CommandRouter(bot.commands); return { ctx, bot, run: async (text: string, actor = user) => { const match = router.match(message(text, actor))!; await match.command.run(match.invocation); } }; } describe('check-in with isolated SQLite', () => { it('enforces unique user/date records across concurrent plugin instances', async () => { const a = plugin(), b = plugin(); await Promise.all([a.run('签到'), b.run('!checkin'), a.run('!签到')]); expect(await a.bot.records()).toHaveLength(1); expect((await b.bot.stats()).get(user)?.total).toBe(1); }); it('keeps room and account data separate in the same database', async () => { const a = plugin(), b = plugin('!second:example.org'), c = plugin(roomId, 'second-account'); await a.run('签到'); await b.run('签到'); expect((await a.bot.stats()).get(user)?.total).toBe(1); expect((await b.bot.stats()).get(user)?.total).toBe(1); expect((await c.bot.stats()).size).toBe(0); }); it('uses native room permissions for administration instead of removed level commands', async () => { const a = plugin(); await a.run('签到'); await a.run('!查询 全部', '@member:example.org'); expect(a.ctx.transport.sent.at(-1)?.text).toContain('无权限'); await a.run('!查询 全部 --原始', '@admin:example.org'); expect(JSON.parse(a.ctx.transport.sent.at(-1)!.text)[0].total).toBe(1); }); it('migrates legacy data once, preserves source and avoids counting today twice', async () => { const legacy = new BotStore(database!.prisma, 'checkin'); await legacy.set(user, { checkinTimesInMonth: 8, checkinUnixMs: Date.parse('2026-01-15T01:00:00Z'), isCheckinToday: true }); await migrateCheckin(database!.prisma, config()); await migrateCheckin(database!.prisma, config()); const a = plugin(); await a.run('签到'); expect((await a.bot.stats()).get(user)).toMatchObject({ total: 8, month: 8, today: true }); expect(await legacy.get(user)).not.toBeNull(); a.ctx.date = new Date('2026-02-01T01:00:00Z'); await a.run('签到'); expect((await a.bot.stats()).get(user)).toMatchObject({ total: 9, month: 1 }); }); it('refuses ambiguous targets, existing destination data and reassignment', async () => { const legacy = new BotStore(database!.prisma, 'checkin'); await legacy.set(user, { checkinTimesInMonth: 1, checkinUnixMs: Date.parse('2026-01-15T01:00:00Z') }); await expect(migrateCheckin(database!.prisma, { ...config(), legacyCheckinTarget: undefined })).rejects.toThrow('explicit'); const a = plugin(); await a.run('签到'); await expect(migrateCheckin(database!.prisma, config())).rejects.toThrow('already contains'); await database!.prisma.botState.deleteMany({ where: { botName: pluginScope('test', roomId, 'checkin') } }); await migrateCheckin(database!.prisma, config()); const other = config(); other.rooms[0].roomId = '!second:example.org'; other.legacyCheckinTarget!.roomId = '!second:example.org'; await expect(migrateCheckin(database!.prisma, other)).rejects.toThrow('another room'); }); it('rolls back the whole migration when a legacy record is invalid', async () => { const legacy = new BotStore(database!.prisma, 'checkin'); await legacy.set(user, { checkinTimesInMonth: 1, checkinUnixMs: Date.parse('2026-01-15T01:00:00Z') }); await legacy.set('@invalid:example.org', { checkinTimesInMonth: -1 }); await expect(migrateCheckin(database!.prisma, config())).rejects.toThrow('Invalid'); expect(await database!.prisma.botState.count({ where: { botName: pluginScope('test', roomId, 'checkin') } })).toBe(0); }); });