export interface RestResponse> { status: number; body: T & { error?: string; errors?: string[] | Record[]; message?: string; }; } export const assertRestApiSuccessfulResponse = >( response: RestResponse, expectedStatus = 200, ) => { if (response.status !== expectedStatus) { throw new Error( `Expected status ${expectedStatus}, received ${response.status}: ${JSON.stringify(response.body)}`, ); } expect(response.status).toBe(expectedStatus); expect(response.body).toBeDefined(); if (response.body.error || response.body.errors) { throw new Error( `Expected successful response but got errors: ${JSON.stringify(response.body)}`, ); } }; export const assertMetadataRestListResponse = ( response: RestResponse<{ data: T[]; pageInfo: { hasNextPage: boolean; hasPreviousPage: boolean; startCursor: string | null; endCursor: string | null; }; totalCount: number; }>, ): T[] => { assertRestApiSuccessfulResponse(response); expect(Array.isArray(response.body.data)).toBe(true); expect(response.body.pageInfo).toEqual({ hasNextPage: expect.any(Boolean), hasPreviousPage: expect.any(Boolean), startCursor: response.body.data.length > 0 ? expect.any(String) : null, endCursor: response.body.data.length > 0 ? expect.any(String) : null, }); expect(response.body.totalCount).toEqual(expect.any(Number)); return response.body.data; }; export const assertRestApiErrorResponse = >( response: RestResponse, expectedStatus = 400, expectedErrorMessage?: string, ) => { expect(response.status).toBe(expectedStatus); if (expectedErrorMessage && response.body.message) { expect(response.body.message).toContain(expectedErrorMessage); } }; export const assertRestApiErrorNotFoundResponse = ( response: RestResponse<{ statusCode: number; messages: [] }>, expectedStatus = 404, expectedErrorMessage?: string, ) => { expect(response.status).toBe(expectedStatus); expect(response.body.statusCode).toBe(expectedStatus); if (expectedErrorMessage && response.body.message) { expect(response.body.message).toContain(expectedErrorMessage); } };