@apifuse/provider-sdk 2.0.0-beta.1 → 2.1.0-beta.1
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/AUTHORING.md +93 -0
- package/CHANGELOG.md +21 -0
- package/README.md +133 -28
- package/bin/apifuse-check.ts +78 -71
- package/bin/apifuse-create.ts +12 -0
- package/bin/apifuse-dev.ts +24 -61
- package/bin/apifuse-pack-check.ts +87 -0
- package/bin/apifuse-pack-smoke.ts +122 -0
- package/bin/apifuse-perf.ts +33 -32
- package/bin/apifuse-record.ts +17 -7
- package/bin/apifuse-test.ts +6 -4
- package/bin/apifuse.ts +36 -35
- package/package.json +29 -9
- package/src/ceremonies/index.ts +768 -0
- package/src/cli/commands.ts +87 -0
- package/src/cli/create.ts +845 -0
- package/src/cli/templates/provider/Dockerfile.tpl +7 -0
- package/src/cli/templates/provider/README.md.tpl +41 -0
- package/src/cli/templates/provider/dev.ts.tpl +5 -0
- package/src/cli/templates/provider/index.test.ts.tpl +13 -0
- package/src/cli/templates/provider/index.ts.tpl +58 -0
- package/src/cli/templates/provider/start.ts.tpl +5 -0
- package/src/config/loader.ts +61 -1
- package/src/define.ts +565 -41
- package/src/dev.ts +2 -6
- package/src/errors.ts +42 -0
- package/src/index.ts +44 -38
- package/src/lint.ts +574 -0
- package/src/provider.ts +13 -0
- package/src/runtime/auth-flow.ts +67 -0
- package/src/runtime/credential.ts +95 -0
- package/src/runtime/env.ts +13 -0
- package/src/runtime/executor.ts +13 -14
- package/src/runtime/http.ts +36 -12
- package/src/runtime/insights.ts +3 -3
- package/src/runtime/key-derivation.ts +122 -0
- package/src/runtime/keyring.ts +148 -0
- package/src/runtime/namespace.ts +33 -0
- package/src/runtime/prevalidate.ts +252 -0
- package/src/runtime/tls.ts +41 -17
- package/src/runtime/waterfall.ts +0 -1
- package/src/schema.ts +77 -0
- package/src/serve.ts +1 -664
- package/src/server/index.ts +22 -0
- package/src/server/serve.ts +624 -0
- package/src/server/types.ts +78 -0
- package/src/stealth/profiles.ts +10 -93
- package/src/testing/run.ts +391 -32
- package/src/types.ts +390 -41
- package/bin/apifuse-init.ts +0 -387
- package/src/__tests__/auth.test.ts +0 -396
- package/src/__tests__/browser-auth.test.ts +0 -180
- package/src/__tests__/browser.test.ts +0 -632
- package/src/__tests__/define.test.ts +0 -225
- package/src/__tests__/errors.test.ts +0 -69
- package/src/__tests__/executor.test.ts +0 -214
- package/src/__tests__/http.test.ts +0 -238
- package/src/__tests__/insights.test.ts +0 -210
- package/src/__tests__/instrumentation.test.ts +0 -290
- package/src/__tests__/otlp.test.ts +0 -141
- package/src/__tests__/perf.test.ts +0 -60
- package/src/__tests__/providers-yaml.test.ts +0 -135
- package/src/__tests__/proxy.test.ts +0 -359
- package/src/__tests__/recipes.test.ts +0 -36
- package/src/__tests__/serve.test.ts +0 -233
- package/src/__tests__/session.test.ts +0 -231
- package/src/__tests__/state.test.ts +0 -100
- package/src/__tests__/stealth.test.ts +0 -57
- package/src/__tests__/testing.test.ts +0 -97
- package/src/__tests__/tls.test.ts +0 -345
- package/src/__tests__/types.test.ts +0 -142
- package/src/__tests__/utils.test.ts +0 -62
- package/src/__tests__/waterfall.test.ts +0 -270
- package/src/config/providers-yaml.ts +0 -370
- package/src/index.test.ts +0 -1
- package/src/protocol.ts +0 -183
- package/src/runtime/auth.ts +0 -245
- package/src/runtime/session.ts +0 -573
- package/src/runtime/state.ts +0 -124
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, mock } from "bun:test";
|
|
2
|
-
|
|
3
|
-
import { wrapWithInstrumentation } from "../runtime/instrumentation";
|
|
4
|
-
import { createTraceContext } from "../runtime/trace";
|
|
5
|
-
import type {
|
|
6
|
-
AuthContext,
|
|
7
|
-
BrowserClient,
|
|
8
|
-
HttpClient,
|
|
9
|
-
ProviderContext,
|
|
10
|
-
SessionStore,
|
|
11
|
-
StateContext,
|
|
12
|
-
TlsClient,
|
|
13
|
-
} from "../types";
|
|
14
|
-
|
|
15
|
-
function createMockContext(): ProviderContext {
|
|
16
|
-
const mockPage = {
|
|
17
|
-
pageId: "page-1",
|
|
18
|
-
goto: mock(async (url: string) => ({ url })),
|
|
19
|
-
fill: mock(async () => undefined),
|
|
20
|
-
click: mock(async () => undefined),
|
|
21
|
-
type: mock(async () => undefined),
|
|
22
|
-
waitForSelector: mock(async () => undefined),
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
http: {
|
|
27
|
-
request: mock(async () => ({
|
|
28
|
-
status: 200,
|
|
29
|
-
ok: true,
|
|
30
|
-
headers: { "content-type": "application/json" },
|
|
31
|
-
data: { ok: true },
|
|
32
|
-
meta: { requestId: "req-0", duration: 10 },
|
|
33
|
-
json: async <T>() => ({ ok: true }) as T,
|
|
34
|
-
text: async () => JSON.stringify({ ok: true }),
|
|
35
|
-
})),
|
|
36
|
-
get: mock(async () => ({
|
|
37
|
-
status: 200,
|
|
38
|
-
ok: true,
|
|
39
|
-
headers: { "content-type": "application/json" },
|
|
40
|
-
data: { ok: true },
|
|
41
|
-
meta: { requestId: "req-1", duration: 15 },
|
|
42
|
-
json: async <T>() => ({ ok: true }) as T,
|
|
43
|
-
text: async () => JSON.stringify({ ok: true }),
|
|
44
|
-
})),
|
|
45
|
-
post: mock(async () => ({
|
|
46
|
-
status: 200,
|
|
47
|
-
ok: true,
|
|
48
|
-
headers: { "content-type": "application/json" },
|
|
49
|
-
data: { ok: true },
|
|
50
|
-
meta: { requestId: "req-2", duration: 20 },
|
|
51
|
-
json: async <T>() => ({ ok: true }) as T,
|
|
52
|
-
text: async () => JSON.stringify({ ok: true }),
|
|
53
|
-
})),
|
|
54
|
-
put: mock(async () => ({
|
|
55
|
-
status: 200,
|
|
56
|
-
ok: true,
|
|
57
|
-
headers: { "content-type": "application/json" },
|
|
58
|
-
data: { ok: true },
|
|
59
|
-
meta: { requestId: "req-3", duration: 25 },
|
|
60
|
-
json: async <T>() => ({ ok: true }) as T,
|
|
61
|
-
text: async () => JSON.stringify({ ok: true }),
|
|
62
|
-
})),
|
|
63
|
-
delete: mock(async () => ({
|
|
64
|
-
status: 200,
|
|
65
|
-
ok: true,
|
|
66
|
-
headers: { "content-type": "application/json" },
|
|
67
|
-
data: { ok: true },
|
|
68
|
-
meta: { requestId: "req-4", duration: 30 },
|
|
69
|
-
json: async <T>() => ({ ok: true }) as T,
|
|
70
|
-
text: async () => JSON.stringify({ ok: true }),
|
|
71
|
-
})),
|
|
72
|
-
} as HttpClient,
|
|
73
|
-
tls: {
|
|
74
|
-
fetch: mock(async () => ({
|
|
75
|
-
status: 201,
|
|
76
|
-
ok: true,
|
|
77
|
-
headers: {},
|
|
78
|
-
rawHeaders: [] as [string, string][],
|
|
79
|
-
body: "created",
|
|
80
|
-
cookies: {
|
|
81
|
-
get: () => undefined,
|
|
82
|
-
getAll: () => ({}),
|
|
83
|
-
toString: () => "",
|
|
84
|
-
},
|
|
85
|
-
json: async <T>() => ({}) as T,
|
|
86
|
-
})),
|
|
87
|
-
createSession: mock(() => ({
|
|
88
|
-
fetch: async () => ({
|
|
89
|
-
status: 200,
|
|
90
|
-
ok: true,
|
|
91
|
-
headers: {},
|
|
92
|
-
rawHeaders: [] as [string, string][],
|
|
93
|
-
body: "ok",
|
|
94
|
-
cookies: {
|
|
95
|
-
get: () => undefined,
|
|
96
|
-
getAll: () => ({}),
|
|
97
|
-
toString: () => "",
|
|
98
|
-
},
|
|
99
|
-
json: async <T>() => ({}) as T,
|
|
100
|
-
}),
|
|
101
|
-
close: () => {},
|
|
102
|
-
})),
|
|
103
|
-
} as TlsClient,
|
|
104
|
-
browser: {
|
|
105
|
-
engine: "playwright-stealth",
|
|
106
|
-
newPage: mock(async () => mockPage),
|
|
107
|
-
goto: mock(async (url: string) => ({ url })),
|
|
108
|
-
} as unknown as BrowserClient,
|
|
109
|
-
session: {} as SessionStore,
|
|
110
|
-
state: {} as StateContext,
|
|
111
|
-
trace: createTraceContext(),
|
|
112
|
-
auth: {} as AuthContext,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
describe("createTraceContext", () => {
|
|
117
|
-
it("collects nested custom spans in start order", async () => {
|
|
118
|
-
const trace = createTraceContext();
|
|
119
|
-
|
|
120
|
-
const result = await trace.span("operation", async () => {
|
|
121
|
-
return trace.span("parse", async () => "done");
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
const spans = trace.getSpans();
|
|
125
|
-
|
|
126
|
-
expect(result).toBe("done");
|
|
127
|
-
expect(spans).toHaveLength(2);
|
|
128
|
-
expect(spans[0]?.name).toBe("operation");
|
|
129
|
-
expect(spans[1]?.name).toBe("parse");
|
|
130
|
-
expect(spans[1]?.parentId).toBe(spans[0]?.id);
|
|
131
|
-
expect(spans[0]?.status).toBe("ok");
|
|
132
|
-
expect(spans[1]?.attributes.duration_ms).toBeNumber();
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it("enforces maxSpans by trimming the oldest completed spans", async () => {
|
|
136
|
-
const trace = createTraceContext({ maxSpans: 2 });
|
|
137
|
-
|
|
138
|
-
await trace.span("first", async () => undefined);
|
|
139
|
-
await trace.span("second", async () => undefined);
|
|
140
|
-
await trace.span("third", async () => undefined);
|
|
141
|
-
|
|
142
|
-
expect(trace.getSpans().map((span) => span.name)).toEqual([
|
|
143
|
-
"second",
|
|
144
|
-
"third",
|
|
145
|
-
]);
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
describe("wrapWithInstrumentation", () => {
|
|
150
|
-
it("creates spans for http, tls, and browser method calls", async () => {
|
|
151
|
-
const onSpan = mock(() => {});
|
|
152
|
-
const ctx = createMockContext();
|
|
153
|
-
const instrumented = wrapWithInstrumentation(ctx, { onSpan });
|
|
154
|
-
|
|
155
|
-
await instrumented.trace.span("provider.search", async () => {
|
|
156
|
-
await instrumented.http.get("https://api.example.com/items");
|
|
157
|
-
await instrumented.tls.fetch("https://secure.example.com/login", {
|
|
158
|
-
method: "POST",
|
|
159
|
-
});
|
|
160
|
-
await (
|
|
161
|
-
instrumented.browser as BrowserClient & {
|
|
162
|
-
goto(url: string): Promise<{ url: string }>;
|
|
163
|
-
}
|
|
164
|
-
).goto("https://app.example.com/dashboard");
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
const spans = instrumented.trace.getSpans();
|
|
168
|
-
|
|
169
|
-
expect(spans.map((span) => span.name)).toEqual([
|
|
170
|
-
"provider.search",
|
|
171
|
-
"http.get",
|
|
172
|
-
"tls.fetch",
|
|
173
|
-
"browser.goto",
|
|
174
|
-
]);
|
|
175
|
-
|
|
176
|
-
const httpSpan = spans[1];
|
|
177
|
-
expect(httpSpan).toMatchObject({
|
|
178
|
-
status: "ok",
|
|
179
|
-
parentId: spans[0]?.id,
|
|
180
|
-
attributes: {
|
|
181
|
-
url: "https://api.example.com/items",
|
|
182
|
-
method: "GET",
|
|
183
|
-
status: 200,
|
|
184
|
-
duration_ms: 15,
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
const tlsSpan = spans[2];
|
|
189
|
-
expect(tlsSpan).toMatchObject({
|
|
190
|
-
status: "ok",
|
|
191
|
-
parentId: spans[0]?.id,
|
|
192
|
-
attributes: {
|
|
193
|
-
url: "https://secure.example.com/login",
|
|
194
|
-
method: "POST",
|
|
195
|
-
status: 201,
|
|
196
|
-
},
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
const browserSpan = spans[3];
|
|
200
|
-
expect(browserSpan).toMatchObject({
|
|
201
|
-
status: "ok",
|
|
202
|
-
parentId: spans[0]?.id,
|
|
203
|
-
attributes: {
|
|
204
|
-
url: "https://app.example.com/dashboard",
|
|
205
|
-
},
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
expect(onSpan).toHaveBeenCalledTimes(4);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it("wraps browser newPage and page methods with spans", async () => {
|
|
212
|
-
const ctx = createMockContext();
|
|
213
|
-
const instrumented = wrapWithInstrumentation(ctx);
|
|
214
|
-
|
|
215
|
-
const page = (await instrumented.browser.newPage()) as {
|
|
216
|
-
goto(url: string): Promise<{ url: string }>;
|
|
217
|
-
fill(selector: string, value: string): Promise<void>;
|
|
218
|
-
click(selector: string): Promise<void>;
|
|
219
|
-
type(selector: string, text: string): Promise<void>;
|
|
220
|
-
waitForSelector(selector: string): Promise<void>;
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
await page.goto("https://app.example.com/login");
|
|
224
|
-
await page.fill("#username", "demo");
|
|
225
|
-
await page.click("button[type=submit]");
|
|
226
|
-
await page.type("#otp", "123456");
|
|
227
|
-
await page.waitForSelector(".dashboard");
|
|
228
|
-
|
|
229
|
-
const spans = instrumented.trace.getSpans();
|
|
230
|
-
expect(spans.map((span) => span.name)).toEqual([
|
|
231
|
-
"browser.newPage",
|
|
232
|
-
"browser.page.goto",
|
|
233
|
-
"browser.page.fill",
|
|
234
|
-
"browser.page.click",
|
|
235
|
-
"browser.page.type",
|
|
236
|
-
"browser.page.waitForSelector",
|
|
237
|
-
]);
|
|
238
|
-
expect(spans[0]?.attributes).toMatchObject({
|
|
239
|
-
allocate_ms: expect.any(Number),
|
|
240
|
-
page_id: "page-1",
|
|
241
|
-
engine: "playwright-stealth",
|
|
242
|
-
});
|
|
243
|
-
expect(spans[1]?.attributes).toMatchObject({
|
|
244
|
-
url: "https://app.example.com/login",
|
|
245
|
-
navigation_ms: expect.any(Number),
|
|
246
|
-
});
|
|
247
|
-
expect(spans[2]?.attributes).toMatchObject({
|
|
248
|
-
selector: "#username",
|
|
249
|
-
action_ms: expect.any(Number),
|
|
250
|
-
});
|
|
251
|
-
expect(spans[3]?.attributes).toMatchObject({
|
|
252
|
-
selector: "button[type=submit]",
|
|
253
|
-
action_ms: expect.any(Number),
|
|
254
|
-
});
|
|
255
|
-
expect(spans[4]?.attributes).toMatchObject({
|
|
256
|
-
selector: "#otp",
|
|
257
|
-
action_ms: expect.any(Number),
|
|
258
|
-
});
|
|
259
|
-
expect(spans[5]?.attributes).toMatchObject({
|
|
260
|
-
selector: ".dashboard",
|
|
261
|
-
wait_ms: expect.any(Number),
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
it("records error spans when instrumented methods throw", async () => {
|
|
266
|
-
const ctx = createMockContext();
|
|
267
|
-
ctx.tls.fetch = mock(async () => {
|
|
268
|
-
const error = new Error("boom") as Error & { status: number };
|
|
269
|
-
error.status = 503;
|
|
270
|
-
throw error;
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
const instrumented = wrapWithInstrumentation(ctx);
|
|
274
|
-
|
|
275
|
-
await expect(
|
|
276
|
-
instrumented.tls.fetch("https://secure.example.com/fail"),
|
|
277
|
-
).rejects.toThrow("boom");
|
|
278
|
-
|
|
279
|
-
expect(instrumented.trace.getSpans()[0]).toMatchObject({
|
|
280
|
-
name: "tls.fetch",
|
|
281
|
-
status: "error",
|
|
282
|
-
error: "boom",
|
|
283
|
-
attributes: {
|
|
284
|
-
url: "https://secure.example.com/fail",
|
|
285
|
-
method: "GET",
|
|
286
|
-
status: 503,
|
|
287
|
-
},
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
});
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
|
|
2
|
-
|
|
3
|
-
import { exportSpansOTLP, spansToOTLP } from "../runtime/otlp";
|
|
4
|
-
import type { TraceSpan } from "../types";
|
|
5
|
-
|
|
6
|
-
function makeSpan(overrides: Partial<TraceSpan> = {}): TraceSpan {
|
|
7
|
-
return {
|
|
8
|
-
id: overrides.id ?? "span-1",
|
|
9
|
-
name: overrides.name ?? "provider.search",
|
|
10
|
-
startedAt: overrides.startedAt ?? 1_000,
|
|
11
|
-
endedAt: overrides.endedAt ?? 1_025,
|
|
12
|
-
duration_ms: overrides.duration_ms ?? 25,
|
|
13
|
-
status: overrides.status ?? "ok",
|
|
14
|
-
attributes: overrides.attributes ?? {
|
|
15
|
-
method: "GET",
|
|
16
|
-
status: 200,
|
|
17
|
-
success: true,
|
|
18
|
-
},
|
|
19
|
-
...(overrides.parentId ? { parentId: overrides.parentId } : {}),
|
|
20
|
-
...(overrides.error ? { error: overrides.error } : {}),
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
describe("otlp export", () => {
|
|
25
|
-
let originalFetch: typeof fetch;
|
|
26
|
-
let originalWarn: typeof console.warn;
|
|
27
|
-
|
|
28
|
-
beforeEach(() => {
|
|
29
|
-
originalFetch = global.fetch;
|
|
30
|
-
originalWarn = console.warn;
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
afterEach(() => {
|
|
34
|
-
global.fetch = originalFetch;
|
|
35
|
-
console.warn = originalWarn;
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("spansToOTLP() returns OTLP resource and scope span payload", () => {
|
|
39
|
-
const payload = spansToOTLP(
|
|
40
|
-
[makeSpan({ id: "abc123", parentId: "def456", name: "tls.fetch" })],
|
|
41
|
-
{ "service.name": "provider-sdk-test" },
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
expect(payload).toEqual({
|
|
45
|
-
resourceSpans: [
|
|
46
|
-
{
|
|
47
|
-
resource: {
|
|
48
|
-
attributes: [
|
|
49
|
-
{
|
|
50
|
-
key: "service.name",
|
|
51
|
-
value: { stringValue: "provider-sdk-test" },
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
},
|
|
55
|
-
scopeSpans: [
|
|
56
|
-
{
|
|
57
|
-
scope: {
|
|
58
|
-
name: "apifuse-provider-sdk",
|
|
59
|
-
version: "0.1.0",
|
|
60
|
-
},
|
|
61
|
-
spans: [
|
|
62
|
-
{
|
|
63
|
-
attributes: [
|
|
64
|
-
{
|
|
65
|
-
key: "method",
|
|
66
|
-
value: { stringValue: "GET" },
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
key: "status",
|
|
70
|
-
value: { doubleValue: 200 },
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
key: "success",
|
|
74
|
-
value: { boolValue: true },
|
|
75
|
-
},
|
|
76
|
-
],
|
|
77
|
-
endTimeUnixNano: "1025000000",
|
|
78
|
-
kind: 2,
|
|
79
|
-
name: "tls.fetch",
|
|
80
|
-
parentSpanId: "0000000000def456",
|
|
81
|
-
spanId: "0000000000abc123",
|
|
82
|
-
startTimeUnixNano: "1000000000",
|
|
83
|
-
status: { code: 1 },
|
|
84
|
-
traceId: "00000000000000000000000000000001",
|
|
85
|
-
},
|
|
86
|
-
],
|
|
87
|
-
},
|
|
88
|
-
],
|
|
89
|
-
},
|
|
90
|
-
],
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("exportSpansOTLP() posts OTLP JSON with merged headers", async () => {
|
|
95
|
-
let requestUrl: string | undefined;
|
|
96
|
-
let requestInit: RequestInit | undefined;
|
|
97
|
-
|
|
98
|
-
global.fetch = mock(
|
|
99
|
-
async (url: string | URL | Request, init?: RequestInit) => {
|
|
100
|
-
requestUrl = typeof url === "string" ? url : url.toString();
|
|
101
|
-
requestInit = init;
|
|
102
|
-
return new Response(null, { status: 200 });
|
|
103
|
-
},
|
|
104
|
-
) as unknown as typeof fetch;
|
|
105
|
-
|
|
106
|
-
await exportSpansOTLP([makeSpan()], {
|
|
107
|
-
endpoint: "http://localhost:4318/v1/traces",
|
|
108
|
-
headers: { Authorization: "Bearer test" },
|
|
109
|
-
timeout: 100,
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
expect(requestUrl).toBe("http://localhost:4318/v1/traces");
|
|
113
|
-
expect(requestInit?.method).toBe("POST");
|
|
114
|
-
expect(requestInit?.headers).toEqual({
|
|
115
|
-
"Content-Type": "application/json",
|
|
116
|
-
Authorization: "Bearer test",
|
|
117
|
-
});
|
|
118
|
-
expect(JSON.parse(String(requestInit?.body))).toEqual(
|
|
119
|
-
spansToOTLP([makeSpan()]),
|
|
120
|
-
);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("exportSpansOTLP() swallows fetch errors and warns", async () => {
|
|
124
|
-
const warn = mock(() => {});
|
|
125
|
-
console.warn = warn;
|
|
126
|
-
global.fetch = mock(async () => {
|
|
127
|
-
throw new Error("network down");
|
|
128
|
-
}) as unknown as typeof fetch;
|
|
129
|
-
|
|
130
|
-
await expect(
|
|
131
|
-
exportSpansOTLP([makeSpan()], {
|
|
132
|
-
endpoint: "http://localhost:4318/v1/traces",
|
|
133
|
-
}),
|
|
134
|
-
).resolves.toBeUndefined();
|
|
135
|
-
|
|
136
|
-
expect(warn).toHaveBeenCalledWith(
|
|
137
|
-
"[apifuse] OTLP export failed:",
|
|
138
|
-
"network down",
|
|
139
|
-
);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "bun:test";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
computePercentile,
|
|
5
|
-
computeStats,
|
|
6
|
-
groupSpansByName,
|
|
7
|
-
} from "../runtime/perf";
|
|
8
|
-
import type { Span } from "../runtime/trace";
|
|
9
|
-
|
|
10
|
-
function makeSpan(name: string, duration_ms: number): Span {
|
|
11
|
-
return {
|
|
12
|
-
id: crypto.randomUUID(),
|
|
13
|
-
name,
|
|
14
|
-
startedAt: 1000,
|
|
15
|
-
endedAt: 1000 + duration_ms,
|
|
16
|
-
duration_ms,
|
|
17
|
-
status: "ok",
|
|
18
|
-
attributes: {},
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
describe("computePercentile", () => {
|
|
23
|
-
it("computes p50 correctly", () => {
|
|
24
|
-
expect(computePercentile([10, 20, 30, 40, 50], 50)).toBe(30);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("computes p95 correctly", () => {
|
|
28
|
-
expect(computePercentile([10, 20, 30, 40, 50], 95)).toBe(48);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("handles single element", () => {
|
|
32
|
-
expect(computePercentile([42], 99)).toBe(42);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("computeStats", () => {
|
|
37
|
-
it("returns p50/p95/p99/avg for sample data", () => {
|
|
38
|
-
expect(computeStats([10, 20, 30, 40, 50])).toEqual({
|
|
39
|
-
p50: 30,
|
|
40
|
-
p95: 48,
|
|
41
|
-
p99: 49.6,
|
|
42
|
-
avg: 30,
|
|
43
|
-
min: 10,
|
|
44
|
-
max: 50,
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe("groupSpansByName", () => {
|
|
50
|
-
it("groups spans across multiple runs", () => {
|
|
51
|
-
const grouped = groupSpansByName([
|
|
52
|
-
[makeSpan("tls.fetch", 100), makeSpan("transformResponse", 5)],
|
|
53
|
-
[makeSpan("tls.fetch", 120), makeSpan("normalizeRequest", 1)],
|
|
54
|
-
]);
|
|
55
|
-
|
|
56
|
-
expect(grouped.get("tls.fetch")).toEqual([100, 120]);
|
|
57
|
-
expect(grouped.get("transformResponse")).toEqual([5]);
|
|
58
|
-
expect(grouped.get("normalizeRequest")).toEqual([1]);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it } from "bun:test";
|
|
2
|
-
import { mkdtempSync } from "node:fs";
|
|
3
|
-
import { rm } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import path from "node:path";
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
loadProvidersYaml,
|
|
9
|
-
parseProvidersYaml,
|
|
10
|
-
resolveProviderConfig,
|
|
11
|
-
} from "../config/providers-yaml";
|
|
12
|
-
|
|
13
|
-
const exampleYaml = `
|
|
14
|
-
defaults:
|
|
15
|
-
container:
|
|
16
|
-
image: auto
|
|
17
|
-
memory: 256Mi
|
|
18
|
-
cpu: 250m
|
|
19
|
-
timeout: 30s
|
|
20
|
-
restart-policy: on-failure
|
|
21
|
-
max-restarts: 3
|
|
22
|
-
security:
|
|
23
|
-
read-only-rootfs: true
|
|
24
|
-
no-new-privileges: true
|
|
25
|
-
|
|
26
|
-
providers:
|
|
27
|
-
coingecko: {}
|
|
28
|
-
catchtable:
|
|
29
|
-
container:
|
|
30
|
-
memory: 1Gi
|
|
31
|
-
cpu: 1000m
|
|
32
|
-
replicas: 2
|
|
33
|
-
security:
|
|
34
|
-
egress: ['ct-api.catchtable.co.kr']
|
|
35
|
-
env: ['CATCHTABLE_PROXY_URL']
|
|
36
|
-
bank-login:
|
|
37
|
-
runtime: browser
|
|
38
|
-
container:
|
|
39
|
-
memory: 2Gi
|
|
40
|
-
cdp:
|
|
41
|
-
pool-size: 2..10
|
|
42
|
-
page-timeout: 120s
|
|
43
|
-
`;
|
|
44
|
-
|
|
45
|
-
const tempDirs: string[] = [];
|
|
46
|
-
|
|
47
|
-
afterEach(async () => {
|
|
48
|
-
await Promise.all(
|
|
49
|
-
tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })),
|
|
50
|
-
);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe("providers.yaml", () => {
|
|
54
|
-
it("parses the documented example and normalizes pool-size", () => {
|
|
55
|
-
const parsed = parseProvidersYaml(exampleYaml);
|
|
56
|
-
|
|
57
|
-
expect(parsed.defaults?.container?.memory).toBe("256Mi");
|
|
58
|
-
expect(parsed.providers.coingecko).toEqual({});
|
|
59
|
-
expect(parsed.providers["bank-login"].cdp?.["pool-size"]).toEqual({
|
|
60
|
-
min: 2,
|
|
61
|
-
max: 10,
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("resolves defaults for empty provider config", () => {
|
|
66
|
-
const parsed = parseProvidersYaml(exampleYaml);
|
|
67
|
-
const resolved = resolveProviderConfig(parsed, "coingecko");
|
|
68
|
-
|
|
69
|
-
expect(resolved.runtime).toBe("standard");
|
|
70
|
-
expect(resolved.replicas).toBe(1);
|
|
71
|
-
expect(resolved.container).toEqual({
|
|
72
|
-
image: "auto",
|
|
73
|
-
memory: "256Mi",
|
|
74
|
-
cpu: "250m",
|
|
75
|
-
timeout: "30s",
|
|
76
|
-
"restart-policy": "on-failure",
|
|
77
|
-
"max-restarts": 3,
|
|
78
|
-
});
|
|
79
|
-
expect(resolved.security).toEqual({
|
|
80
|
-
"read-only-rootfs": true,
|
|
81
|
-
"no-new-privileges": true,
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("merges provider overrides with defaults", () => {
|
|
86
|
-
const parsed = parseProvidersYaml(exampleYaml);
|
|
87
|
-
const resolved = resolveProviderConfig(parsed, "catchtable");
|
|
88
|
-
|
|
89
|
-
expect(resolved.replicas).toBe(2);
|
|
90
|
-
expect(resolved.container.memory).toBe("1Gi");
|
|
91
|
-
expect(resolved.container.cpu).toBe("1000m");
|
|
92
|
-
expect(resolved.container.timeout).toBe("30s");
|
|
93
|
-
expect(resolved.security.egress).toEqual(["ct-api.catchtable.co.kr"]);
|
|
94
|
-
expect(resolved.security.env).toEqual(["CATCHTABLE_PROXY_URL"]);
|
|
95
|
-
expect(resolved.security["read-only-rootfs"]).toBe(true);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("applies browser cdp defaults while preserving parsed pool range", () => {
|
|
99
|
-
const parsed = parseProvidersYaml(exampleYaml);
|
|
100
|
-
const resolved = resolveProviderConfig(parsed, "bank-login");
|
|
101
|
-
|
|
102
|
-
expect(resolved.runtime).toBe("browser");
|
|
103
|
-
expect(resolved.container.memory).toBe("2Gi");
|
|
104
|
-
expect(resolved.cdp).toEqual({
|
|
105
|
-
"pool-size": { min: 2, max: 10 },
|
|
106
|
-
"page-timeout": "120s",
|
|
107
|
-
"pages-per-instance": 4,
|
|
108
|
-
"idle-timeout": "5m",
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("loads providers yaml from disk", async () => {
|
|
113
|
-
const dir = mkdtempSync(path.join(tmpdir(), "providers-yaml-"));
|
|
114
|
-
tempDirs.push(dir);
|
|
115
|
-
const filePath = path.join(dir, "providers.yaml");
|
|
116
|
-
|
|
117
|
-
await Bun.write(filePath, exampleYaml);
|
|
118
|
-
|
|
119
|
-
const loaded = await loadProvidersYaml(filePath);
|
|
120
|
-
|
|
121
|
-
expect(loaded.providers.catchtable.replicas).toBe(2);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("rejects invalid pool-size ranges", () => {
|
|
125
|
-
expect(() =>
|
|
126
|
-
parseProvidersYaml(`
|
|
127
|
-
providers:
|
|
128
|
-
bank-login:
|
|
129
|
-
runtime: browser
|
|
130
|
-
cdp:
|
|
131
|
-
pool-size: 10..2
|
|
132
|
-
`),
|
|
133
|
-
).toThrow("Invalid providers.yaml configuration");
|
|
134
|
-
});
|
|
135
|
-
});
|