import definition, { CheckinPlugin, checkinConfig, recordKey } from './checkin.plugin'; import { CommandRouter } from '../../core/command/command-router'; import { FakeContext, message } from '../../test/fakes'; function setup(config: Record = {}) { const ctx = new FakeContext(); const plugin = new CheckinPlugin(ctx, checkinConfig.parse(config)); const router = new CommandRouter(plugin.commands); const run = async (text: string, user = '@member:example.org') => { const match = router.match(message(text, user)); if (!match) throw new Error('Missing command'); await match.command.run(match.invocation); }; return { ctx, plugin, run }; } const admin = '@admin:example.org', member = '@member:example.org'; const last = (ctx: FakeContext) => ctx.transport.sent.at(-1)!.text; describe('date-based check-in plugin', () => { it('Chinese and English commands share the atomic daily record', async () => { const { ctx, plugin, run } = setup(); await Promise.all([run('签到'), run('/checkin'), run('!签到')]); expect((await plugin.records())).toHaveLength(1); expect((await plugin.stats()).get(member)).toMatchObject({ total: 1, month: 1, today: true }); expect(ctx.transport.sent.filter((s) => s.text.includes('签到成功'))).toHaveLength(1); }); it('starts a new day at midnight in the configured timezone without a report/reset', async () => { const { ctx, plugin, run } = setup(); ctx.date = new Date('2026-01-15T15:59:59Z'); await run('!checkin'); ctx.date = new Date('2026-01-15T16:00:00Z'); await run('!checkin'); expect((await plugin.records()).map((r) => r.date)).toEqual(['2026-01-15', '2026-01-16']); expect((await plugin.stats()).get(member)?.total).toBe(2); }); it('monthly and daily reports preserve all records and counters', async () => { const { ctx, run } = setup(); await run('!checkin'); const before = await ctx.db.entries(); await run('!日报', admin); await run('!report monthly', admin); await run('!月报', admin); expect(await ctx.db.entries()).toEqual(before); }); it('uses lifetime known records for levels across month boundaries', async () => { const { ctx, plugin, run } = setup(); for (let day = 0; day < 45; day++) { ctx.date = new Date(Date.UTC(2026, 0, 1 + day, 2)); await run('!签到'); } expect((await plugin.stats()).get(member)).toMatchObject({ total: 45, month: 14 }); expect(last(ctx)).toContain('钻石'); }); it('allows own summary but restricts all, other users and raw data', async () => { const { ctx, run } = setup(); await run('签到'); await run('!查询'); expect(last(ctx)).toContain(member); for (const query of ['!list all', '!查询 全部', '!list @other:example.org', '!list --raw']) { await run(query); expect(last(ctx)).toContain('无权限'); } await run('!查询 全部 --原始', admin); expect(JSON.parse(last(ctx))[0].total).toBe(1); }); it('supports English replies through the same resources', async () => { const { ctx, run } = setup(); ctx.locale = 'en'; await run('签到'); expect(last(ctx)).toContain('Checked in'); await run('!签到'); expect(last(ctx)).toContain('Already'); }); it('rejects invalid dates and prevents monthly report access by members', async () => { const { ctx, run } = setup(); await run('!日报 2026-02-30', admin); expect(last(ctx)).toContain('用法:!日报'); await run('!月报'); expect(last(ctx)).toContain('无权限'); }); it('catches up reports once without deleting history or executing cleanup on enable', async () => { const { ctx, plugin, run } = setup({ cleanupEnabled: true, autoCleanup: true }); await run('!checkin'); await plugin.start(); ctx.date = new Date('2026-03-03T02:00:00Z'); await plugin.settle(false); const count = ctx.transport.sent.length; expect(ctx.transport.kicks).toHaveLength(0); expect(last(ctx)).toContain('2026-02'); await plugin.settle(false); expect(ctx.transport.sent).toHaveLength(count); expect((await plugin.stats('2026-01')).get(member)?.month).toBe(1); }); }); describe('cleanup previews and confirmation', () => { async function inactive(config: Record = {}) { const result = setup({ cleanupEnabled: true, ...config }); await result.run('签到'); result.ctx.date = new Date('2026-03-01T02:00:00Z'); return result; } const token = (ctx: FakeContext) => /cleanup confirm ([a-f0-9]+)/.exec(last(ctx))![1]; it('is disabled by default', async () => { const { ctx, run } = setup(); await run('!清理 预览', admin); expect(last(ctx)).toContain('未启用'); expect(ctx.transport.kicks).toHaveLength(0); }); it('previews without kicking; requires the same administrator and consumes the token', async () => { const { ctx, run } = await inactive(); await run('!清理 预览', admin); const code = token(ctx); expect(ctx.transport.kicks).toHaveLength(0); ctx.transport.powers.set('@second:example.org', 100); await run(`!cleanup confirm ${code}`, '@second:example.org'); expect(ctx.transport.kicks).toHaveLength(0); await run(`!清理 确认 ${code}`, admin); expect(ctx.transport.kicks).toHaveLength(1); await run(`!cleanup confirm ${code}`, admin); expect(last(ctx)).toContain('无效'); }); it('rechecks check-in and privileges at confirmation', async () => { const { ctx, run } = await inactive(); await run('!cleanup preview', admin); const code = token(ctx); await run('签到'); await run(`!cleanup confirm ${code}`, admin); expect(ctx.transport.kicks).toHaveLength(0); }); it('expires previews and refuses a demoted administrator', async () => { const { ctx, run } = await inactive(); await run('!cleanup preview', admin); const code = token(ctx); ctx.transport.powers.set(admin, 0); await run(`!cleanup confirm ${code}`, admin); expect(last(ctx)).toContain('无权限'); ctx.transport.powers.set(admin, 100); ctx.date = new Date(ctx.date.getTime() + 300_001); await run(`!cleanup confirm ${code}`, admin); expect(last(ctx)).toContain('过期'); expect(ctx.transport.kicks).toHaveLength(0); }); it('skips permission lookup failures, protected users and never-checked members', async () => { const { ctx, plugin } = await inactive(); ctx.transport.powerFailures.add(member); ctx.transport.memberMap.set(ctx.roomId, [member, '@new:example.org']); expect(await plugin.cleanupCandidates()).toEqual({ users: [], skipped: [member] }); ctx.transport.powerFailures.clear(); ctx.transport.powers.set(member, 100); expect((await plugin.cleanupCandidates()).users).toEqual([]); }); it('does not turn a failed member lookup into an empty successful report', async () => { const { ctx, plugin } = setup(); ctx.transport.memberFailures.add(ctx.roomId); await expect(plugin.dailyText('2026-01-15')).rejects.toThrow('Unavailable'); }); it('validates automation prerequisites', () => { expect(() => definition.validate({ autoCleanup: true })).toThrow(); expect(recordKey(member, '2026-01-15')).not.toBe(recordKey('@other:example.org', '2026-01-15')); }); it('never includes its own account in cleanup candidates', async () => { const { ctx, plugin, run } = setup({ cleanupEnabled: true }); await run('签到', ctx.botUserId); ctx.date = new Date('2026-03-01T02:00:00Z'); ctx.transport.memberMap.set(ctx.roomId, [ctx.botUserId]); expect(await plugin.cleanupCandidates()).toEqual({ users: [], skipped: [] }); }); });