80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
import mongoose from 'mongoose';
|
|
import { ConnectionService } from '../global/database/connection.service';
|
|
import {
|
|
buildStaleAccountsPipeline,
|
|
StaleAccountRepository,
|
|
} from './stale-account.repository';
|
|
|
|
describe('buildStaleAccountsPipeline', () => {
|
|
const olderThan = new Date('2026-05-03T00:00:00.000Z');
|
|
|
|
it('filters to active users before unwinding Riot accounts', () => {
|
|
const pipeline = buildStaleAccountsPipeline({ olderThan, limit: 20 });
|
|
|
|
expect(pipeline[0]).toEqual({ $match: { isActivated: true } });
|
|
expect(pipeline[1]).toEqual({ $unwind: '$riotAccounts' });
|
|
});
|
|
|
|
it('matches missing or older lastRefreshRequestedAt values', () => {
|
|
const pipeline = buildStaleAccountsPipeline({ olderThan, limit: 20 });
|
|
|
|
expect(pipeline[2]).toEqual({
|
|
$match: {
|
|
$or: [
|
|
{ 'riotAccounts.lastRefreshRequestedAt': { $exists: false } },
|
|
{ 'riotAccounts.lastRefreshRequestedAt': { $lt: olderThan } },
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it('sorts missing refresh timestamps first', () => {
|
|
const pipeline = buildStaleAccountsPipeline({ olderThan, limit: 20 });
|
|
|
|
expect(pipeline[3]).toEqual({
|
|
$addFields: {
|
|
'riotAccounts.lastRefreshRequestedAtSort': {
|
|
$ifNull: ['$riotAccounts.lastRefreshRequestedAt', new Date(0)],
|
|
},
|
|
},
|
|
});
|
|
expect(pipeline[4]).toEqual({
|
|
$sort: { 'riotAccounts.lastRefreshRequestedAtSort': 1 },
|
|
});
|
|
});
|
|
|
|
it('applies the provided limit', () => {
|
|
const pipeline = buildStaleAccountsPipeline({ olderThan, limit: 7 });
|
|
|
|
expect(pipeline[5]).toEqual({ $limit: 7 });
|
|
});
|
|
});
|
|
|
|
describe('StaleAccountRepository', () => {
|
|
it('returns aggregate candidates from the user model', async () => {
|
|
const userId = new mongoose.Types.ObjectId();
|
|
const candidates = [{ userId, puuid: 'puuid-1' }];
|
|
const exec = jest.fn().mockResolvedValue(candidates);
|
|
const aggregate = jest.fn().mockReturnValue({ exec });
|
|
const connectionService = {
|
|
getModel: jest.fn().mockResolvedValue({ aggregate }),
|
|
} as unknown as ConnectionService;
|
|
|
|
const repository = new StaleAccountRepository(connectionService);
|
|
await expect(
|
|
repository.findStaleAccounts({
|
|
olderThan: new Date('2026-05-03T00:00:00.000Z'),
|
|
limit: 1,
|
|
}),
|
|
).resolves.toBe(candidates);
|
|
|
|
expect(connectionService.getModel).toHaveBeenCalledWith('user');
|
|
expect(aggregate).toHaveBeenCalledWith(
|
|
buildStaleAccountsPipeline({
|
|
olderThan: new Date('2026-05-03T00:00:00.000Z'),
|
|
limit: 1,
|
|
}),
|
|
);
|
|
});
|
|
});
|