@mandujs/core 0.13.0 → 0.13.2
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/README.ko.md +4 -4
- package/README.md +653 -653
- package/package.json +1 -1
- package/src/bundler/build.ts +91 -91
- package/src/bundler/css.ts +302 -302
- package/src/client/Link.tsx +227 -227
- package/src/client/globals.ts +44 -44
- package/src/client/hooks.ts +267 -267
- package/src/client/index.ts +5 -5
- package/src/client/island.ts +8 -8
- package/src/client/router.ts +435 -435
- package/src/client/runtime.ts +23 -23
- package/src/client/serialize.ts +404 -404
- package/src/client/window-state.ts +101 -101
- package/src/config/mandu.ts +9 -0
- package/src/config/validate.ts +12 -0
- package/src/config/watcher.ts +311 -311
- package/src/constants.ts +40 -40
- package/src/content/content-layer.ts +314 -314
- package/src/content/content.test.ts +433 -433
- package/src/content/data-store.ts +245 -245
- package/src/content/digest.ts +133 -133
- package/src/content/index.ts +164 -164
- package/src/content/loader-context.ts +172 -172
- package/src/content/loaders/api.ts +216 -216
- package/src/content/loaders/file.ts +169 -169
- package/src/content/loaders/glob.ts +252 -252
- package/src/content/loaders/index.ts +34 -34
- package/src/content/loaders/types.ts +137 -137
- package/src/content/meta-store.ts +209 -209
- package/src/content/types.ts +282 -282
- package/src/content/watcher.ts +135 -135
- package/src/contract/client-safe.test.ts +42 -42
- package/src/contract/client-safe.ts +114 -114
- package/src/contract/client.ts +16 -16
- package/src/contract/define.ts +459 -459
- package/src/contract/handler.ts +10 -10
- package/src/contract/normalize.test.ts +276 -276
- package/src/contract/normalize.ts +404 -404
- package/src/contract/registry.test.ts +206 -206
- package/src/contract/registry.ts +568 -568
- package/src/contract/schema.ts +48 -48
- package/src/contract/types.ts +58 -58
- package/src/contract/validator.ts +32 -32
- package/src/devtools/ai/context-builder.ts +375 -375
- package/src/devtools/ai/index.ts +25 -25
- package/src/devtools/ai/mcp-connector.ts +465 -465
- package/src/devtools/client/catchers/error-catcher.ts +327 -327
- package/src/devtools/client/catchers/index.ts +18 -18
- package/src/devtools/client/catchers/network-proxy.ts +363 -363
- package/src/devtools/client/components/index.ts +39 -39
- package/src/devtools/client/components/kitchen-root.tsx +362 -362
- package/src/devtools/client/components/mandu-character.tsx +241 -241
- package/src/devtools/client/components/overlay.tsx +368 -368
- package/src/devtools/client/components/panel/errors-panel.tsx +259 -259
- package/src/devtools/client/components/panel/guard-panel.tsx +244 -244
- package/src/devtools/client/components/panel/index.ts +32 -32
- package/src/devtools/client/components/panel/islands-panel.tsx +304 -304
- package/src/devtools/client/components/panel/network-panel.tsx +292 -292
- package/src/devtools/client/components/panel/panel-container.tsx +259 -259
- package/src/devtools/client/filters/context-filters.ts +282 -282
- package/src/devtools/client/filters/index.ts +16 -16
- package/src/devtools/client/index.ts +63 -63
- package/src/devtools/client/persistence.ts +335 -335
- package/src/devtools/client/state-manager.ts +478 -478
- package/src/devtools/design-tokens.ts +263 -263
- package/src/devtools/hook/create-hook.ts +207 -207
- package/src/devtools/hook/index.ts +13 -13
- package/src/devtools/index.ts +439 -439
- package/src/devtools/init.ts +266 -266
- package/src/devtools/protocol.ts +237 -237
- package/src/devtools/server/index.ts +17 -17
- package/src/devtools/server/source-context.ts +444 -444
- package/src/devtools/types.ts +319 -319
- package/src/devtools/worker/index.ts +25 -25
- package/src/devtools/worker/redaction-worker.ts +222 -222
- package/src/devtools/worker/worker-manager.ts +409 -409
- package/src/error/domains.ts +265 -265
- package/src/error/result.ts +46 -46
- package/src/error/types.ts +6 -6
- package/src/errors/extractor.ts +409 -409
- package/src/errors/index.ts +19 -19
- package/src/filling/auth.ts +308 -308
- package/src/filling/context.ts +24 -1
- package/src/filling/deps.ts +238 -238
- package/src/filling/index.ts +4 -0
- package/src/filling/sse-catchup.test.ts +56 -0
- package/src/filling/sse-catchup.ts +67 -0
- package/src/filling/sse.test.ts +168 -0
- package/src/filling/sse.ts +162 -0
- package/src/generator/index.ts +3 -3
- package/src/guard/analyzer.ts +360 -360
- package/src/guard/ast-analyzer.ts +806 -806
- package/src/guard/contract-guard.ts +9 -9
- package/src/guard/file-type.test.ts +24 -24
- package/src/guard/presets/atomic.ts +70 -70
- package/src/guard/presets/clean.ts +77 -77
- package/src/guard/presets/fsd.ts +79 -79
- package/src/guard/presets/hexagonal.ts +68 -68
- package/src/guard/presets/index.ts +291 -291
- package/src/guard/reporter.ts +445 -445
- package/src/guard/rules.ts +12 -12
- package/src/guard/statistics.ts +578 -578
- package/src/guard/suggestions.ts +358 -358
- package/src/guard/types.ts +348 -348
- package/src/guard/validator.ts +834 -834
- package/src/guard/watcher.ts +404 -404
- package/src/index.ts +6 -1
- package/src/intent/index.ts +310 -310
- package/src/island/index.ts +304 -304
- package/src/logging/index.ts +22 -22
- package/src/logging/transports.ts +365 -365
- package/src/plugins/index.ts +38 -38
- package/src/plugins/registry.ts +377 -377
- package/src/plugins/types.ts +363 -363
- package/src/report/index.ts +1 -1
- package/src/router/fs-patterns.ts +387 -387
- package/src/router/fs-scanner.ts +497 -497
- package/src/runtime/boundary.tsx +232 -232
- package/src/runtime/compose.ts +222 -222
- package/src/runtime/escape.ts +44 -0
- package/src/runtime/lifecycle.ts +381 -381
- package/src/runtime/logger.test.ts +345 -345
- package/src/runtime/logger.ts +677 -677
- package/src/runtime/router.test.ts +476 -476
- package/src/runtime/router.ts +105 -105
- package/src/runtime/security.ts +155 -155
- package/src/runtime/server.ts +257 -0
- package/src/runtime/session-key.ts +328 -328
- package/src/runtime/ssr.ts +16 -21
- package/src/runtime/streaming-ssr.ts +24 -33
- package/src/runtime/trace.ts +144 -144
- package/src/seo/index.ts +214 -214
- package/src/seo/integration/ssr.ts +307 -307
- package/src/seo/render/basic.ts +427 -427
- package/src/seo/render/index.ts +143 -143
- package/src/seo/render/jsonld.ts +539 -539
- package/src/seo/render/opengraph.ts +191 -191
- package/src/seo/render/robots.ts +116 -116
- package/src/seo/render/sitemap.ts +137 -137
- package/src/seo/render/twitter.ts +126 -126
- package/src/seo/resolve/index.ts +353 -353
- package/src/seo/resolve/opengraph.ts +143 -143
- package/src/seo/resolve/robots.ts +73 -73
- package/src/seo/resolve/title.ts +94 -94
- package/src/seo/resolve/twitter.ts +73 -73
- package/src/seo/resolve/url.ts +97 -97
- package/src/seo/routes/index.ts +290 -290
- package/src/seo/types.ts +575 -575
- package/src/slot/validator.ts +39 -39
- package/src/spec/index.ts +3 -3
- package/src/spec/load.ts +76 -76
- package/src/spec/lock.ts +56 -56
- package/src/utils/bun.ts +8 -8
- package/src/utils/lru-cache.ts +75 -75
- package/src/utils/safe-io.ts +188 -188
- package/src/utils/string-safe.ts +298 -298
|
@@ -1,345 +1,345 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mandu Runtime Logger Tests
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { describe, test, expect, mock, beforeEach } from "bun:test";
|
|
6
|
-
import { ManduContext } from "../filling/context";
|
|
7
|
-
import { logger, devLogger, prodLogger, type LogEntry } from "./logger";
|
|
8
|
-
|
|
9
|
-
// Mock Request 생성 헬퍼
|
|
10
|
-
function createMockRequest(
|
|
11
|
-
method: string,
|
|
12
|
-
url: string,
|
|
13
|
-
headers: Record<string, string> = {}
|
|
14
|
-
): Request {
|
|
15
|
-
return new Request(url, {
|
|
16
|
-
method,
|
|
17
|
-
headers: new Headers(headers),
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Mock ManduContext 생성 헬퍼
|
|
22
|
-
function createMockContext(
|
|
23
|
-
method: string = "GET",
|
|
24
|
-
path: string = "/api/test",
|
|
25
|
-
headers: Record<string, string> = {}
|
|
26
|
-
): ManduContext {
|
|
27
|
-
const request = createMockRequest(method, `http://localhost${path}`, headers);
|
|
28
|
-
return new ManduContext(request);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
describe("logger", () => {
|
|
32
|
-
describe("기본 동작", () => {
|
|
33
|
-
test("logger()는 훅 객체를 반환", () => {
|
|
34
|
-
const log = logger();
|
|
35
|
-
|
|
36
|
-
expect(log).toHaveProperty("onRequest");
|
|
37
|
-
expect(log).toHaveProperty("onError");
|
|
38
|
-
expect(log).toHaveProperty("afterHandle");
|
|
39
|
-
expect(log).toHaveProperty("afterResponse");
|
|
40
|
-
expect(typeof log.onRequest).toBe("function");
|
|
41
|
-
expect(typeof log.onError).toBe("function");
|
|
42
|
-
expect(typeof log.afterHandle).toBe("function");
|
|
43
|
-
expect(typeof log.afterResponse).toBe("function");
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("onRequest는 시작 시간과 requestId를 저장", () => {
|
|
47
|
-
const log = logger();
|
|
48
|
-
const ctx = createMockContext();
|
|
49
|
-
|
|
50
|
-
log.onRequest(ctx);
|
|
51
|
-
|
|
52
|
-
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
53
|
-
expect(ctx.has("__mandu_logger_request_id")).toBe(true);
|
|
54
|
-
expect(typeof ctx.get("__mandu_logger_start")).toBe("number");
|
|
55
|
-
expect(typeof ctx.get("__mandu_logger_request_id")).toBe("string");
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("onError는 에러를 저장", () => {
|
|
59
|
-
const log = logger();
|
|
60
|
-
const ctx = createMockContext();
|
|
61
|
-
const error = new Error("Test error");
|
|
62
|
-
|
|
63
|
-
log.onError(ctx, error);
|
|
64
|
-
|
|
65
|
-
expect(ctx.get("__mandu_logger_error")).toBe(error);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test("afterHandle은 응답을 저장하고 반환", () => {
|
|
69
|
-
const log = logger();
|
|
70
|
-
const ctx = createMockContext();
|
|
71
|
-
const response = new Response("OK");
|
|
72
|
-
|
|
73
|
-
const result = log.afterHandle(ctx, response);
|
|
74
|
-
|
|
75
|
-
expect(ctx.get("__mandu_logger_response")).toBe(response);
|
|
76
|
-
expect(result).toBe(response);
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
describe("skip 옵션", () => {
|
|
81
|
-
test("skip 패턴에 매칭되는 경로는 로깅하지 않음", () => {
|
|
82
|
-
const log = logger({ skip: ["/health", "/metrics"] });
|
|
83
|
-
const ctx = createMockContext("GET", "/health");
|
|
84
|
-
|
|
85
|
-
log.onRequest(ctx);
|
|
86
|
-
|
|
87
|
-
// skip된 요청은 시작 시간이 저장되지 않음
|
|
88
|
-
expect(ctx.has("__mandu_logger_start")).toBe(false);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("정규식 패턴도 지원", () => {
|
|
92
|
-
const log = logger({ skip: [/^\/static\//] });
|
|
93
|
-
const ctx = createMockContext("GET", "/static/image.png");
|
|
94
|
-
|
|
95
|
-
log.onRequest(ctx);
|
|
96
|
-
|
|
97
|
-
expect(ctx.has("__mandu_logger_start")).toBe(false);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test("skip 패턴에 매칭되지 않으면 로깅", () => {
|
|
101
|
-
const log = logger({ skip: ["/health"] });
|
|
102
|
-
const ctx = createMockContext("GET", "/api/users");
|
|
103
|
-
|
|
104
|
-
log.onRequest(ctx);
|
|
105
|
-
|
|
106
|
-
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe("sampleRate 옵션", () => {
|
|
111
|
-
test("sampleRate: 0이면 모든 요청 skip", () => {
|
|
112
|
-
const log = logger({ sampleRate: 0 });
|
|
113
|
-
const ctx = createMockContext();
|
|
114
|
-
|
|
115
|
-
log.onRequest(ctx);
|
|
116
|
-
|
|
117
|
-
expect(ctx.has("__mandu_logger_start")).toBe(false);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test("sampleRate: 1이면 모든 요청 로깅", () => {
|
|
121
|
-
const log = logger({ sampleRate: 1 });
|
|
122
|
-
const ctx = createMockContext();
|
|
123
|
-
|
|
124
|
-
log.onRequest(ctx);
|
|
125
|
-
|
|
126
|
-
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe("requestId 옵션", () => {
|
|
131
|
-
test("auto 모드는 자동 생성", () => {
|
|
132
|
-
const log = logger({ requestId: "auto" });
|
|
133
|
-
const ctx = createMockContext();
|
|
134
|
-
|
|
135
|
-
log.onRequest(ctx);
|
|
136
|
-
|
|
137
|
-
const requestId = ctx.get<string>("__mandu_logger_request_id");
|
|
138
|
-
expect(requestId).toBeDefined();
|
|
139
|
-
expect(requestId!.length).toBeGreaterThan(0);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
test("커스텀 함수로 requestId 생성", () => {
|
|
143
|
-
const log = logger({
|
|
144
|
-
requestId: (ctx) => `custom-${ctx.method}-123`,
|
|
145
|
-
});
|
|
146
|
-
const ctx = createMockContext("POST", "/api/test");
|
|
147
|
-
|
|
148
|
-
log.onRequest(ctx);
|
|
149
|
-
|
|
150
|
-
expect(ctx.get("__mandu_logger_request_id")).toBe("custom-POST-123");
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
describe("sink 옵션", () => {
|
|
155
|
-
test("sink 함수로 로그 엔트리 캡처", async () => {
|
|
156
|
-
const entries: LogEntry[] = [];
|
|
157
|
-
const log = logger({
|
|
158
|
-
sink: (entry) => entries.push(entry),
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const ctx = createMockContext("GET", "/api/users");
|
|
162
|
-
const response = new Response("OK", { status: 200 });
|
|
163
|
-
|
|
164
|
-
log.onRequest(ctx);
|
|
165
|
-
log.afterHandle(ctx, response);
|
|
166
|
-
await log.afterResponse(ctx);
|
|
167
|
-
|
|
168
|
-
expect(entries.length).toBe(1);
|
|
169
|
-
expect(entries[0].method).toBe("GET");
|
|
170
|
-
expect(entries[0].path).toBe("/api/users");
|
|
171
|
-
expect(entries[0].status).toBe(200);
|
|
172
|
-
expect(entries[0].duration).toBeGreaterThanOrEqual(0);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test("에러 시 level이 error", async () => {
|
|
176
|
-
const entries: LogEntry[] = [];
|
|
177
|
-
const log = logger({
|
|
178
|
-
sink: (entry) => entries.push(entry),
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
const ctx = createMockContext();
|
|
182
|
-
const error = new Error("Something went wrong");
|
|
183
|
-
|
|
184
|
-
log.onRequest(ctx);
|
|
185
|
-
log.onError(ctx, error);
|
|
186
|
-
await log.afterResponse(ctx);
|
|
187
|
-
|
|
188
|
-
expect(entries[0].level).toBe("error");
|
|
189
|
-
expect(entries[0].error?.message).toBe("Something went wrong");
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe("레드액션", () => {
|
|
194
|
-
test("기본 민감 헤더는 자동 마스킹", async () => {
|
|
195
|
-
const entries: LogEntry[] = [];
|
|
196
|
-
const log = logger({
|
|
197
|
-
sink: (entry) => entries.push(entry),
|
|
198
|
-
includeHeaders: true,
|
|
199
|
-
level: "debug",
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
const ctx = createMockContext("GET", "/api/test", {
|
|
203
|
-
Authorization: "Bearer secret-token",
|
|
204
|
-
"X-Api-Key": "my-api-key",
|
|
205
|
-
"Content-Type": "application/json",
|
|
206
|
-
});
|
|
207
|
-
const response = new Response("OK");
|
|
208
|
-
|
|
209
|
-
log.onRequest(ctx);
|
|
210
|
-
log.afterHandle(ctx, response);
|
|
211
|
-
await log.afterResponse(ctx);
|
|
212
|
-
|
|
213
|
-
const headers = entries[1].headers!;
|
|
214
|
-
expect(headers["authorization"]).toBe("[REDACTED]");
|
|
215
|
-
expect(headers["x-api-key"]).toBe("[REDACTED]");
|
|
216
|
-
expect(headers["content-type"]).toBe("application/json");
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
test("커스텀 레드액션 패턴 추가", async () => {
|
|
220
|
-
const entries: LogEntry[] = [];
|
|
221
|
-
const log = logger({
|
|
222
|
-
sink: (entry) => entries.push(entry),
|
|
223
|
-
includeHeaders: true,
|
|
224
|
-
level: "debug",
|
|
225
|
-
redact: ["x-custom-secret"],
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
const ctx = createMockContext("GET", "/api/test", {
|
|
229
|
-
"X-Custom-Secret": "my-secret",
|
|
230
|
-
"X-Public": "public-value",
|
|
231
|
-
});
|
|
232
|
-
const response = new Response("OK");
|
|
233
|
-
|
|
234
|
-
log.onRequest(ctx);
|
|
235
|
-
log.afterHandle(ctx, response);
|
|
236
|
-
await log.afterResponse(ctx);
|
|
237
|
-
|
|
238
|
-
const headers = entries[1].headers!;
|
|
239
|
-
expect(headers["x-custom-secret"]).toBe("[REDACTED]");
|
|
240
|
-
expect(headers["x-public"]).toBe("public-value");
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
describe("느린 요청 감지", () => {
|
|
245
|
-
test("slowThresholdMs 초과 시 slow 플래그 설정", async () => {
|
|
246
|
-
const entries: LogEntry[] = [];
|
|
247
|
-
const log = logger({
|
|
248
|
-
sink: (entry) => entries.push(entry),
|
|
249
|
-
slowThresholdMs: 0, // 모든 요청이 "느림"
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
const ctx = createMockContext();
|
|
253
|
-
const response = new Response("OK");
|
|
254
|
-
|
|
255
|
-
log.onRequest(ctx);
|
|
256
|
-
log.afterHandle(ctx, response);
|
|
257
|
-
await log.afterResponse(ctx);
|
|
258
|
-
|
|
259
|
-
expect(entries[0].slow).toBe(true);
|
|
260
|
-
expect(entries[0].level).toBe("warn");
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
describe("devLogger", () => {
|
|
266
|
-
test("개발용 기본 설정", () => {
|
|
267
|
-
const log = devLogger();
|
|
268
|
-
const ctx = createMockContext();
|
|
269
|
-
|
|
270
|
-
// devLogger는 debug 레벨이므로 요청 시작도 로깅해야 함
|
|
271
|
-
// (console.log가 호출되는지는 sink로 테스트)
|
|
272
|
-
log.onRequest(ctx);
|
|
273
|
-
|
|
274
|
-
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
test("커스텀 옵션 병합", async () => {
|
|
278
|
-
const entries: LogEntry[] = [];
|
|
279
|
-
const log = devLogger({
|
|
280
|
-
sink: (entry) => entries.push(entry),
|
|
281
|
-
skip: ["/health"],
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
const ctx1 = createMockContext("GET", "/api/test");
|
|
285
|
-
const ctx2 = createMockContext("GET", "/health");
|
|
286
|
-
const response = new Response("OK");
|
|
287
|
-
|
|
288
|
-
log.onRequest(ctx1);
|
|
289
|
-
log.afterHandle(ctx1, response);
|
|
290
|
-
await log.afterResponse(ctx1);
|
|
291
|
-
|
|
292
|
-
log.onRequest(ctx2);
|
|
293
|
-
|
|
294
|
-
// /health는 skip되어야 함
|
|
295
|
-
expect(ctx2.has("__mandu_logger_start")).toBe(false);
|
|
296
|
-
// /api/test는 debug 모드라 요청+응답 2개
|
|
297
|
-
expect(entries.length).toBe(2);
|
|
298
|
-
});
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
describe("prodLogger", () => {
|
|
302
|
-
test("프로덕션용 기본 설정", async () => {
|
|
303
|
-
const entries: LogEntry[] = [];
|
|
304
|
-
const log = prodLogger({
|
|
305
|
-
sink: (entry) => entries.push(entry),
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
const ctx = createMockContext();
|
|
309
|
-
const response = new Response("OK");
|
|
310
|
-
|
|
311
|
-
log.onRequest(ctx);
|
|
312
|
-
log.afterHandle(ctx, response);
|
|
313
|
-
await log.afterResponse(ctx);
|
|
314
|
-
|
|
315
|
-
// prodLogger는 info 레벨이므로 응답만 로깅
|
|
316
|
-
expect(entries.length).toBe(1);
|
|
317
|
-
expect(entries[0].headers).toBeUndefined(); // includeHeaders: false
|
|
318
|
-
expect(entries[0].body).toBeUndefined(); // includeBody: false
|
|
319
|
-
});
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
describe("로그 포맷", () => {
|
|
323
|
-
test("JSON 포맷은 파싱 가능한 JSON 출력", async () => {
|
|
324
|
-
let output = "";
|
|
325
|
-
const originalLog = console.log;
|
|
326
|
-
console.log = (msg: string) => {
|
|
327
|
-
output = msg;
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
const log = logger({ format: "json" });
|
|
331
|
-
const ctx = createMockContext();
|
|
332
|
-
const response = new Response("OK");
|
|
333
|
-
|
|
334
|
-
log.onRequest(ctx);
|
|
335
|
-
log.afterHandle(ctx, response);
|
|
336
|
-
await log.afterResponse(ctx);
|
|
337
|
-
|
|
338
|
-
console.log = originalLog;
|
|
339
|
-
|
|
340
|
-
const parsed = JSON.parse(output);
|
|
341
|
-
expect(parsed.method).toBe("GET");
|
|
342
|
-
expect(parsed.path).toBe("/api/test");
|
|
343
|
-
expect(parsed.status).toBe(200);
|
|
344
|
-
});
|
|
345
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Mandu Runtime Logger Tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, test, expect, mock, beforeEach } from "bun:test";
|
|
6
|
+
import { ManduContext } from "../filling/context";
|
|
7
|
+
import { logger, devLogger, prodLogger, type LogEntry } from "./logger";
|
|
8
|
+
|
|
9
|
+
// Mock Request 생성 헬퍼
|
|
10
|
+
function createMockRequest(
|
|
11
|
+
method: string,
|
|
12
|
+
url: string,
|
|
13
|
+
headers: Record<string, string> = {}
|
|
14
|
+
): Request {
|
|
15
|
+
return new Request(url, {
|
|
16
|
+
method,
|
|
17
|
+
headers: new Headers(headers),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Mock ManduContext 생성 헬퍼
|
|
22
|
+
function createMockContext(
|
|
23
|
+
method: string = "GET",
|
|
24
|
+
path: string = "/api/test",
|
|
25
|
+
headers: Record<string, string> = {}
|
|
26
|
+
): ManduContext {
|
|
27
|
+
const request = createMockRequest(method, `http://localhost${path}`, headers);
|
|
28
|
+
return new ManduContext(request);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("logger", () => {
|
|
32
|
+
describe("기본 동작", () => {
|
|
33
|
+
test("logger()는 훅 객체를 반환", () => {
|
|
34
|
+
const log = logger();
|
|
35
|
+
|
|
36
|
+
expect(log).toHaveProperty("onRequest");
|
|
37
|
+
expect(log).toHaveProperty("onError");
|
|
38
|
+
expect(log).toHaveProperty("afterHandle");
|
|
39
|
+
expect(log).toHaveProperty("afterResponse");
|
|
40
|
+
expect(typeof log.onRequest).toBe("function");
|
|
41
|
+
expect(typeof log.onError).toBe("function");
|
|
42
|
+
expect(typeof log.afterHandle).toBe("function");
|
|
43
|
+
expect(typeof log.afterResponse).toBe("function");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("onRequest는 시작 시간과 requestId를 저장", () => {
|
|
47
|
+
const log = logger();
|
|
48
|
+
const ctx = createMockContext();
|
|
49
|
+
|
|
50
|
+
log.onRequest(ctx);
|
|
51
|
+
|
|
52
|
+
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
53
|
+
expect(ctx.has("__mandu_logger_request_id")).toBe(true);
|
|
54
|
+
expect(typeof ctx.get("__mandu_logger_start")).toBe("number");
|
|
55
|
+
expect(typeof ctx.get("__mandu_logger_request_id")).toBe("string");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("onError는 에러를 저장", () => {
|
|
59
|
+
const log = logger();
|
|
60
|
+
const ctx = createMockContext();
|
|
61
|
+
const error = new Error("Test error");
|
|
62
|
+
|
|
63
|
+
log.onError(ctx, error);
|
|
64
|
+
|
|
65
|
+
expect(ctx.get("__mandu_logger_error")).toBe(error);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("afterHandle은 응답을 저장하고 반환", () => {
|
|
69
|
+
const log = logger();
|
|
70
|
+
const ctx = createMockContext();
|
|
71
|
+
const response = new Response("OK");
|
|
72
|
+
|
|
73
|
+
const result = log.afterHandle(ctx, response);
|
|
74
|
+
|
|
75
|
+
expect(ctx.get("__mandu_logger_response")).toBe(response);
|
|
76
|
+
expect(result).toBe(response);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("skip 옵션", () => {
|
|
81
|
+
test("skip 패턴에 매칭되는 경로는 로깅하지 않음", () => {
|
|
82
|
+
const log = logger({ skip: ["/health", "/metrics"] });
|
|
83
|
+
const ctx = createMockContext("GET", "/health");
|
|
84
|
+
|
|
85
|
+
log.onRequest(ctx);
|
|
86
|
+
|
|
87
|
+
// skip된 요청은 시작 시간이 저장되지 않음
|
|
88
|
+
expect(ctx.has("__mandu_logger_start")).toBe(false);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("정규식 패턴도 지원", () => {
|
|
92
|
+
const log = logger({ skip: [/^\/static\//] });
|
|
93
|
+
const ctx = createMockContext("GET", "/static/image.png");
|
|
94
|
+
|
|
95
|
+
log.onRequest(ctx);
|
|
96
|
+
|
|
97
|
+
expect(ctx.has("__mandu_logger_start")).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("skip 패턴에 매칭되지 않으면 로깅", () => {
|
|
101
|
+
const log = logger({ skip: ["/health"] });
|
|
102
|
+
const ctx = createMockContext("GET", "/api/users");
|
|
103
|
+
|
|
104
|
+
log.onRequest(ctx);
|
|
105
|
+
|
|
106
|
+
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("sampleRate 옵션", () => {
|
|
111
|
+
test("sampleRate: 0이면 모든 요청 skip", () => {
|
|
112
|
+
const log = logger({ sampleRate: 0 });
|
|
113
|
+
const ctx = createMockContext();
|
|
114
|
+
|
|
115
|
+
log.onRequest(ctx);
|
|
116
|
+
|
|
117
|
+
expect(ctx.has("__mandu_logger_start")).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("sampleRate: 1이면 모든 요청 로깅", () => {
|
|
121
|
+
const log = logger({ sampleRate: 1 });
|
|
122
|
+
const ctx = createMockContext();
|
|
123
|
+
|
|
124
|
+
log.onRequest(ctx);
|
|
125
|
+
|
|
126
|
+
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("requestId 옵션", () => {
|
|
131
|
+
test("auto 모드는 자동 생성", () => {
|
|
132
|
+
const log = logger({ requestId: "auto" });
|
|
133
|
+
const ctx = createMockContext();
|
|
134
|
+
|
|
135
|
+
log.onRequest(ctx);
|
|
136
|
+
|
|
137
|
+
const requestId = ctx.get<string>("__mandu_logger_request_id");
|
|
138
|
+
expect(requestId).toBeDefined();
|
|
139
|
+
expect(requestId!.length).toBeGreaterThan(0);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("커스텀 함수로 requestId 생성", () => {
|
|
143
|
+
const log = logger({
|
|
144
|
+
requestId: (ctx) => `custom-${ctx.method}-123`,
|
|
145
|
+
});
|
|
146
|
+
const ctx = createMockContext("POST", "/api/test");
|
|
147
|
+
|
|
148
|
+
log.onRequest(ctx);
|
|
149
|
+
|
|
150
|
+
expect(ctx.get("__mandu_logger_request_id")).toBe("custom-POST-123");
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("sink 옵션", () => {
|
|
155
|
+
test("sink 함수로 로그 엔트리 캡처", async () => {
|
|
156
|
+
const entries: LogEntry[] = [];
|
|
157
|
+
const log = logger({
|
|
158
|
+
sink: (entry) => entries.push(entry),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const ctx = createMockContext("GET", "/api/users");
|
|
162
|
+
const response = new Response("OK", { status: 200 });
|
|
163
|
+
|
|
164
|
+
log.onRequest(ctx);
|
|
165
|
+
log.afterHandle(ctx, response);
|
|
166
|
+
await log.afterResponse(ctx);
|
|
167
|
+
|
|
168
|
+
expect(entries.length).toBe(1);
|
|
169
|
+
expect(entries[0].method).toBe("GET");
|
|
170
|
+
expect(entries[0].path).toBe("/api/users");
|
|
171
|
+
expect(entries[0].status).toBe(200);
|
|
172
|
+
expect(entries[0].duration).toBeGreaterThanOrEqual(0);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("에러 시 level이 error", async () => {
|
|
176
|
+
const entries: LogEntry[] = [];
|
|
177
|
+
const log = logger({
|
|
178
|
+
sink: (entry) => entries.push(entry),
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const ctx = createMockContext();
|
|
182
|
+
const error = new Error("Something went wrong");
|
|
183
|
+
|
|
184
|
+
log.onRequest(ctx);
|
|
185
|
+
log.onError(ctx, error);
|
|
186
|
+
await log.afterResponse(ctx);
|
|
187
|
+
|
|
188
|
+
expect(entries[0].level).toBe("error");
|
|
189
|
+
expect(entries[0].error?.message).toBe("Something went wrong");
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("레드액션", () => {
|
|
194
|
+
test("기본 민감 헤더는 자동 마스킹", async () => {
|
|
195
|
+
const entries: LogEntry[] = [];
|
|
196
|
+
const log = logger({
|
|
197
|
+
sink: (entry) => entries.push(entry),
|
|
198
|
+
includeHeaders: true,
|
|
199
|
+
level: "debug",
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const ctx = createMockContext("GET", "/api/test", {
|
|
203
|
+
Authorization: "Bearer secret-token",
|
|
204
|
+
"X-Api-Key": "my-api-key",
|
|
205
|
+
"Content-Type": "application/json",
|
|
206
|
+
});
|
|
207
|
+
const response = new Response("OK");
|
|
208
|
+
|
|
209
|
+
log.onRequest(ctx);
|
|
210
|
+
log.afterHandle(ctx, response);
|
|
211
|
+
await log.afterResponse(ctx);
|
|
212
|
+
|
|
213
|
+
const headers = entries[1].headers!;
|
|
214
|
+
expect(headers["authorization"]).toBe("[REDACTED]");
|
|
215
|
+
expect(headers["x-api-key"]).toBe("[REDACTED]");
|
|
216
|
+
expect(headers["content-type"]).toBe("application/json");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("커스텀 레드액션 패턴 추가", async () => {
|
|
220
|
+
const entries: LogEntry[] = [];
|
|
221
|
+
const log = logger({
|
|
222
|
+
sink: (entry) => entries.push(entry),
|
|
223
|
+
includeHeaders: true,
|
|
224
|
+
level: "debug",
|
|
225
|
+
redact: ["x-custom-secret"],
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
const ctx = createMockContext("GET", "/api/test", {
|
|
229
|
+
"X-Custom-Secret": "my-secret",
|
|
230
|
+
"X-Public": "public-value",
|
|
231
|
+
});
|
|
232
|
+
const response = new Response("OK");
|
|
233
|
+
|
|
234
|
+
log.onRequest(ctx);
|
|
235
|
+
log.afterHandle(ctx, response);
|
|
236
|
+
await log.afterResponse(ctx);
|
|
237
|
+
|
|
238
|
+
const headers = entries[1].headers!;
|
|
239
|
+
expect(headers["x-custom-secret"]).toBe("[REDACTED]");
|
|
240
|
+
expect(headers["x-public"]).toBe("public-value");
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("느린 요청 감지", () => {
|
|
245
|
+
test("slowThresholdMs 초과 시 slow 플래그 설정", async () => {
|
|
246
|
+
const entries: LogEntry[] = [];
|
|
247
|
+
const log = logger({
|
|
248
|
+
sink: (entry) => entries.push(entry),
|
|
249
|
+
slowThresholdMs: 0, // 모든 요청이 "느림"
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const ctx = createMockContext();
|
|
253
|
+
const response = new Response("OK");
|
|
254
|
+
|
|
255
|
+
log.onRequest(ctx);
|
|
256
|
+
log.afterHandle(ctx, response);
|
|
257
|
+
await log.afterResponse(ctx);
|
|
258
|
+
|
|
259
|
+
expect(entries[0].slow).toBe(true);
|
|
260
|
+
expect(entries[0].level).toBe("warn");
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe("devLogger", () => {
|
|
266
|
+
test("개발용 기본 설정", () => {
|
|
267
|
+
const log = devLogger();
|
|
268
|
+
const ctx = createMockContext();
|
|
269
|
+
|
|
270
|
+
// devLogger는 debug 레벨이므로 요청 시작도 로깅해야 함
|
|
271
|
+
// (console.log가 호출되는지는 sink로 테스트)
|
|
272
|
+
log.onRequest(ctx);
|
|
273
|
+
|
|
274
|
+
expect(ctx.has("__mandu_logger_start")).toBe(true);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("커스텀 옵션 병합", async () => {
|
|
278
|
+
const entries: LogEntry[] = [];
|
|
279
|
+
const log = devLogger({
|
|
280
|
+
sink: (entry) => entries.push(entry),
|
|
281
|
+
skip: ["/health"],
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const ctx1 = createMockContext("GET", "/api/test");
|
|
285
|
+
const ctx2 = createMockContext("GET", "/health");
|
|
286
|
+
const response = new Response("OK");
|
|
287
|
+
|
|
288
|
+
log.onRequest(ctx1);
|
|
289
|
+
log.afterHandle(ctx1, response);
|
|
290
|
+
await log.afterResponse(ctx1);
|
|
291
|
+
|
|
292
|
+
log.onRequest(ctx2);
|
|
293
|
+
|
|
294
|
+
// /health는 skip되어야 함
|
|
295
|
+
expect(ctx2.has("__mandu_logger_start")).toBe(false);
|
|
296
|
+
// /api/test는 debug 모드라 요청+응답 2개
|
|
297
|
+
expect(entries.length).toBe(2);
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe("prodLogger", () => {
|
|
302
|
+
test("프로덕션용 기본 설정", async () => {
|
|
303
|
+
const entries: LogEntry[] = [];
|
|
304
|
+
const log = prodLogger({
|
|
305
|
+
sink: (entry) => entries.push(entry),
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const ctx = createMockContext();
|
|
309
|
+
const response = new Response("OK");
|
|
310
|
+
|
|
311
|
+
log.onRequest(ctx);
|
|
312
|
+
log.afterHandle(ctx, response);
|
|
313
|
+
await log.afterResponse(ctx);
|
|
314
|
+
|
|
315
|
+
// prodLogger는 info 레벨이므로 응답만 로깅
|
|
316
|
+
expect(entries.length).toBe(1);
|
|
317
|
+
expect(entries[0].headers).toBeUndefined(); // includeHeaders: false
|
|
318
|
+
expect(entries[0].body).toBeUndefined(); // includeBody: false
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe("로그 포맷", () => {
|
|
323
|
+
test("JSON 포맷은 파싱 가능한 JSON 출력", async () => {
|
|
324
|
+
let output = "";
|
|
325
|
+
const originalLog = console.log;
|
|
326
|
+
console.log = (msg: string) => {
|
|
327
|
+
output = msg;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const log = logger({ format: "json" });
|
|
331
|
+
const ctx = createMockContext();
|
|
332
|
+
const response = new Response("OK");
|
|
333
|
+
|
|
334
|
+
log.onRequest(ctx);
|
|
335
|
+
log.afterHandle(ctx, response);
|
|
336
|
+
await log.afterResponse(ctx);
|
|
337
|
+
|
|
338
|
+
console.log = originalLog;
|
|
339
|
+
|
|
340
|
+
const parsed = JSON.parse(output);
|
|
341
|
+
expect(parsed.method).toBe("GET");
|
|
342
|
+
expect(parsed.path).toBe("/api/test");
|
|
343
|
+
expect(parsed.status).toBe(200);
|
|
344
|
+
});
|
|
345
|
+
});
|