ef20839f99
Phase 1-2: Setup, SQL schema, enums, queue constants, base entities
Phase 3 (US1): ReviewTeam, ReviewTeamMember, ReviewTask, TaskCreationService
Phase 4 (US2): ResponseCode, ResponseCodeRule, ImplicationsService, NotificationTriggerService
Phase 5 (US3): Delegation entity, CircularDetectionService, DelegationService/Controller/Module
Phase 6 (US4): ReminderRule, SchedulerService, EscalationService, ReminderProcessor, ReminderModule
Phase 7 (US5): DistributionMatrix, DistributionRecipient, ApprovalListenerService (Strangler),
TransmittalCreatorService, DistributionProcessor, DistributionModule
Phase 8 (US6): MatrixManagementService, InheritanceService (global→project override)
Phase 9 (Polish): AggregateStatusService, ConsensusService, VetoOverrideService,
ParallelGatewayHandler, review-validators, optimistic locking in completeReview,
test stubs (unit/integration/e2e), jest.config.js updated for tests/ directory
Frontend: ReviewTaskInbox, ParallelProgress, VetoOverrideDialog, DelegationForm,
DelegatedBadge, MatrixEditor, ProjectOverrideManager, DistributionStatus,
ReminderHistory, ResponseCodeSelector, CodeImplications, CompleteReviewForm,
ReviewTeamForm, ReviewTeamSelector, TeamMemberManager
Closes #1
70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
// File: tests/unit/delegation/circular-detection.service.spec.ts
|
|
// Unit tests สำหรับ CircularDetectionService — ป้องกัน delegation loops (T075)
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
import { CircularDetectionService } from '../../../src/modules/delegation/services/circular-detection.service';
|
|
import { Delegation } from '../../../src/modules/delegation/entities/delegation.entity';
|
|
|
|
const mockDelegationRepo = {
|
|
find: jest.fn(),
|
|
};
|
|
|
|
describe('CircularDetectionService', () => {
|
|
let service: CircularDetectionService;
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
CircularDetectionService,
|
|
{ provide: getRepositoryToken(Delegation), useValue: mockDelegationRepo },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<CircularDetectionService>(CircularDetectionService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe('wouldCreateCircle', () => {
|
|
it('should return false when no delegations exist', async () => {
|
|
mockDelegationRepo.find.mockResolvedValue([]);
|
|
const result = await service.wouldCreateCircle(1, 2);
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it('should detect direct circular delegation A→B when B→A exists', async () => {
|
|
// B (id=2) delegates to A (id=1)
|
|
mockDelegationRepo.find.mockResolvedValue([
|
|
{ delegatorId: 2, delegateId: 1 },
|
|
]);
|
|
// Now trying to add A→B — would create cycle
|
|
const result = await service.wouldCreateCircle(1, 2);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it('should detect indirect cycle A→B→C when trying C→A', async () => {
|
|
// A→B and B→C already exist
|
|
mockDelegationRepo.find.mockResolvedValue([
|
|
{ delegatorId: 1, delegateId: 2 },
|
|
{ delegatorId: 2, delegateId: 3 },
|
|
]);
|
|
// Now trying C→A — would create A→B→C→A cycle
|
|
const result = await service.wouldCreateCircle(3, 1);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it('should return false for non-circular delegations', async () => {
|
|
// A→B and B→C — adding D→A is fine
|
|
mockDelegationRepo.find.mockResolvedValue([
|
|
{ delegatorId: 1, delegateId: 2 },
|
|
{ delegatorId: 2, delegateId: 3 },
|
|
]);
|
|
const result = await service.wouldCreateCircle(4, 1);
|
|
expect(result).toBe(false);
|
|
});
|
|
});
|
|
});
|