langfuse

Форк
0
/
ingestion-unit.servertest.ts 
106 строк · 4.5 Кб
1
/** @jest-environment node */
2

3
import { createMocks } from "node-mocks-http";
4
import handler from "@/src/pages/api/public/ingestion";
5
import { type NextApiResponse, type NextApiRequest } from "next";
6
import { Prisma } from "@prisma/client";
7

8
/*
9

10
ERROR	Error verifying auth header:  PrismaClientKnownRequestError: 
11
Invalid `prisma.apiKey.findUnique()` invocation:
12
Server has closed the connection.
13
    at ai.handleRequestError (/var/task/node_modules/@prisma/client/runtime/library.js:126:6775)
14
    at ai.handleAndLogRequestError (/var/task/node_modules/@prisma/client/runtime/library.js:126:6109)
15
    at ai.request (/var/task/node_modules/@prisma/client/runtime/library.js:126:5817)
16
    at async l (/var/task/node_modules/@prisma/client/runtime/library.js:131:9709)
17
    at async d (/var/task/.next/server/chunks/5811.js:1:9768)
18
    at async f (/var/task/.next/server/pages/api/public/generations.js:1:1026)
19
    at async /var/task/node_modules/@sentry/nextjs/cjs/common/wrapApiHandlerWithSentry.js:136:41
20
    at async K (/var/task/node_modules/next/dist/compiled/next-server/pages-api.runtime.prod.js:20:16545)
21
    at async U.render (/var/task/node_modules/next/dist/compiled/next-server/pages-api.runtime.prod.js:20:16981)
22
    at async r3.runApi (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:41752) {
23
  code: 'P1017',
24
  clientVersion: '5.9.1',
25
  meta: { modelName: 'ApiKey' }
26
*/
27
/*
28
ERROR	Error verifying auth header:  PrismaClientKnownRequestError: 
29
Invalid `prisma.apiKey.findUnique()` invocation:
30
Timed out fetching a new connection from the connection pool. More info:  (Current connection pool timeout: 10, connection limit: 1)
31
    at ai.handleRequestError (/var/task/node_modules/@prisma/client/runtime/library.js:126:6775)
32
    at ai.handleAndLogRequestError (/var/task/node_modules/@prisma/client/runtime/library.js:126:6109)
33
    at ai.request (/var/task/node_modules/@prisma/client/runtime/library.js:126:5817)
34
    at async l (/var/task/node_modules/@prisma/client/runtime/library.js:131:9709)
35
    at async l (/var/task/.next/server/chunks/5811.js:1:9768)
36
    at async y (/var/task/.next/server/pages/api/public/spans.js:1:1018)
37
    at async /var/task/node_modules/@sentry/nextjs/cjs/common/wrapApiHandlerWithSentry.js:136:41
38
    at async K (/var/task/node_modules/next/dist/compiled/next-server/pages-api.runtime.prod.js:20:16545)
39
    at async U.render (/var/task/node_modules/next/dist/compiled/next-server/pages-api.runtime.prod.js:20:16981)
40
    at async r3.runApi (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:41752) {
41
  code: 'P2024',
42
  clientVersion: '5.9.1',
43
  meta: { modelName: 'ApiKey', connection_limit: 1, timeout: 10 }
44
*/
45

46
jest.mock("../server/db.ts", () => {
47
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
48
  const originalModule = jest.requireActual("../server/db.ts");
49

50
  // Create a mock for PrismaClient
51
  const mockPrismaClient = {
52
    apiKey: {
53
      findMany: jest.fn(),
54
      findFirst: jest.fn(),
55
      findUnique: jest.fn(() => {
56
        throw new Prisma.PrismaClientKnownRequestError(
57
          "Timed out fetching a new connection from the connection pool. More info: (Current connection pool timeout: 10, connection limit: 1)",
58
          {
59
            code: "P2024",
60
            clientVersion: "5.9.1",
61
            meta: { modelName: "ApiKey", connection_limit: 1, timeout: 10 },
62
          },
63
        );
64
      }),
65
      findUniqueOrThrow: jest.fn(),
66
      findFirstOrThrow: jest.fn(),
67
      create: jest.fn(),
68
      createMany: jest.fn(),
69
      delete: jest.fn(),
70
      update: jest.fn(),
71
      updateMany: jest.fn(),
72
      upsert: jest.fn(),
73
      deleteMany: jest.fn(),
74
      count: jest.fn(),
75
      aggregate: jest.fn(),
76
      groupBy: jest.fn(),
77
    },
78
  };
79

80
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
81
  return {
82
    __esModule: true,
83
    ...originalModule,
84
    prisma: mockPrismaClient,
85
  };
86
});
87

88
describe("/api/public/ingestion API Endpoint", () => {
89
  it(`should return 500 for prisma exception`, async () => {
90
    const { req, res } = createMocks({
91
      method: "POST",
92
      headers: {
93
        authorization: "Bearer mock-token",
94
      },
95
      body: {},
96
    });
97

98
    // Extend the req object to include the missing env property
99
    const extendedReq = req as unknown as NextApiRequest;
100
    // Cast the res object to NextApiResponse to satisfy the type requirement
101
    const extendedRes = res as unknown as NextApiResponse;
102

103
    await handler(extendedReq, extendedRes);
104
    expect(res._getStatusCode()).toBe(500);
105
  });
106
});
107

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.