good forst commit
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { handleStarReaction } from './approval';
|
||||
import { handleReaction } from './approval';
|
||||
import { approveMessage } from './approve-message';
|
||||
import { NormalizedReaction } from '@tower/types';
|
||||
|
||||
jest.mock('./approve-message');
|
||||
|
||||
function makeReaction(overrides: Partial<NormalizedReaction> = {}): NormalizedReaction {
|
||||
return {
|
||||
reactorJid: '919876543210@s.whatsapp.net',
|
||||
@@ -12,131 +15,88 @@ function makeReaction(overrides: Partial<NormalizedReaction> = {}): NormalizedRe
|
||||
};
|
||||
}
|
||||
|
||||
const adminJids = ['919876543210@s.whatsapp.net'];
|
||||
|
||||
function makeMessage(overrides: object = {}) {
|
||||
return {
|
||||
id: 'msg_1',
|
||||
tenantId: 'tnt_1',
|
||||
status: 'PENDING',
|
||||
approval: null,
|
||||
content: 'hello world',
|
||||
senderName: 'Alice',
|
||||
sourceGroupId: 'grp_1',
|
||||
tags: ['#important'],
|
||||
platform: 'whatsapp',
|
||||
sourceGroup: { name: 'UP Parivar Dallas', syncRoutesFrom: [] },
|
||||
sourceGroup: { platformId: '120363043312345678@g.us' },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makePrisma(messageOverrides: object = {}, txCount = 1) {
|
||||
function makePool(isAdmin: boolean) {
|
||||
return {
|
||||
message: { findUnique: jest.fn().mockResolvedValue(makeMessage(messageOverrides)) },
|
||||
$transaction: jest.fn().mockImplementation(async (fn: any) =>
|
||||
fn({
|
||||
message: { updateMany: jest.fn().mockResolvedValue({ count: txCount }) },
|
||||
approval: { create: jest.fn().mockResolvedValue({}) },
|
||||
get: jest.fn().mockReturnValue({
|
||||
groupMetadata: jest.fn().mockResolvedValue({
|
||||
participants: [
|
||||
{ id: '919876543210@s.whatsapp.net', admin: isAdmin ? 'admin' : undefined },
|
||||
{ id: 'other@s.whatsapp.net', admin: 'superadmin' },
|
||||
],
|
||||
}),
|
||||
),
|
||||
} as any;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
describe('handleStarReaction', () => {
|
||||
it('returns null for non-star emoji', async () => {
|
||||
expect(await handleStarReaction(makeReaction({ emoji: '👍' }), adminJids, {} as any)).toBeNull();
|
||||
});
|
||||
const mockPrisma = {
|
||||
message: { findUnique: jest.fn() },
|
||||
tenantRule: { findMany: jest.fn() },
|
||||
};
|
||||
|
||||
it('returns null when reactor is not an admin', async () => {
|
||||
expect(
|
||||
await handleStarReaction(makeReaction({ reactorJid: 'stranger@s.whatsapp.net' }), adminJids, {} as any),
|
||||
).toBeNull();
|
||||
const mockForwardQueue = { add: jest.fn() };
|
||||
const mockIndexQueue = { add: jest.fn() };
|
||||
|
||||
describe('handleReaction', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
mockPrisma.message.findUnique.mockResolvedValue(makeMessage());
|
||||
(approveMessage as jest.Mock).mockResolvedValue({ forwardJobs: [], indexDoc: {} });
|
||||
});
|
||||
|
||||
it('returns null when message not found', async () => {
|
||||
const prisma = { message: { findUnique: jest.fn().mockResolvedValue(null) } } as any;
|
||||
expect(await handleStarReaction(makeReaction(), adminJids, prisma)).toBeNull();
|
||||
expect(prisma.message.findUnique).toHaveBeenCalledWith({
|
||||
where: { platform_platformMsgId: { platform: 'whatsapp', platformMsgId: 'TARGET_MSG_123' } },
|
||||
include: {
|
||||
approval: true,
|
||||
sourceGroup: {
|
||||
include: { syncRoutesFrom: { where: { isActive: true }, include: { targetGroup: true } } },
|
||||
},
|
||||
},
|
||||
});
|
||||
mockPrisma.message.findUnique.mockResolvedValue(null);
|
||||
expect(await handleReaction(makeReaction(), mockPrisma, makePool(true))).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when message status is not PENDING', async () => {
|
||||
const prisma = {
|
||||
message: { findUnique: jest.fn().mockResolvedValue(makeMessage({ status: 'REJECTED' })) },
|
||||
} as any;
|
||||
expect(await handleStarReaction(makeReaction(), adminJids, prisma)).toBeNull();
|
||||
it('returns null when message is not PENDING', async () => {
|
||||
mockPrisma.message.findUnique.mockResolvedValue(makeMessage({ status: 'APPROVED' }));
|
||||
expect(await handleReaction(makeReaction(), mockPrisma, makePool(true))).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when approval record already exists', async () => {
|
||||
const prisma = {
|
||||
message: {
|
||||
findUnique: jest.fn().mockResolvedValue(makeMessage({ status: 'APPROVED', approval: { id: 'appr_1' } })),
|
||||
},
|
||||
} as any;
|
||||
expect(await handleStarReaction(makeReaction(), adminJids, prisma)).toBeNull();
|
||||
it('returns null when no reaction rule matches', async () => {
|
||||
mockPrisma.tenantRule.findMany.mockResolvedValue([]);
|
||||
expect(await handleReaction(makeReaction(), mockPrisma, makePool(true))).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null on double-approval race (updateMany count=0)', async () => {
|
||||
const result = await handleStarReaction(makeReaction(), adminJids, makePrisma({}, 0));
|
||||
expect(result).toBeNull();
|
||||
it('returns null when reactor is not a group admin', async () => {
|
||||
mockPrisma.tenantRule.findMany.mockResolvedValue([
|
||||
{ id: 'r1', matchType: 'REACTION_EMOJI', matchValue: '⭐', action: 'AUTO_APPROVE', priority: 0 },
|
||||
]);
|
||||
expect(await handleReaction(makeReaction(), mockPrisma, makePool(false))).toBeNull();
|
||||
});
|
||||
|
||||
it('returns ApprovalResult with empty forwardJobs and valid indexDoc when no sync routes', async () => {
|
||||
const result = await handleStarReaction(makeReaction(), adminJids, makePrisma());
|
||||
it('approves when reactor is admin and reaction rule matches', async () => {
|
||||
mockPrisma.tenantRule.findMany.mockResolvedValue([
|
||||
{ id: 'r1', matchType: 'REACTION_EMOJI', matchValue: '⭐', action: 'AUTO_APPROVE', priority: 0 },
|
||||
]);
|
||||
const result = await handleReaction(makeReaction(), mockPrisma, makePool(true));
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.forwardJobs).toEqual([]);
|
||||
expect(result!.indexDoc).toMatchObject({
|
||||
messageId: 'msg_1',
|
||||
content: 'hello world',
|
||||
senderName: 'Alice',
|
||||
sourceGroupId: 'grp_1',
|
||||
sourceGroupName: 'UP Parivar Dallas',
|
||||
tags: ['#important'],
|
||||
platform: 'whatsapp',
|
||||
});
|
||||
expect(result!.indexDoc.approvedAt).toMatch(/^\d{4}-\d{2}-\d{2}T/);
|
||||
expect(approveMessage).toHaveBeenCalledWith('msg_1', 'tnt_1', '919876543210@s.whatsapp.net', mockPrisma);
|
||||
});
|
||||
|
||||
it('returns ForwardJobData for each active sync route', async () => {
|
||||
const prisma = makePrisma({
|
||||
content: 'important announcement',
|
||||
senderName: 'Bob',
|
||||
sourceGroup: {
|
||||
name: 'Source Group',
|
||||
syncRoutesFrom: [
|
||||
{ targetGroup: { platformId: '999@g.us', accountId: 'acc_2' } },
|
||||
{ targetGroup: { platformId: '888@g.us', accountId: null } },
|
||||
],
|
||||
},
|
||||
});
|
||||
it('returns null when socket not available', async () => {
|
||||
mockPrisma.tenantRule.findMany.mockResolvedValue([
|
||||
{ id: 'r1', matchType: 'REACTION_EMOJI', matchValue: '⭐', action: 'AUTO_APPROVE', priority: 0 },
|
||||
]);
|
||||
const pool = { get: jest.fn().mockReturnValue(undefined) };
|
||||
expect(await handleReaction(makeReaction(), mockPrisma, pool)).toBeNull();
|
||||
});
|
||||
|
||||
const result = await handleStarReaction(makeReaction(), adminJids, prisma);
|
||||
expect(result!.forwardJobs).toHaveLength(2);
|
||||
expect(result!.forwardJobs[0]).toMatchObject({
|
||||
messageId: 'msg_1',
|
||||
content: 'important announcement',
|
||||
sourceGroupName: 'Source Group',
|
||||
senderName: 'Bob',
|
||||
toGroupJid: '999@g.us',
|
||||
fromAccountId: 'acc_2',
|
||||
});
|
||||
expect(result!.forwardJobs[1]).toMatchObject({
|
||||
toGroupJid: '888@g.us',
|
||||
fromAccountId: 'acc_1',
|
||||
});
|
||||
expect(result!.indexDoc).toMatchObject({
|
||||
messageId: 'msg_1',
|
||||
content: 'important announcement',
|
||||
senderName: 'Bob',
|
||||
sourceGroupId: 'grp_1',
|
||||
tags: ['#important'],
|
||||
platform: 'whatsapp',
|
||||
});
|
||||
it('returns null when approveMessage returns null', async () => {
|
||||
mockPrisma.tenantRule.findMany.mockResolvedValue([
|
||||
{ id: 'r1', matchType: 'REACTION_EMOJI', matchValue: '⭐', action: 'AUTO_APPROVE', priority: 0 },
|
||||
]);
|
||||
(approveMessage as jest.Mock).mockResolvedValue(null);
|
||||
expect(await handleReaction(makeReaction(), mockPrisma, makePool(true))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user