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.

5.8 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { AcceptanceListItem } from '@/services/verify';

import {
  ACCEPTANCE_BATCH_CHUNK,
  acceptanceBatchTargets,
  acceptanceProjectTargets,
  acceptanceRedirectAfterDelete,
  acceptanceSelectAllState,
  chunkAcceptanceBatch,
  nextAcceptanceSelectAll,
  rangeAcceptanceSelection,
  toggleAcceptanceSelection,
  visibleAcceptanceSelection,
} from './batchSelection';

const item = (id: string, status: string, projectId?: string) =>
  ({
    id,
    project: projectId ? { id: projectId, name: projectId } : undefined,
    status,
    subject: { title: id },
    subjectId: id,
  }) as unknown as AcceptanceListItem;

describe('toggleAcceptanceSelection', () => {
  it('adds an unselected row and removes a selected one', () => {
    expect(toggleAcceptanceSelection(['a'], 'b')).toEqual(['a', 'b']);
    expect(toggleAcceptanceSelection(['a', 'b'], 'a')).toEqual(['b']);
  });
});

describe('rangeAcceptanceSelection', () => {
  const order = ['a', 'b', 'c', 'd', 'e'];

  it('selects everything between anchor and target in either direction', () => {
    expect(rangeAcceptanceSelection(order, 'b', 'd', [])).toEqual(['b', 'c', 'd']);
    expect(rangeAcceptanceSelection(order, 'd', 'b', ['e'])).toEqual(['e', 'b', 'c', 'd']);
  });

  it('falls back to a single pick when the anchor is missing or out of order', () => {
    expect(rangeAcceptanceSelection(order, null, 'c', ['a'])).toEqual(['a', 'c']);
    expect(rangeAcceptanceSelection(order, 'gone', 'c', [])).toEqual(['c']);
  });
});

describe('visibleAcceptanceSelection', () => {
  it('drops picks the active filter no longer shows', () => {
    // A sweep must act on exactly the ticked boxes on screen — never on a row
    // that left the list when the filter changed.
    expect(visibleAcceptanceSelection(['a', 'b'], [item('a', 'delivered')])).toEqual(['a']);
  });
});

describe('acceptanceSelectAllState', () => {
  it('reads none / partial / all from the visible counts', () => {
    expect(acceptanceSelectAllState(3, 0)).toBe('none');
    expect(acceptanceSelectAllState(3, 2)).toBe('partial');
    expect(acceptanceSelectAllState(3, 3)).toBe('all');
    expect(acceptanceSelectAllState(0, 0)).toBe('none');
  });
});

describe('nextAcceptanceSelectAll', () => {
  const visible = [item('a', 'delivered'), item('b', 'accepted')];

  it('selects every visible row from a partial selection', () => {
    expect(nextAcceptanceSelectAll(['a'], visible)).toEqual(['a', 'b']);
  });

  it('clears only the visible rows when everything visible is selected', () => {
    // 'hidden' was picked under another filter; unticking the visible boxes
    // must not silently discard it.
    expect(nextAcceptanceSelectAll(['a', 'b', 'hidden'], visible)).toEqual(['hidden']);
  });
});

describe('acceptanceBatchTargets', () => {
  const items = [
    item('delivered', 'delivered'),
    item('accepted', 'accepted'),
    item('verifying', 'verifying'),
    item('closed', 'closed'),
  ];
  const selected = items.map((entry) => entry.id);

  it('accepts only rows that can still be decided', () => {
    expect(acceptanceBatchTargets(items, selected, 'accept')).toEqual(['delivered']);
  });

  it('closes everything except what is already closed', () => {
    expect(acceptanceBatchTargets(items, selected, 'close')).toEqual([
      'delivered',
      'accepted',
      'verifying',
    ]);
  });

  it('ignores rows outside the selection', () => {
    expect(acceptanceBatchTargets(items, ['accepted'], 'close')).toEqual(['accepted']);
  });
});

describe('acceptanceProjectTargets', () => {
  const items = [
    item('loose', 'delivered'),
    item('in-p1', 'delivered', 'p1'),
    item('in-p2', 'accepted', 'p2'),
  ];
  const selected = items.map((entry) => entry.id);

  it('moves every selected row not already in the target project', () => {
    // 'in-p1' is already where the sweep is headed; counting it would report a
    // move that never happened.
    expect(acceptanceProjectTargets(items, selected, 'p1')).toEqual(['loose', 'in-p2']);
  });

  it('removes only rows that actually have a project', () => {
    expect(acceptanceProjectTargets(items, selected, null)).toEqual(['in-p1', 'in-p2']);
  });

  it('ignores rows outside the selection', () => {
    expect(acceptanceProjectTargets(items, ['loose'], 'p1')).toEqual(['loose']);
  });
});

describe('chunkAcceptanceBatch', () => {
  const ids = (count: number) => Array.from({ length: count }, (_, index) => `id-${index}`);

  it('keeps a selection the server accepts in one request', () => {
    expect(chunkAcceptanceBatch(ids(3))).toEqual([ids(3)]);
    expect(chunkAcceptanceBatch(ids(ACCEPTANCE_BATCH_CHUNK))).toHaveLength(1);
  });

  it('splits a selection past the endpoint cap instead of letting it be refused', () => {
    // Select-all after enough scrolling can exceed the cap; one oversized
    // request fails before anything changes at all.
    const chunks = chunkAcceptanceBatch(ids(ACCEPTANCE_BATCH_CHUNK + 1));

    expect(chunks).toHaveLength(2);
    expect(chunks[0]).toHaveLength(ACCEPTANCE_BATCH_CHUNK);
    expect(chunks[1]).toEqual(['id-200']);
    expect(chunks.flat()).toEqual(ids(ACCEPTANCE_BATCH_CHUNK + 1));
  });

  it('has nothing to send for an empty selection', () => {
    expect(chunkAcceptanceBatch([])).toEqual([]);
  });
});

describe('acceptanceRedirectAfterDelete', () => {
  it('returns to the acceptance list, not the app home, when the open row is deleted', () => {
    expect(acceptanceRedirectAfterDelete('a', ['a', 'b'], [])).toBe('/acceptance');
  });

  it('stays put when the open row survived or was not targeted', () => {
    expect(acceptanceRedirectAfterDelete('a', ['a'], ['a'])).toBeUndefined();
    expect(acceptanceRedirectAfterDelete('a', ['b'], [])).toBeUndefined();
    expect(acceptanceRedirectAfterDelete(undefined, ['a'], [])).toBeUndefined();
  });
});