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.

4.7 KB
Raw Blame
import { createTagFacetContext } from '@app/features/soup';
import type { TagSetResponse } from '@service-properties/generated/schemas/tagSetResponse';
import { describe, expect, it, vi } from 'vitest';
import {
  createDateBucketBoundaries,
  taskDueDatePredicate,
} from '../core/task-due-date';
import { taskMatchesView } from '../filters/task-predicates';
import { boardEntity, boardProperty } from '../tests/task-board-fixture';
import { buildTaskSearchRequest } from './task-search';

vi.mock('@app/features/soup', async () => ({
  ...(await import('@app/features/soup/filters')),
}));
vi.mock('@entity', async () => ({
  ...(await import('@entity/types/entity')),
  ...(await import('@entity/utils/notification')),
  ...(await import('@entity/utils/task-properties')),
  ...(await import('@entity/utils/company-properties')),
}));
vi.mock('@notifications', async () => await import('@notifications/types'));

// The soup barrel these pull in transitively imports the websocket client
// modules, which open real sockets at module scope and reject under jsdom.
vi.mock('@service-storage/websocket', () => ({
  storageWS: { reconnectIfDisconnected: vi.fn() },
  createWebSocketJob: vi.fn(),
}));
vi.mock('@service-connection/websocket', () => ({
  ws: { addEventListener: vi.fn(), send: vi.fn() },
  state: () => 'closed',
  createConnectionBlockWebsocketEffect: vi.fn(),
  createConnectionWebsocketEffect: vi.fn(),
}));

const TAG_SETS = [
  {
    scope: 'user',
    definition: { id: 'tag-definition' },
    options: [
      {
        id: 'urgent',
        propertyDefinitionId: 'tag-definition',
        displayOrder: 0,
        value: { type: 'string', value: 'Urgent' },
      },
    ],
  },
] as TagSetResponse[];

const requestFor = (facets: Record<string, string[]>, withTagSets = true) => {
  const { body } = buildTaskSearchRequest({
    query: 'launch',
    matchType: 'partial',
    tab: 'team-tasks',
    userId: 'user-1',
    facets,
    facetContext: withTagSets ? createTagFacetContext(TAG_SETS) : undefined,
  });
  if (!body.filters) throw new Error('search request has no filters');

  return body.filters;
};

describe('buildTaskSearchRequest', () => {
  it('sends selected tags as an entity-wide filter', () => {
    const filters = requestFor({ tags: ['urgent', 'urgent'] });

    expect(filters.tag_option_ids).toEqual(['urgent']);
    expect(filters.tag_filter_mode).toBe('any');
  });

  it('drops a selected tag that no longer exists, like the list query', () => {
    expect(
      requestFor({ tags: ['urgent', 'deleted-tag'] }).tag_option_ids
    ).toEqual(['urgent']);

    const unresolved = requestFor({ tags: ['deleted-tag'] }, false);
    expect(unresolved.tag_option_ids).toBeUndefined();
    expect(unresolved.tag_filter_mode).toBeUndefined();
  });
});

it('compiles the same DST range for remote and local search without changing text ordering', () => {
  const calendar = createDateBucketBoundaries(
    new Date('2026-03-08T17:00:00Z'),
    'America/New_York'
  );
  const dueDate = taskDueDatePredicate({ type: 'today' }, calendar);
  const request = buildTaskSearchRequest({
    query: 'launch',
    matchType: 'partial',
    tab: 'team-tasks',
    userId: 'user',
    facets: {},
    dueDate,
  });
  expect(request.body.filters?.property_filters).toEqual([
    {
      property_definition_id: '00000001-0000-0000-0000-000000000004',
      entity_type: 'TASK',
      date_range: {
        start: '2026-03-08T05:00:00.000Z',
        end: '2026-03-09T04:00:00.000Z',
      },
    },
  ]);
  expect(request.body).not.toHaveProperty('sort');
  const context = {
    tab: 'team-tasks' as const,
    userId: 'user',
    facets: {},
    dueDate,
  };
  expect(taskMatchesView(boardEntity('missing'), context)).toBe(false);
  expect(
    taskMatchesView(
      boardEntity('today', [
        boardProperty(
          '00000001-0000-0000-0000-000000000004',
          { type: 'Date', value: '2026-03-09T03:59:59Z' },
          'DATE'
        ),
      ]),
      context
    )
  ).toBe(true);
  expect(
    taskMatchesView(
      boardEntity('tomorrow', [
        boardProperty(
          '00000001-0000-0000-0000-000000000004',
          { type: 'Date', value: '2026-03-09T04:00:00Z' },
          'DATE'
        ),
      ]),
      context
    )
  ).toBe(false);
});

it('sends explicit false presence rather than omitting the no-date filter', () => {
  const request = buildTaskSearchRequest({
    query: 'launch',
    matchType: 'partial',
    tab: 'team-tasks',
    userId: 'user',
    facets: {},
    dueDate: { hasDate: false },
  });
  expect(request.body.filters?.property_filters).toEqual([
    {
      property_definition_id: '00000001-0000-0000-0000-000000000004',
      entity_type: 'TASK',
      has_date: false,
    },
  ]);
});