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.

4.0 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';

import { resolveAssigneeUserId } from './assignee';

vi.mock('../../api/workspace', () => ({ resolveWorkspaceId: vi.fn() }));

const { resolveWorkspaceId } = await import('../../api/workspace');

const members = [
  { role: 'member', user: { email: '[email protected]', username: 'neko' }, userId: 'user_neko' },
  {
    role: 'owner',
    user: { email: '[email protected]', username: 'arvinxx' },
    userId: 'user_arvin',
  },
];

const clientWith = (list = members) => {
  const query = vi.fn().mockResolvedValue(list);
  return { client: { workspaceMember: { list: { query } } } as never, query };
};

describe('resolveAssigneeUserId', () => {
  afterEach(() => {
    vi.mocked(resolveWorkspaceId).mockReset();
  });

  it('passes a raw user id through in personal scope, where there is no member list', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue(undefined);
    const { client, query } = clientWith();

    await expect(resolveAssigneeUserId(client, 'user_neko')).resolves.toBe('user_neko');
    expect(query).not.toHaveBeenCalled();
  });

  it('matches a member by user id under a workspace scope', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue('ws-1');

    await expect(resolveAssigneeUserId(clientWith().client, 'user_neko')).resolves.toBe(
      'user_neko',
    );
  });

  it('resolves a username that happens to start with user_ instead of treating it as an id', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue('ws-1');
    const list = [
      ...members,
      {
        role: 'member',
        user: { email: '[email protected]', username: 'user_ops' },
        userId: 'user_real_ops',
      },
    ];

    await expect(resolveAssigneeUserId(clientWith(list).client, 'user_ops')).resolves.toBe(
      'user_real_ops',
    );
  });

  it('resolves a member by email or username, case-insensitively', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue('ws-1');
    const { client } = clientWith();

    await expect(resolveAssigneeUserId(client, '[email protected]')).resolves.toBe('user_neko');
    await expect(resolveAssigneeUserId(client, 'arvinxx')).resolves.toBe('user_arvin');
  });

  it('refuses a non-id value outside a workspace scope', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue(undefined);
    const { client, query } = clientWith();

    await expect(resolveAssigneeUserId(client, 'neko')).rejects.toThrow(/outside a workspace/);
    expect(query).not.toHaveBeenCalled();
  });

  it('reports no match and ambiguous matches instead of guessing', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue('ws-1');

    await expect(resolveAssigneeUserId(clientWith().client, 'ghost')).rejects.toThrow(
      /No workspace member/,
    );

    const dup = [
      ...members,
      { role: 'admin', user: { email: '[email protected]', username: 'neko' }, userId: 'user_other' },
    ];
    await expect(resolveAssigneeUserId(clientWith(dup).client, 'neko')).rejects.toThrow(
      /matches 2 members/,
    );
  });

  it('rejects a viewer, who cannot own tasks, instead of letting the mutation fail', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue('ws-1');
    const list = [
      ...members,
      { role: 'viewer', user: { email: '[email protected]', username: 'viewer' }, userId: 'user_v' },
    ];

    await expect(resolveAssigneeUserId(clientWith(list).client, 'viewer')).rejects.toThrow(
      /is a viewer .* can't be assigned tasks/,
    );
    await expect(resolveAssigneeUserId(clientWith(list).client, 'user_v')).rejects.toThrow(
      /is a viewer/,
    );
  });

  it('does not let a viewer sharing a username make an eligible match ambiguous', async () => {
    vi.mocked(resolveWorkspaceId).mockReturnValue('ws-1');
    const list = [
      ...members,
      { role: 'viewer', user: { email: '[email protected]', username: 'neko' }, userId: 'user_v' },
    ];

    await expect(resolveAssigneeUserId(clientWith(list).client, 'neko')).resolves.toBe('user_neko');
  });
});