import { describe, expect, it } from 'vitest';

import {
  buildWorkflowListRouteQuery,
  mergeWorkflowListRouteQuery,
  parseWorkflowListRouteState,
} from './workflow-list-route-state';

describe('workflow list route state', () => {
  it('restores page, page size, category and keyword from list query', () => {
    expect(
      parseWorkflowListRouteState({
        categoryId: '7',
        keyword: '月报',
        pageNumber: '3',
        pageSize: '18',
      }),
    ).toEqual({
      categoryId: '7',
      keyword: '月报',
      pageNumber: 3,
      pageSize: 18,
    });
  });

  it('falls back safely for invalid pagination query', () => {
    expect(
      parseWorkflowListRouteState({
        pageNumber: '-2',
        pageSize: '999',
      }),
    ).toEqual({
      categoryId: '',
      keyword: '',
      pageNumber: 1,
      pageSize: 12,
    });
  });

  it('serializes workflow list state into canonical list query', () => {
    const state = {
      categoryId: '7',
      keyword: '月报',
      pageNumber: 3,
      pageSize: 18,
    };

    expect(buildWorkflowListRouteQuery(state)).toEqual({
      categoryId: '7',
      keyword: '月报',
      pageNumber: '3',
      pageSize: '18',
    });
  });

  it('preserves unrelated query while removing cleared list state', () => {
    expect(
      mergeWorkflowListRouteQuery(
        {
          categoryId: '7',
          devLogin: 'admin',
          keyword: '旧关键字',
          pageNumber: '5',
        },
        {
          categoryId: '',
          keyword: '',
          pageNumber: 1,
          pageSize: 12,
        },
      ),
    ).toEqual({ devLogin: 'admin' });
  });
});
