@adviser/cement 0.2.29 → 0.2.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,314 +0,0 @@
1
- import { Time } from "./time.js";
2
- import { WebSysAbstraction } from "./web/index.js";
3
- import { TimeMode } from "./sys-abstraction.js";
4
- import { TraceNode } from "./tracer.js";
5
- import { MockLogger } from "./test/mock-logger.js";
6
-
7
- describe("trace", () => {
8
- let time: Time;
9
- let refTime: Time;
10
- let trace: TraceNode;
11
- const logger = MockLogger().logger.With().Module("trace").Str("value", "important").Logger();
12
- beforeEach(() => {
13
- time = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
14
- trace = TraceNode.root(time, logger);
15
- refTime = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
16
- });
17
- it("a simple trace", () => {
18
- expect(
19
- trace.span("test", (trace) => {
20
- const r1 = trace.span("test.1", () => {
21
- return 1;
22
- }) as number;
23
- const r2 = trace.span("test.2", () => {
24
- return 1;
25
- }) as number;
26
- return r1 + r2;
27
- }),
28
- ).toBe(2);
29
- const childs = Array.from(trace.childs.values());
30
- expect(childs.map((v) => v.invokes())).toEqual([
31
- {
32
- ctx: {
33
- module: "trace",
34
- spanId: "test",
35
- value: "important",
36
- },
37
- invokations: [
38
- {
39
- start: refTime.Now().getTime(),
40
- result: "success",
41
- end: refTime.Now(5).getTime(),
42
- },
43
- ],
44
- },
45
- ]);
46
- const layered = Array.from(trace.childs.get("test")?.childs.values() || []);
47
- refTime = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
48
- expect(layered.map((v) => v.invokes())).toEqual([
49
- {
50
- ctx: {
51
- module: "trace",
52
- spanId: "test.1",
53
- value: "important",
54
- },
55
- invokations: [
56
- {
57
- start: refTime.Now(2).getTime(),
58
- result: "success",
59
- end: refTime.Now().getTime(),
60
- },
61
- ],
62
- },
63
- {
64
- ctx: {
65
- module: "trace",
66
- spanId: "test.2",
67
- value: "important",
68
- },
69
- invokations: [
70
- {
71
- start: refTime.Now().getTime(),
72
- result: "success",
73
- end: refTime.Now(1).getTime(),
74
- },
75
- ],
76
- },
77
- ]);
78
- });
79
-
80
- it("a async simple trace", async () => {
81
- const log = trace.ctx.logger?.With().Str("value", "test").Logger();
82
- const ret = await trace.span(trace.ctxWith("test", log), async (trace) => {
83
- const r1 = trace.span(trace.ctxWith("test.1"), () => 1) as number;
84
- const log2 = trace.ctx.logger?.With().Module("xxx").Str("r2", "test.2").Logger();
85
- const r2 = (await trace.span(trace.ctxWith("test.2", log2), async () => {
86
- time.Now();
87
- await new Promise<void>((resolve) =>
88
- setTimeout(() => {
89
- time.Now();
90
- time.Now();
91
- resolve();
92
- }, 100),
93
- );
94
- return 1;
95
- })) as number;
96
- return r1 + r2;
97
- });
98
- expect(ret).toBe(2);
99
- const childs = Array.from(trace.childs.values());
100
- const exp = childs.map((v) => v.invokes());
101
- expect(exp).toEqual([
102
- {
103
- ctx: {
104
- module: "trace",
105
- spanId: "test",
106
- value: "test",
107
- },
108
- invokations: [
109
- {
110
- start: refTime.Now().getTime(),
111
- result: "success",
112
- end: refTime.Now(8).getTime(),
113
- },
114
- ],
115
- },
116
- ]);
117
- const layered = Array.from(trace.childs.get("test")?.childs.values() || []);
118
- expect(layered.map((v) => v.invokes())).toEqual([
119
- {
120
- ctx: {
121
- module: "trace",
122
- spanId: "test.1",
123
- value: "test",
124
- },
125
- invokations: [
126
- {
127
- result: "success",
128
- start: refTime.Now(-2).getTime(),
129
- end: refTime.Now().getTime(),
130
- },
131
- ],
132
- },
133
- {
134
- ctx: {
135
- module: "xxx",
136
- r2: "test.2",
137
- spanId: "test.2",
138
- value: "test",
139
- },
140
- invokations: [
141
- {
142
- start: refTime.Now().getTime(),
143
- end: refTime.Now(4).getTime(),
144
- result: "success",
145
- },
146
- ],
147
- },
148
- ]);
149
- });
150
-
151
- it("a async exception trace", async () => {
152
- const ret = await trace.span("test", async (trace) => {
153
- let r1 = 0;
154
- let r2 = 0;
155
- for (let i = 0; i < 3; i++) {
156
- try {
157
- r1 += trace.span("test.1", (trace) => {
158
- if (i % 2) {
159
- throw new Error("test.1");
160
- }
161
- trace.metrics.get("i.1").add([i]);
162
- return 1;
163
- }) as number;
164
- } catch (e) {
165
- if (i % 2) {
166
- expect((e as Error).message).toEqual("test.1");
167
- } else {
168
- assert(false, "should not happen");
169
- }
170
- }
171
- try {
172
- r2 += await trace.span("test.2", async (trace) => {
173
- time.Now();
174
- await new Promise<void>((resolve, reject) =>
175
- setTimeout(() => {
176
- time.Now();
177
- time.Now();
178
- if (i % 2) {
179
- trace.metrics.get("i.2").add(i);
180
- resolve();
181
- } else {
182
- reject("test.2");
183
- }
184
- }, 10),
185
- );
186
- return 1;
187
- });
188
- } catch (e) {
189
- if (i % 2) {
190
- assert(false, "should not happen");
191
- } else {
192
- expect(e).toEqual("test.2");
193
- }
194
- }
195
- }
196
- return r1 + r2;
197
- });
198
- expect(ret).toBe(3);
199
- expect(trace.metrics.toJSON()).toEqual({
200
- "/test/test.1/i.1": [0, 2],
201
- "/test/test.2/i.2": 1,
202
- });
203
- const childs = Array.from(trace.childs.values());
204
- const exp = childs.map((v) => v.invokes());
205
- expect(exp).toEqual([
206
- {
207
- ctx: {
208
- module: "trace",
209
- spanId: "test",
210
- value: "important",
211
- },
212
- invokations: [
213
- {
214
- start: refTime.Now(1).getTime(),
215
- end: refTime.Now(22).getTime(),
216
- result: "success",
217
- },
218
- ],
219
- },
220
- ]);
221
- const layered = Array.from(trace.childs.get("test")?.childs.values() || []);
222
- expect(layered.map((v) => v.invokes())).toEqual([
223
- {
224
- ctx: {
225
- module: "trace",
226
- spanId: "test.1",
227
- value: "important",
228
- },
229
- invokations: [
230
- {
231
- start: refTime.Now(-2).getTime(),
232
- end: refTime.Now().getTime(),
233
- result: "success",
234
- },
235
- {
236
- start: refTime.Now(-9).getTime(),
237
- end: refTime.Now().getTime(),
238
- result: "error",
239
- },
240
- {
241
- start: refTime.Now(-16).getTime(),
242
- end: refTime.Now().getTime(),
243
- result: "success",
244
- },
245
- ],
246
- metricRefs: {
247
- "/test/test.1/i.1": [0, 2],
248
- },
249
- },
250
- {
251
- ctx: {
252
- module: "trace",
253
- spanId: "test.2",
254
- value: "important",
255
- },
256
- invokations: [
257
- {
258
- start: refTime.Now(-4).getTime(),
259
- end: refTime.Now(4).getTime(),
260
- result: "error",
261
- },
262
- {
263
- start: refTime.Now(-11).getTime(),
264
- end: refTime.Now(4).getTime(),
265
- result: "success",
266
- },
267
- {
268
- start: refTime.Now(-18).getTime(),
269
- end: refTime.Now(4).getTime(),
270
- result: "error",
271
- },
272
- ],
273
- metricRefs: {
274
- "/test/test.2/i.2": 1,
275
- },
276
- },
277
- ]);
278
- });
279
- });
280
-
281
- describe("metrics", () => {
282
- let time: Time;
283
- let trace: TraceNode;
284
- // const logger = MockLogger().logger.With().Module("trace").Str("value", "important").Logger()
285
- beforeEach(() => {
286
- time = WebSysAbstraction({ TimeMode: TimeMode.STEP }).Time();
287
- trace = TraceNode.root(time);
288
- });
289
-
290
- it("a simple metrics", () => {
291
- ["/test", "test", "/test/wurst", "bla"].forEach((path) => {
292
- const abs = path.startsWith("/") ? path : "/" + path;
293
- expect(trace.metrics.get(path).path).toBe(abs);
294
- expect(trace.metrics.get(path).value).toBeFalsy();
295
- trace.metrics.get(path).add(4711);
296
- expect(trace.metrics.get(path).value).toBe(4711);
297
- trace.metrics.get(path).set(undefined);
298
- });
299
- });
300
- it("create metrics path", () => {
301
- trace.span("test", (trace) => {
302
- trace.span("test.1", (trace) => {
303
- trace.metrics.get("m1.1").add(1);
304
- trace.metrics.get("/test/test.1/m1.1").add(1);
305
- expect(trace.metrics.get("m1.1").path).toBe("/test/test.1/m1.1");
306
- expect(trace.metrics.get("m1.1").value).toBe(2);
307
- });
308
- });
309
- });
310
- it("typed span promise or literal", async () => {
311
- expect(trace.span("test", () => "1")).toBe("1");
312
- expect(await trace.span("test", async () => 1)).toBe(1);
313
- });
314
- });
package/src/uri.test.ts DELETED
@@ -1,155 +0,0 @@
1
- import { BuildURI, MutableURL, URI } from "@adviser/cement";
2
-
3
- describe("BuildURI", () => {
4
- let uri: BuildURI;
5
- beforeEach(() => {
6
- uri = BuildURI.from(new MutableURL("http://example.com"));
7
- uri.hostname("example");
8
- uri.setParam("key", "value");
9
- });
10
-
11
- it("toString", () => {
12
- expect(uri.toString()).toBe("http://example/?key=value");
13
- });
14
-
15
- it("build", () => {
16
- expect(uri.URI().toString()).toBe("http://example/?key=value");
17
- });
18
-
19
- it("defParam", () => {
20
- uri.defParam("key", "value2");
21
- uri.defParam("key2", "value2");
22
- expect(uri.toString()).toBe("http://example/?key=value&key2=value2");
23
- });
24
-
25
- it("searchParams sorted in toString", () => {
26
- uri.setParam("z", "value");
27
- uri.setParam("a", "value");
28
- uri.setParam("m", "value");
29
- expect(uri.toString()).toBe("http://example/?a=value&key=value&m=value&z=value");
30
- });
31
- });
32
-
33
- describe("URI", () => {
34
- // static from(strURLUri: string | URL | URI | NullOrUndef, defaultProtocol = "file:"): URI {
35
- it("from str with default", () => {
36
- const uri = URI.from("/example/wurst");
37
- expect(uri.toString()).toBe("file:///example/wurst");
38
- });
39
- // it("from str defect", () => {
40
- // expect(URI.from("doof:meno")).toBe(null);
41
- // })
42
- it("from URL str", () => {
43
- expect(URI.from("bla://example/com?key=value").toString()).toBe("bla://example/com?key=value");
44
- });
45
- it("from URL", () => {
46
- expect(URI.from(new MutableURL("blix://example.com?key=value")).toString()).toBe("blix://example.com?key=value");
47
- });
48
- it("from URI", () => {
49
- expect(URI.from(URI.from("blix://example.com?key=value")).toString()).toBe("blix://example.com?key=value");
50
- });
51
- it("from undef", () => {
52
- expect(URI.from(null).toString()).toBe("file:///");
53
- });
54
-
55
- it("build", () => {
56
- expect(URI.from("blix://example.com?key=value").build().toString()).toBe("blix://example.com?key=value");
57
- });
58
-
59
- it("clone", () => {
60
- expect(URI.from("blix://example.com?key=value").clone().toString()).toBe("blix://example.com?key=value");
61
- });
62
-
63
- it("asURL", () => {
64
- expect(URI.from("blix://example.com?key=value").asURL().toString()).toBe("blix://example.com?key=value");
65
- });
66
-
67
- it("toString", () => {
68
- expect(URI.from("blix://example.com?key=value").toString()).toBe("blix://example.com?key=value");
69
- });
70
-
71
- it("searchParams sorted in toString", () => {
72
- expect(URI.from("blix://example.com?z=value&a=value&m=value").toString()).toBe("blix://example.com?a=value&m=value&z=value");
73
- });
74
- it("searchParams sorted in asURL", () => {
75
- expect(URI.from("blix://example.com?z=value&a=value&m=value").asURL().toString()).toBe(
76
- "blix://example.com?a=value&m=value&z=value",
77
- );
78
- });
79
-
80
- it("merge", () => {
81
- expect(URI.merge("blix://example.com?key=value&into=4", "murk://bla/com?key=from&z=value").toString()).toBe(
82
- "murk://bla/com?into=4&key=from&z=value",
83
- );
84
- });
85
-
86
- it("merge empty", () => {
87
- expect(URI.merge("blix://example/com?key=value&into=4", "murk://?key=from&z=value").toString()).toBe(
88
- "murk://example/com?into=4&key=from&z=value",
89
- );
90
- });
91
-
92
- it("firefox file relative into", () => {
93
- expect(URI.from("file://./dist/tests/key.bag").toString()).toBe("file://./dist/tests/key.bag");
94
- });
95
-
96
- it("from empty", () => {
97
- expect(URI.merge(`file://./dist/tests/key.bag`, "").toString()).toBe("file://./dist/tests/key.bag");
98
- });
99
-
100
- it("merge thing about", () => {
101
- const result = URI.merge("./dist/what?byKey=4444", "murk://bla.com?key=from&z=value");
102
- expect(result.toString()).toBe("murk://bla.com?byKey=4444&key=from&z=value");
103
- });
104
-
105
- it("isURI real", () => {
106
- expect(URI.is(URI.from())).toBe(true);
107
- });
108
- it("isURI fake", () => {
109
- expect(
110
- URI.is({
111
- asURL: () => new URL("http://example.com"),
112
- hasParam: () => false,
113
- getParam: () => "",
114
- }),
115
- ).toBe(true);
116
- });
117
-
118
- it("safari has a different pathname behavior", () => {
119
- // chrome -> new URL("indexdb://fp/?name=test&store=meta").pathname -> //fp/
120
- // safari -> new URL("indexdb://fp/?name=test&store=meta").pathname -> /
121
- const uri = URI.from("indexdb://fp/?name=test&store=meta");
122
- expect(uri.pathname).toBe("fp/");
123
- });
124
-
125
- it("passing URL to fetch", async () => {
126
- const uri = URI.from("https://jsonplaceholder.typicode.com/todos/1");
127
- const res = await fetch(uri.asURL());
128
- expect(res.status).toBeGreaterThan(199);
129
- });
130
-
131
- it("MutableURL is instance of URL", () => {
132
- expect(new MutableURL("http://example.com") instanceof URL).toBe(true);
133
- });
134
-
135
- it("file url", () => {
136
- const uri = URI.from("file://fp?storagekey=zTvTPEPQRWij8rfb3FrFqBm");
137
- expect(uri.pathname).toBe("fp");
138
- });
139
-
140
- it("unregistered protocol with hostPart", () => {
141
- const withoutHostpart = URI.from("indexdb://fp:bla/test/?name=test&store=meta");
142
- expect(() => withoutHostpart.hostname).toThrowError('you can use hostname only if protocol is ["http","https","ws","wss"]');
143
- });
144
-
145
- it("register protocol with hostPart", () => {
146
- const unreg = URI.protocolHasHostpart("indexdb:");
147
- const withHostpart = URI.from("indexdb://fp1:88/test/wurst?name=test&store=meta");
148
- expect(withHostpart.host).toBe("fp1:88");
149
- expect(withHostpart.pathname).toBe("/test/wurst");
150
- const withHostpartNoPath = URI.from("indexdb://fp2:88?name=test&store=meta");
151
- expect(withHostpartNoPath.host).toBe("fp2:88");
152
- expect(withHostpartNoPath.pathname).toBe("/");
153
- unreg();
154
- });
155
- });