import { connect, disconnect } from "../src/lib/db/mongodb"; import { createUser, deleteUser } from "../src/lib/db/users"; import { calculateCurrentAvailableResources, getUserResources, updateUserResources } from "../src/lib/utils/resourceManager"; import User from "../src/types/User"; let user: User; describe('Test resource manager', () => { beforeEach(async () => { await connect(); user = await createUser("test", "test@example.com", "password"); }); afterEach(async () => { await deleteUser(user._id); await disconnect(); }); describe('Test initial resources amount', () => { test('initialResourcesAmount', async () => { const resources = await getUserResources(user._id); let i = 1; resources.forEach(res => { expect(res.amount).toBe(i * 11); expect(res.perHourMiningRate).toBe(i * 11); i++; }); }); }); describe('Test calculation of available resources', () => { test('calculationOfAvailableResources', async () => { const resources = await getUserResources(user._id); resources.forEach(res => { res.amount = 0; res.lastUpdated = new Date(Date.now() - 1000 * 60 * 60); }); await updateUserResources(user._id, resources); const availableResources = await calculateCurrentAvailableResources(user._id); let i = 1; availableResources.forEach(res => { expect(res.amount).toBeCloseTo(i * 11); i++; }); const resourcesAfter = await getUserResources(user._id); i = 1; resourcesAfter.forEach(res => { expect(res.amount).toBeCloseTo(i * 11); i++; }); }); }); describe('Test taking resources', () => { test('takingResources', async () => { const resources = await getUserResources(user._id); resources.forEach(res => { res.amount = 10_000; }); let i = 1; resources.forEach(res => { res.amount -= i * 100; i++; }); await updateUserResources(user._id, resources); const resourcesAfter = await getUserResources(user._id); i = 1; resourcesAfter.forEach(res => { expect(res.amount).toBeCloseTo(10_000 - i * 100); i++; }); }); }); });