4.8.11 test (#2794)
* perf: version list type * perf: add node default value * perf: snapshot status * fix: version detail auth * fix: export defalt
This commit is contained in:
@@ -2,20 +2,32 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { NextAPI } from '@/service/middleware/entry';
|
||||
import { MongoAppVersion } from '@fastgpt/service/core/app/version/schema';
|
||||
import { authApp } from '@fastgpt/service/support/permission/app/auth';
|
||||
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
|
||||
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
|
||||
import { AppVersionSchemaType } from '@fastgpt/global/core/app/version';
|
||||
import { formatTime2YMDHM } from '@fastgpt/global/common/string/time';
|
||||
|
||||
type Props = {
|
||||
versionId: string;
|
||||
appId: string;
|
||||
};
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<any>
|
||||
): Promise<AppVersionSchemaType> {
|
||||
const { versionId, appId } = req.query as Props;
|
||||
|
||||
await authApp({ req, authToken: true, appId, per: ReadPermissionVal });
|
||||
const result = await MongoAppVersion.findById(versionId);
|
||||
await authApp({ req, authToken: true, appId, per: WritePermissionVal });
|
||||
const result = await MongoAppVersion.findById(versionId).lean();
|
||||
|
||||
return result;
|
||||
if (!result) {
|
||||
return Promise.reject('version not found');
|
||||
}
|
||||
|
||||
return {
|
||||
...result,
|
||||
versionName: result?.versionName || formatTime2YMDHM(result?.time)
|
||||
};
|
||||
}
|
||||
|
||||
export default NextAPI(handler);
|
||||
|
||||
69
projects/app/src/pages/api/core/app/version/lis.test.ts
Normal file
69
projects/app/src/pages/api/core/app/version/lis.test.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import '@/pages/api/__mocks__/base';
|
||||
import { root } from '@/pages/api/__mocks__/db/init';
|
||||
import { getTestRequest } from '@/test/utils';
|
||||
import handler, { versionListBody, versionListResponse } from './list';
|
||||
|
||||
// Import the schema
|
||||
import { MongoAppVersion } from '@fastgpt/service/core/app/version/schema';
|
||||
|
||||
const total = 22;
|
||||
|
||||
beforeAll(async () => {
|
||||
const arr = new Array(total).fill(0);
|
||||
await MongoAppVersion.insertMany(
|
||||
arr.map((_, index) => ({
|
||||
appId: root.appId,
|
||||
nodes: [],
|
||||
edges: [],
|
||||
chatConfig: {},
|
||||
isPublish: index % 2 === 0,
|
||||
versionName: `v` + index,
|
||||
tmbId: root.tmbId,
|
||||
time: new Date(index * 1000)
|
||||
}))
|
||||
);
|
||||
});
|
||||
|
||||
test('Get version list and check', async () => {
|
||||
const offset = 0;
|
||||
const pageSize = 10;
|
||||
|
||||
const _res = (await handler(
|
||||
...getTestRequest<{}, versionListBody>({
|
||||
body: {
|
||||
offset,
|
||||
pageSize,
|
||||
appId: root.appId
|
||||
},
|
||||
user: root
|
||||
})
|
||||
)) as any;
|
||||
const res = _res.data as versionListResponse;
|
||||
|
||||
expect(res.total).toBe(total);
|
||||
expect(res.list.length).toBe(pageSize);
|
||||
expect(res.list[0].versionName).toBe('v21');
|
||||
expect(res.list[9].versionName).toBe('v12');
|
||||
});
|
||||
|
||||
test('Get version list with offset 20', async () => {
|
||||
const offset = 20;
|
||||
const pageSize = 10;
|
||||
|
||||
const _res = (await handler(
|
||||
...getTestRequest<{}, versionListBody>({
|
||||
body: {
|
||||
offset,
|
||||
pageSize,
|
||||
appId: root.appId
|
||||
},
|
||||
user: root
|
||||
})
|
||||
)) as any;
|
||||
const res = _res.data as versionListResponse;
|
||||
|
||||
expect(res.total).toBe(total);
|
||||
expect(res.list.length).toBe(2);
|
||||
expect(res.list[0].versionName).toBe('v1');
|
||||
expect(res.list[1].versionName).toBe('v0');
|
||||
});
|
||||
@@ -1,27 +1,26 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import type { NextApiResponse } from 'next';
|
||||
import { NextAPI } from '@/service/middleware/entry';
|
||||
import { MongoAppVersion } from '@fastgpt/service/core/app/version/schema';
|
||||
import { PaginationProps, PaginationResponse } from '@fastgpt/web/common/fetch/type';
|
||||
import { ApiRequestProps } from '@fastgpt/service/type/next';
|
||||
import { authApp } from '@fastgpt/service/support/permission/app/auth';
|
||||
import { WritePermissionVal } from '@fastgpt/global/support/permission/constant';
|
||||
import { VersionListItemType } from '@fastgpt/global/core/app/version';
|
||||
|
||||
type Props = PaginationProps<{
|
||||
export type versionListBody = PaginationProps<{
|
||||
appId: string;
|
||||
}>;
|
||||
|
||||
export type versionListResponse = {
|
||||
_id: string;
|
||||
appId: string;
|
||||
versionName: string;
|
||||
time: Date;
|
||||
isPublish: boolean | undefined;
|
||||
tmbId: string;
|
||||
};
|
||||
export type versionListResponse = PaginationResponse<VersionListItemType>;
|
||||
|
||||
type Response = PaginationResponse<versionListResponse>;
|
||||
|
||||
async function handler(req: ApiRequestProps<Props>, res: NextApiResponse<any>): Promise<Response> {
|
||||
async function handler(
|
||||
req: ApiRequestProps<versionListBody>,
|
||||
res: NextApiResponse<any>
|
||||
): Promise<versionListResponse> {
|
||||
const { offset, pageSize, appId } = req.body;
|
||||
|
||||
await authApp({ appId, req, per: WritePermissionVal, authToken: true });
|
||||
|
||||
const [result, total] = await Promise.all([
|
||||
MongoAppVersion.find(
|
||||
{
|
||||
Reference in New Issue
Block a user