import {z} from 'zod' export type TestStepInput = {description: string, credential_id?: (string | null) | undefined, kind?: ('setup' | 'act' | 'verify' | null) | undefined}; export type TestCaseVariableInput = {name: string, type: string, required?: boolean | undefined, default: unknown, description?: (string | null) | undefined}; export type TestCaseFileAttachmentInput = {file_id: string, description?: (string | null) | undefined}; export type CreateTestCaseRequest = {name: string, steps: Array, expected_result: string, priority?: (string | null) | undefined, description?: (string | null) | undefined, category_id?: (string | null) | undefined, change_source?: (string | null) | undefined, variables?: (Array | null) | undefined, file_attachments?: (Array | null) | undefined, file_ids?: (Array | null) | undefined}; export type CreateApiV1TestCasesArgs = {'x-api-key'?: string | undefined, body: CreateTestCaseRequest}; export type CreateApiV1TestCasesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const testCaseFileAttachmentOutput = z.object({file_id: z.string(), description: z.string().nullable().optional(), file_path: z.string().nullable().optional(), original_name: z.string().nullable().optional(), file_type: z.string().nullable().optional(), file_size: z.number().int().nullable().optional()}); export const variableWarning = z.object({code: z.string(), message: z.string()}); export const createTestCaseResponse = z.object({id: z.string(), name: z.string(), version_id: z.string(), files: z.array(testCaseFileAttachmentOutput).optional(), warnings: z.array(variableWarning).nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a test case * * POST /v1/test-cases */ export const createApiV1TestCases = async (args: CreateApiV1TestCasesArgs, options?: CreateApiV1TestCasesOptions) => { const {baseUrl = "https://api.qualgent.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/test-cases${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createTestCaseResponse, await res.json()) };