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.

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

import { type BriefStore } from '@/store/brief/store';
import { type BriefItem } from '@/store/brief/types';

import { initialBriefListState } from './initialState';
import { BRIEF_LIST_KEY } from './projection';
import { briefListSelectors, briefNewsSelectors } from './selectors';

const createState = (overrides: Partial<BriefStore> = {}) =>
  ({
    ...initialBriefListState,
    ...overrides,
  }) as BriefStore;

const feed = (briefs: BriefItem[]) => ({ [BRIEF_LIST_KEY]: briefs });

describe('briefListSelectors', () => {
  describe('briefs', () => {
    it('should return empty array by default', () => {
      expect(briefListSelectors.briefs(createState())).toEqual([]);
    });

    it('should return the loaded feed', () => {
      const briefs = [{ id: 'brief-1' }] as unknown as BriefItem[];
      const state = createState({ briefListMap: feed(briefs) });
      expect(briefListSelectors.briefs(state)).toBe(briefs);
    });

    // An identity scope with no value yet must not churn `shallow`-compared
    // subscribers (and must not look like an empty feed).
    it('should keep a stable identity for an unloaded feed', () => {
      const state = createState();
      expect(briefListSelectors.briefs(state)).toBe(briefListSelectors.briefs(state));
    });
  });

  describe('hasBriefs', () => {
    it('should return false when empty', () => {
      expect(briefListSelectors.hasBriefs(createState())).toBe(false);
    });

    it('should return true when has briefs', () => {
      const state = createState({ briefListMap: feed([{ id: 'brief-1' } as BriefItem]) });
      expect(briefListSelectors.hasBriefs(state)).toBe(true);
    });
  });

  describe('isBriefsInit', () => {
    it('should return false while the feed has no value for the active scope', () => {
      expect(briefListSelectors.isBriefsInit(createState())).toBe(false);
    });

    it('should report loaded once the feed has a value, even an empty one', () => {
      expect(briefListSelectors.isBriefsInit(createState({ briefListMap: feed([]) }))).toBe(true);
    });
  });
});

describe('briefNewsSelectors.paintableNewsDay', () => {
  const DAY = '2026-07-31';
  const newsState = (news: BriefItem[], source?: 'server' | 'storage') =>
    createState({
      briefNewsMap: { [DAY]: { day: DAY, hasEarlier: false, news } },
      briefNewsReplica: {
        entries: source ? { [DAY]: { pending: [], source, updatedAt: 1 } } : {},
      },
    });

  it('is undefined before the day is hydrated or fetched', () => {
    expect(briefNewsSelectors.paintableNewsDay(DAY)(createState())).toBeUndefined();
  });

  it('paints restored rows before the server answers', () => {
    const state = newsState([{ id: 'b1' }] as unknown as BriefItem[], 'storage');
    expect(briefNewsSelectors.paintableNewsDay(DAY)(state)?.news).toHaveLength(1);
  });

  // A stale empty snapshot must not claim "nothing happened today".
  it('keeps a restored empty day loading until the server confirms it', () => {
    expect(briefNewsSelectors.paintableNewsDay(DAY)(newsState([], 'storage'))).toBeUndefined();
    expect(briefNewsSelectors.paintableNewsDay(DAY)(newsState([], 'server'))?.news).toEqual([]);
  });
});