import { AuditService } from './audit.service';

describe('AuditService V2.7 adapter contract', () => {
  it('writes an append-only audit event with actor and resource scope', async () => {
    const prisma = {
      auditLog: {
        create: jest.fn().mockResolvedValue({ id: 'audit-1' }),
      },
    };
    const service = new AuditService(prisma as any);

    await service.record({
      actorId: 'm-admin',
      action: 'project_member.role_changed',
      resourceType: 'project_member',
      resourceId: 'pm-1',
      projectId: 'project-1',
      before: { role: 'member' },
      after: { role: 'admin' },
    });

    expect(prisma.auditLog.create).toHaveBeenCalledWith({
      data: expect.objectContaining({
        actorId: 'm-admin',
        action: 'project_member.role_changed',
        resourceType: 'project_member',
        resourceId: 'pm-1',
        projectId: 'project-1',
        before: { role: 'member' },
        after: { role: 'admin' },
      }),
    });
  });

  it('does not fail the business action when audit storage is unavailable', async () => {
    const prisma = {
      auditLog: {
        create: jest.fn().mockRejectedValue(new Error('database unavailable')),
      },
    };
    const service = new AuditService(prisma as any);
    (service as any).logger.warn = jest.fn();

    await expect(service.record({
      actorId: 'm-admin',
      action: 'comment.created',
      resourceType: 'comment',
      resourceId: 'comment-1',
    })).resolves.toBeUndefined();
  });
});
