-
Notifications
You must be signed in to change notification settings - Fork 32
[PB-4369]: chore/migrate limt and usage calls #1532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jzunigax2
wants to merge
6
commits into
chore/migrate-thumbnail-endpoint
from
chore/migrate-limt-and-usage-calls
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1fb1e93
Merge branch 'master' of https://github.com/internxt/drive-web into c…
jzunigax2 4b981dc
chore: update storage client to new API and adjust limit and usage fe…
jzunigax2 29c19ba
Merge branch 'chore/migrate-thumbnail-endpoint' into chore/migrate-li…
jzunigax2 4116d8e
fix: correctly mock size service in limit test
jzunigax2 5b4e312
refactor: update usage response type to UsageResponseV2 across servic…
jzunigax2 a2617ec
refactor: remove UsageResponse type and streamline usage handling wit…
jzunigax2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import limitService from './limit.service'; | ||
| import { SdkFactory } from '../../core/factory/sdk'; | ||
| import { HUNDRED_TB } from 'app/core/components/Sidenav/Sidenav'; | ||
|
|
||
| vi.mock('@internxt/sdk', () => ({ | ||
| Network: {}, | ||
| })); | ||
|
|
||
| vi.mock('app/store', () => ({ | ||
| default: { | ||
| dispatch: vi.fn(), | ||
| getState: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('app/store/slices/session', () => ({ | ||
| sessionActions: { | ||
| resetState: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../core/factory/sdk', () => ({ | ||
| SdkFactory: { | ||
| getNewApiInstance: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('./size.service', () => { | ||
| const bytesToStringMock = (size) => { | ||
| return size > 0 ? 'formatted-size' : ''; | ||
| }; | ||
|
|
||
| return { | ||
| bytesToString: bytesToStringMock, | ||
| default: { | ||
| bytesToString: bytesToStringMock, | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| describe('limitService', () => { | ||
| const mockStorageClient = { | ||
| spaceLimitV2: vi.fn(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| vi.spyOn(SdkFactory, 'getNewApiInstance').mockReturnValue({ | ||
| createNewStorageClient: vi.fn().mockReturnValue(mockStorageClient), | ||
| } as any); | ||
| }); | ||
|
|
||
| describe('fetchLimit', () => { | ||
| it('should fetch space limit successfully', async () => { | ||
| const maxSpaceBytes = 1024 * 1024 * 1024 * 10; // 10GB | ||
| mockStorageClient.spaceLimitV2.mockResolvedValue({ maxSpaceBytes }); | ||
|
|
||
| const result = await limitService.fetchLimit(); | ||
|
|
||
| expect(SdkFactory.getNewApiInstance).toHaveBeenCalled(); | ||
| expect(mockStorageClient.spaceLimitV2).toHaveBeenCalled(); | ||
| expect(result).toEqual(maxSpaceBytes); | ||
| }); | ||
|
|
||
| it('should handle errors when fetching space limit', async () => { | ||
| const error = new Error('Failed to fetch space limit'); | ||
| mockStorageClient.spaceLimitV2.mockRejectedValue(error); | ||
|
|
||
| await expect(limitService.fetchLimit()).rejects.toThrow(error); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatLimit', () => { | ||
| it('should return ellipsis for zero or negative limit values', () => { | ||
| expect(limitService.formatLimit(0)).toBe('...'); | ||
| expect(limitService.formatLimit(-100)).toBe('...'); | ||
| }); | ||
|
|
||
| it('should return infinity symbol for limits greater than 100TB', () => { | ||
| const largeLimit = HUNDRED_TB + 1024; | ||
| const result = limitService.formatLimit(largeLimit); | ||
| expect(result).toBe('\u221E'); | ||
| }); | ||
|
|
||
| it('should format positive values less than 100TB', () => { | ||
| const smallLimit = 1024 * 1024 * 50; // 50MB | ||
| const result = limitService.formatLimit(smallLimit); | ||
|
|
||
| expect(result).not.toBe('...'); | ||
| expect(result).not.toBe('\u221E'); | ||
| expect(typeof result).toBe('string'); | ||
| expect(result.length).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import usageService from './usage.service'; | ||
| import { SdkFactory } from '../../core/factory/sdk'; | ||
| import errorService from '../../core/services/error.service'; | ||
|
|
||
| vi.mock('../../core/factory/sdk', () => ({ | ||
| SdkFactory: { | ||
| getNewApiInstance: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../core/services/error.service', () => ({ | ||
| default: { | ||
| reportError: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe('usageService', () => { | ||
| const mockStorageClient = { | ||
| spaceUsageV2: vi.fn(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| vi.spyOn(SdkFactory, 'getNewApiInstance').mockReturnValue({ | ||
| createNewStorageClient: vi.fn().mockReturnValue(mockStorageClient), | ||
| } as any); | ||
| }); | ||
|
|
||
| describe('fetchUsage', () => { | ||
| it('should fetch usage successfully', async () => { | ||
| const usageResponse = { | ||
| drive: 1024 * 1024 * 100, // 100MB | ||
| backups: 1024 * 1024 * 50, // 50MB | ||
| photos: 0, | ||
| total: 1024 * 1024 * 150, // 150MB | ||
| }; | ||
| mockStorageClient.spaceUsageV2.mockResolvedValue(usageResponse); | ||
|
|
||
| const result = await usageService.fetchUsage(); | ||
|
|
||
| expect(SdkFactory.getNewApiInstance).toHaveBeenCalled(); | ||
| expect(mockStorageClient.spaceUsageV2).toHaveBeenCalled(); | ||
| expect(result).toEqual(usageResponse); | ||
| }); | ||
|
|
||
| it('should handle errors when fetching usage', async () => { | ||
| const error = new Error('Failed to fetch usage'); | ||
| mockStorageClient.spaceUsageV2.mockRejectedValue(error); | ||
|
|
||
| await expect(usageService.fetchUsage()).rejects.toThrow(error); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getUsageDetails', () => { | ||
| it('should get usage details successfully', async () => { | ||
| const usageResponse = { | ||
| drive: 1024 * 1024 * 100, // 100MB | ||
| backups: 1024 * 1024 * 50, // 50MB | ||
| }; | ||
| mockStorageClient.spaceUsageV2.mockResolvedValue(usageResponse); | ||
|
|
||
| const result = await usageService.getUsageDetails(); | ||
|
|
||
| expect(SdkFactory.getNewApiInstance).toHaveBeenCalled(); | ||
| expect(mockStorageClient.spaceUsageV2).toHaveBeenCalled(); | ||
| expect(result).toEqual({ | ||
| drive: usageResponse.drive, | ||
| photos: 0, | ||
| backups: usageResponse.backups, | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle errors and report them', async () => { | ||
| const error = new Error('Failed to fetch usage details'); | ||
| mockStorageClient.spaceUsageV2.mockRejectedValue(error); | ||
|
|
||
| const result = await usageService.getUsageDetails(); | ||
|
|
||
| expect(errorService.reportError).toHaveBeenCalledWith(error); | ||
| expect(result).toEqual({ drive: 0, photos: 0, backups: 0 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getUsagePercent', () => { | ||
| it('should calculate usage percentage correctly', () => { | ||
| const usage = 250; | ||
| const limit = 1000; | ||
| const expectedPercent = 25; | ||
|
|
||
| const result = usageService.getUsagePercent(usage, limit); | ||
|
|
||
| expect(result).toBe(expectedPercent); | ||
| }); | ||
|
|
||
| it('should round up percentage to nearest integer', () => { | ||
| const usage = 101; | ||
| const limit = 1000; | ||
| const expectedPercent = 11; | ||
|
|
||
| const result = usageService.getUsagePercent(usage, limit); | ||
|
|
||
| expect(result).toBe(expectedPercent); | ||
| }); | ||
|
|
||
| it('should handle 100% usage correctly', () => { | ||
| const usage = 1000; | ||
| const limit = 1000; | ||
| const expectedPercent = 100; | ||
|
|
||
| const result = usageService.getUsagePercent(usage, limit); | ||
|
|
||
| expect(result).toBe(expectedPercent); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.