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

import type { WorkActivity } from './work-activity';
import { getEntityActivityLogEntries } from './entity-activity-log';

function activity(patch: Partial<WorkActivity>): WorkActivity {
  return {
    id: 'act-1',
    actorId: '张三',
    date: '2026-06-29',
    occurredAt: '2026-06-29T01:00:00.000Z',
    sourceType: 'dev_task',
    sourceId: 'dev-1',
    action: 'dev_task_started',
    category: 'progress',
    title: '开发任务',
    summary: '开始开发：开发任务',
    ...patch,
  };
}

test('getEntityActivityLogEntries filters by source and sorts newest first', () => {
  const entries = getEntityActivityLogEntries([
    activity({ id: 'old', occurredAt: '2026-06-29T01:00:00.000Z' }),
    activity({ id: 'other-source', sourceType: 'test_case', sourceId: 'tc-1' }),
    activity({ id: 'new', occurredAt: '2026-06-29T03:00:00.000Z', action: 'dev_task_submitted' }),
  ], 'dev_task', 'dev-1');

  assert.deepEqual(entries.map((entry) => entry.id), ['new', 'old']);
  assert.equal(entries[0].label, '已提测');
});

test('getEntityActivityLogEntries merges legacy logs with activity entries', () => {
  const entries = getEntityActivityLogEntries([
    activity({ id: 'activity-log', occurredAt: '2026-06-29T02:00:00.000Z' }),
  ], 'dev_task', 'dev-1', [
    {
      id: 'legacy-log',
      actorId: '李四',
      occurredAt: '2026-06-29T04:00:00.000Z',
      label: '旧日志',
      summary: '历史操作记录',
    },
  ]);

  assert.deepEqual(entries.map((entry) => entry.id), ['legacy-log', 'activity-log']);
});
