Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

7.8 KB
Raw Blame
import type { ActivityEventFieldsFragment } from '@service-storage/graphql/generated/graphql';
import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
import type { JSX } from 'solid-js';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { ActivityContextProvider } from '../context/activity-context';
import {
  createdEvent,
  databaseCreatedEvent,
  editedEvent,
  openedEvent,
  propertyChangedEvent,
} from '../queries/fixtures';
import { createMockActivityContext } from '../tests/mock-context';
import { soupPage } from '../tests/wire';
import { EntityActivitySection } from './entity-activity-section';

// The section registers with the side panel accordion, which needs the
// panel layout; here only the section body is under test.
vi.mock('@components/app/side-panel/SidePanel', () => ({
  SidePanel: {
    Section: (props: { children: JSX.Element }) => props.children,
    Loading: () => <div data-loading />,
    EmptyPill: (props: { label: string }) => <span>{props.label}</span>,
  },
}));

vi.mock(
  '@core/component/LexicalMarkdown/component/core/StaticMarkdown',
  () => ({
    StaticMarkdownContext: (props: { children: unknown }) => props.children,
    StaticMarkdown: () => null,
  })
);

vi.mock('@service-connection/websocket', () => ({
  ws: { send() {}, addEventListener() {}, removeEventListener() {} },
  state: () => 'closed',
  createConnectionBlockWebsocketEffect() {},
  createConnectionWebsocketEffect() {},
  parseWebsocketPayload: () => undefined,
}));

vi.mock('@service-storage/websocket', () => ({
  storageWS: { send() {}, addEventListener() {}, removeEventListener() {} },
  createWebSocketJob: () => Promise.reject(new Error('no websocket in tests')),
}));

// Module-load quarantine: the real EntityIcon reaches the command launcher
// through the `@ui` barrel, and the launcher reads `getIconConfig` back
// before EntityIcon finishes initializing. The mock context supplies every
// icon these tests render.
vi.mock('@core/component/EntityIcon', () => ({
  EntityIcon: () => null,
  getIconConfig: () => ({ icon: () => null }),
}));

afterEach(cleanup);

function renderSection() {
  const context = createMockActivityContext();
  const result = render(() => (
    <ActivityContextProvider value={context}>
      <EntityActivitySection entityId="doc-1" entityType="DOCUMENT" />
    </ActivityContextProvider>
  ));
  return { ...result, graphql: context.graphqlMock };
}

function resolve(
  graphql: ReturnType<typeof renderSection>['graphql'],
  activity: ActivityEventFieldsFragment[]
) {
  graphql
    .latest('EntityActivity')
    .resolve(
      soupPage([{ __typename: 'GraphqlSoupDocument', id: 'doc-1', activity }])
    );
}

const rows = () => document.querySelectorAll('[data-activity-row]');
const RAIL_ABOVE = '[data-activity-rail="above"]';
const RAIL_BELOW = '[data-activity-rail="below"]';
const at = (id: string, event: ActivityEventFieldsFragment) => ({
  ...event,
  id,
});

// Newest first: three edits fold into one run, then four singles, with the
// creation row oldest. Six entries for eight events.
const HISTORY: ActivityEventFieldsFragment[] = [
  at('e8', editedEvent),
  at('e7', editedEvent),
  at('e6', editedEvent),
  at('e5', openedEvent),
  at('e4', editedEvent),
  at('e3', propertyChangedEvent),
  at('e2', openedEvent),
  at('e1', createdEvent),
];

describe('EntityActivitySection', () => {
  it('shows every entry unfolded when the history is short', () => {
    const { container, graphql } = renderSection();
    resolve(graphql, HISTORY.slice(4));

    const visible = rows();
    expect(visible).toHaveLength(4);
    expect(container.querySelector('[data-activity-fold-toggle]')).toBeNull();
    expect(visible[0]?.querySelector(RAIL_ABOVE)?.classList).toContain(
      'invisible'
    );
    expect(visible[3]?.querySelector(RAIL_BELOW)?.classList).toContain(
      'invisible'
    );
  });

  it('folds to three newest entries, a toggle, and the pinned creation row', () => {
    const { container, graphql } = renderSection();
    resolve(graphql, HISTORY);

    const visible = rows();
    expect(visible).toHaveLength(4);
    expect(visible[0]?.getAttribute('data-activity-run-size')).toBe('3');
    // No entity on the panel's own rows, so the count folds into the verb.
    expect(visible[0]?.textContent).toContain('made 3 edits');
    expect(visible[0]?.textContent).not.toContain('times');
    expect(visible[1]?.getAttribute('data-activity-action')).toBe('opened');
    expect(visible[2]?.getAttribute('data-activity-action')).toBe('edited');
    expect(visible[3]?.getAttribute('data-activity-action')).toBe('created');

    const toggle = screen.getByRole('button', { name: 'View all activities' });
    expect(toggle.getAttribute('aria-expanded')).toBe('false');
    expect(container.textContent).not.toContain('Show all');

    // The rail starts at the first glyph, runs through the toggle, and ends
    // at the pinned row's glyph.
    expect(visible[0]?.querySelector(RAIL_ABOVE)?.classList).toContain(
      'invisible'
    );
    expect(visible[0]?.querySelector(RAIL_BELOW)?.classList).not.toContain(
      'invisible'
    );
    expect(visible[2]?.querySelector(RAIL_BELOW)?.classList).not.toContain(
      'invisible'
    );
    expect(toggle.querySelector(RAIL_BELOW)?.classList).not.toContain(
      'invisible'
    );
    expect(visible[3]?.querySelector(RAIL_ABOVE)?.classList).not.toContain(
      'invisible'
    );
    expect(visible[3]?.querySelector(RAIL_BELOW)?.classList).toContain(
      'invisible'
    );
    expect(container.querySelector('.ring')).toBeNull();

    fireEvent.click(toggle);
    expect(rows()).toHaveLength(6);
    const showLess = screen.getByRole('button', { name: 'Show less' });
    expect(showLess.getAttribute('aria-expanded')).toBe('true');
    expect(rows()[5]?.getAttribute('data-activity-action')).toBe('created');
    expect(rows()[5]?.querySelector(RAIL_BELOW)?.classList).not.toContain(
      'invisible'
    );
    expect(showLess.querySelector(RAIL_BELOW)?.classList).toContain(
      'invisible'
    );

    fireEvent.click(screen.getByRole('button', { name: 'Show less' }));
    expect(rows()).toHaveLength(4);
    expect(
      screen.getByRole('button', { name: 'View all activities' })
    ).toBeTruthy();
  });

  it('reads compact rows with the actor, the phrase, and a compact inline time', () => {
    const { graphql } = renderSection();
    resolve(graphql, [at('e1', createdEvent)]);

    const row = rows()[0];
    if (!row) throw new Error('row not rendered');
    expect(row.textContent).toContain('sarah');
    expect(row.textContent).toContain('created this');
    const time = row.querySelector('time');
    expect(time?.getAttribute('dateTime')).toBe(createdEvent.occurredAt);
    expect(time?.textContent).toMatch(/^(now|\d+(m|h|d|w|mo|y))$/);
    expect(time?.getAttribute('title')).toBeTruthy();
    expect(row.textContent).toContain('·');
    expect(row.textContent).not.toContain('ago');
  });

  it('shows the empty and error states', () => {
    const empty = renderSection();
    resolve(empty.graphql, []);
    expect(empty.container.textContent).toContain('No activity yet');
    cleanup();

    const failed = renderSection();
    failed.graphql.latest('EntityActivity').fail('boom');
    expect(failed.container.textContent).toContain('Activity is unavailable');
  });

  it('reads a database, which is not a soup item, by its id', () => {
    const context = createMockActivityContext();
    render(() => (
      <ActivityContextProvider value={context}>
        <EntityActivitySection entityId="database-1" entityType="DATABASE" />
      </ActivityContextProvider>
    ));
    context.graphqlMock.latest('DatabaseActivity').resolve({
      user: { id: 'user-1', databaseActivity: [databaseCreatedEvent] },
    });

    expect(rows()).toHaveLength(1);
    expect(rows()[0]?.getAttribute('data-activity-action')).toBe('created');
    expect(rows()[0]?.textContent).toContain('created this');
  });
});