feat: add POST /accounts endpoint to create new WhatsApp account records
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AccountsService } from './accounts.service';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import * as QRCode from 'qrcode';
|
||||
@@ -11,13 +12,28 @@ const mockAccounts = [
|
||||
{ id: 'acc_1', platform: 'whatsapp', jid: '111@s.whatsapp.net', displayName: 'Test Account', status: 'ACTIVE' },
|
||||
];
|
||||
|
||||
const mockCreatedAccount = {
|
||||
id: 'acc_new',
|
||||
platform: 'whatsapp',
|
||||
jid: 'pending_uuid@placeholder',
|
||||
displayName: 'My Number',
|
||||
status: 'ACTIVE',
|
||||
};
|
||||
|
||||
const mockPrisma = {
|
||||
account: {
|
||||
findMany: jest.fn().mockResolvedValue(mockAccounts),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn().mockResolvedValue(mockCreatedAccount),
|
||||
},
|
||||
};
|
||||
|
||||
const mockConfig = {
|
||||
get: jest.fn().mockImplementation((key: string, def: string) =>
|
||||
key === 'WHATSAPP_SESSION_PATH' ? './sessions' : def,
|
||||
),
|
||||
};
|
||||
|
||||
describe('AccountsService', () => {
|
||||
let service: AccountsService;
|
||||
|
||||
@@ -27,6 +43,7 @@ describe('AccountsService', () => {
|
||||
providers: [
|
||||
AccountsService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
{ provide: ConfigService, useValue: mockConfig },
|
||||
],
|
||||
}).compile();
|
||||
service = module.get<AccountsService>(AccountsService);
|
||||
@@ -63,4 +80,42 @@ describe('AccountsService', () => {
|
||||
expect(result).toEqual({ status: 'not_found', qrDataUrl: null });
|
||||
});
|
||||
});
|
||||
|
||||
describe('create()', () => {
|
||||
it('creates account with platform whatsapp and status ACTIVE', async () => {
|
||||
await service.create('My Number');
|
||||
expect(mockPrisma.account.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
platform: 'whatsapp',
|
||||
status: 'ACTIVE',
|
||||
displayName: 'My Number',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('generates a unique sessionPath under WHATSAPP_SESSION_PATH', async () => {
|
||||
await service.create();
|
||||
const call = mockPrisma.account.create.mock.calls[0][0];
|
||||
expect(call.data.sessionPath).toMatch(/^\.\/sessions\/.+/);
|
||||
});
|
||||
|
||||
it('generates a placeholder jid prefixed with pending_', async () => {
|
||||
await service.create();
|
||||
const call = mockPrisma.account.create.mock.calls[0][0];
|
||||
expect(call.data.jid).toMatch(/^pending_/);
|
||||
});
|
||||
|
||||
it('sets displayName to null when not provided', async () => {
|
||||
await service.create();
|
||||
const call = mockPrisma.account.create.mock.calls[0][0];
|
||||
expect(call.data.displayName).toBeNull();
|
||||
});
|
||||
|
||||
it('returns the created account summary', async () => {
|
||||
const result = await service.create('My Number');
|
||||
expect(result).toEqual(mockCreatedAccount);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user