Skip to content
Merged
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
84 changes: 84 additions & 0 deletions cueweb/app/__tests__/api/utils/limit_action_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
createLimit,
deleteLimit,
renameLimit,
setLimitMaxValue,
} from '@/app/utils/action_utils';
import { accessActionApi } from '@/app/utils/api_utils';

jest.mock('@/app/utils/api_utils', () => ({
accessActionApi: jest.fn(),
accessGetApi: jest.fn(),
}));

jest.mock('@/app/utils/notify_utils', () => ({
toastSuccess: jest.fn(),
toastWarning: jest.fn(),
handleError: jest.fn(),
}));

jest.mock('@/app/utils/get_utils', () => ({
getJobForLayer: jest.fn(),
getFrameLogDir: jest.fn(),
}));

describe('limit action_utils helpers', () => {
beforeEach(() => jest.clearAllMocks());

it('createLimit posts { name, max_value }', async () => {
(accessActionApi as jest.Mock).mockResolvedValue({ success: true });
await expect(createLimit('test1', 0)).resolves.toBe(true);
expect(accessActionApi).toHaveBeenCalledWith(
'/api/limit/action/create',
[JSON.stringify({ name: 'test1', max_value: 0 })],
);
});

it('deleteLimit posts { name }', async () => {
(accessActionApi as jest.Mock).mockResolvedValue({ success: true });
await deleteLimit('test1');
expect(accessActionApi).toHaveBeenCalledWith(
'/api/limit/action/delete',
[JSON.stringify({ name: 'test1' })],
);
});

it('renameLimit posts { old_name, new_name }', async () => {
(accessActionApi as jest.Mock).mockResolvedValue({ success: true });
await renameLimit('test1', 'test2');
expect(accessActionApi).toHaveBeenCalledWith(
'/api/limit/action/rename',
[JSON.stringify({ old_name: 'test1', new_name: 'test2' })],
);
});

it('setLimitMaxValue posts { name, max_value }', async () => {
(accessActionApi as jest.Mock).mockResolvedValue({ success: true });
await setLimitMaxValue('test1', 50);
expect(accessActionApi).toHaveBeenCalledWith(
'/api/limit/action/setmaxvalue',
[JSON.stringify({ name: 'test1', max_value: 50 })],
);
});

it('returns false when the action reports failure', async () => {
(accessActionApi as jest.Mock).mockResolvedValue({ error: 'boom' });
await expect(createLimit('test1', 0)).resolves.toBe(false);
});
});
39 changes: 39 additions & 0 deletions cueweb/app/__tests__/utils/limit_get_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { getLimits } from '@/app/utils/get_utils';
import { accessGetApi } from '@/app/utils/api_utils';

jest.mock('@/app/utils/api_utils', () => ({
accessGetApi: jest.fn(),
}));

describe('getLimits', () => {
beforeEach(() => jest.clearAllMocks());

it('posts to /api/limit/getall and returns the array', async () => {
const limits = [{ id: 'l1', name: 'asdf', maxValue: 0, currentRunning: 0 }];
(accessGetApi as jest.Mock).mockResolvedValue(limits);

await expect(getLimits()).resolves.toEqual(limits);
expect(accessGetApi).toHaveBeenCalledWith('/api/limit/getall', JSON.stringify({}));
});

it('returns [] when the gateway returns a non-array', async () => {
(accessGetApi as jest.Mock).mockResolvedValue(null);
await expect(getLimits()).resolves.toEqual([]);
});
});
50 changes: 50 additions & 0 deletions cueweb/app/api/limit/action/create/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { handleRoute } from '@/app/utils/api_utils';
import { NextRequest, NextResponse } from "next/server";

// Create a limit. Request: { name, max_value }. RPC: /limit.LimitInterface/Create.
export async function POST(request: NextRequest) {
const endpoint = "/limit.LimitInterface/Create";
const method = request.method;
if (method !== 'POST') {
return NextResponse.json({ error: 'Invalid method. Only POST is allowed.' }, { status: 405 });
}

let jsonBody: any;
try {
jsonBody = await request.json();
} catch {
return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 });
}
if (
!jsonBody ||
typeof jsonBody !== 'object' ||
typeof jsonBody.name !== 'string' ||
jsonBody.name.trim().length === 0 ||
typeof jsonBody.max_value !== 'number'
) {
return NextResponse.json({ error: 'Invalid request body: name and max_value are required' }, { status: 400 });
}
Comment thread
ramonfigueiredo marked this conversation as resolved.

const body = JSON.stringify({ name: jsonBody.name.trim(), max_value: jsonBody.max_value });
const response = await handleRoute(method, endpoint, body, true);
const responseData = await response.json();

if (!response.ok) return NextResponse.json({ error: responseData.error }, { status: response.status });
return NextResponse.json({ data: responseData.data }, { status: response.status });
}
44 changes: 44 additions & 0 deletions cueweb/app/api/limit/action/delete/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { handleRoute } from '@/app/utils/api_utils';
import { NextRequest, NextResponse } from "next/server";

// Delete a limit by name. Request: { name }. RPC: /limit.LimitInterface/Delete.
export async function POST(request: NextRequest) {
const endpoint = "/limit.LimitInterface/Delete";
const method = request.method;
if (method !== 'POST') {
return NextResponse.json({ error: 'Invalid method. Only POST is allowed.' }, { status: 405 });
}

let jsonBody: any;
try {
jsonBody = await request.json();
} catch {
return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 });
}
if (!jsonBody || typeof jsonBody !== 'object' || typeof jsonBody.name !== 'string' || jsonBody.name.length === 0) {
return NextResponse.json({ error: 'Invalid request body: name is required' }, { status: 400 });
}

const body = JSON.stringify({ name: jsonBody.name });
Comment thread
ramonfigueiredo marked this conversation as resolved.
const response = await handleRoute(method, endpoint, body, true);
const responseData = await response.json();

if (!response.ok) return NextResponse.json({ error: responseData.error }, { status: response.status });
return NextResponse.json({ data: responseData.data }, { status: response.status });
}
51 changes: 51 additions & 0 deletions cueweb/app/api/limit/action/rename/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { handleRoute } from '@/app/utils/api_utils';
import { NextRequest, NextResponse } from "next/server";

// Rename a limit. Request: { old_name, new_name }. RPC: /limit.LimitInterface/Rename.
export async function POST(request: NextRequest) {
const endpoint = "/limit.LimitInterface/Rename";
const method = request.method;
if (method !== 'POST') {
return NextResponse.json({ error: 'Invalid method. Only POST is allowed.' }, { status: 405 });
}

let jsonBody: any;
try {
jsonBody = await request.json();
} catch {
return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 });
}
if (
!jsonBody ||
typeof jsonBody !== 'object' ||
typeof jsonBody.old_name !== 'string' ||
jsonBody.old_name.length === 0 ||
typeof jsonBody.new_name !== 'string' ||
jsonBody.new_name.trim().length === 0
) {
return NextResponse.json({ error: 'Invalid request body: old_name and new_name are required' }, { status: 400 });
}

const body = JSON.stringify({ old_name: jsonBody.old_name, new_name: jsonBody.new_name.trim() });
Comment thread
ramonfigueiredo marked this conversation as resolved.
const response = await handleRoute(method, endpoint, body, true);
const responseData = await response.json();

if (!response.ok) return NextResponse.json({ error: responseData.error }, { status: response.status });
return NextResponse.json({ data: responseData.data }, { status: response.status });
}
52 changes: 52 additions & 0 deletions cueweb/app/api/limit/action/setmaxvalue/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { handleRoute } from '@/app/utils/api_utils';
import { NextRequest, NextResponse } from "next/server";

// Set a limit's max value. Request: { name, max_value }. The value must be a
// non-negative integer. RPC: /limit.LimitInterface/SetMaxValue.
export async function POST(request: NextRequest) {
const endpoint = "/limit.LimitInterface/SetMaxValue";
const method = request.method;
if (method !== 'POST') {
return NextResponse.json({ error: 'Invalid method. Only POST is allowed.' }, { status: 405 });
}

let jsonBody: any;
try {
jsonBody = await request.json();
} catch {
return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 });
}
if (
!jsonBody ||
typeof jsonBody !== 'object' ||
typeof jsonBody.name !== 'string' ||
jsonBody.name.length === 0 ||
typeof jsonBody.max_value !== 'number' ||
jsonBody.max_value < 0
) {
return NextResponse.json({ error: 'Invalid request body: name and a non-negative max_value are required' }, { status: 400 });
}
Comment thread
ramonfigueiredo marked this conversation as resolved.

const body = JSON.stringify({ name: jsonBody.name, max_value: jsonBody.max_value });
const response = await handleRoute(method, endpoint, body, true);
const responseData = await response.json();

if (!response.ok) return NextResponse.json({ error: responseData.error }, { status: response.status });
return NextResponse.json({ data: responseData.data }, { status: response.status });
}
50 changes: 50 additions & 0 deletions cueweb/app/api/limit/getall/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { handleRoute } from '@/app/utils/api_utils';
import { NextRequest, NextResponse } from "next/server";

// Lists every limit for the Limits page. Unlike host/show GetAll, the gateway
// nests the result a single level as { limits: [...] } (LimitGetAllResponse has
// a `repeated Limit limits`, no inner *Seq), so we unwrap one level.
// RPC: /limit.LimitInterface/GetAll.
export async function POST(request: NextRequest) {
const endpoint = "/limit.LimitInterface/GetAll";
const method = request.method;
if (method !== 'POST') {
return NextResponse.json({ error: 'Invalid method. Only POST is allowed.' }, { status: 405 });
}

let parsed: unknown = {};
try {
parsed = await request.json();
} catch {
// Empty body is acceptable - GetAll takes no parameters.
}
const body = JSON.stringify(parsed ?? {});

const response = await handleRoute(method, endpoint, body);
const responseData = await response.json();

if (!response.ok) {
return NextResponse.json(
{ error: responseData?.error ?? "Failed to fetch limits" },
{ status: response.status },
);
}
const limits = responseData?.data?.limits ?? [];
return NextResponse.json({ data: limits }, { status: response.status });
}
Loading
Loading