-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathjest.setup.ts
More file actions
145 lines (136 loc) · 6.43 KB
/
Copy pathjest.setup.ts
File metadata and controls
145 lines (136 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Test environment stub — sets all required env vars before any module loads.
// Values are non-functional placeholders sufficient for schema validation.
process.env.MODE ??= 'test';
process.env.DATABASE_URL ??= 'postgresql://test:test@localhost:5432/test';
process.env.GMAIL_USER ??= 'test@example.com';
process.env.GMAIL_APP_PASSWORD ??= 'test-password';
process.env.GOOGLE_CLIENT_ID ??= 'test-google-client-id';
process.env.GOOGLE_CLIENT_SECRET ??= 'test-google-client-secret';
process.env.BACKEND_URL ??= 'http://localhost:3000';
process.env.FRONTEND_URL ??= 'http://localhost:5173';
process.env.CLOUDINARY_CLOUD_NAME ??= 'test-cloud';
process.env.CLOUDINARY_API_KEY ??= 'test-api-key';
process.env.CLOUDINARY_API_SECRET ??= 'test-api-secret';
process.env.PAYSTACK_SECRET_KEY ??= 'test-paystack-secret';
process.env.APP_SECRET ??= 'accesslayer_test_secret_key_32_bytes_long_xxxx';
process.env.DB_QUERY_TIMEOUT_MS = '30000';
process.env.REDIS_URL = 'redis://localhost:6379';
process.env.JWT_SECRET = 'accesslayer_test_jwt_secret_key_32_bytes_long_xx';
process.env.JWT_EXPIRES_IN = '1h';
process.env.SSE_HEARTBEAT_INTERVAL_MS = '100';
process.env.SSE_QUEUE_CAPACITY = '10';
process.env.SSE_QUEUE_FULL_TIMEOUT_MS = '2000';
process.env.SSE_THROTTLE_DURATION_MS = '5000';
process.env.SSE_MAX_CONNECTIONS_PER_WALLET = '2';
process.env.SSE_MAX_SUBSCRIPTIONS_PER_WALLET = '5';
process.env.SSE_SUBSCRIPTION_TTL_MS = '86400000';
process.env.SSE_REPLAY_MAX_EVENTS = '1000';
process.env.SSE_PRUNE_INTERVAL_MS = '300000';
jest.mock('@prisma/client', () => {
const modelMocks: Record<string, any> = {};
let auditLogsStore: any[] = [];
const getModelMock = (modelName: string) => {
if (modelName === 'auditLog') {
if (!modelMocks.auditLog) {
modelMocks.auditLog = {
create: jest.fn().mockImplementation(async (args: any) => {
const entry = {
id: 'audit-' + Math.random().toString(36).substring(2, 9),
actorWallet: args?.data?.actorWallet || '',
actionType: args?.data?.actionType || '',
targetId: args?.data?.targetId ?? null,
payload: args?.data?.payload ?? null,
createdAt: args?.data?.createdAt ? new Date(args.data.createdAt) : new Date(),
};
auditLogsStore.push(entry);
return entry;
}),
findMany: jest.fn().mockImplementation(async (args?: any) => {
let results = [...auditLogsStore];
if (args?.where?.actionType) {
results = results.filter((r) => r.actionType === args.where.actionType);
}
if (args?.where?.createdAt) {
const { gte, lte } = args.where.createdAt;
if (gte) results = results.filter((r) => new Date(r.createdAt) >= new Date(gte));
if (lte) results = results.filter((r) => new Date(r.createdAt) <= new Date(lte));
}
results.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
if (args?.cursor?.id) {
const idx = results.findIndex((r) => r.id === args.cursor.id);
if (idx !== -1) {
const skip = args.skip ?? 0;
results = results.slice(idx + skip);
}
}
if (args?.take) {
results = results.slice(0, args.take);
}
return results;
}),
findFirst: jest.fn().mockImplementation(async (args?: any) => {
let results = [...auditLogsStore];
if (args?.where?.actionType) {
results = results.filter((r) => r.actionType === args.where.actionType);
}
results.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
return results[0] ?? null;
}),
deleteMany: jest.fn().mockImplementation(async (_args?: any) => {
const count = auditLogsStore.length;
auditLogsStore.length = 0;
return { count };
}),
count: jest.fn().mockImplementation(async () => auditLogsStore.length),
};
}
return modelMocks.auditLog;
}
if (!modelMocks[modelName]) {
modelMocks[modelName] = {
findMany: jest.fn().mockResolvedValue([]),
findFirst: jest.fn().mockResolvedValue(null),
findUnique: jest.fn().mockResolvedValue(null),
findUniqueOrThrow: jest.fn().mockResolvedValue({}),
create: jest.fn().mockImplementation(async (args: any) => ({ id: 'mock-id', ...args?.data })),
createMany: jest.fn().mockResolvedValue({ count: 0 }),
update: jest.fn().mockResolvedValue({}),
updateMany: jest.fn().mockResolvedValue({ count: 0 }),
delete: jest.fn().mockResolvedValue({}),
deleteMany: jest.fn().mockResolvedValue({ count: 0 }),
count: jest.fn().mockResolvedValue(0),
aggregate: jest.fn().mockResolvedValue({}),
groupBy: jest.fn().mockResolvedValue([]),
upsert: jest.fn().mockImplementation(async (args: any) => ({ id: 'mock-id', ...args?.create })),
};
}
return modelMocks[modelName];
};
const createProxyClient = (): any => {
const baseObj: any = {
$disconnect: jest.fn().mockResolvedValue(undefined),
$connect: jest.fn().mockResolvedValue(undefined),
$transaction: jest.fn().mockImplementation(async (cbOrArr: any) => {
if (typeof cbOrArr === 'function') {
return cbOrArr(mockPrismaClient);
}
return Promise.all(cbOrArr);
}),
$extends: jest.fn(() => mockPrismaClient),
};
return new Proxy(baseObj, {
get(target: any, prop: string) {
if (prop in target) return target[prop];
if (typeof prop === 'string' && !prop.startsWith('$')) {
return getModelMock(prop);
}
return undefined;
},
});
};
const mockPrismaClient = createProxyClient();
return {
PrismaClient: jest.fn(() => mockPrismaClient),
};
}, { virtual: true });
jest.setTimeout(30000);