From c3cb1924866e3b94c868d9eacc5c43190a955452 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 17 Sep 2024 16:09:38 +0700 Subject: [PATCH] fix: #3667 - The recommended label should be hidden (#3687) --- web/containers/ModelLabel/ModelLabel.test.tsx | 100 ++++++++++++++++++ web/containers/ModelLabel/index.tsx | 6 +- 2 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 web/containers/ModelLabel/ModelLabel.test.tsx diff --git a/web/containers/ModelLabel/ModelLabel.test.tsx b/web/containers/ModelLabel/ModelLabel.test.tsx new file mode 100644 index 0000000000..48504ff6a9 --- /dev/null +++ b/web/containers/ModelLabel/ModelLabel.test.tsx @@ -0,0 +1,100 @@ +import React from 'react' +import { render, waitFor, screen } from '@testing-library/react' +import { useAtomValue } from 'jotai' +import { useActiveModel } from '@/hooks/useActiveModel' +import { useSettings } from '@/hooks/useSettings' +import ModelLabel from '@/containers/ModelLabel' + +jest.mock('jotai', () => ({ + useAtomValue: jest.fn(), + atom: jest.fn(), +})) + +jest.mock('@/hooks/useActiveModel', () => ({ + useActiveModel: jest.fn(), +})) + +jest.mock('@/hooks/useSettings', () => ({ + useSettings: jest.fn(), +})) + +describe('ModelLabel', () => { + const mockUseAtomValue = useAtomValue as jest.Mock + const mockUseActiveModel = useActiveModel as jest.Mock + const mockUseSettings = useSettings as jest.Mock + + const defaultProps: any = { + metadata: { + author: 'John Doe', // Add the 'author' property with a value + tags: ['8B'], + size: 100, + }, + compact: false, + } + + beforeEach(() => { + jest.clearAllMocks() + }) + + it('renders NotEnoughMemoryLabel when minimumRamModel is greater than totalRam', async () => { + mockUseAtomValue + .mockReturnValueOnce(0) + .mockReturnValueOnce(0) + .mockReturnValueOnce(0) + mockUseActiveModel.mockReturnValue({ + activeModel: { metadata: { size: 0 } }, + }) + mockUseSettings.mockReturnValue({ settings: { run_mode: 'cpu' } }) + + render() + await waitFor(() => { + expect(screen.getByText('Not enough RAM')).toBeDefined() + }) + }) + + it('renders SlowOnYourDeviceLabel when minimumRamModel is less than totalRam but greater than availableRam', async () => { + mockUseAtomValue + .mockReturnValueOnce(100) + .mockReturnValueOnce(50) + .mockReturnValueOnce(10) + mockUseActiveModel.mockReturnValue({ + activeModel: { metadata: { size: 0 } }, + }) + mockUseSettings.mockReturnValue({ settings: { run_mode: 'cpu' } }) + + const props = { + ...defaultProps, + metadata: { + ...defaultProps.metadata, + size: 50, + }, + } + + render() + await waitFor(() => { + expect(screen.getByText('Slow on your device')).toBeDefined() + }) + }) + + it('renders nothing when minimumRamModel is less than availableRam', () => { + mockUseAtomValue + .mockReturnValueOnce(100) + .mockReturnValueOnce(50) + .mockReturnValueOnce(0) + mockUseActiveModel.mockReturnValue({ + activeModel: { metadata: { size: 0 } }, + }) + mockUseSettings.mockReturnValue({ settings: { run_mode: 'cpu' } }) + + const props = { + ...defaultProps, + metadata: { + ...defaultProps.metadata, + size: 10, + }, + } + + const { container } = render() + expect(container.firstChild).toBeNull() + }) +}) diff --git a/web/containers/ModelLabel/index.tsx b/web/containers/ModelLabel/index.tsx index 2c32e288c0..b0a3da96f8 100644 --- a/web/containers/ModelLabel/index.tsx +++ b/web/containers/ModelLabel/index.tsx @@ -10,8 +10,6 @@ import { useSettings } from '@/hooks/useSettings' import NotEnoughMemoryLabel from './NotEnoughMemoryLabel' -import RecommendedLabel from './RecommendedLabel' - import SlowOnYourDeviceLabel from './SlowOnYourDeviceLabel' import { @@ -53,9 +51,7 @@ const ModelLabel = ({ metadata, compact }: Props) => { /> ) } - if (minimumRamModel < availableRam && !compact) { - return - } + if (minimumRamModel < totalRam && minimumRamModel > availableRam) { return }