@c15t/node-sdk 1.2.0-canary.13 → 1.2.0-canary.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.test.ts DELETED
@@ -1,218 +0,0 @@
1
- import { createServer } from 'node:http';
2
- import { c15tInstance } from '@c15t/backend';
3
- import type { C15TOptions } from '@c15t/backend';
4
- import {
5
- afterAll,
6
- beforeAll,
7
- beforeEach,
8
- describe,
9
- expect,
10
- it,
11
- vi,
12
- } from 'vitest';
13
- import { c15tClient } from './index';
14
-
15
- // Server configuration for integration tests
16
- const mockOptions: C15TOptions = {
17
- appName: 'C15T Test Server',
18
- basePath: '/',
19
- trustedOrigins: ['localhost', 'test.example.com'],
20
- cors: true,
21
- advanced: {
22
- cors: {
23
- allowHeaders: ['content-type', 'x-request-id'],
24
- },
25
- },
26
- // logger: {
27
- // level: 'debug',
28
- // },
29
- };
30
-
31
- describe('C15T Node SDK', () => {
32
- const PORT = 8787;
33
- const mockBaseUrl = `http://localhost:${PORT}`;
34
- let server: ReturnType<typeof c15tInstance>;
35
- let httpServer: ReturnType<typeof createServer>;
36
- let client: ReturnType<typeof c15tClient>;
37
-
38
- beforeAll(async () => {
39
- // Initialize the server for integration tests
40
- server = c15tInstance(mockOptions);
41
-
42
- // Create and start HTTP server
43
- httpServer = createServer(async (req, res) => {
44
- try {
45
- // Read request body if present
46
- let body: string | undefined;
47
- if (req.method !== 'GET' && req.method !== 'HEAD') {
48
- const chunks: Uint8Array[] = [];
49
- for await (const chunk of req) {
50
- chunks.push(chunk);
51
- }
52
- body = Buffer.concat(chunks).toString();
53
- }
54
-
55
- // Convert Node.js request to web standard Request
56
- const request = new Request(`http://localhost:${PORT}${req.url}`, {
57
- method: req.method,
58
- headers: req.headers as Record<string, string>,
59
- body: body,
60
- duplex: 'half',
61
- });
62
-
63
- // Handle the request with c15tInstance
64
- const response = await server.handler(request);
65
-
66
- // Set response status and headers
67
- res.statusCode = response.status;
68
- for (const [key, value] of response.headers.entries()) {
69
- res.setHeader(key, value);
70
- }
71
-
72
- // Ensure content-type is set for JSON responses
73
- if (!response.headers.get('content-type')) {
74
- res.setHeader('content-type', 'application/json');
75
- }
76
-
77
- // Get response body
78
- const responseBody = await response.text();
79
-
80
- // Send response
81
- res.end(responseBody);
82
- } catch (error) {
83
- // biome-ignore lint/suspicious/noConsoleLog: its a test
84
- // biome-ignore lint/suspicious/noConsole: its a test
85
- console.error('Server error:', error);
86
- res.statusCode = 500;
87
- res.setHeader('content-type', 'application/json');
88
- res.end(
89
- JSON.stringify({
90
- error: 'Internal Server Error',
91
- message: error instanceof Error ? error.message : String(error),
92
- })
93
- );
94
- }
95
- });
96
-
97
- await new Promise<void>((resolve) => {
98
- httpServer.listen(PORT, () => {
99
- // biome-ignore lint/suspicious/noConsoleLog: its a test
100
- // biome-ignore lint/suspicious/noConsole: its a test
101
- console.log(`Test server listening on port ${PORT}`);
102
- resolve();
103
- });
104
- });
105
-
106
- // Initialize the client for testing
107
- client = c15tClient({
108
- baseUrl: mockBaseUrl,
109
- prefix: '/',
110
- });
111
- });
112
-
113
- afterAll(async () => {
114
- // Clean up server
115
- await new Promise<void>((resolve) => {
116
- httpServer.close(() => {
117
- // biome-ignore lint/suspicious/noConsoleLog: its a test
118
- // biome-ignore lint/suspicious/noConsole: its a test
119
- console.log('Test server closed');
120
- resolve();
121
- });
122
- });
123
- });
124
-
125
- beforeEach(() => {
126
- vi.clearAllMocks();
127
- });
128
-
129
- describe('Unit Tests', () => {
130
- describe('Client Creation', () => {
131
- it('should create a client with basic configuration', () => {
132
- const client = c15tClient({ baseUrl: mockBaseUrl });
133
-
134
- expect(client).toBeDefined();
135
- expect(client.consent).toBeDefined();
136
- expect(client.meta).toBeDefined();
137
- });
138
-
139
- it('should include authorization header when token is provided', () => {
140
- const client = c15tClient({
141
- baseUrl: mockBaseUrl,
142
- token: 'test-token',
143
- });
144
-
145
- expect(client).toBeDefined();
146
- });
147
-
148
- it('should include custom headers when provided', () => {
149
- const client = c15tClient({
150
- baseUrl: mockBaseUrl,
151
- headers: { 'X-Custom-Header': 'test-value' },
152
- });
153
-
154
- expect(client).toBeDefined();
155
- });
156
-
157
- it('should apply prefix to base URL when provided', () => {
158
- const prefix = '/api/v1';
159
- const client = c15tClient({
160
- baseUrl: mockBaseUrl,
161
- prefix,
162
- });
163
-
164
- expect(client).toBeDefined();
165
- });
166
-
167
- it('should handle URL with existing path', () => {
168
- const baseUrl = 'http://localhost:8787/existing';
169
- const prefix = '/api/v1';
170
- const client = c15tClient({
171
- baseUrl,
172
- prefix,
173
- });
174
-
175
- expect(client).toBeDefined();
176
- });
177
- });
178
-
179
- describe('Error Handling', () => {
180
- it('should handle invalid base URL', () => {
181
- expect(() => {
182
- c15tClient({ baseUrl: 'invalid-url' });
183
- }).toThrow();
184
- });
185
-
186
- it('should handle empty base URL', () => {
187
- expect(() => {
188
- c15tClient({ baseUrl: '' });
189
- }).toThrow();
190
- });
191
- });
192
- });
193
-
194
- describe('Integration Tests', () => {
195
- it('should connect to status endpoint', async () => {
196
- const response = await client.meta.status();
197
-
198
- // Test exact structure based on actual response
199
- expect(response).toEqual({
200
- status: 'ok',
201
- version: expect.any(String),
202
- timestamp: expect.any(String),
203
- storage: {
204
- type: 'memory',
205
- available: true,
206
- },
207
- client: {
208
- ip: expect.any(String),
209
- userAgent: expect.any(String),
210
- region: {
211
- countryCode: null,
212
- regionCode: null,
213
- },
214
- },
215
- });
216
- });
217
- });
218
- });