Frun1na commented on code in PR #4631: URL: https://github.com/apache/rocketmq-dashboard/pull/4631#discussion_r4058377478
########## web/src/pages/ai/hooks/useLlmRuntime.test.ts: ########## @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { act, renderHook } from '@testing-library/react'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { LlmConfig } from '../../../api/llm'; +import { useLlmRuntime } from './useLlmRuntime'; + +vi.mock('../../../api/llm', () => ({ + getLlmConfig: vi.fn(), + getLlmModels: vi.fn(), +})); + +import { getLlmConfig, getLlmModels } from '../../../api/llm'; + +const configMock = vi.mocked(getLlmConfig); +const modelsMock = vi.mocked(getLlmModels); + +function deferred<T>() { + let resolve!: (value: T) => void; + const promise = new Promise<T>((res) => { + resolve = res; + }); + return { promise, resolve }; +} + +const config: LlmConfig = { + provider: 'openai', + engine: 'http', + apiBase: 'https://example.invalid', + model: 'gpt-test', + maxTokens: 1024, + temperature: 0, + enabled: true, + ready: true, +}; + +function render(enabled: boolean) { + return renderHook(({ enabled }: { enabled: boolean }) => useLlmRuntime({ enabled }), { + initialProps: { enabled }, + }); +} + +describe('useLlmRuntime', () => { + beforeEach(() => { + configMock.mockReset(); + modelsMock.mockReset(); + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('a late response for a disabled runtime never repopulates the state', async () => { + const configDeferred = deferred<LlmConfig>(); + const modelsDeferred = deferred({ status: 0, data: [{ id: 'gpt-test' }] }); + configMock.mockReturnValue(configDeferred.promise as never); + modelsMock.mockReturnValue(modelsDeferred.promise as never); + + const { result, rerender } = render(true); + await act(async () => { + // getLlmConfig resolves before the toggle; getLlmModels is still in flight. + configDeferred.resolve(config); + }); + rerender({ enabled: false }); Review Comment: Good catch — the config promise resolving before the toggle means the original case only proves the disabled branch, not the guard. Fixed in eee620ea: the config request now stays in flight across the toggle and resolves afterwards, with assertions that config stays null, the model options stay empty, llmReady stays false, and getLlmModels is never called. Verified the new case fails when the requestId guards are removed. ########## web/src/pages/ai/hooks/useLlmRuntime.test.ts: ########## @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { act, renderHook } from '@testing-library/react'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import type { LlmConfig } from '../../../api/llm'; +import { useLlmRuntime } from './useLlmRuntime'; + +vi.mock('../../../api/llm', () => ({ + getLlmConfig: vi.fn(), + getLlmModels: vi.fn(), +})); + +import { getLlmConfig, getLlmModels } from '../../../api/llm'; + +const configMock = vi.mocked(getLlmConfig); +const modelsMock = vi.mocked(getLlmModels); + +function deferred<T>() { + let resolve!: (value: T) => void; + const promise = new Promise<T>((res) => { + resolve = res; + }); + return { promise, resolve }; +} + +const config: LlmConfig = { + provider: 'openai', + engine: 'http', + apiBase: 'https://example.invalid', + model: 'gpt-test', + maxTokens: 1024, + temperature: 0, + enabled: true, + ready: true, +}; + +function render(enabled: boolean) { + return renderHook(({ enabled }: { enabled: boolean }) => useLlmRuntime({ enabled }), { + initialProps: { enabled }, + }); +} + +describe('useLlmRuntime', () => { + beforeEach(() => { + configMock.mockReset(); + modelsMock.mockReset(); + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('a late response for a disabled runtime never repopulates the state', async () => { + const configDeferred = deferred<LlmConfig>(); + const modelsDeferred = deferred({ status: 0, data: [{ id: 'gpt-test' }] }); + configMock.mockReturnValue(configDeferred.promise as never); + modelsMock.mockReturnValue(modelsDeferred.promise as never); + + const { result, rerender } = render(true); + await act(async () => { + // getLlmConfig resolves before the toggle; getLlmModels is still in flight. + configDeferred.resolve(config); + }); + rerender({ enabled: false }); + expect(result.current.config).toBeNull(); + expect(result.current.modelOptions).toEqual([]); + expect(result.current.llmReady).toBe(false); + + await act(async () => { + modelsDeferred.resolve({ status: 0, data: [{ id: 'gpt-test' }] }); + }); + expect(result.current.config).toBeNull(); + expect(result.current.modelOptions).toEqual([]); + expect(result.current.selectedModel).toBe(''); + expect(result.current.llmReady).toBe(false); + expect(result.current.modelsLoading).toBe(false); + }); + + it('keeps the last request winning when reload overlaps', async () => { + // The initial effect load, the first reload and the second reload each suspend on the + // deferred the mock hands out at call time. + const superseded = deferred<LlmConfig>(); + const latest = deferred<LlmConfig>(); + let current = superseded; + configMock.mockImplementation(() => current.promise as never); + modelsMock.mockResolvedValue({ status: 0, data: [] }); + + const { result } = render(true); + await act(async () => { + const first = result.current.reload(); + current = latest; + const second = result.current.reload(); + current = superseded; + superseded.resolve(config); + latest.resolve(config); + await Promise.all([first, second]); + }); + expect(result.current.config).toEqual(config); Review Comment: Agreed, the old ordering made the assertion pass trivially. Fixed in eee620ea: the latest reload now resolves a distinct config (model `gpt-latest`) first and the superseded response resolves second, and the test asserts the latest config (and its model) is retained. With the requestId guards removed both new cases fail, so the overlap test now actually pins the fix. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
