@fedify/testing 1.8.14-pr.470.1835 → 1.8.14-pr.470.1850

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