|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, memo } from 'react'; |
| 3 | +import { createRoot, type Root } from 'react-dom/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { Model, Sequence } from '@/lib/data-mappings'; |
| 7 | +import type * as UrlStateModule from '@/lib/url-state'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Regression coverage for the stale-`i_seq` snapshot bug behind the paramless |
| 11 | + * AgentX hero links (see `agentxDashboardHref`). |
| 12 | + * |
| 13 | + * The module-level URL snapshot retains self-written params across soft |
| 14 | + * navigations: every dashboard visit pins its resolved sequence via the |
| 15 | + * URL-sync effect, and `refreshUrlParams` does not evict keys absent from the |
| 16 | + * live URL. `getUrlParam('i_seq')` can therefore return the PREVIOUS page's |
| 17 | + * sequence on a navigation whose URL carries no `i_seq` at all. Applying that |
| 18 | + * value flips `sequenceExplicit`, which blocks the availability-driven AgentX |
| 19 | + * default — a bare `/inference/kimi-k3` hero link would open on 8K/1K. |
| 20 | + * |
| 21 | + * The provider must consult `hasExplicitUrlParam('i_seq')` (which IS refreshed |
| 22 | + * per navigation) before honoring the snapshot value. |
| 23 | + */ |
| 24 | + |
| 25 | +const mocks = vi.hoisted(() => ({ |
| 26 | + availability: { |
| 27 | + data: [] as unknown[], |
| 28 | + error: null as Error | null, |
| 29 | + isSuccess: true, |
| 30 | + }, |
| 31 | + workflow: { |
| 32 | + data: undefined, |
| 33 | + isLoading: false, |
| 34 | + error: null as Error | null, |
| 35 | + }, |
| 36 | + getUrlParam: vi.fn((key: string): string | undefined => |
| 37 | + key === 'i_seq' ? '8k/1k' : undefined, |
| 38 | + ), |
| 39 | + setUrlParams: vi.fn(), |
| 40 | + hasExplicitUrlParam: vi.fn((_key: string) => false), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('@/hooks/api/use-availability', () => ({ |
| 44 | + useAvailability: () => mocks.availability, |
| 45 | +})); |
| 46 | +vi.mock('@/hooks/api/use-workflow-info', () => ({ |
| 47 | + useWorkflowInfo: () => mocks.workflow, |
| 48 | +})); |
| 49 | +vi.mock('@/hooks/useUrlState', () => ({ |
| 50 | + useUrlState: () => ({ getUrlParam: mocks.getUrlParam, setUrlParams: mocks.setUrlParams }), |
| 51 | +})); |
| 52 | +vi.mock('@/lib/url-state', async (importOriginal) => ({ |
| 53 | + ...(await importOriginal<typeof UrlStateModule>()), |
| 54 | + hasExplicitUrlParam: (key: string) => mocks.hasExplicitUrlParam(key), |
| 55 | + refreshUrlParams: () => ({}), |
| 56 | +})); |
| 57 | +vi.mock('@/components/unofficial-run-provider', () => ({ |
| 58 | + useUnofficialRun: () => ({ availableModelsAndSequences: [] }), |
| 59 | +})); |
| 60 | + |
| 61 | +import { GlobalFilterProvider, useGlobalFilterSelection } from './GlobalFilterContext'; |
| 62 | + |
| 63 | +/** Kimi-K3 availability: both the Agentic and the 8K/1K scenario exist. */ |
| 64 | +const KIMI_K3_ROWS = [ |
| 65 | + { model: 'kimik3', isl: null, osl: null, benchmark_type: 'agentic_traces' }, |
| 66 | + { model: 'kimik3', isl: 8192, osl: 1024, benchmark_type: 'single_turn' }, |
| 67 | +]; |
| 68 | + |
| 69 | +let root: Root | undefined; |
| 70 | +let container: HTMLDivElement | undefined; |
| 71 | +let observedSequence: Sequence | undefined; |
| 72 | +let observedResolved: boolean | undefined; |
| 73 | + |
| 74 | +const SequenceProbe = memo(() => { |
| 75 | + const selection = useGlobalFilterSelection(); |
| 76 | + observedSequence = selection.effectiveSequence; |
| 77 | + observedResolved = selection.sequenceResolved; |
| 78 | + return null; |
| 79 | +}); |
| 80 | + |
| 81 | +function mountProvider(): void { |
| 82 | + container = document.createElement('div'); |
| 83 | + root = createRoot(container); |
| 84 | + act(() => |
| 85 | + root?.render( |
| 86 | + <GlobalFilterProvider initialModel={Model.Kimi_K3}> |
| 87 | + <SequenceProbe /> |
| 88 | + </GlobalFilterProvider>, |
| 89 | + ), |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +beforeEach(() => { |
| 94 | + mocks.availability.data = KIMI_K3_ROWS; |
| 95 | + mocks.getUrlParam.mockClear(); |
| 96 | + mocks.setUrlParams.mockClear(); |
| 97 | + mocks.hasExplicitUrlParam.mockClear(); |
| 98 | + observedSequence = undefined; |
| 99 | + observedResolved = undefined; |
| 100 | +}); |
| 101 | + |
| 102 | +afterEach(() => { |
| 103 | + if (root) act(() => root?.unmount()); |
| 104 | + container?.remove(); |
| 105 | + root = undefined; |
| 106 | + container = undefined; |
| 107 | +}); |
| 108 | + |
| 109 | +describe('GlobalFilterProvider stale i_seq snapshot', () => { |
| 110 | + it('ignores a retained i_seq the current URL does not carry — AgentX default wins', () => { |
| 111 | + mocks.hasExplicitUrlParam.mockReturnValue(false); |
| 112 | + mountProvider(); |
| 113 | + expect(observedResolved).toBe(true); |
| 114 | + expect(observedSequence).toBe(Sequence.AgenticTraces); |
| 115 | + }); |
| 116 | + |
| 117 | + it('still honors an i_seq explicitly present in the current URL', () => { |
| 118 | + mocks.hasExplicitUrlParam.mockImplementation((key: string) => key === 'i_seq'); |
| 119 | + mountProvider(); |
| 120 | + expect(observedResolved).toBe(true); |
| 121 | + expect(observedSequence).toBe(Sequence.EightK_OneK); |
| 122 | + }); |
| 123 | +}); |
0 commit comments