import { WorkActivityService } from './work-activity.service';

describe('WorkActivityService relational evidence', () => {
  const makeService = () => {
    const prisma = {
      workActivity: {
        create: jest.fn().mockResolvedValue({ id: 'activity-1' }),
      },
      xiaobaoRiskSummary: {
        upsert: jest.fn().mockResolvedValue({}),
      },
    };

    return {
      prisma,
      service: new WorkActivityService(prisma as any),
    };
  };

  it('records activity rows and marks the affected version summary dirty', async () => {
    const { prisma, service } = makeService();

    await service.record({
      versionId: 'version-1',
      productId: 'product-1',
      projectId: 'project-1',
      actorId: 'member-1',
      sourceType: 'dev_task',
      sourceId: 'task-1',
      action: 'dev_task_started',
      title: '开发登录',
      category: 'progress',
      summary: '开始开发：开发登录',
      metadata: { fromStatus: 'todo', toStatus: 'in_progress' },
    });

    expect(prisma.workActivity.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        versionId: 'version-1',
        productId: 'product-1',
        projectId: 'project-1',
        actorId: 'member-1',
        sourceType: 'dev_task',
        sourceId: 'task-1',
        sourceVersionId: 'version-1',
        action: 'dev_task_started',
        title: '开发登录',
        metadata: expect.objectContaining({
          category: 'progress',
          summary: '开始开发：开发登录',
          fromStatus: 'todo',
          toStatus: 'in_progress',
        }),
      }),
    });
    expect(prisma.xiaobaoRiskSummary.upsert).toHaveBeenCalledWith(
      expect.objectContaining({
        where: { versionId: 'version-1' },
        update: { dirty: true },
      }),
    );
  });
});
