2026-01-28 13:03:28 +08:00
|
|
|
import { Test, TestingModule } from "@nestjs/testing";
|
|
|
|
|
import { getRepositoryToken } from "@nestjs/typeorm";
|
2026-01-28 10:42:06 +08:00
|
|
|
import {
|
|
|
|
|
NotFoundException,
|
|
|
|
|
BadRequestException,
|
|
|
|
|
ForbiddenException,
|
2026-01-28 13:03:28 +08:00
|
|
|
} from "@nestjs/common";
|
|
|
|
|
import { LedgersService } from "./ledgers.service";
|
|
|
|
|
import { Ledger } from "../../entities/ledger.entity";
|
|
|
|
|
import { Group } from "../../entities/group.entity";
|
|
|
|
|
import { GroupMember } from "../../entities/group-member.entity";
|
2026-01-28 10:42:06 +08:00
|
|
|
|
|
|
|
|
enum LedgerType {
|
2026-01-28 13:03:28 +08:00
|
|
|
INCOME = "income",
|
|
|
|
|
EXPENSE = "expense",
|
2026-01-28 10:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("LedgersService", () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
let service: LedgersService;
|
|
|
|
|
let mockLedgerRepository: any;
|
|
|
|
|
let mockGroupRepository: any;
|
|
|
|
|
let mockGroupMemberRepository: any;
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const mockUser = { id: "user-1", username: "testuser" };
|
|
|
|
|
const mockGroup = {
|
|
|
|
|
id: "group-1",
|
|
|
|
|
name: "测试小组",
|
2026-01-28 10:42:06 +08:00
|
|
|
isActive: true,
|
|
|
|
|
parentId: null,
|
|
|
|
|
};
|
|
|
|
|
const mockMembership = {
|
2026-01-28 13:03:28 +08:00
|
|
|
id: "member-1",
|
|
|
|
|
userId: "user-1",
|
|
|
|
|
groupId: "group-1",
|
|
|
|
|
role: "member",
|
2026-01-28 10:42:06 +08:00
|
|
|
isActive: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mockLedger = {
|
2026-01-28 13:03:28 +08:00
|
|
|
id: "ledger-1",
|
|
|
|
|
groupId: "group-1",
|
|
|
|
|
creatorId: "user-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
type: LedgerType.INCOME,
|
|
|
|
|
amount: 100,
|
2026-01-28 13:03:28 +08:00
|
|
|
category: "聚餐费用",
|
|
|
|
|
description: "周末聚餐",
|
|
|
|
|
createdAt: new Date("2024-01-20T10:00:00Z"),
|
2026-01-28 10:42:06 +08:00
|
|
|
updatedAt: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
mockLedgerRepository = {
|
|
|
|
|
create: jest.fn(),
|
|
|
|
|
save: jest.fn(),
|
|
|
|
|
findOne: jest.fn(),
|
|
|
|
|
remove: jest.fn(),
|
|
|
|
|
createQueryBuilder: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockGroupRepository = {
|
|
|
|
|
findOne: jest.fn(),
|
|
|
|
|
find: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockGroupMemberRepository = {
|
|
|
|
|
findOne: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
|
|
|
providers: [
|
|
|
|
|
LedgersService,
|
|
|
|
|
{
|
|
|
|
|
provide: getRepositoryToken(Ledger),
|
|
|
|
|
useValue: mockLedgerRepository,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: getRepositoryToken(Group),
|
|
|
|
|
useValue: mockGroupRepository,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: getRepositoryToken(GroupMember),
|
|
|
|
|
useValue: mockGroupMemberRepository,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}).compile();
|
|
|
|
|
|
|
|
|
|
service = module.get<LedgersService>(LedgersService);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("create", () => {
|
|
|
|
|
it("应该成功创建账目", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
|
|
|
|
mockLedgerRepository.create.mockReturnValue(mockLedger);
|
|
|
|
|
mockLedgerRepository.save.mockResolvedValue(mockLedger);
|
|
|
|
|
mockLedgerRepository.findOne.mockResolvedValue({
|
|
|
|
|
...mockLedger,
|
|
|
|
|
group: mockGroup,
|
|
|
|
|
creator: mockUser,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.create("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
type: LedgerType.INCOME,
|
|
|
|
|
amount: 100,
|
2026-01-28 13:03:28 +08:00
|
|
|
category: "聚餐费用",
|
|
|
|
|
description: "周末聚餐",
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
expect(result).toHaveProperty("id");
|
2026-01-28 10:42:06 +08:00
|
|
|
expect(result.amount).toBe(100);
|
|
|
|
|
expect(mockLedgerRepository.save).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在小组不存在时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockGroupRepository.findOne.mockResolvedValue(null);
|
|
|
|
|
|
|
|
|
|
await expect(
|
2026-01-28 13:03:28 +08:00
|
|
|
service.create("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
type: LedgerType.INCOME,
|
|
|
|
|
amount: 100,
|
2026-01-28 13:03:28 +08:00
|
|
|
category: "聚餐费用",
|
|
|
|
|
description: "测试",
|
2026-01-28 10:42:06 +08:00
|
|
|
}),
|
|
|
|
|
).rejects.toThrow(NotFoundException);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在用户不在小组中时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(null);
|
|
|
|
|
|
|
|
|
|
await expect(
|
2026-01-28 13:03:28 +08:00
|
|
|
service.create("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
type: LedgerType.INCOME,
|
|
|
|
|
amount: 100,
|
2026-01-28 13:03:28 +08:00
|
|
|
category: "聚餐费用",
|
|
|
|
|
description: "测试",
|
2026-01-28 10:42:06 +08:00
|
|
|
}),
|
|
|
|
|
).rejects.toThrow(ForbiddenException);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在金额无效时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
|
|
|
|
|
|
|
|
|
await expect(
|
2026-01-28 13:03:28 +08:00
|
|
|
service.create("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
type: LedgerType.INCOME,
|
|
|
|
|
amount: -100,
|
2026-01-28 13:03:28 +08:00
|
|
|
category: "聚餐费用",
|
|
|
|
|
description: "测试",
|
2026-01-28 10:42:06 +08:00
|
|
|
}),
|
|
|
|
|
).rejects.toThrow(BadRequestException);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("findAll", () => {
|
|
|
|
|
it("应该成功获取账目列表", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
const mockQueryBuilder = {
|
|
|
|
|
leftJoinAndSelect: jest.fn().mockReturnThis(),
|
|
|
|
|
where: jest.fn().mockReturnThis(),
|
|
|
|
|
andWhere: jest.fn().mockReturnThis(),
|
|
|
|
|
orderBy: jest.fn().mockReturnThis(),
|
|
|
|
|
skip: jest.fn().mockReturnThis(),
|
|
|
|
|
take: jest.fn().mockReturnThis(),
|
|
|
|
|
getManyAndCount: jest.fn().mockResolvedValue([[mockLedger], 1]),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockLedgerRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.findAll("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
page: 1,
|
|
|
|
|
limit: 10,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
expect(result).toHaveProperty("items");
|
|
|
|
|
expect(result).toHaveProperty("total");
|
2026-01-28 10:42:06 +08:00
|
|
|
expect(result.items).toHaveLength(1);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该支持按类型筛选", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
const mockQueryBuilder = {
|
|
|
|
|
leftJoinAndSelect: jest.fn().mockReturnThis(),
|
|
|
|
|
where: jest.fn().mockReturnThis(),
|
|
|
|
|
andWhere: jest.fn().mockReturnThis(),
|
|
|
|
|
orderBy: jest.fn().mockReturnThis(),
|
|
|
|
|
skip: jest.fn().mockReturnThis(),
|
|
|
|
|
take: jest.fn().mockReturnThis(),
|
|
|
|
|
getManyAndCount: jest.fn().mockResolvedValue([[mockLedger], 1]),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockLedgerRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.findAll("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
type: LedgerType.INCOME,
|
|
|
|
|
page: 1,
|
|
|
|
|
limit: 10,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.items).toHaveLength(1);
|
|
|
|
|
expect(mockQueryBuilder.andWhere).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("findOne", () => {
|
|
|
|
|
it("应该成功获取账目详情", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne.mockResolvedValue({
|
|
|
|
|
...mockLedger,
|
|
|
|
|
group: mockGroup,
|
|
|
|
|
creator: mockUser,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.findOne("ledger-1");
|
2026-01-28 10:42:06 +08:00
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
expect(result).toHaveProperty("id");
|
|
|
|
|
expect(result.id).toBe("ledger-1");
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在账目不存在时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne.mockResolvedValue(null);
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
await expect(service.findOne("ledger-1")).rejects.toThrow(
|
2026-01-28 10:42:06 +08:00
|
|
|
NotFoundException,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("update", () => {
|
|
|
|
|
it("应该成功更新账目", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne
|
|
|
|
|
.mockResolvedValueOnce(mockLedger)
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
...mockLedger,
|
|
|
|
|
amount: 200,
|
|
|
|
|
group: mockGroup,
|
|
|
|
|
creator: mockUser,
|
|
|
|
|
});
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue({
|
|
|
|
|
...mockMembership,
|
2026-01-28 13:03:28 +08:00
|
|
|
role: "admin",
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
mockLedgerRepository.save.mockResolvedValue({
|
|
|
|
|
...mockLedger,
|
|
|
|
|
amount: 200,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.update("user-1", "ledger-1", {
|
2026-01-28 10:42:06 +08:00
|
|
|
amount: 200,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.amount).toBe(200);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在账目不存在时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne.mockResolvedValue(null);
|
|
|
|
|
|
|
|
|
|
await expect(
|
2026-01-28 13:03:28 +08:00
|
|
|
service.update("user-1", "ledger-1", { amount: 200 }),
|
2026-01-28 10:42:06 +08:00
|
|
|
).rejects.toThrow(NotFoundException);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在无权限时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne.mockResolvedValue(mockLedger);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue({
|
|
|
|
|
...mockMembership,
|
2026-01-28 13:03:28 +08:00
|
|
|
role: "member",
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(
|
2026-01-28 13:03:28 +08:00
|
|
|
service.update("user-2", "ledger-1", { amount: 200 }),
|
2026-01-28 10:42:06 +08:00
|
|
|
).rejects.toThrow(ForbiddenException);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("remove", () => {
|
|
|
|
|
it("应该成功删除账目", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne.mockResolvedValue(mockLedger);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue({
|
|
|
|
|
...mockMembership,
|
2026-01-28 13:03:28 +08:00
|
|
|
role: "admin",
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
mockLedgerRepository.remove.mockResolvedValue(mockLedger);
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.remove("user-1", "ledger-1");
|
2026-01-28 10:42:06 +08:00
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
expect(result).toHaveProperty("message");
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在无权限时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockLedgerRepository.findOne.mockResolvedValue(mockLedger);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue({
|
|
|
|
|
...mockMembership,
|
2026-01-28 13:03:28 +08:00
|
|
|
role: "member",
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
await expect(service.remove("user-2", "ledger-1")).rejects.toThrow(
|
|
|
|
|
ForbiddenException,
|
|
|
|
|
);
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("getMonthlyStatistics", () => {
|
|
|
|
|
it("应该成功获取月度统计", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
const mockQueryBuilder = {
|
|
|
|
|
where: jest.fn().mockReturnThis(),
|
|
|
|
|
andWhere: jest.fn().mockReturnThis(),
|
|
|
|
|
getMany: jest.fn().mockResolvedValue([
|
|
|
|
|
{ ...mockLedger, type: LedgerType.INCOME, amount: 100 },
|
|
|
|
|
{ ...mockLedger, type: LedgerType.EXPENSE, amount: 50 },
|
|
|
|
|
]),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockLedgerRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.getMonthlyStatistics("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
year: 2024,
|
|
|
|
|
month: 1,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
expect(result).toHaveProperty("income");
|
|
|
|
|
expect(result).toHaveProperty("expense");
|
|
|
|
|
expect(result).toHaveProperty("balance");
|
|
|
|
|
expect(result).toHaveProperty("categories");
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
it("应该在用户不在小组时抛出异常", async () => {
|
2026-01-28 10:42:06 +08:00
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(null);
|
|
|
|
|
|
|
|
|
|
await expect(
|
2026-01-28 13:03:28 +08:00
|
|
|
service.getMonthlyStatistics("user-1", {
|
|
|
|
|
groupId: "group-1",
|
2026-01-28 10:42:06 +08:00
|
|
|
year: 2024,
|
|
|
|
|
month: 1,
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrow(ForbiddenException);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
describe("getHierarchicalSummary", () => {
|
|
|
|
|
it("应该成功获取层级汇总", async () => {
|
|
|
|
|
const childGroup = { id: "group-2", name: "子小组", parentId: "group-1" };
|
2026-01-28 10:42:06 +08:00
|
|
|
const mockQueryBuilder = {
|
|
|
|
|
where: jest.fn().mockReturnThis(),
|
|
|
|
|
getMany: jest.fn().mockResolvedValue([mockLedger]),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
|
|
|
|
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
|
|
|
|
mockGroupRepository.find.mockResolvedValue([childGroup]);
|
|
|
|
|
mockLedgerRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
|
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
const result = await service.getHierarchicalSummary("user-1", "group-1");
|
2026-01-28 10:42:06 +08:00
|
|
|
|
2026-01-28 13:03:28 +08:00
|
|
|
expect(result).toHaveProperty("groupId");
|
|
|
|
|
expect(result).toHaveProperty("income");
|
|
|
|
|
expect(result).toHaveProperty("expense");
|
|
|
|
|
expect(result).toHaveProperty("balance");
|
2026-01-28 10:42:06 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|