fix(test): remove async from getMany mock - no await expression
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
// File: .agents/skills/bugfix/SKILL.md
|
|
||||||
// Change Log: 2026-05-13 - Initial version improved from docs/bugfix.md
|
|
||||||
|
|
||||||
---
|
---
|
||||||
name: bugfix
|
name: bugfix
|
||||||
description: Quick bugfix workflow with minimal impact. Focused on surgical fixes without unrelated refactoring.
|
description: Quick bugfix workflow with minimal impact. Focused on surgical fixes without unrelated refactoring.
|
||||||
version: 1.0.0
|
version: 1.9.0
|
||||||
---
|
---
|
||||||
|
|
||||||
# Bugfix
|
# Bugfix
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
// File: .agents/skills/bugfix/SKILL.md
|
|
||||||
// Change Log: 2026-05-13 - Initial version improved from docs/bugfix.md
|
|
||||||
|
|
||||||
---
|
---
|
||||||
name: bugfix
|
name: bugfix
|
||||||
description: Quick bugfix workflow with minimal impact. Focused on surgical fixes without unrelated refactoring.
|
description: Quick bugfix workflow with minimal impact. Focused on surgical fixes without unrelated refactoring.
|
||||||
version: 1.0.0
|
version: 1.9.0
|
||||||
---
|
---
|
||||||
|
|
||||||
# Bugfix
|
# Bugfix
|
||||||
|
|||||||
@@ -1,17 +1,75 @@
|
|||||||
// File: tests/unit/delegation/circular-detection.service.spec.ts
|
// File: tests/unit/delegation/circular-detection.service.spec.ts
|
||||||
|
// Change Log
|
||||||
|
// - 2026-05-13: ปรับ mock repository ให้ตรงกับ QueryBuilder contract ของ CircularDetectionService
|
||||||
// Unit tests สำหรับ CircularDetectionService — ป้องกัน delegation loops (T075)
|
// Unit tests สำหรับ CircularDetectionService — ป้องกัน delegation loops (T075)
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||||
import { CircularDetectionService } from '../../../src/modules/delegation/services/circular-detection.service';
|
import { CircularDetectionService } from '../../../src/modules/delegation/services/circular-detection.service';
|
||||||
import { Delegation } from '../../../src/modules/delegation/entities/delegation.entity';
|
import { Delegation } from '../../../src/modules/delegation/entities/delegation.entity';
|
||||||
|
|
||||||
const mockDelegationRepo = {
|
type DelegationRow = Pick<Delegation, 'delegatorUserId' | 'delegateUserId'>;
|
||||||
find: jest.fn(),
|
|
||||||
|
type QueryBuilderMock = {
|
||||||
|
where: jest.MockedFunction<
|
||||||
|
(
|
||||||
|
condition: string,
|
||||||
|
parameters?: Record<string, unknown>
|
||||||
|
) => QueryBuilderMock
|
||||||
|
>;
|
||||||
|
andWhere: jest.MockedFunction<
|
||||||
|
(
|
||||||
|
condition: string,
|
||||||
|
parameters?: Record<string, unknown>
|
||||||
|
) => QueryBuilderMock
|
||||||
|
>;
|
||||||
|
select: jest.MockedFunction<(selection: string[]) => QueryBuilderMock>;
|
||||||
|
getMany: jest.MockedFunction<() => Promise<DelegationRow[]>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type DelegationRepositoryMock = {
|
||||||
|
createQueryBuilder: jest.MockedFunction<(alias: string) => QueryBuilderMock>;
|
||||||
|
};
|
||||||
|
|
||||||
|
const createQueryBuilderMock = (
|
||||||
|
delegations: DelegationRow[]
|
||||||
|
): QueryBuilderMock => {
|
||||||
|
const queryBuilder = {} as QueryBuilderMock;
|
||||||
|
queryBuilder.where = jest.fn(
|
||||||
|
(
|
||||||
|
_condition: string,
|
||||||
|
_parameters?: Record<string, unknown>
|
||||||
|
): QueryBuilderMock => queryBuilder
|
||||||
|
);
|
||||||
|
queryBuilder.andWhere = jest.fn(
|
||||||
|
(
|
||||||
|
_condition: string,
|
||||||
|
_parameters?: Record<string, unknown>
|
||||||
|
): QueryBuilderMock => queryBuilder
|
||||||
|
);
|
||||||
|
queryBuilder.select = jest.fn(
|
||||||
|
(_selection: string[]): QueryBuilderMock => queryBuilder
|
||||||
|
);
|
||||||
|
queryBuilder.getMany = jest.fn(
|
||||||
|
(): Promise<DelegationRow[]> => Promise.resolve(delegations)
|
||||||
|
);
|
||||||
|
return queryBuilder;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockDelegationRepo: DelegationRepositoryMock = {
|
||||||
|
createQueryBuilder: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('CircularDetectionService', () => {
|
describe('CircularDetectionService', () => {
|
||||||
let service: CircularDetectionService;
|
let service: CircularDetectionService;
|
||||||
|
|
||||||
|
const mockActiveDelegations = (
|
||||||
|
delegations: DelegationRow[]
|
||||||
|
): QueryBuilderMock => {
|
||||||
|
const queryBuilder = createQueryBuilderMock(delegations);
|
||||||
|
mockDelegationRepo.createQueryBuilder.mockReturnValue(queryBuilder);
|
||||||
|
return queryBuilder;
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
@@ -33,16 +91,14 @@ describe('CircularDetectionService', () => {
|
|||||||
|
|
||||||
describe('wouldCreateCircle', () => {
|
describe('wouldCreateCircle', () => {
|
||||||
it('should return false when no delegations exist', async () => {
|
it('should return false when no delegations exist', async () => {
|
||||||
mockDelegationRepo.find.mockResolvedValue([]);
|
mockActiveDelegations([]);
|
||||||
const result = await service.wouldCreateCircle(1, 2);
|
const result = await service.wouldCreateCircle(1, 2);
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should detect direct circular delegation A→B when B→A exists', async () => {
|
it('should detect direct circular delegation A→B when B→A exists', async () => {
|
||||||
// B (id=2) delegates to A (id=1)
|
// B (id=2) delegates to A (id=1)
|
||||||
mockDelegationRepo.find.mockResolvedValue([
|
mockActiveDelegations([{ delegatorUserId: 2, delegateUserId: 1 }]);
|
||||||
{ delegatorId: 2, delegateId: 1 },
|
|
||||||
]);
|
|
||||||
// Now trying to add A→B — would create cycle
|
// Now trying to add A→B — would create cycle
|
||||||
const result = await service.wouldCreateCircle(1, 2);
|
const result = await service.wouldCreateCircle(1, 2);
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
@@ -50,9 +106,9 @@ describe('CircularDetectionService', () => {
|
|||||||
|
|
||||||
it('should detect indirect cycle A→B→C when trying C→A', async () => {
|
it('should detect indirect cycle A→B→C when trying C→A', async () => {
|
||||||
// A→B and B→C already exist
|
// A→B and B→C already exist
|
||||||
mockDelegationRepo.find.mockResolvedValue([
|
mockActiveDelegations([
|
||||||
{ delegatorId: 1, delegateId: 2 },
|
{ delegatorUserId: 1, delegateUserId: 2 },
|
||||||
{ delegatorId: 2, delegateId: 3 },
|
{ delegatorUserId: 2, delegateUserId: 3 },
|
||||||
]);
|
]);
|
||||||
// Now trying C→A — would create A→B→C→A cycle
|
// Now trying C→A — would create A→B→C→A cycle
|
||||||
const result = await service.wouldCreateCircle(3, 1);
|
const result = await service.wouldCreateCircle(3, 1);
|
||||||
@@ -61,9 +117,9 @@ describe('CircularDetectionService', () => {
|
|||||||
|
|
||||||
it('should return false for non-circular delegations', async () => {
|
it('should return false for non-circular delegations', async () => {
|
||||||
// A→B and B→C — adding D→A is fine
|
// A→B and B→C — adding D→A is fine
|
||||||
mockDelegationRepo.find.mockResolvedValue([
|
mockActiveDelegations([
|
||||||
{ delegatorId: 1, delegateId: 2 },
|
{ delegatorUserId: 1, delegateUserId: 2 },
|
||||||
{ delegatorId: 2, delegateId: 3 },
|
{ delegatorUserId: 2, delegateUserId: 3 },
|
||||||
]);
|
]);
|
||||||
const result = await service.wouldCreateCircle(4, 1);
|
const result = await service.wouldCreateCircle(4, 1);
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user