@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/dist/mod.d.cts ADDED
@@ -0,0 +1,126 @@
1
+ import { Context, Federation, InboxContext, RequestContext } from "@fedify/fedify/federation";
2
+ import { Activity } from "@fedify/vocab";
3
+
4
+ //#region src/context.d.ts
5
+ declare function createContext<TContextData>(values: Partial<Context<TContextData>> & {
6
+ url?: URL;
7
+ data: TContextData;
8
+ federation: Federation<TContextData>;
9
+ }): Context<TContextData>;
10
+ /**
11
+ * Creates a RequestContext for testing purposes.
12
+ * Not exported - used internally only. Public API is in mock.ts
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
+ * Test-specific InboxContext type alias.
24
+ * This indirection helps avoid JSR type analyzer issues.
25
+ * @since 1.9.1
26
+ */
27
+ type TestInboxContext<TContextData> = InboxContext<TContextData>;
28
+ /**
29
+ * Creates an InboxContext for testing purposes.
30
+ * Not exported - used internally only. Public API is in mock.ts
31
+ * @param args Partial InboxContext properties
32
+ * @returns An InboxContext instance
33
+ * @since 1.8.0
34
+ */
35
+ declare function createInboxContext<TContextData>(args: Partial<InboxContext<TContextData>> & {
36
+ url?: URL;
37
+ data: TContextData;
38
+ recipient?: string | null;
39
+ federation: Federation<TContextData>;
40
+ }): TestInboxContext<TContextData>;
41
+ //#endregion
42
+ //#region src/mock.d.ts
43
+ /**
44
+ * Represents a sent activity with metadata about how it was sent.
45
+ * @since 1.8.0
46
+ */
47
+ interface SentActivity {
48
+ /** Whether the activity was queued or sent immediately. */
49
+ queued: boolean;
50
+ /** Which queue was used (if queued). */
51
+ queue?: "inbox" | "outbox" | "fanout";
52
+ /** The activity that was sent. */
53
+ activity: Activity;
54
+ /** The order in which the activity was sent (auto-incrementing counter). */
55
+ sentOrder: number;
56
+ }
57
+ /**
58
+ * A mock Context interface for testing purposes.
59
+ * Extends the standard Context interface with additional testing utilities.
60
+ * @since 1.9.1
61
+ */
62
+ interface TestContext<TContextData> extends Omit<Context<TContextData>, "clone">, Pick<RequestContext<TContextData>, "request" | "url" | "getActor" | "getObject" | "getSignedKey" | "getSignedKeyOwner" | "sendActivity" | "routeActivity"> {
63
+ clone(data: TContextData): TestContext<TContextData>;
64
+ getSentActivities(): Array<{
65
+ sender: any;
66
+ recipients: any;
67
+ activity: Activity;
68
+ }>;
69
+ reset(): void;
70
+ }
71
+ /**
72
+ * A mock Federation interface for testing purposes.
73
+ * Extends the standard Federation interface with additional testing utilities.
74
+ * @since 1.9.1
75
+ */
76
+ interface TestFederation<TContextData> extends Omit<Federation<TContextData>, "createContext"> {
77
+ sentActivities: SentActivity[];
78
+ queueStarted: boolean;
79
+ sentCounter: number;
80
+ receiveActivity(activity: Activity): Promise<void>;
81
+ reset(): void;
82
+ createContext(baseUrlOrRequest: URL | Request, contextData: TContextData): TestContext<TContextData>;
83
+ }
84
+ /**
85
+ * Creates a mock Federation instance for testing purposes.
86
+ *
87
+ * @template TContextData The type of context data to use
88
+ * @param options Optional configuration for the mock federation
89
+ * @returns A Federation instance that can be used for testing
90
+ * @since 1.9.1
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * import { Create } from "@fedify/vocab";
95
+ * import { createFederation } from "@fedify/testing";
96
+ *
97
+ * // Create a mock federation with contextData
98
+ * const federation = createFederation<{ userId: string }>({
99
+ * contextData: { userId: "test-user" }
100
+ * });
101
+ *
102
+ * // Set up inbox listeners
103
+ * federation
104
+ * .setInboxListeners("/users/{identifier}/inbox")
105
+ * .on(Create, async (ctx, activity) => {
106
+ * console.log("Received:", activity);
107
+ * });
108
+ *
109
+ * // Simulate receiving an activity
110
+ * const createActivity = new Create({
111
+ * id: new URL("https://example.com/create/1"),
112
+ * actor: new URL("https://example.com/users/alice")
113
+ * });
114
+ * await federation.receiveActivity(createActivity);
115
+ *
116
+ * // Check sent activities
117
+ * console.log(federation.sentActivities);
118
+ * ```
119
+ */
120
+ declare function createFederation<TContextData>(options?: {
121
+ contextData?: TContextData;
122
+ origin?: string;
123
+ tracerProvider?: any;
124
+ }): TestFederation<TContextData>;
125
+ //#endregion
126
+ export { createContext, createFederation, createInboxContext, createRequestContext };
package/dist/mod.d.ts CHANGED
@@ -1,12 +1,45 @@
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, WebFingerLinksDispatcher } 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 { Context, Federation, InboxContext, RequestContext } from "@fedify/fedify/federation";
2
+ import { Activity } from "@fedify/vocab";
7
3
 
4
+ //#region src/context.d.ts
5
+ declare function createContext<TContextData>(values: Partial<Context<TContextData>> & {
6
+ url?: URL;
7
+ data: TContextData;
8
+ federation: Federation<TContextData>;
9
+ }): Context<TContextData>;
10
+ /**
11
+ * Creates a RequestContext for testing purposes.
12
+ * Not exported - used internally only. Public API is in mock.ts
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
+ * Test-specific InboxContext type alias.
24
+ * This indirection helps avoid JSR type analyzer issues.
25
+ * @since 1.9.1
26
+ */
27
+ type TestInboxContext<TContextData> = InboxContext<TContextData>;
28
+ /**
29
+ * Creates an InboxContext for testing purposes.
30
+ * Not exported - used internally only. Public API is in mock.ts
31
+ * @param args Partial InboxContext properties
32
+ * @returns An InboxContext instance
33
+ * @since 1.8.0
34
+ */
35
+ declare function createInboxContext<TContextData>(args: Partial<InboxContext<TContextData>> & {
36
+ url?: URL;
37
+ data: TContextData;
38
+ recipient?: string | null;
39
+ federation: Federation<TContextData>;
40
+ }): TestInboxContext<TContextData>;
41
+ //#endregion
8
42
  //#region src/mock.d.ts
9
-
10
43
  /**
11
44
  * Represents a sent activity with metadata about how it was sent.
12
45
  * @since 1.8.0
@@ -22,17 +55,47 @@ interface SentActivity {
22
55
  sentOrder: number;
23
56
  }
24
57
  /**
25
- * A mock implementation of the {@link Federation} interface for unit testing.
26
- * This class provides a way to test Fedify applications without needing
27
- * a real federation setup.
58
+ * A mock Context interface for testing purposes.
59
+ * Extends the standard Context interface with additional testing utilities.
60
+ * @since 1.9.1
61
+ */
62
+ interface TestContext<TContextData> extends Omit<Context<TContextData>, "clone">, Pick<RequestContext<TContextData>, "request" | "url" | "getActor" | "getObject" | "getSignedKey" | "getSignedKeyOwner" | "sendActivity" | "routeActivity"> {
63
+ clone(data: TContextData): TestContext<TContextData>;
64
+ getSentActivities(): Array<{
65
+ sender: any;
66
+ recipients: any;
67
+ activity: Activity;
68
+ }>;
69
+ reset(): void;
70
+ }
71
+ /**
72
+ * A mock Federation interface for testing purposes.
73
+ * Extends the standard Federation interface with additional testing utilities.
74
+ * @since 1.9.1
75
+ */
76
+ interface TestFederation<TContextData> extends Omit<Federation<TContextData>, "createContext"> {
77
+ sentActivities: SentActivity[];
78
+ queueStarted: boolean;
79
+ sentCounter: number;
80
+ receiveActivity(activity: Activity): Promise<void>;
81
+ reset(): void;
82
+ createContext(baseUrlOrRequest: URL | Request, contextData: TContextData): TestContext<TContextData>;
83
+ }
84
+ /**
85
+ * Creates a mock Federation instance for testing purposes.
86
+ *
87
+ * @template TContextData The type of context data to use
88
+ * @param options Optional configuration for the mock federation
89
+ * @returns A Federation instance that can be used for testing
90
+ * @since 1.9.1
28
91
  *
29
92
  * @example
30
93
  * ```typescript
31
- * import { Create } from "@fedify/fedify/vocab";
32
- * import { MockFederation } from "@fedify/testing";
94
+ * import { Create } from "@fedify/vocab";
95
+ * import { createFederation } from "@fedify/testing";
33
96
  *
34
97
  * // Create a mock federation with contextData
35
- * const federation = new MockFederation<{ userId: string }>({
98
+ * const federation = createFederation<{ userId: string }>({
36
99
  * contextData: { userId: "test-user" }
37
100
  * });
38
101
  *
@@ -49,249 +112,15 @@ interface SentActivity {
49
112
  * actor: new URL("https://example.com/users/alice")
50
113
  * });
51
114
  * await federation.receiveActivity(createActivity);
52
- * ```
53
- *
54
- * @template TContextData The context data to pass to the {@link Context}.
55
- * @since 1.8.0
56
- */
57
- declare class MockFederation<TContextData> implements Federation<TContextData> {
58
- private options;
59
- sentActivities: SentActivity[];
60
- queueStarted: boolean;
61
- private activeQueues;
62
- sentCounter: number;
63
- private nodeInfoDispatcher?;
64
- private webFingerDispatcher?;
65
- private actorDispatchers;
66
- actorPath?: string;
67
- inboxPath?: string;
68
- outboxPath?: string;
69
- followingPath?: string;
70
- followersPath?: string;
71
- likedPath?: string;
72
- featuredPath?: string;
73
- featuredTagsPath?: string;
74
- nodeInfoPath?: string;
75
- sharedInboxPath?: string;
76
- objectPaths: Map<string, string>;
77
- private objectDispatchers;
78
- private inboxDispatcher?;
79
- private outboxDispatcher?;
80
- private followingDispatcher?;
81
- private followersDispatcher?;
82
- private likedDispatcher?;
83
- private featuredDispatcher?;
84
- private featuredTagsDispatcher?;
85
- private inboxListeners;
86
- private contextData?;
87
- private receivedActivities;
88
- constructor(options?: {
89
- contextData?: TContextData;
90
- origin?: string;
91
- tracerProvider?: TracerProvider;
92
- });
93
- setNodeInfoDispatcher(path: string, dispatcher: NodeInfoDispatcher<TContextData>): void;
94
- setWebFingerLinksDispatcher(dispatcher: WebFingerLinksDispatcher<TContextData>): void;
95
- setActorDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: ActorDispatcher<TContextData>): ActorCallbackSetters<TContextData>;
96
- setObjectDispatcher<TObject extends Object$1, TParam extends string>(cls: (new (...args: any[]) => TObject) & {
97
- typeId: URL;
98
- }, path: string, dispatcher: ObjectDispatcher<TContextData, TObject, TParam>): ObjectCallbackSetters<TContextData, TObject, TParam>;
99
- setInboxDispatcher(_path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Activity, RequestContext<TContextData>, TContextData, void>): CollectionCallbackSetters<RequestContext<TContextData>, TContextData, void>;
100
- setOutboxDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Activity, RequestContext<TContextData>, TContextData, void>): CollectionCallbackSetters<RequestContext<TContextData>, TContextData, void>;
101
- setFollowingDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Actor | URL, RequestContext<TContextData>, TContextData, void>): CollectionCallbackSetters<RequestContext<TContextData>, TContextData, void>;
102
- setFollowersDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Recipient, Context<TContextData>, TContextData, URL>): CollectionCallbackSetters<Context<TContextData>, TContextData, URL>;
103
- setLikedDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Object$1 | URL, RequestContext<TContextData>, TContextData, void>): CollectionCallbackSetters<RequestContext<TContextData>, TContextData, void>;
104
- setFeaturedDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Object$1, RequestContext<TContextData>, TContextData, void>): CollectionCallbackSetters<RequestContext<TContextData>, TContextData, void>;
105
- setFeaturedTagsDispatcher(path: `${string}{identifier}${string}` | `${string}{handle}${string}`, dispatcher: CollectionDispatcher<Hashtag, RequestContext<TContextData>, TContextData, void>): CollectionCallbackSetters<RequestContext<TContextData>, TContextData, void>;
106
- setInboxListeners(inboxPath: `${string}{identifier}${string}` | `${string}{handle}${string}`, sharedInboxPath?: string): InboxListenerSetters<TContextData>;
107
- startQueue(contextData: TContextData, options?: FederationStartQueueOptions): Promise<void>;
108
- processQueuedTask(contextData: TContextData, _message: Message): Promise<void>;
109
- createContext(baseUrl: URL, contextData: TContextData): Context<TContextData>;
110
- createContext(request: Request, contextData: TContextData): RequestContext<TContextData>;
111
- fetch(request: Request, options: FederationFetchOptions<TContextData>): Promise<Response>;
112
- /**
113
- * Simulates receiving an activity. This method is specific to the mock
114
- * implementation and is used for testing purposes.
115
- *
116
- * @param activity The activity to receive.
117
- * @returns A promise that resolves when the activity has been processed.
118
- * @since 1.8.0
119
- */
120
- receiveActivity(activity: Activity): Promise<void>;
121
- /**
122
- * Clears all sent activities from the mock federation.
123
- * This method is specific to the mock implementation and is used for
124
- * testing purposes.
125
- *
126
- * @since 1.8.0
127
- */
128
- reset(): void;
129
- setCollectionDispatcher<TObject extends Object$1, TParams extends Record<string, string>>(_name: string | symbol, _itemType: any, _path: any, _dispatcher: any): any;
130
- setOrderedCollectionDispatcher<TObject extends Object$1, TParams extends Record<string, string>>(_name: string | symbol, _itemType: any, _path: any, _dispatcher: any): any;
131
- }
132
- /**
133
- * A mock implementation of the {@link Context} interface for unit testing.
134
- * This class provides a way to test Fedify applications without needing
135
- * a real federation context.
136
- *
137
- * @example
138
- * ```typescript
139
- * import { Person, Create } from "@fedify/fedify/vocab";
140
- * import { MockContext, MockFederation } from "@fedify/testing";
141
- *
142
- * // Create a mock context
143
- * const mockFederation = new MockFederation<{ userId: string }>();
144
- * const context = new MockContext({
145
- * url: new URL("https://example.com"),
146
- * data: { userId: "test-user" },
147
- * federation: mockFederation
148
- * });
149
- *
150
- * // Send an activity
151
- * const recipient = new Person({ id: new URL("https://example.com/users/bob") });
152
- * const activity = new Create({
153
- * id: new URL("https://example.com/create/1"),
154
- * actor: new URL("https://example.com/users/alice")
155
- * });
156
- * await context.sendActivity(
157
- * { identifier: "alice" },
158
- * recipient,
159
- * activity
160
- * );
161
115
  *
162
116
  * // Check sent activities
163
- * const sent = context.getSentActivities();
164
- * console.log(sent[0].activity);
117
+ * console.log(federation.sentActivities);
165
118
  * ```
166
- *
167
- * @template TContextData The context data to pass to the {@link Context}.
168
- * @since 1.8.0
169
119
  */
170
- declare class MockContext<TContextData> implements Context<TContextData> {
171
- readonly origin: string;
172
- readonly canonicalOrigin: string;
173
- readonly host: string;
174
- readonly hostname: string;
175
- readonly data: TContextData;
176
- readonly federation: Federation<TContextData>;
177
- readonly documentLoader: DocumentLoader;
178
- readonly contextLoader: DocumentLoader;
179
- readonly tracerProvider: TracerProvider;
180
- private sentActivities;
181
- constructor(options: {
182
- url?: URL;
183
- data: TContextData;
184
- federation: Federation<TContextData>;
185
- documentLoader?: DocumentLoader;
186
- contextLoader?: DocumentLoader;
187
- tracerProvider?: TracerProvider;
188
- });
189
- clone(data: TContextData): Context<TContextData>;
190
- getNodeInfoUri(): URL;
191
- getActorUri(identifier: string): URL;
192
- getObjectUri<TObject extends Object$1>(cls: (new (...args: any[]) => TObject) & {
193
- typeId: URL;
194
- }, values: Record<string, string>): URL;
195
- getOutboxUri(identifier: string): URL;
196
- getInboxUri(identifier: string): URL;
197
- getInboxUri(): URL;
198
- getFollowingUri(identifier: string): URL;
199
- getFollowersUri(identifier: string): URL;
200
- getLikedUri(identifier: string): URL;
201
- getFeaturedUri(identifier: string): URL;
202
- getFeaturedTagsUri(identifier: string): URL;
203
- getCollectionUri<TParam extends Record<string, string>>(_name: string | symbol, values: TParam): URL;
204
- parseUri(uri: URL): ParseUriResult | null;
205
- getActorKeyPairs(_identifier: string): Promise<ActorKeyPair[]>;
206
- getDocumentLoader(params: {
207
- handle: string;
208
- } | {
209
- identifier: string;
210
- }): Promise<DocumentLoader>;
211
- getDocumentLoader(params: {
212
- keyId: URL;
213
- privateKey: CryptoKey;
214
- }): DocumentLoader;
215
- lookupObject(_uri: URL | string, _options?: LookupObjectOptions): Promise<Object$1 | null>;
216
- traverseCollection<TItem, TContext extends Context<TContextData>>(_collection: Collection | URL | null, _options?: TraverseCollectionOptions): AsyncIterable<TItem>;
217
- lookupNodeInfo(url: URL | string, options?: {
218
- parse?: "strict" | "best-effort";
219
- } & any): Promise<NodeInfo | undefined>;
220
- lookupNodeInfo(url: URL | string, options?: {
221
- parse: "none";
222
- } & any): Promise<JsonValue | undefined>;
223
- lookupWebFinger(_resource: URL | `acct:${string}@${string}` | string, _options?: any): Promise<ResourceDescriptor | null>;
224
- sendActivity(sender: SenderKeyPair | SenderKeyPair[] | {
225
- identifier: string;
226
- } | {
227
- username: string;
228
- } | {
229
- handle: string;
230
- }, recipients: Recipient | Recipient[], activity: Activity, options?: SendActivityOptions): Promise<void>;
231
- sendActivity(sender: {
232
- identifier: string;
233
- } | {
234
- username: string;
235
- } | {
236
- handle: string;
237
- }, recipients: "followers", activity: Activity, options?: SendActivityOptionsForCollection): Promise<void>;
238
- sendActivity(sender: SenderKeyPair | SenderKeyPair[] | {
239
- identifier: string;
240
- } | {
241
- username: string;
242
- } | {
243
- handle: string;
244
- }, recipients: Recipient | Recipient[], activity: Activity, options?: SendActivityOptions): Promise<void>;
245
- sendActivity(sender: {
246
- identifier: string;
247
- } | {
248
- username: string;
249
- } | {
250
- handle: string;
251
- }, recipients: "followers", activity: Activity, options?: SendActivityOptionsForCollection): Promise<void>;
252
- routeActivity(_recipient: string | null, _activity: Activity, _options?: RouteActivityOptions): Promise<boolean>;
253
- /**
254
- * Gets all activities that have been sent through this mock context.
255
- * This method is specific to the mock implementation and is used for
256
- * testing purposes.
257
- *
258
- * @returns An array of sent activity records.
259
- */
260
- getSentActivities(): Array<{
261
- sender: SenderKeyPair | SenderKeyPair[] | {
262
- identifier: string;
263
- } | {
264
- username: string;
265
- } | {
266
- handle: string;
267
- };
268
- recipients: Recipient | Recipient[] | "followers";
269
- activity: Activity;
270
- }>;
271
- /**
272
- * Clears all sent activities from the mock context.
273
- * This method is specific to the mock implementation and is used for
274
- * testing purposes.
275
- */
276
- reset(): void;
277
- }
278
- //#endregion
279
- //#region src/context.d.ts
280
- declare function createContext<TContextData>(values: Partial<Context<TContextData>> & {
281
- url?: URL;
282
- data: TContextData;
283
- federation: Federation<TContextData>;
284
- }): Context<TContextData>;
285
- declare function createRequestContext<TContextData>(args: Partial<RequestContext<TContextData>> & {
286
- url: URL;
287
- data: TContextData;
288
- federation: Federation<TContextData>;
289
- }): RequestContext<TContextData>;
290
- declare function createInboxContext<TContextData>(args: Partial<InboxContext<TContextData>> & {
291
- url?: URL;
292
- data: TContextData;
293
- recipient?: string | null;
294
- federation: Federation<TContextData>;
295
- }): InboxContext<TContextData>;
120
+ declare function createFederation<TContextData>(options?: {
121
+ contextData?: TContextData;
122
+ origin?: string;
123
+ tracerProvider?: any;
124
+ }): TestFederation<TContextData>;
296
125
  //#endregion
297
- export { MockContext, MockFederation, SentActivity, createContext, createInboxContext, createRequestContext };
126
+ export { createContext, createFederation, createInboxContext, createRequestContext };