@fedify/testing 1.8.13 → 1.8.14-dev.1853

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.
Files changed (3) hide show
  1. package/dist/mod.d.ts +47 -146
  2. package/dist/mod.js +57 -30
  3. package/package.json +2 -5
package/dist/mod.d.ts CHANGED
@@ -1,12 +1,38 @@
1
- import { TracerProvider } from "@opentelemetry/api";
2
- import { ActorCallbackSetters, ActorDispatcher, ActorKeyPair, CollectionCallbackSetters, CollectionDispatcher, Context, Federation, FederationFetchOptions, FederationStartQueueOptions, InboxContext, InboxListenerSetters, Message, NodeInfoDispatcher, ObjectCallbackSetters, ObjectDispatcher, ParseUriResult, RequestContext, RouteActivityOptions, SendActivityOptions, SendActivityOptionsForCollection, SenderKeyPair } from "@fedify/fedify/federation";
3
- import { Activity, Actor, Collection, Hashtag, LookupObjectOptions, Object as Object$1, Recipient, TraverseCollectionOptions } from "@fedify/fedify/vocab";
4
- import { ResourceDescriptor } from "@fedify/fedify/webfinger";
5
- import { JsonValue, NodeInfo } from "@fedify/fedify/nodeinfo";
6
- import { DocumentLoader } from "@fedify/fedify/runtime";
1
+ import { ActorCallbackSetters, ActorDispatcher, CollectionCallbackSetters, CollectionDispatcher, Context, Federation, FederationFetchOptions, FederationStartQueueOptions, InboxContext, InboxListenerSetters, Message, NodeInfoDispatcher, ObjectCallbackSetters, ObjectDispatcher, RequestContext } from "@fedify/fedify/federation";
2
+ import { Activity, Actor, Hashtag, Object as Object$1, Recipient } from "@fedify/fedify/vocab";
3
+ import "@fedify/fedify/runtime";
7
4
 
5
+ //#region src/context.d.ts
6
+ declare function createContext<TContextData>(values: Partial<Context<TContextData>> & {
7
+ url?: URL;
8
+ data: TContextData;
9
+ federation: Federation<TContextData>;
10
+ }): Context<TContextData>;
11
+ /**
12
+ * Creates a RequestContext for testing purposes.
13
+ * @param args Partial RequestContext properties
14
+ * @returns A RequestContext instance
15
+ * @since 1.8.0
16
+ */
17
+ declare function createRequestContext<TContextData>(args: Partial<RequestContext<TContextData>> & {
18
+ url: URL;
19
+ data: TContextData;
20
+ federation: Federation<TContextData>;
21
+ }): RequestContext<TContextData>;
22
+ /**
23
+ * Creates an InboxContext for testing purposes.
24
+ * @param args Partial InboxContext properties
25
+ * @returns An InboxContext instance
26
+ * @since 1.8.0
27
+ */
28
+ declare function createInboxContext<TContextData>(args: Partial<InboxContext<TContextData>> & {
29
+ url?: URL;
30
+ data: TContextData;
31
+ recipient?: string | null;
32
+ federation: Federation<TContextData>;
33
+ }): InboxContext<TContextData>;
34
+ //#endregion
8
35
  //#region src/mock.d.ts
9
-
10
36
  /**
11
37
  * Represents a sent activity with metadata about how it was sent.
12
38
  * @since 1.8.0
@@ -87,7 +113,7 @@ declare class MockFederation<TContextData> implements Federation<TContextData> {
87
113
  constructor(options?: {
88
114
  contextData?: TContextData;
89
115
  origin?: string;
90
- tracerProvider?: TracerProvider;
116
+ tracerProvider?: any;
91
117
  });
92
118
  setNodeInfoDispatcher(path: string, dispatcher: NodeInfoDispatcher<TContextData>): void;
93
119
  setActorDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: ActorDispatcher<TContextData>): ActorCallbackSetters<TContextData>;
@@ -104,8 +130,7 @@ declare class MockFederation<TContextData> implements Federation<TContextData> {
104
130
  setInboxListeners(inboxPath: `${string}{identifier}${string}` | `${string}{handle}${string}`, sharedInboxPath?: string): InboxListenerSetters<TContextData>;
105
131
  startQueue(contextData: TContextData, options?: FederationStartQueueOptions): Promise<void>;
106
132
  processQueuedTask(contextData: TContextData, _message: Message): Promise<void>;
107
- createContext(baseUrl: URL, contextData: TContextData): Context<TContextData>;
108
- createContext(request: Request, contextData: TContextData): RequestContext<TContextData>;
133
+ createContext(baseUrlOrRequest: any, contextData: TContextData): any;
109
134
  fetch(request: Request, options: FederationFetchOptions<TContextData>): Promise<Response>;
110
135
  /**
111
136
  * Simulates receiving an activity. This method is specific to the mock
@@ -132,18 +157,20 @@ declare class MockFederation<TContextData> implements Federation<TContextData> {
132
157
  * This class provides a way to test Fedify applications without needing
133
158
  * a real federation context.
134
159
  *
160
+ * Note: This class is not exported from the public API. Use
161
+ * {@link MockFederation.createContext} instead.
162
+ *
135
163
  * @example
136
164
  * ```typescript
137
165
  * import { Person, Create } from "@fedify/fedify/vocab";
138
- * import { MockContext, MockFederation } from "@fedify/testing";
166
+ * import { MockFederation } from "@fedify/testing";
139
167
  *
140
- * // Create a mock context
168
+ * // Create a mock federation and context
141
169
  * const mockFederation = new MockFederation<{ userId: string }>();
142
- * const context = new MockContext({
143
- * url: new URL("https://example.com"),
144
- * data: { userId: "test-user" },
145
- * federation: mockFederation
146
- * });
170
+ * const context = mockFederation.createContext(
171
+ * new URL("https://example.com"),
172
+ * { userId: "test-user" }
173
+ * );
147
174
  *
148
175
  * // Send an activity
149
176
  * const recipient = new Person({ id: new URL("https://example.com/users/bob") });
@@ -157,139 +184,13 @@ declare class MockFederation<TContextData> implements Federation<TContextData> {
157
184
  * activity
158
185
  * );
159
186
  *
160
- * // Check sent activities
161
- * const sent = context.getSentActivities();
187
+ * // Check sent activities from the federation
188
+ * const sent = mockFederation.sentActivities;
162
189
  * console.log(sent[0].activity);
163
190
  * ```
164
191
  *
165
192
  * @template TContextData The context data to pass to the {@link Context}.
166
193
  * @since 1.8.0
167
194
  */
168
- declare class MockContext<TContextData> implements Context<TContextData> {
169
- readonly origin: string;
170
- readonly canonicalOrigin: string;
171
- readonly host: string;
172
- readonly hostname: string;
173
- readonly data: TContextData;
174
- readonly federation: Federation<TContextData>;
175
- readonly documentLoader: DocumentLoader;
176
- readonly contextLoader: DocumentLoader;
177
- readonly tracerProvider: TracerProvider;
178
- private sentActivities;
179
- constructor(options: {
180
- url?: URL;
181
- data: TContextData;
182
- federation: Federation<TContextData>;
183
- documentLoader?: DocumentLoader;
184
- contextLoader?: DocumentLoader;
185
- tracerProvider?: TracerProvider;
186
- });
187
- clone(data: TContextData): Context<TContextData>;
188
- getNodeInfoUri(): URL;
189
- getActorUri(identifier: string): URL;
190
- getObjectUri<TObject extends Object$1>(cls: (new (...args: any[]) => TObject) & {
191
- typeId: URL;
192
- }, values: Record<string, string>): URL;
193
- getOutboxUri(identifier: string): URL;
194
- getInboxUri(identifier: string): URL;
195
- getInboxUri(): URL;
196
- getFollowingUri(identifier: string): URL;
197
- getFollowersUri(identifier: string): URL;
198
- getLikedUri(identifier: string): URL;
199
- getFeaturedUri(identifier: string): URL;
200
- getFeaturedTagsUri(identifier: string): URL;
201
- getCollectionUri<TParam extends Record<string, string>>(_name: string | symbol, values: TParam): URL;
202
- parseUri(uri: URL): ParseUriResult | null;
203
- getActorKeyPairs(_identifier: string): Promise<ActorKeyPair[]>;
204
- getDocumentLoader(params: {
205
- handle: string;
206
- } | {
207
- identifier: string;
208
- }): Promise<DocumentLoader>;
209
- getDocumentLoader(params: {
210
- keyId: URL;
211
- privateKey: CryptoKey;
212
- }): DocumentLoader;
213
- lookupObject(_uri: URL | string, _options?: LookupObjectOptions): Promise<Object$1 | null>;
214
- traverseCollection<TItem, TContext extends Context<TContextData>>(_collection: Collection | URL | null, _options?: TraverseCollectionOptions): AsyncIterable<TItem>;
215
- lookupNodeInfo(url: URL | string, options?: {
216
- parse?: "strict" | "best-effort";
217
- } & any): Promise<NodeInfo | undefined>;
218
- lookupNodeInfo(url: URL | string, options?: {
219
- parse: "none";
220
- } & any): Promise<JsonValue | undefined>;
221
- lookupWebFinger(_resource: URL | `acct:${string}@${string}` | string, _options?: any): Promise<ResourceDescriptor | null>;
222
- sendActivity(sender: SenderKeyPair | SenderKeyPair[] | {
223
- identifier: string;
224
- } | {
225
- username: string;
226
- } | {
227
- handle: string;
228
- }, recipients: Recipient | Recipient[], activity: Activity, options?: SendActivityOptions): Promise<void>;
229
- sendActivity(sender: {
230
- identifier: string;
231
- } | {
232
- username: string;
233
- } | {
234
- handle: string;
235
- }, recipients: "followers", activity: Activity, options?: SendActivityOptionsForCollection): Promise<void>;
236
- sendActivity(sender: SenderKeyPair | SenderKeyPair[] | {
237
- identifier: string;
238
- } | {
239
- username: string;
240
- } | {
241
- handle: string;
242
- }, recipients: Recipient | Recipient[], activity: Activity, options?: SendActivityOptions): Promise<void>;
243
- sendActivity(sender: {
244
- identifier: string;
245
- } | {
246
- username: string;
247
- } | {
248
- handle: string;
249
- }, recipients: "followers", activity: Activity, options?: SendActivityOptionsForCollection): Promise<void>;
250
- routeActivity(_recipient: string | null, _activity: Activity, _options?: RouteActivityOptions): Promise<boolean>;
251
- /**
252
- * Gets all activities that have been sent through this mock context.
253
- * This method is specific to the mock implementation and is used for
254
- * testing purposes.
255
- *
256
- * @returns An array of sent activity records.
257
- */
258
- getSentActivities(): Array<{
259
- sender: SenderKeyPair | SenderKeyPair[] | {
260
- identifier: string;
261
- } | {
262
- username: string;
263
- } | {
264
- handle: string;
265
- };
266
- recipients: Recipient | Recipient[] | "followers";
267
- activity: Activity;
268
- }>;
269
- /**
270
- * Clears all sent activities from the mock context.
271
- * This method is specific to the mock implementation and is used for
272
- * testing purposes.
273
- */
274
- reset(): void;
275
- }
276
- //#endregion
277
- //#region src/context.d.ts
278
- declare function createContext<TContextData>(values: Partial<Context<TContextData>> & {
279
- url?: URL;
280
- data: TContextData;
281
- federation: Federation<TContextData>;
282
- }): Context<TContextData>;
283
- declare function createRequestContext<TContextData>(args: Partial<RequestContext<TContextData>> & {
284
- url: URL;
285
- data: TContextData;
286
- federation: Federation<TContextData>;
287
- }): RequestContext<TContextData>;
288
- declare function createInboxContext<TContextData>(args: Partial<InboxContext<TContextData>> & {
289
- url?: URL;
290
- data: TContextData;
291
- recipient?: string | null;
292
- federation: Federation<TContextData>;
293
- }): InboxContext<TContextData>;
294
195
  //#endregion
295
- export { MockContext, MockFederation, SentActivity, createContext, createInboxContext, createRequestContext };
196
+ export { MockFederation, SentActivity, createContext, createInboxContext, createRequestContext };
package/dist/mod.js CHANGED
@@ -1,4 +1,3 @@
1
- import { trace } from "@opentelemetry/api";
2
1
  import { RouterError } from "@fedify/fedify/federation";
3
2
  import { lookupObject, traverseCollection } from "@fedify/fedify/vocab";
4
3
  import { lookupWebFinger } from "@fedify/fedify/webfinger";
@@ -12,6 +11,10 @@ const mockDocumentLoader = async (url) => ({
12
11
 
13
12
  //#endregion
14
13
  //#region src/context.ts
14
+ const noopTracerProvider$1 = { getTracer: () => ({
15
+ startActiveSpan: () => void 0,
16
+ startSpan: () => void 0
17
+ }) };
15
18
  function createContext(values) {
16
19
  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: lookupWebFinger$1, sendActivity, routeActivity } = values;
17
20
  function throwRouteError() {
@@ -26,7 +29,7 @@ function createContext(values) {
26
29
  hostname: url.hostname,
27
30
  documentLoader: documentLoader ?? mockDocumentLoader,
28
31
  contextLoader: contextLoader ?? mockDocumentLoader,
29
- tracerProvider: tracerProvider ?? trace.getTracerProvider(),
32
+ tracerProvider: tracerProvider ?? noopTracerProvider$1,
30
33
  clone: clone ?? ((data$1) => createContext({
31
34
  ...values,
32
35
  data: data$1
@@ -75,6 +78,12 @@ function createContext(values) {
75
78
  })
76
79
  };
77
80
  }
81
+ /**
82
+ * Creates a RequestContext for testing purposes.
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,12 @@ function createRequestContext(args) {
93
102
  })
94
103
  };
95
104
  }
105
+ /**
106
+ * Creates an InboxContext for testing purposes.
107
+ * @param args Partial InboxContext properties
108
+ * @returns An InboxContext instance
109
+ * @since 1.8.0
110
+ */
96
111
  function createInboxContext(args) {
97
112
  return {
98
113
  ...createContext(args),
@@ -109,6 +124,10 @@ function createInboxContext(args) {
109
124
 
110
125
  //#endregion
111
126
  //#region src/mock.ts
127
+ const noopTracerProvider = { getTracer: () => ({
128
+ startActiveSpan: () => void 0,
129
+ startSpan: () => void 0
130
+ }) };
112
131
  /**
113
132
  * Helper function to expand URI templates with values.
114
133
  * Supports simple placeholders like {identifier}, {handle}, etc.
@@ -122,6 +141,25 @@ function expandUriTemplate(template, values) {
122
141
  });
123
142
  }
124
143
  /**
144
+ * Creates an InboxContext for testing purposes.
145
+ * @param args Partial InboxContext properties
146
+ * @returns An InboxContext instance
147
+ * @since 1.8.0
148
+ */
149
+ function createInboxContext$1(args) {
150
+ return {
151
+ ...createContext(args),
152
+ clone: args.clone ?? ((data) => createInboxContext$1({
153
+ ...args,
154
+ data
155
+ })),
156
+ recipient: args.recipient ?? null,
157
+ forwardActivity: args.forwardActivity ?? ((_params) => {
158
+ throw new Error("Not implemented");
159
+ })
160
+ };
161
+ }
162
+ /**
125
163
  * A mock implementation of the {@link Federation} interface for unit testing.
126
164
  * This class provides a way to test Fedify applications without needing
127
165
  * a real federation setup.
@@ -309,22 +347,9 @@ var MockFederation = class {
309
347
  }
310
348
  createContext(baseUrlOrRequest, contextData) {
311
349
  const mockFederation = this;
312
- if (baseUrlOrRequest instanceof Request) return createRequestContext({
313
- url: new URL(baseUrlOrRequest.url),
314
- request: baseUrlOrRequest,
315
- data: contextData,
316
- federation: mockFederation,
317
- sendActivity: async (sender, recipients, activity, options) => {
318
- const tempContext = new MockContext({
319
- url: new URL(baseUrlOrRequest.url),
320
- data: contextData,
321
- federation: mockFederation
322
- });
323
- await tempContext.sendActivity(sender, recipients, activity, options);
324
- }
325
- });
326
- else return new MockContext({
327
- url: baseUrlOrRequest,
350
+ const url = baseUrlOrRequest instanceof Request ? new URL(baseUrlOrRequest.url) : baseUrlOrRequest;
351
+ return new MockContext({
352
+ url,
328
353
  data: contextData,
329
354
  federation: mockFederation
330
355
  });
@@ -347,7 +372,7 @@ var MockFederation = class {
347
372
  const listeners = this.inboxListeners.get(typeName) || [];
348
373
  if (listeners.length > 0 && this.contextData === void 0) throw new Error("MockFederation.receiveActivity(): contextData is not initialized. Please provide contextData through the constructor or call startQueue() before receiving activities.");
349
374
  for (const listener of listeners) {
350
- const context = createInboxContext({
375
+ const context = createInboxContext$1({
351
376
  data: this.contextData,
352
377
  federation: this
353
378
  });
@@ -386,18 +411,20 @@ var MockFederation = class {
386
411
  * This class provides a way to test Fedify applications without needing
387
412
  * a real federation context.
388
413
  *
414
+ * Note: This class is not exported from the public API. Use
415
+ * {@link MockFederation.createContext} instead.
416
+ *
389
417
  * @example
390
418
  * ```typescript
391
419
  * import { Person, Create } from "@fedify/fedify/vocab";
392
- * import { MockContext, MockFederation } from "@fedify/testing";
420
+ * import { MockFederation } from "@fedify/testing";
393
421
  *
394
- * // Create a mock context
422
+ * // Create a mock federation and context
395
423
  * const mockFederation = new MockFederation<{ userId: string }>();
396
- * const context = new MockContext({
397
- * url: new URL("https://example.com"),
398
- * data: { userId: "test-user" },
399
- * federation: mockFederation
400
- * });
424
+ * const context = mockFederation.createContext(
425
+ * new URL("https://example.com"),
426
+ * { userId: "test-user" }
427
+ * );
401
428
  *
402
429
  * // Send an activity
403
430
  * const recipient = new Person({ id: new URL("https://example.com/users/bob") });
@@ -411,8 +438,8 @@ var MockFederation = class {
411
438
  * activity
412
439
  * );
413
440
  *
414
- * // Check sent activities
415
- * const sent = context.getSentActivities();
441
+ * // Check sent activities from the federation
442
+ * const sent = mockFederation.sentActivities;
416
443
  * console.log(sent[0].activity);
417
444
  * ```
418
445
  *
@@ -444,7 +471,7 @@ var MockContext = class MockContext {
444
471
  documentUrl: url$1
445
472
  }));
446
473
  this.contextLoader = options.contextLoader ?? this.documentLoader;
447
- this.tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
474
+ this.tracerProvider = options.tracerProvider ?? noopTracerProvider;
448
475
  }
449
476
  clone(data) {
450
477
  return new MockContext({
@@ -630,4 +657,4 @@ var MockContext = class MockContext {
630
657
  };
631
658
 
632
659
  //#endregion
633
- export { MockContext, MockFederation, createContext, createInboxContext, createRequestContext };
660
+ export { MockFederation, createContext, createInboxContext, createRequestContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/testing",
3
- "version": "1.8.13",
3
+ "version": "1.8.14-dev.1853+21177540",
4
4
  "description": "Testing utilities for Fedify applications",
5
5
  "keywords": [
6
6
  "fedify",
@@ -45,10 +45,7 @@
45
45
  "package.json"
46
46
  ],
47
47
  "peerDependencies": {
48
- "@fedify/fedify": "^1.8.13"
49
- },
50
- "dependencies": {
51
- "@opentelemetry/api": "^1.9.0"
48
+ "@fedify/fedify": "^1.8.14-dev.1853+21177540"
52
49
  },
53
50
  "devDependencies": {
54
51
  "@js-temporal/polyfill": "^0.5.1",