import test from 'node:test';
import assert from 'node:assert/strict';

import { addVersionMembers, filterVersionMemberCandidates, inferVersionMemberRole } from './version-members';

test('filterVersionMemberCandidates searches people and excludes already added members', () => {
  const result = filterVersionMemberCandidates(
    [
      { id: 'm-1', name: 'Alice' },
      { id: 'm-2', name: 'Bob' },
      { id: 'm-3', name: 'Alicia' },
    ],
    [{ name: 'Alice', role: 'frontend' }],
    'ali',
  );

  assert.deepEqual(result.map((member) => member.name), ['Alicia']);
});

test('addVersionMembers appends selected people once with the default version role', () => {
  const result = addVersionMembers(
    [{ name: 'Alice', role: 'product' }],
    ['Bob', 'Alicia', 'Alice'],
    [
      { id: 'm-1', name: 'Alice' },
      { id: 'm-2', name: 'Bob' },
      { id: 'm-3', name: 'Alicia' },
    ],
  );

  assert.deepEqual(result, [
    { name: 'Alice', role: 'product' },
    { name: 'Bob', role: 'frontend' },
    { name: 'Alicia', role: 'frontend' },
  ]);
});

test('inferVersionMemberRole maps department names to project roles', () => {
  assert.equal(inferVersionMemberRole({ id: 'm-1', name: 'Alice', departmentName: '产品部' }), 'product');
  assert.equal(inferVersionMemberRole({ id: 'm-2', name: 'Bob', departmentName: '设计部' }), 'ui');
  assert.equal(inferVersionMemberRole({ id: 'm-3', name: 'Cathy', departmentName: '前端组' }), 'frontend');
  assert.equal(inferVersionMemberRole({ id: 'm-4', name: 'David', departmentName: '后端组' }), 'backend');
  assert.equal(inferVersionMemberRole({ id: 'm-5', name: 'Eve', departmentName: '测试组' }), 'testing');
  assert.equal(inferVersionMemberRole({ id: 'm-6', name: 'Frank', departmentName: '运营部' }), 'frontend');
});

test('addVersionMembers infers each selected person role from department', () => {
  const result = addVersionMembers(
    [],
    ['Product', 'Designer', 'Backend', 'QA'],
    [
      { id: 'm-1', name: 'Product', departmentName: '产品部' },
      { id: 'm-2', name: 'Designer', departmentName: 'UI设计部' },
      { id: 'm-3', name: 'Backend', departmentName: '后端组' },
      { id: 'm-4', name: 'QA', departmentName: '质量测试部' },
    ],
  );

  assert.deepEqual(result, [
    { name: 'Product', role: 'product' },
    { name: 'Designer', role: 'ui' },
    { name: 'Backend', role: 'backend' },
    { name: 'QA', role: 'testing' },
  ]);
});
