/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import test from 'node:test'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { Table as AntTable } from 'antd'; import { loadFrontendModule } from './helpers/frontendModule.js'; async function fixture(kind) { const calls = []; const models = Array.from( { length: 24 }, (_, i) => 'aa-' + String(i + 1).padStart(2, '0'), ); const states = kind === 'Missing' ? [5, false, models, 'AA', 2] : [5, {}, 2, 'AA']; let cursor = 0; const react = { ...React, useEffect: () => {}, useMemo: (fn) => fn(), useCallback: (fn) => fn, useState: () => { const index = cursor++; return [states[index], (value) => calls.push([index, value])]; }, }; const ui = { ...Object.fromEntries( [ 'Modal', 'Table', 'Spin', 'Button', 'Empty', 'Input', 'Checkbox', 'Tag', 'Popover', ].map((name) => [name, name]), ), Typography: { Text: 'Text' }, }; const file = kind === 'Missing' ? 'MissingModelsModal' : 'UpstreamConflictModal'; const { default: Component } = await loadFrontendModule( new URL( '../../src/components/table/models/modals/' + file + '.jsx', import.meta.url, ), { react, 'react-i18next': { useTranslation: () => ({ t: (key) => key }) }, '@/components/ui': ui, '../../../../hooks/common/useEffectEvent': { useEffectEvent: (callback) => callback, }, '../../../../hooks/common/useIsMobile': { useIsMobile: () => false }, '../../../../constants': { MODEL_TABLE_PAGE_SIZE: 10 }, '../../../../helpers': { API: { get: () => assert.fail('Local pagination must not send requests'), }, showError: assert.fail, hoverOrClickTrigger: () => 'hover', }, '../../../common/illustrations': { IllustrationNoResult: 'Illustration', IllustrationNoResultDark: 'Illustration', }, '@/components/ui/icons': { IconSearch: 'Search' }, 'lucide-react': { MousePointerClick: 'Pointer' }, }, ); const tree = Component({ visible: true, t: (key) => key, conflicts: models.map((model_name) => ({ model_name, fields: [] })), }); const find = (type, node = tree) => React.isValidElement(node) ? [ ...(node.type === type ? [node] : []), ...React.Children.toArray(node.props.children).flatMap((child) => find(type, child), ), ] : []; return { calls, models, table: find('Table')[0].props, input: find('Input')[0].props, }; } for (const kind of ['Missing', 'Upstream']) { test( kind + ': case-insensitive local filtering uses selected page and size', async () => { const { table, models } = await fixture(kind); assert.deepEqual( Array.from(table.dataSource, (row) => row.model || row.model_name), models.slice(5, 10), ); assert.equal(table.pagination.total, 24); assert.equal(table.pagination.pageSize, 5); assert.equal(table.pagination.currentPage, 2); }, ); test( kind + ': search and size changes reset the current page without network calls', async () => { const { calls, table, input } = await fixture(kind); table.pagination.onPageSizeChange(20); assert.ok(calls.some(([, value]) => value === 1)); assert.ok(calls.some(([, value]) => value === 20)); calls.length = 0; input.onChange('aa-24'); assert.ok(calls.some(([, value]) => value === 'aa-24')); assert.ok(calls.some(([, value]) => value === 1)); }, ); } async function modelTestFixture() { const calls = []; const models = Array.from({ length: 24 }, (_, index) => `aa-${index + 1}`); const { default: Component } = await loadFrontendModule( new URL( '../../src/components/table/channels/modals/ModelTestModal.jsx', import.meta.url, ), { react: { ...React, useState: () => [5, (value) => calls.push(['size', value])], }, antd: { ...Object.fromEntries( ['Modal', 'Button', 'Input', 'Table', 'Tag', 'Select'].map((name) => [ name, name, ]), ), Typography: { Text: 'Text' }, }, '../../../ui/CenterActionTable': 'Table', 'lucide-react': { Search: 'Search' }, './model-test-modal.css': {}, '../../../../helpers': {}, '../../../../constants': { MODEL_TABLE_PAGE_SIZE: 10 }, }, ); const tree = Component({ currentTestChannel: { id: 7, name: 'channel', models: models.join(',') }, modelSearchKeyword: 'AA', modelTablePage: 2, selectedModelKeys: [], modelTestResults: {}, testingModels: new Set(), setModelTablePage: (value) => calls.push(['page', value]), setModelSearchKeyword: (value) => calls.push(['search', value]), t: (key) => key, }); const find = (type, node = tree) => React.isValidElement(node) ? [ ...(node.type === type ? [node] : []), ...React.Children.toArray(node.props.children).flatMap((child) => find(type, child), ), ] : []; return { calls, models, table: find('Table')[0].props, input: find('Input')[0].props, }; } test('Model test dialog passes the complete filtered list to native Ant pagination', async () => { const { table, models } = await modelTestFixture(); assert.deepEqual( Array.from(table.dataSource, (row) => row.model), models, ); assert.equal(table.pagination.total, 24); assert.equal(table.pagination.current, 2); assert.equal(table.pagination.pageSize, 5); const html = renderToStaticMarkup(React.createElement(AntTable, table)); assert.deepEqual( Array.from(html.matchAll(/data-row-key="([^"]+)"/g), (match) => match[1]), models.slice(5, 10), 'Ant Table renders only the selected page, with no second manual slice', ); }); test('Model test dialog preserves page changes and resets after search or size changes', async () => { const { calls, table, input } = await modelTestFixture(); table.pagination.onChange(3, 5); assert.deepEqual(calls.splice(0), [ ['page', 3], ['size', 5], ]); table.pagination.onChange(3, 20); assert.deepEqual(calls.splice(0), [ ['page', 1], ['size', 20], ]); input.onChange({ target: { value: 'aa-24' } }); assert.deepEqual(calls, [ ['search', 'aa-24'], ['page', 1], ]); });