Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

13.5 KB
Raw Blame
/**
 * @vitest-environment happy-dom
 *
 * The dataset lists (benchmark-scoped + scope-wide) and the dataset detail are
 * `local-replica` resources whose views are `datasetListMap` /
 * `datasetDetailMap`. This suite drives the real replica sync through SWR (no
 * mocked fetch hook) to show the behaviour the migration must preserve: a
 * persisted list paints while the network answers, a server response settles
 * and persists the view, each benchmark keeps its own entry, the scope-wide
 * list has its own key, a detail page fills its map entry, and a cache-scope
 * switch drops the previous identity's rows.
 */
import { randomUUID } from 'node:crypto';

import type { AgentEvalDatasetListItem } from '@lobechat/types';
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
import type { PropsWithChildren } from 'react';
import { createElement, useEffect } from 'react';
import { SWRConfig, useSWRConfig } from 'swr';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { cacheScope } from '@/libs/replica';
import { setScopedMutate } from '@/libs/swr/mutate';
import { agentEvalService } from '@/services/agentEval';
import { useEvalStore } from '@/store/eval';

import { ALL_DATASETS_KEY, datasetDetailResource, datasetListResource } from './projection';

const ALL_PARAMS = {} as Record<string, never>;
const BENCH = 'bench-1';
const DETAIL_ID = 'd1';

const MutateBridge = () => {
  const { mutate } = useSWRConfig();
  useEffect(() => setScopedMutate(mutate), [mutate]);
  return null;
};
const wrapper = ({ children }: PropsWithChildren) =>
  createElement(
    SWRConfig,
    // `shouldRetryOnError: false`: a NOT_FOUND revalidation must settle now, not
    // through SWR's backoff timers.
    { value: { dedupingInterval: 0, provider: () => new Map(), shouldRetryOnError: false } },
    createElement(MutateBridge),
    children,
  );

const BENCH_STORAGE_KEY = datasetListResource.storageKey({ benchmarkId: BENCH });
const ALL_STORAGE_KEY = datasetListResource.storageKey(ALL_PARAMS);
const DETAIL_STORAGE_KEY = datasetDetailResource.storageKey(DETAIL_ID);

const listRow = (id: string, name: string) =>
  ({
    benchmarkId: BENCH,
    createdAt: new Date('2026-01-01T00:00:00.000Z'),
    id,
    identifier: id,
    name,
    updatedAt: new Date('2026-01-01T00:00:00.000Z'),
  }) as AgentEvalDatasetListItem;

/** Never-resolving fetch: what the store holds can only have come from storage. */
const pending = () => new Promise<never>(() => {});

const renderBenchList = (benchmarkId: string = BENCH) =>
  renderHook(() => useEvalStore.getState().useFetchDatasets(benchmarkId), { wrapper });
const renderAllList = () =>
  renderHook(() => useEvalStore.getState().useFetchAllDatasets(), { wrapper });
const renderDetail = (id: string) =>
  renderHook(() => useEvalStore.getState().useFetchDatasetDetail(id), { wrapper });

const benchList = (benchmarkId: string = BENCH) =>
  useEvalStore.getState().datasetListMap[benchmarkId];

describe('dataset replica', () => {
  const scopes = new Set<string>();
  let scope = '';

  const useScope = (next: string) => {
    scope = next;
    scopes.add(next);
    vi.spyOn(cacheScope, 'get').mockImplementation(() => scope);
    vi.spyOn(cacheScope, 'use').mockImplementation(() => scope);
    vi.spyOn(cacheScope, 'canPersist').mockReturnValue(true);
  };

  beforeEach(() => {
    useScope(`eval-user-${randomUUID()}:personal`);
  });

  afterEach(async () => {
    cleanup();
    await Promise.all(
      [...scopes].flatMap((value) => [
        datasetListResource.storage!.remove({ queryKey: BENCH_STORAGE_KEY, scope: value }),
        datasetListResource.storage!.remove({ queryKey: ALL_STORAGE_KEY, scope: value }),
        datasetDetailResource.storage!.remove({ queryKey: DETAIL_STORAGE_KEY, scope: value }),
      ]),
    );
    scopes.clear();
    vi.restoreAllMocks();
    vi.clearAllMocks();
  });

  it('paints the persisted benchmark list before the network answers', async () => {
    await datasetListResource.storage!.set(
      { queryKey: BENCH_STORAGE_KEY, scope },
      { data: [listRow('d1', 'Cached')], updatedAt: 1 },
    );
    vi.spyOn(agentEvalService, 'listDatasets').mockImplementation(pending);

    renderBenchList();

    await waitFor(() => expect(benchList()?.[0]?.name).toBe('Cached'));
  });

  it('settles the benchmark list from the server and persists it', async () => {
    vi.spyOn(agentEvalService, 'listDatasets').mockResolvedValue([listRow('d1', 'MMLU')]);

    renderBenchList();

    await waitFor(() => expect(benchList()?.[0]?.name).toBe('MMLU'));
    expect(agentEvalService.listDatasets).toHaveBeenCalledWith(BENCH);
    await waitFor(async () =>
      expect(
        (await datasetListResource.storage!.get({ queryKey: BENCH_STORAGE_KEY, scope }))?.data?.[0]
          ?.name,
      ).toBe('MMLU'),
    );
  });

  it('keeps each benchmark’s list under its own key', async () => {
    vi.spyOn(agentEvalService, 'listDatasets').mockImplementation(
      async (benchmarkId: string) => [listRow(`${benchmarkId}-row`, benchmarkId)] as any,
    );

    renderHook(
      () => {
        useEvalStore.getState().useFetchDatasets('bench-a');
        useEvalStore.getState().useFetchDatasets('bench-b');
      },
      { wrapper },
    );

    await waitFor(() => {
      expect(benchList('bench-a')?.[0]?.name).toBe('bench-a');
      expect(benchList('bench-b')?.[0]?.name).toBe('bench-b');
    });
  });

  it('reads the scope-wide list from its own entry', async () => {
    const listAll = vi
      .spyOn(agentEvalService, 'listAllDatasets')
      .mockResolvedValue([listRow('d1', 'Every')]);
    const listBenchmark = vi.spyOn(agentEvalService, 'listDatasets');

    renderAllList();

    await waitFor(() =>
      expect(useEvalStore.getState().datasetListMap[ALL_DATASETS_KEY]?.[0]?.name).toBe('Every'),
    );
    expect(listAll).toHaveBeenCalledTimes(1);
    expect(listBenchmark).not.toHaveBeenCalled();
  });

  it('fills the detail map from the server', async () => {
    vi.spyOn(agentEvalService, 'getDataset').mockResolvedValue({
      id: DETAIL_ID,
      name: 'MMLU',
    } as any);

    renderDetail(DETAIL_ID);

    await waitFor(() =>
      expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]?.name).toBe('MMLU'),
    );
    expect(agentEvalService.getDataset).toHaveBeenCalledWith(DETAIL_ID);
  });

  it('keeps the detail reference when a revalidation answers an unchanged dataset', async () => {
    const getDataset = vi
      .spyOn(agentEvalService, 'getDataset')
      .mockResolvedValue({ id: DETAIL_ID, name: 'MMLU' } as any);

    renderDetail(DETAIL_ID);
    await waitFor(() =>
      expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]?.name).toBe('MMLU'),
    );
    const before = useEvalStore.getState().datasetDetailMap[DETAIL_ID];

    // Same content, fresh object — what every TRPC response is.
    getDataset.mockResolvedValue({ id: DETAIL_ID, name: 'MMLU' } as any);
    await act(() => useEvalStore.getState().refreshDatasetDetail(DETAIL_ID));
    await waitFor(() => expect(getDataset).toHaveBeenCalledTimes(2));

    expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]).toBe(before);
  });

  it('keeps embedded test cases out of the stored and persisted detail', async () => {
    vi.spyOn(agentEvalService, 'getDataset').mockResolvedValue({
      id: DETAIL_ID,
      name: 'MMLU',
      testCases: [{ id: 'tc-1' }, { id: 'tc-2' }],
    } as any);

    renderDetail(DETAIL_ID);

    await waitFor(() =>
      expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]?.name).toBe('MMLU'),
    );
    expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]).not.toHaveProperty('testCases');
    await waitFor(async () => {
      const persisted = await datasetDetailResource.storage!.get({
        queryKey: DETAIL_STORAGE_KEY,
        scope,
      });
      expect(persisted?.data).toEqual({ id: DETAIL_ID, name: 'MMLU' });
    });
  });

  it('drops a persisted detail when its revalidation answers NOT_FOUND', async () => {
    const getDataset = vi
      .spyOn(agentEvalService, 'getDataset')
      .mockResolvedValue({ id: DETAIL_ID, name: 'MMLU' } as any);

    renderDetail(DETAIL_ID);
    await waitFor(() =>
      expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]?.name).toBe('MMLU'),
    );
    await waitFor(async () =>
      expect(
        (await datasetDetailResource.storage!.get({ queryKey: DETAIL_STORAGE_KEY, scope }))?.data,
      ).toEqual({ id: DETAIL_ID, name: 'MMLU' }),
    );

    // Deleted elsewhere: every read now answers NOT_FOUND. The persisted row
    // must not keep the page painting a dataset that no longer exists.
    getDataset.mockRejectedValue({ data: { code: 'NOT_FOUND' } });
    await act(async () => {
      await useEvalStore
        .getState()
        .refreshDatasetDetail(DETAIL_ID)
        .catch(() => {});
    });

    await waitFor(() =>
      expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]).toBeUndefined(),
    );
    await waitFor(async () =>
      expect(
        await datasetDetailResource.storage!.get({ queryKey: DETAIL_STORAGE_KEY, scope }),
      ).toBeUndefined(),
    );
  });

  it('keeps a deleted dataset gone when its hydrate lands after NOT_FOUND', async () => {
    const getDataset = vi
      .spyOn(agentEvalService, 'getDataset')
      .mockRejectedValue({ data: { code: 'NOT_FOUND' } });

    // Cold revisit: a stale row is still in storage, but its read only finishes
    // after the 404 has already evicted the entry.
    const storage = datasetDetailResource.storage!;
    await storage.set(
      { queryKey: DETAIL_STORAGE_KEY, scope },
      { data: { id: DETAIL_ID, name: 'Cached' } as any, updatedAt: 1 },
    );
    const originalGet = storage.get.bind(storage);
    // The snapshot the in-flight hydrate is holding while the 404 lands.
    const staleRow = await originalGet({ queryKey: DETAIL_STORAGE_KEY, scope });
    let detailReads = 0;
    let releaseRead!: () => void;
    const held = new Promise<void>((resolve) => (releaseRead = resolve));
    vi.spyOn(storage, 'get').mockImplementation((key) => {
      if (key.queryKey !== DETAIL_STORAGE_KEY) return originalGet(key);
      detailReads += 1;
      return held.then(() => staleRow);
    });

    const { result } = renderDetail(DETAIL_ID);

    // The definitive NOT_FOUND settles first…
    await waitFor(() => expect(getDataset).toHaveBeenCalled());
    await waitFor(() => expect(result.current.error).toBeTruthy());

    // …then the stale snapshot the hydrate already read lands.
    await act(async () => {
      releaseRead();
      await new Promise((resolve) => setTimeout(resolve, 0));
      await new Promise((resolve) => setTimeout(resolve, 0));
    });

    // The hydration did read a row, but the late stale value must not repaint it.
    expect(detailReads).toBeGreaterThan(0);
    expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]).toBeUndefined();
    await waitFor(async () =>
      expect(await originalGet({ queryKey: DETAIL_STORAGE_KEY, scope })).toBeUndefined(),
    );
  });

  it('drops a deleted dataset from its persisted detail and every list', async () => {
    vi.spyOn(agentEvalService, 'getDataset').mockResolvedValue({
      id: DETAIL_ID,
      name: 'MMLU',
    } as any);
    vi.spyOn(agentEvalService, 'listDatasets').mockResolvedValue([
      listRow(DETAIL_ID, 'MMLU'),
      listRow('d2', 'Other'),
    ]);
    const deleteDataset = vi.spyOn(agentEvalService, 'deleteDataset').mockResolvedValue({} as any);

    renderDetail(DETAIL_ID);
    renderBenchList();
    await waitFor(() => {
      expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]?.name).toBe('MMLU');
      expect(benchList()).toHaveLength(2);
    });
    await waitFor(async () =>
      expect(
        (await datasetDetailResource.storage!.get({ queryKey: DETAIL_STORAGE_KEY, scope }))?.data,
      ).toEqual({ id: DETAIL_ID, name: 'MMLU' }),
    );

    await act(() => useEvalStore.getState().deleteDataset(DETAIL_ID));

    expect(deleteDataset).toHaveBeenCalledWith(DETAIL_ID);
    expect(useEvalStore.getState().datasetDetailMap[DETAIL_ID]).toBeUndefined();
    expect(benchList()?.map((row) => row.id)).toEqual(['d2']);
    // The next visit must not hydrate the deleted dataset back.
    await waitFor(async () =>
      expect(
        await datasetDetailResource.storage!.get({ queryKey: DETAIL_STORAGE_KEY, scope }),
      ).toBeUndefined(),
    );
    await waitFor(async () =>
      expect(
        (await datasetListResource.storage!.get({ queryKey: BENCH_STORAGE_KEY, scope }))?.data?.map(
          (row) => row.id,
        ),
      ).toEqual(['d2']),
    );
  });

  it('repaints the refreshed benchmark list after a mutation', async () => {
    const listDatasets = vi
      .spyOn(agentEvalService, 'listDatasets')
      .mockResolvedValue([listRow('d1', 'Before')]);
    renderBenchList();
    await waitFor(() => expect(benchList()?.[0]?.name).toBe('Before'));

    listDatasets.mockResolvedValue([listRow('d1', 'After')]);
    await act(() => useEvalStore.getState().refreshDatasets(BENCH));

    await waitFor(() => expect(benchList()?.[0]?.name).toBe('After'));
  });

  it('drops the previous scope’s lists on a cache-scope switch', async () => {
    vi.spyOn(agentEvalService, 'listDatasets').mockResolvedValue([listRow('d1', 'Personal')]);
    const { rerender } = renderBenchList();
    await waitFor(() => expect(benchList()).toHaveLength(1));

    // Switch identity: the new scope's rows are still in flight.
    useScope(`${scope.split(':')[0]}:ws-1`);
    vi.mocked(agentEvalService.listDatasets).mockImplementation(pending);
    rerender();

    expect(useEvalStore.getState().datasetListMap[BENCH]).toBeUndefined();
  });
});