Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/backend/src/bot/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,21 @@ describe('BotService', () => {
await expect(service.update(body)).rejects.toBeInstanceOf(BadRequestException);
});

it('should reject layout with an empty cluster', async () => {
const body: UpdateBotDto = {
layout: [[0, 1], [], [2]],
};

(prismaService.bot.findFirst as jest.Mock).mockResolvedValueOnce({
id: HALLMASTER_BOT_ID,
totalShards: 0,
clusters: [],
dockerImage: MOCK_DOCKER_IMAGE,
});

await expect(service.update(body)).rejects.toBeInstanceOf(BadRequestException);
});

it('should throw NotFoundException when updating non-existent bot', async () => {
const body: UpdateBotDto = {
layout: [
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export class BotService {
}

private validateLayout(layout: number[][]): void {
for (const [index, cluster] of layout.entries()) {
if (cluster.length === 0) {
throw new BadRequestException(`Cluster at index ${index} is empty.`);
}
}

const allShardIds = layout.flat();
const uniqueShardIds = new Set(allShardIds);
const totalShards = allShardIds.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/bot/dto/update-bot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from 'zod';
import { CreateBotDockerImageSchema, CreateBotSchema } from './create-bot.dto.js';

export const UpdateBotSchema = CreateBotSchema.extend({
layout: z.array(z.array(z.number().nonnegative())).min(1).meta({
layout: z.array(z.array(z.number().nonnegative()).min(1)).min(1).meta({
description:
'The cluster layout. Each element is an array of shard IDs assigned to that cluster. Example: [[0, 1, 2], [3]] creates 2 clusters with 4 total shards.',
}),
Expand Down
Loading