@fedify/testing 2.0.0-dev.1561 → 2.0.0-dev.158
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/LICENSE +1 -1
- package/README.md +1 -1
- package/dist/mod.cjs +733 -0
- package/dist/mod.d.cts +126 -0
- package/dist/mod.d.ts +83 -254
- package/dist/mod.js +111 -41
- package/package.json +10 -8
package/dist/mod.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { trace } from "@opentelemetry/api";
|
|
2
1
|
import { RouterError } from "@fedify/fedify/federation";
|
|
3
|
-
import { lookupObject, traverseCollection } from "@fedify/
|
|
4
|
-
import { lookupWebFinger } from "@fedify/fedify/webfinger";
|
|
2
|
+
import { lookupObject, traverseCollection } from "@fedify/vocab";
|
|
5
3
|
|
|
6
4
|
//#region src/docloader.ts
|
|
7
5
|
const mockDocumentLoader = async (url) => ({
|
|
@@ -12,8 +10,12 @@ const mockDocumentLoader = async (url) => ({
|
|
|
12
10
|
|
|
13
11
|
//#endregion
|
|
14
12
|
//#region src/context.ts
|
|
13
|
+
const noopTracerProvider$1 = { getTracer: () => ({
|
|
14
|
+
startActiveSpan: () => void 0,
|
|
15
|
+
startSpan: () => void 0
|
|
16
|
+
}) };
|
|
15
17
|
function createContext(values) {
|
|
16
|
-
const { federation, url = new URL("http://example.com/"), canonicalOrigin, data, documentLoader, contextLoader, tracerProvider, clone, getNodeInfoUri, getActorUri, getObjectUri, getCollectionUri, getOutboxUri, getInboxUri, getFollowingUri, getFollowersUri, getLikedUri, getFeaturedUri, getFeaturedTagsUri, parseUri, getActorKeyPairs, getDocumentLoader, lookupObject: lookupObject$1, traverseCollection: traverseCollection$1, lookupNodeInfo, lookupWebFinger
|
|
18
|
+
const { federation, url = new URL("http://example.com/"), canonicalOrigin, data, documentLoader, contextLoader, tracerProvider, clone, getNodeInfoUri, getActorUri, getObjectUri, getCollectionUri, getOutboxUri, getInboxUri, getFollowingUri, getFollowersUri, getLikedUri, getFeaturedUri, getFeaturedTagsUri, parseUri, getActorKeyPairs, getDocumentLoader, lookupObject: lookupObject$1, traverseCollection: traverseCollection$1, lookupNodeInfo, lookupWebFinger, sendActivity, routeActivity } = values;
|
|
17
19
|
function throwRouteError() {
|
|
18
20
|
throw new RouterError("Not implemented");
|
|
19
21
|
}
|
|
@@ -26,7 +28,7 @@ function createContext(values) {
|
|
|
26
28
|
hostname: url.hostname,
|
|
27
29
|
documentLoader: documentLoader ?? mockDocumentLoader,
|
|
28
30
|
contextLoader: contextLoader ?? mockDocumentLoader,
|
|
29
|
-
tracerProvider: tracerProvider ??
|
|
31
|
+
tracerProvider: tracerProvider ?? noopTracerProvider$1,
|
|
30
32
|
clone: clone ?? ((data$1) => createContext({
|
|
31
33
|
...values,
|
|
32
34
|
data: data$1
|
|
@@ -64,8 +66,8 @@ function createContext(values) {
|
|
|
64
66
|
lookupNodeInfo: lookupNodeInfo ?? ((_params) => {
|
|
65
67
|
throw new Error("Not implemented");
|
|
66
68
|
}),
|
|
67
|
-
lookupWebFinger: lookupWebFinger
|
|
68
|
-
return
|
|
69
|
+
lookupWebFinger: lookupWebFinger ?? ((_resource, _options = {}) => {
|
|
70
|
+
return Promise.resolve(null);
|
|
69
71
|
}),
|
|
70
72
|
sendActivity: sendActivity ?? ((_params) => {
|
|
71
73
|
throw new Error("Not implemented");
|
|
@@ -75,6 +77,13 @@ function createContext(values) {
|
|
|
75
77
|
})
|
|
76
78
|
};
|
|
77
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Creates a RequestContext for testing purposes.
|
|
82
|
+
* Not exported - used internally only. Public API is in mock.ts
|
|
83
|
+
* @param args Partial RequestContext properties
|
|
84
|
+
* @returns A RequestContext instance
|
|
85
|
+
* @since 1.8.0
|
|
86
|
+
*/
|
|
78
87
|
function createRequestContext(args) {
|
|
79
88
|
return {
|
|
80
89
|
...createContext(args),
|
|
@@ -93,6 +102,13 @@ function createRequestContext(args) {
|
|
|
93
102
|
})
|
|
94
103
|
};
|
|
95
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Creates an InboxContext for testing purposes.
|
|
107
|
+
* Not exported - used internally only. Public API is in mock.ts
|
|
108
|
+
* @param args Partial InboxContext properties
|
|
109
|
+
* @returns An InboxContext instance
|
|
110
|
+
* @since 1.8.0
|
|
111
|
+
*/
|
|
96
112
|
function createInboxContext(args) {
|
|
97
113
|
return {
|
|
98
114
|
...createContext(args),
|
|
@@ -109,6 +125,10 @@ function createInboxContext(args) {
|
|
|
109
125
|
|
|
110
126
|
//#endregion
|
|
111
127
|
//#region src/mock.ts
|
|
128
|
+
const noopTracerProvider = { getTracer: () => ({
|
|
129
|
+
startActiveSpan: () => void 0,
|
|
130
|
+
startSpan: () => void 0
|
|
131
|
+
}) };
|
|
112
132
|
/**
|
|
113
133
|
* Helper function to expand URI templates with values.
|
|
114
134
|
* Supports simple placeholders like {identifier}, {handle}, etc.
|
|
@@ -128,18 +148,18 @@ function expandUriTemplate(template, values) {
|
|
|
128
148
|
*
|
|
129
149
|
* @example
|
|
130
150
|
* ```typescript
|
|
131
|
-
* import { Create } from "@fedify/
|
|
132
|
-
* import {
|
|
151
|
+
* import { Create } from "@fedify/vocab";
|
|
152
|
+
* import { createFederation } from "@fedify/testing";
|
|
133
153
|
*
|
|
134
154
|
* // Create a mock federation with contextData
|
|
135
|
-
* const federation =
|
|
155
|
+
* const federation = createFederation<{ userId: string }>({
|
|
136
156
|
* contextData: { userId: "test-user" }
|
|
137
157
|
* });
|
|
138
158
|
*
|
|
139
159
|
* // Set up inbox listeners
|
|
140
160
|
* federation
|
|
141
161
|
* .setInboxListeners("/users/{identifier}/inbox")
|
|
142
|
-
* .on(Create, async (ctx, activity) => {
|
|
162
|
+
* .on(Create, async (ctx: any, activity: any) => {
|
|
143
163
|
* console.log("Received:", activity);
|
|
144
164
|
* });
|
|
145
165
|
*
|
|
@@ -295,6 +315,9 @@ var MockFederation = class {
|
|
|
295
315
|
},
|
|
296
316
|
setSharedKeyDispatcher() {
|
|
297
317
|
return this;
|
|
318
|
+
},
|
|
319
|
+
withIdempotency() {
|
|
320
|
+
return this;
|
|
298
321
|
}
|
|
299
322
|
};
|
|
300
323
|
}
|
|
@@ -313,22 +336,9 @@ var MockFederation = class {
|
|
|
313
336
|
}
|
|
314
337
|
createContext(baseUrlOrRequest, contextData) {
|
|
315
338
|
const mockFederation = this;
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
data: contextData,
|
|
320
|
-
federation: mockFederation,
|
|
321
|
-
sendActivity: async (sender, recipients, activity, options) => {
|
|
322
|
-
const tempContext = new MockContext({
|
|
323
|
-
url: new URL(baseUrlOrRequest.url),
|
|
324
|
-
data: contextData,
|
|
325
|
-
federation: mockFederation
|
|
326
|
-
});
|
|
327
|
-
await tempContext.sendActivity(sender, recipients, activity, options);
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
else return new MockContext({
|
|
331
|
-
url: baseUrlOrRequest,
|
|
339
|
+
const url = baseUrlOrRequest instanceof Request ? new URL(baseUrlOrRequest.url) : baseUrlOrRequest;
|
|
340
|
+
return new MockContext({
|
|
341
|
+
url,
|
|
332
342
|
data: contextData,
|
|
333
343
|
federation: mockFederation
|
|
334
344
|
});
|
|
@@ -386,22 +396,66 @@ var MockFederation = class {
|
|
|
386
396
|
}
|
|
387
397
|
};
|
|
388
398
|
/**
|
|
399
|
+
* Creates a mock Federation instance for testing purposes.
|
|
400
|
+
*
|
|
401
|
+
* @template TContextData The type of context data to use
|
|
402
|
+
* @param options Optional configuration for the mock federation
|
|
403
|
+
* @returns A Federation instance that can be used for testing
|
|
404
|
+
* @since 1.9.1
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```typescript
|
|
408
|
+
* import { Create } from "@fedify/vocab";
|
|
409
|
+
* import { createFederation } from "@fedify/testing";
|
|
410
|
+
*
|
|
411
|
+
* // Create a mock federation with contextData
|
|
412
|
+
* const federation = createFederation<{ userId: string }>({
|
|
413
|
+
* contextData: { userId: "test-user" }
|
|
414
|
+
* });
|
|
415
|
+
*
|
|
416
|
+
* // Set up inbox listeners
|
|
417
|
+
* federation
|
|
418
|
+
* .setInboxListeners("/users/{identifier}/inbox")
|
|
419
|
+
* .on(Create, async (ctx, activity) => {
|
|
420
|
+
* console.log("Received:", activity);
|
|
421
|
+
* });
|
|
422
|
+
*
|
|
423
|
+
* // Simulate receiving an activity
|
|
424
|
+
* const createActivity = new Create({
|
|
425
|
+
* id: new URL("https://example.com/create/1"),
|
|
426
|
+
* actor: new URL("https://example.com/users/alice")
|
|
427
|
+
* });
|
|
428
|
+
* await federation.receiveActivity(createActivity);
|
|
429
|
+
*
|
|
430
|
+
* // Check sent activities
|
|
431
|
+
* console.log(federation.sentActivities);
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
function createFederation(options = {}) {
|
|
435
|
+
return new MockFederation(options);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
389
438
|
* A mock implementation of the {@link Context} interface for unit testing.
|
|
390
439
|
* This class provides a way to test Fedify applications without needing
|
|
391
440
|
* a real federation context.
|
|
392
441
|
*
|
|
442
|
+
* Note: This class is not exported from the public API to avoid JSR type
|
|
443
|
+
* analyzer issues. The MockContext class has complex type dependencies that
|
|
444
|
+
* can cause JSR's type analyzer to hang during processing (issue #468).
|
|
445
|
+
* Use {@link MockFederation.createContext}, {@link createContext},
|
|
446
|
+
* {@link createRequestContext}, or {@link createInboxContext} instead.
|
|
447
|
+
*
|
|
393
448
|
* @example
|
|
394
449
|
* ```typescript
|
|
395
|
-
* import { Person, Create } from "@fedify/
|
|
396
|
-
* import {
|
|
450
|
+
* import { Person, Create } from "@fedify/vocab";
|
|
451
|
+
* import { createFederation } from "@fedify/testing";
|
|
397
452
|
*
|
|
398
|
-
* // Create a mock context
|
|
399
|
-
* const
|
|
400
|
-
* const context =
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
* });
|
|
453
|
+
* // Create a mock federation and context
|
|
454
|
+
* const federation = createFederation<{ userId: string }>();
|
|
455
|
+
* const context = federation.createContext(
|
|
456
|
+
* new URL("https://example.com"),
|
|
457
|
+
* { userId: "test-user" }
|
|
458
|
+
* );
|
|
405
459
|
*
|
|
406
460
|
* // Send an activity
|
|
407
461
|
* const recipient = new Person({ id: new URL("https://example.com/users/bob") });
|
|
@@ -415,8 +469,8 @@ var MockFederation = class {
|
|
|
415
469
|
* activity
|
|
416
470
|
* );
|
|
417
471
|
*
|
|
418
|
-
* // Check sent activities
|
|
419
|
-
* const sent =
|
|
472
|
+
* // Check sent activities from the federation
|
|
473
|
+
* const sent = federation.sentActivities;
|
|
420
474
|
* console.log(sent[0].activity);
|
|
421
475
|
* ```
|
|
422
476
|
*
|
|
@@ -433,6 +487,8 @@ var MockContext = class MockContext {
|
|
|
433
487
|
documentLoader;
|
|
434
488
|
contextLoader;
|
|
435
489
|
tracerProvider;
|
|
490
|
+
request;
|
|
491
|
+
url;
|
|
436
492
|
sentActivities = [];
|
|
437
493
|
constructor(options) {
|
|
438
494
|
const url = options.url ?? new URL("https://example.com");
|
|
@@ -440,6 +496,8 @@ var MockContext = class MockContext {
|
|
|
440
496
|
this.canonicalOrigin = url.origin;
|
|
441
497
|
this.host = url.host;
|
|
442
498
|
this.hostname = url.hostname;
|
|
499
|
+
this.url = url;
|
|
500
|
+
this.request = new Request(url);
|
|
443
501
|
this.data = options.data;
|
|
444
502
|
this.federation = options.federation;
|
|
445
503
|
this.documentLoader = options.documentLoader ?? (async (url$1) => ({
|
|
@@ -448,11 +506,23 @@ var MockContext = class MockContext {
|
|
|
448
506
|
documentUrl: url$1
|
|
449
507
|
}));
|
|
450
508
|
this.contextLoader = options.contextLoader ?? this.documentLoader;
|
|
451
|
-
this.tracerProvider = options.tracerProvider ??
|
|
509
|
+
this.tracerProvider = options.tracerProvider ?? noopTracerProvider;
|
|
510
|
+
}
|
|
511
|
+
getActor(_handle) {
|
|
512
|
+
return Promise.resolve(null);
|
|
513
|
+
}
|
|
514
|
+
getObject(_cls, _values) {
|
|
515
|
+
return Promise.resolve(null);
|
|
516
|
+
}
|
|
517
|
+
getSignedKey() {
|
|
518
|
+
return Promise.resolve(null);
|
|
519
|
+
}
|
|
520
|
+
getSignedKeyOwner() {
|
|
521
|
+
return Promise.resolve(null);
|
|
452
522
|
}
|
|
453
523
|
clone(data) {
|
|
454
524
|
return new MockContext({
|
|
455
|
-
url:
|
|
525
|
+
url: this.url,
|
|
456
526
|
data,
|
|
457
527
|
federation: this.federation,
|
|
458
528
|
documentLoader: this.documentLoader,
|
|
@@ -634,4 +704,4 @@ var MockContext = class MockContext {
|
|
|
634
704
|
};
|
|
635
705
|
|
|
636
706
|
//#endregion
|
|
637
|
-
export {
|
|
707
|
+
export { createContext, createFederation, createInboxContext, createRequestContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/testing",
|
|
3
|
-
"version": "2.0.0-dev.
|
|
3
|
+
"version": "2.0.0-dev.158+628cd89e",
|
|
4
4
|
"description": "Testing utilities for Fedify applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fedify",
|
|
@@ -29,13 +29,18 @@
|
|
|
29
29
|
"https://github.com/sponsors/dahlia"
|
|
30
30
|
],
|
|
31
31
|
"type": "module",
|
|
32
|
-
"main": "./dist/mod.
|
|
32
|
+
"main": "./dist/mod.cjs",
|
|
33
33
|
"module": "./dist/mod.js",
|
|
34
34
|
"types": "./dist/mod.d.ts",
|
|
35
35
|
"exports": {
|
|
36
36
|
".": {
|
|
37
|
-
"types":
|
|
37
|
+
"types": {
|
|
38
|
+
"import": "./dist/mod.d.ts",
|
|
39
|
+
"require": "./dist/mod.d.cts",
|
|
40
|
+
"default": "./dist/mod.d.ts"
|
|
41
|
+
},
|
|
38
42
|
"import": "./dist/mod.js",
|
|
43
|
+
"require": "./dist/mod.cjs",
|
|
39
44
|
"default": "./dist/mod.js"
|
|
40
45
|
},
|
|
41
46
|
"./package.json": "./package.json"
|
|
@@ -45,17 +50,14 @@
|
|
|
45
50
|
"package.json"
|
|
46
51
|
],
|
|
47
52
|
"peerDependencies": {
|
|
48
|
-
"@fedify/fedify": "2.0.0-dev.
|
|
49
|
-
},
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"@opentelemetry/api": "^1.9.0"
|
|
53
|
+
"@fedify/fedify": "^2.0.0-dev.158+628cd89e"
|
|
52
54
|
},
|
|
53
55
|
"devDependencies": {
|
|
54
56
|
"@js-temporal/polyfill": "^0.5.1",
|
|
55
57
|
"@std/assert": "npm:@jsr/std__assert@^1.0.13",
|
|
56
58
|
"@std/async": "npm:@jsr/std__async@^1.0.13",
|
|
57
59
|
"tsdown": "^0.12.9",
|
|
58
|
-
"typescript": "^5.9.
|
|
60
|
+
"typescript": "^5.9.3"
|
|
59
61
|
},
|
|
60
62
|
"scripts": {
|
|
61
63
|
"build": "tsdown",
|