forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapiRequestLogger.test.ts
More file actions
190 lines (169 loc) · 7.04 KB
/
apiRequestLogger.test.ts
File metadata and controls
190 lines (169 loc) · 7.04 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import fs from 'fs';
import path from 'path';
import { apiRequestLogger } from './apiRequestLogger';
// The module writes to <cwd>/api-logs. We clean that directory before/after
// each test to keep tests isolated and avoid polluting the workspace.
const API_LOGS_DIR = path.join(process.cwd(), 'api-logs');
function getLogFiles(): string[] {
if (!fs.existsSync(API_LOGS_DIR)) return [];
return fs.readdirSync(API_LOGS_DIR);
}
function cleanApiLogs(): void {
if (!fs.existsSync(API_LOGS_DIR)) return;
for (const file of fs.readdirSync(API_LOGS_DIR)) {
fs.unlinkSync(path.join(API_LOGS_DIR, file));
}
}
// ---------------------------------------------------------------------------
// log() — write behaviour
// ---------------------------------------------------------------------------
describe('log', () => {
beforeEach(() => cleanApiLogs());
afterEach(() => cleanApiLogs());
test('writes a JSON file for a successful response', () => {
apiRequestLogger.log(
'user123',
'redeemcoupon',
{ url: 'https://example.com/api?hash=abc123&user_id=99990000', method: 'GET' },
{ status: 200, ok: true, body: { success: true } }
);
const files = getLogFiles();
expect(files).toHaveLength(1);
const content = JSON.parse(fs.readFileSync(path.join(API_LOGS_DIR, files[0]!), 'utf-8'));
expect(content.action).toBe('redeemcoupon');
expect(content.response.status).toBe(200);
expect(content.response.ok).toBe(true);
});
test('sanitizes hash and user_id from the URL', () => {
apiRequestLogger.log(
'user123',
'getuserdetails',
{ url: 'https://play.idlechampions.com/api?hash=supersecret&user_id=12345678&code=MYCODE', method: 'GET' },
{ status: 200, ok: true }
);
const files = getLogFiles();
const content = JSON.parse(fs.readFileSync(path.join(API_LOGS_DIR, files[0]!), 'utf-8'));
expect(content.request.url).not.toContain('supersecret');
expect(content.request.url).toContain('***');
expect(content.request.url).not.toContain('MYCODE');
});
test('uses "system" as user prefix when userId is undefined', () => {
apiRequestLogger.log(
undefined,
'ping',
{ url: 'https://example.com/ping', method: 'GET' },
{ status: 200, ok: true }
);
const files = getLogFiles();
expect(files[0]).toMatch(/^system_ping_/);
});
test('truncates userId to 8 chars in the filename', () => {
apiRequestLogger.log(
'longUserId12345',
'testaction',
{ url: 'https://example.com', method: 'GET' },
{ status: 500, ok: false, error: 'server error' }
);
const files = getLogFiles();
expect(files[0]).toMatch(/^longUser_testaction_/);
});
test('stores the error field when provided', () => {
apiRequestLogger.log(
'user123',
'failcall',
{ url: 'https://example.com/api', method: 'GET' },
{ status: 503, ok: false, error: 'Service unavailable' }
);
const files = getLogFiles();
const content = JSON.parse(fs.readFileSync(path.join(API_LOGS_DIR, files[0]!), 'utf-8'));
expect(content.response.error).toBe('Service unavailable');
expect(content.response.ok).toBe(false);
});
test('includes request body when provided', () => {
apiRequestLogger.log(
'user123',
'submitcode',
{ url: 'https://example.com/api', method: 'POST', body: { code: 'TESTCODE' } },
{ status: 200, ok: true }
);
const files = getLogFiles();
const content = JSON.parse(fs.readFileSync(path.join(API_LOGS_DIR, files[0]!), 'utf-8'));
expect(content.request.body).toEqual({ code: 'TESTCODE' });
});
test('sanitizes non-URL strings gracefully (returns raw string)', () => {
apiRequestLogger.log(
'user123',
'rawurl',
{ url: 'not-a-valid-url', method: 'GET' },
{ status: 200, ok: true }
);
const files = getLogFiles();
const content = JSON.parse(fs.readFileSync(path.join(API_LOGS_DIR, files[0]!), 'utf-8'));
expect(content.request.url).toBe('not-a-valid-url');
});
});
// ---------------------------------------------------------------------------
// getLogs() — directory listing
// ---------------------------------------------------------------------------
describe('getLogs', () => {
beforeEach(() => {
cleanApiLogs();
if (!fs.existsSync(API_LOGS_DIR)) fs.mkdirSync(API_LOGS_DIR, { recursive: true });
// Create two fake log files
fs.writeFileSync(path.join(API_LOGS_DIR, 'abcdef12_redeemcoupon_2025-01-01T00-00-00-000Z.json'), '{}');
fs.writeFileSync(path.join(API_LOGS_DIR, '99999999_getuserdetails_2025-01-02T00-00-00-000Z.json'), '{}');
});
afterEach(() => cleanApiLogs());
test('returns all log files sorted most-recent first (descending by filename)', () => {
const logs = apiRequestLogger.getLogs();
expect(logs).toHaveLength(2);
// localeCompare descending: 'abcdef12...' > '99999999...' (a > 9 in ASCII)
expect(logs[0]!.filename).toBe('abcdef12_redeemcoupon_2025-01-01T00-00-00-000Z.json');
expect(logs[1]!.filename).toBe('99999999_getuserdetails_2025-01-02T00-00-00-000Z.json');
});
test('filters by userId prefix (first 8 chars)', () => {
const logs = apiRequestLogger.getLogs('abcdef12extra');
expect(logs).toHaveLength(1);
expect(logs[0]!.filename).toMatch(/^abcdef12/);
});
test('returns empty array when directory does not exist', () => {
// Temporarily remove the dir
fs.rmSync(API_LOGS_DIR, { recursive: true, force: true });
const logs = apiRequestLogger.getLogs();
expect(logs).toEqual([]);
});
test('each entry has filename and path properties', () => {
const logs = apiRequestLogger.getLogs();
expect(logs[0]).toHaveProperty('filename');
expect(logs[0]).toHaveProperty('path');
});
});
// ---------------------------------------------------------------------------
// getLogContent() — file reading and path traversal guard
// ---------------------------------------------------------------------------
describe('getLogContent', () => {
beforeEach(() => {
cleanApiLogs();
if (!fs.existsSync(API_LOGS_DIR)) fs.mkdirSync(API_LOGS_DIR, { recursive: true });
});
afterEach(() => cleanApiLogs());
test('returns null when the file does not exist', () => {
expect(apiRequestLogger.getLogContent('nonexistent.json')).toBeNull();
});
test('returns parsed JSON content for a valid log file', () => {
const filename = 'user1234_testaction_2025-01-01T00-00-00-000Z.json';
const payload = { action: 'testaction', userId: 'user1234' };
fs.writeFileSync(path.join(API_LOGS_DIR, filename), JSON.stringify(payload));
const content = apiRequestLogger.getLogContent(filename);
expect(content).toEqual(payload);
});
});
// ---------------------------------------------------------------------------
// shutdown()
// ---------------------------------------------------------------------------
describe('shutdown', () => {
test('can be called without throwing even when not initialized', () => {
expect(() => apiRequestLogger.shutdown()).not.toThrow();
});
});