@@ -23,6 +23,7 @@ import {
2323 newFolder ,
2424 newUser ,
2525 newWorkspace ,
26+ newUsage ,
2627} from '../../../test/fixtures' ;
2728import { FolderUseCases } from '../folder/folder.usecase' ;
2829import { v4 } from 'uuid' ;
@@ -31,6 +32,8 @@ import { SharingItemType } from '../sharing/sharing.domain';
3132import { CreateFileDto } from './dto/create-file.dto' ;
3233import { UpdateFileMetaDto } from './dto/update-file-meta.dto' ;
3334import { ThumbnailUseCases } from '../thumbnail/thumbnail.usecase' ;
35+ import { UsageService } from '../usage/usage.service' ;
36+ import { Time } from '../../lib/time' ;
3437
3538const fileId = '6295c99a241bb000083f1c6a' ;
3639const userId = 1 ;
@@ -43,6 +46,7 @@ describe('FileUseCases', () => {
4346 let bridgeService : BridgeService ;
4447 let cryptoService : CryptoService ;
4548 let thumbnailUseCases : ThumbnailUseCases ;
49+ let usageService : UsageService ;
4650
4751 const userMocked = newUser ( ) ;
4852
@@ -61,6 +65,7 @@ describe('FileUseCases', () => {
6165 cryptoService = module . get < CryptoService > ( CryptoService ) ;
6266 sharingService = module . get < SharingService > ( SharingService ) ;
6367 thumbnailUseCases = module . get < ThumbnailUseCases > ( ThumbnailUseCases ) ;
68+ usageService = module . get < UsageService > ( UsageService ) ;
6469 } ) ;
6570
6671 afterEach ( ( ) => {
@@ -1443,4 +1448,106 @@ describe('FileUseCases', () => {
14431448 } ) ;
14441449 } ) ;
14451450 } ) ;
1451+
1452+ describe ( 'getUserUsedStorageIncrementally' , ( ) => {
1453+ beforeEach ( ( ) => {
1454+ jest . useFakeTimers ( ) ;
1455+ } ) ;
1456+
1457+ afterEach ( ( ) => {
1458+ jest . useRealTimers ( ) ;
1459+ jest . clearAllMocks ( ) ;
1460+ } ) ;
1461+
1462+ it ( 'When user has no existing usage, then it should create first usage calculation' , async ( ) => {
1463+ const mockUser = newUser ( ) ;
1464+ const today = new Date ( '2024-01-02T10:00:00Z' ) ;
1465+ const mockFirstUsage = newUsage ( {
1466+ attributes : { period : new Date ( '2024-01-01T00:00:00Z' ) } ,
1467+ } ) ;
1468+
1469+ // Set today to the next period start date according to mockUsage
1470+ jest . setSystemTime ( today ) ;
1471+
1472+ jest
1473+ . spyOn ( usageService , 'getUserMostRecentUsage' )
1474+ . mockResolvedValue ( null ) ;
1475+ jest
1476+ . spyOn ( usageService , 'createFirstUsageCalculation' )
1477+ . mockResolvedValue ( mockFirstUsage ) ;
1478+
1479+ await service . getUserUsedStorageIncrementally ( mockUser ) ;
1480+
1481+ expect ( usageService . getUserMostRecentUsage ) . toHaveBeenCalledWith (
1482+ mockUser . uuid ,
1483+ ) ;
1484+ expect ( usageService . createFirstUsageCalculation ) . toHaveBeenCalledWith (
1485+ mockUser . uuid ,
1486+ ) ;
1487+ } ) ;
1488+
1489+ it ( 'When user has recent usage and is up to date, then it should not create new usage' , async ( ) => {
1490+ const mockUser = newUser ( ) ;
1491+ const today = new Date ( '2024-01-02T00:00:00Z' ) ;
1492+
1493+ const mockUsage = newUsage ( {
1494+ attributes : { period : new Date ( '2024-01-01T00:00:00Z' ) } ,
1495+ } ) ;
1496+
1497+ // Set today to the next period start date according to mockUsage
1498+ jest . setSystemTime ( today ) ;
1499+
1500+ jest
1501+ . spyOn ( usageService , 'getUserMostRecentUsage' )
1502+ . mockResolvedValue ( mockUsage ) ;
1503+
1504+ await service . getUserUsedStorageIncrementally ( mockUser ) ;
1505+
1506+ expect ( usageService . getUserMostRecentUsage ) . toHaveBeenCalledWith (
1507+ mockUser . uuid ,
1508+ ) ;
1509+ expect ( usageService . createMonthlyUsage ) . not . toHaveBeenCalled ( ) ;
1510+ expect (
1511+ fileRepository . sumFileSizeDeltaBetweenDates ,
1512+ ) . not . toHaveBeenCalled ( ) ;
1513+ } ) ;
1514+
1515+ it ( 'When user has recent usage but needs update, then it should calculate gap delta and create monthly usage' , async ( ) => {
1516+ const mockUser = newUser ( ) ;
1517+ const today = new Date ( '2024-01-04T10:00:00Z' ) ;
1518+ const yesterday = Time . dateWithDaysAdded ( - 1 , today ) ;
1519+ const mockUsage = newUsage ( {
1520+ attributes : { period : new Date ( '2024-01-01T00:00:00Z' ) } ,
1521+ } ) ;
1522+ const mockGapDelta = 500 ;
1523+
1524+ // Set today to a date after the next period start date according to mockUsage
1525+ jest . setSystemTime ( today ) ;
1526+ jest
1527+ . spyOn ( usageService , 'getUserMostRecentUsage' )
1528+ . mockResolvedValue ( mockUsage ) ;
1529+ jest
1530+ . spyOn ( fileRepository , 'sumFileSizeDeltaBetweenDates' )
1531+ . mockResolvedValue ( mockGapDelta ) ;
1532+ jest
1533+ . spyOn ( usageService , 'createMonthlyUsage' )
1534+ . mockResolvedValue ( undefined ) ;
1535+
1536+ await service . getUserUsedStorageIncrementally ( mockUser ) ;
1537+
1538+ expect ( usageService . getUserMostRecentUsage ) . toHaveBeenCalledWith (
1539+ mockUser . uuid ,
1540+ ) ;
1541+ expect ( fileRepository . sumFileSizeDeltaBetweenDates ) . toHaveBeenCalledWith (
1542+ mockUser . id ,
1543+ mockUsage . getNextPeriodStartDate ( ) ,
1544+ Time . endOfDay ( yesterday ) ,
1545+ ) ;
1546+ expect ( usageService . createMonthlyUsage ) . toHaveBeenCalledWith (
1547+ mockUser . uuid ,
1548+ yesterday ,
1549+ mockGapDelta ,
1550+ ) ;
1551+ } ) ;
1552+ } ) ;
14461553} ) ;
0 commit comments