@slopit/adapter-shared 0.1.0
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 +21 -0
- package/dist/index.d.ts +880 -0
- package/dist/index.js +672 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,880 @@
|
|
|
1
|
+
import { BehavioralCaptureConfig, BehavioralCapture } from '@slopit/behavioral';
|
|
2
|
+
import { BehavioralData, CaptureFlag, StimulusInfo, ResponseInfo, PlatformInfo, SlopitSession, SlopitTrial, ModifierState } from '@slopit/core';
|
|
3
|
+
export { EnvironmentInfo, PlatformInfo, ResponseInfo, StimulusInfo, captureEnvironment } from '@slopit/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Shared types for platform adapters.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Base configuration for platform adapters.
|
|
13
|
+
*
|
|
14
|
+
* Extends BehavioralCaptureConfig with common adapter options.
|
|
15
|
+
*/
|
|
16
|
+
interface AdapterConfig extends BehavioralCaptureConfig {
|
|
17
|
+
/**
|
|
18
|
+
* Whether the adapter is enabled.
|
|
19
|
+
*
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
enabled?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* CSS selector for the input element to monitor.
|
|
25
|
+
*
|
|
26
|
+
* If not specified, the adapter will search for common input elements.
|
|
27
|
+
*/
|
|
28
|
+
targetSelector?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Data collected by the adapter during a trial.
|
|
32
|
+
*/
|
|
33
|
+
interface AdapterData {
|
|
34
|
+
/**
|
|
35
|
+
* Behavioral capture data including events and metrics.
|
|
36
|
+
*/
|
|
37
|
+
behavioral: BehavioralData;
|
|
38
|
+
/**
|
|
39
|
+
* Flags generated during capture indicating potential AI-assisted responses.
|
|
40
|
+
*/
|
|
41
|
+
flags: CaptureFlag[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* State of a trial currently being recorded.
|
|
45
|
+
*/
|
|
46
|
+
interface TrialState {
|
|
47
|
+
/**
|
|
48
|
+
* Unique identifier for the trial.
|
|
49
|
+
*/
|
|
50
|
+
trialId: string;
|
|
51
|
+
/**
|
|
52
|
+
* Zero-indexed position of the trial in the session.
|
|
53
|
+
*/
|
|
54
|
+
trialIndex: number;
|
|
55
|
+
/**
|
|
56
|
+
* Type identifier for the trial.
|
|
57
|
+
*/
|
|
58
|
+
trialType?: string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Start time in milliseconds (performance.now()).
|
|
61
|
+
*/
|
|
62
|
+
startTime: number;
|
|
63
|
+
/**
|
|
64
|
+
* Stimulus information for this trial.
|
|
65
|
+
*/
|
|
66
|
+
stimulus?: StimulusInfo | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* DOM element being monitored.
|
|
69
|
+
*/
|
|
70
|
+
element?: HTMLElement | undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Configuration for starting a new trial.
|
|
74
|
+
*/
|
|
75
|
+
interface TrialConfig {
|
|
76
|
+
/**
|
|
77
|
+
* Unique identifier for the trial.
|
|
78
|
+
* If not provided, one will be generated.
|
|
79
|
+
*/
|
|
80
|
+
trialId?: string;
|
|
81
|
+
/**
|
|
82
|
+
* Zero-indexed position of the trial in the session.
|
|
83
|
+
* If not provided, will be auto-incremented.
|
|
84
|
+
*/
|
|
85
|
+
trialIndex?: number;
|
|
86
|
+
/**
|
|
87
|
+
* Type identifier for the trial.
|
|
88
|
+
*/
|
|
89
|
+
trialType?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Stimulus information for this trial.
|
|
92
|
+
*/
|
|
93
|
+
stimulus?: StimulusInfo;
|
|
94
|
+
/**
|
|
95
|
+
* DOM element to monitor for behavioral capture.
|
|
96
|
+
*/
|
|
97
|
+
targetElement?: HTMLElement;
|
|
98
|
+
/**
|
|
99
|
+
* CSS selector to find the target element.
|
|
100
|
+
* Used if targetElement is not provided.
|
|
101
|
+
*/
|
|
102
|
+
targetSelector?: string;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Configuration for the base recorder.
|
|
106
|
+
*/
|
|
107
|
+
interface RecorderConfig {
|
|
108
|
+
/**
|
|
109
|
+
* Participant identifier from recruitment platform.
|
|
110
|
+
*/
|
|
111
|
+
participantId?: string;
|
|
112
|
+
/**
|
|
113
|
+
* Study identifier.
|
|
114
|
+
*/
|
|
115
|
+
studyId?: string;
|
|
116
|
+
/**
|
|
117
|
+
* Session-level metadata.
|
|
118
|
+
*/
|
|
119
|
+
metadata?: Record<string, unknown>;
|
|
120
|
+
/**
|
|
121
|
+
* Behavioral capture configuration.
|
|
122
|
+
*/
|
|
123
|
+
behavioral?: BehavioralCaptureConfig;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Complete trial data after recording.
|
|
127
|
+
*/
|
|
128
|
+
interface CompletedTrial {
|
|
129
|
+
/**
|
|
130
|
+
* Unique identifier for the trial.
|
|
131
|
+
*/
|
|
132
|
+
trialId: string;
|
|
133
|
+
/**
|
|
134
|
+
* Zero-indexed position of the trial in the session.
|
|
135
|
+
*/
|
|
136
|
+
trialIndex: number;
|
|
137
|
+
/**
|
|
138
|
+
* Type identifier for the trial.
|
|
139
|
+
*/
|
|
140
|
+
trialType?: string | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* Start time in milliseconds (Unix timestamp).
|
|
143
|
+
*/
|
|
144
|
+
startTime: number;
|
|
145
|
+
/**
|
|
146
|
+
* End time in milliseconds (Unix timestamp).
|
|
147
|
+
*/
|
|
148
|
+
endTime: number;
|
|
149
|
+
/**
|
|
150
|
+
* Stimulus information for this trial.
|
|
151
|
+
*/
|
|
152
|
+
stimulus?: StimulusInfo | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* Response information.
|
|
155
|
+
*/
|
|
156
|
+
response?: ResponseInfo | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* Behavioral capture data.
|
|
159
|
+
*/
|
|
160
|
+
behavioral?: BehavioralData | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Flags generated during capture.
|
|
163
|
+
*/
|
|
164
|
+
captureFlags?: CaptureFlag[] | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* Platform-specific trial data.
|
|
167
|
+
*/
|
|
168
|
+
platformData?: Record<string, unknown> | undefined;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Metadata for session creation.
|
|
172
|
+
*/
|
|
173
|
+
interface SessionMetadata {
|
|
174
|
+
/**
|
|
175
|
+
* Participant identifier.
|
|
176
|
+
*/
|
|
177
|
+
participantId?: string;
|
|
178
|
+
/**
|
|
179
|
+
* Study identifier.
|
|
180
|
+
*/
|
|
181
|
+
studyId?: string;
|
|
182
|
+
/**
|
|
183
|
+
* Custom metadata.
|
|
184
|
+
*/
|
|
185
|
+
metadata?: Record<string, unknown>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Abstract base class for session recording across platforms.
|
|
190
|
+
*
|
|
191
|
+
* @module
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Abstract base class for platform-specific session recorders.
|
|
196
|
+
*
|
|
197
|
+
* Manages session state, trial lifecycle, and behavioral capture.
|
|
198
|
+
* Subclasses must implement getPlatformInfo() to provide platform-specific metadata.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* class MyPlatformRecorder extends BaseRecorder {
|
|
203
|
+
* protected getPlatformInfo(): PlatformInfo {
|
|
204
|
+
* return {
|
|
205
|
+
* name: "my-platform",
|
|
206
|
+
* version: "1.0.0",
|
|
207
|
+
* adapterVersion: "0.1.0",
|
|
208
|
+
* };
|
|
209
|
+
* }
|
|
210
|
+
* }
|
|
211
|
+
*
|
|
212
|
+
* const recorder = new MyPlatformRecorder({
|
|
213
|
+
* participantId: "P001",
|
|
214
|
+
* studyId: "study-123",
|
|
215
|
+
* });
|
|
216
|
+
*
|
|
217
|
+
* recorder.startTrial({ trialType: "text-entry" });
|
|
218
|
+
* // ... participant completes trial ...
|
|
219
|
+
* recorder.endTrial({ type: "text", value: "response text" });
|
|
220
|
+
*
|
|
221
|
+
* const session = recorder.exportSession();
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
declare abstract class BaseRecorder {
|
|
225
|
+
/**
|
|
226
|
+
* Unique session identifier.
|
|
227
|
+
*/
|
|
228
|
+
protected sessionId: string;
|
|
229
|
+
/**
|
|
230
|
+
* Participant identifier from recruitment platform.
|
|
231
|
+
*/
|
|
232
|
+
protected participantId: string | undefined;
|
|
233
|
+
/**
|
|
234
|
+
* Study identifier.
|
|
235
|
+
*/
|
|
236
|
+
protected studyId: string | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* Session-level metadata.
|
|
239
|
+
*/
|
|
240
|
+
protected metadata: Record<string, unknown> | undefined;
|
|
241
|
+
/**
|
|
242
|
+
* Configuration for behavioral capture.
|
|
243
|
+
*/
|
|
244
|
+
protected behavioralConfig: BehavioralCaptureConfig;
|
|
245
|
+
/**
|
|
246
|
+
* Completed trials.
|
|
247
|
+
*/
|
|
248
|
+
protected trials: CompletedTrial[];
|
|
249
|
+
/**
|
|
250
|
+
* Current trial state, if a trial is in progress.
|
|
251
|
+
*/
|
|
252
|
+
protected currentTrial: TrialState | null;
|
|
253
|
+
/**
|
|
254
|
+
* Behavioral capture instance for the current trial.
|
|
255
|
+
*/
|
|
256
|
+
protected capture: BehavioralCapture | null;
|
|
257
|
+
/**
|
|
258
|
+
* Session start time in milliseconds (Unix timestamp).
|
|
259
|
+
*/
|
|
260
|
+
protected sessionStartTime: number;
|
|
261
|
+
/**
|
|
262
|
+
* Counter for auto-incrementing trial indices.
|
|
263
|
+
*/
|
|
264
|
+
private trialCounter;
|
|
265
|
+
/**
|
|
266
|
+
* Creates a new BaseRecorder.
|
|
267
|
+
*
|
|
268
|
+
* @param config - Recorder configuration
|
|
269
|
+
*/
|
|
270
|
+
constructor(config?: RecorderConfig);
|
|
271
|
+
/**
|
|
272
|
+
* Gets platform-specific information.
|
|
273
|
+
*
|
|
274
|
+
* Subclasses must implement this method to provide platform metadata.
|
|
275
|
+
*
|
|
276
|
+
* @returns Platform information
|
|
277
|
+
*/
|
|
278
|
+
protected abstract getPlatformInfo(): PlatformInfo;
|
|
279
|
+
/**
|
|
280
|
+
* Starts a new trial.
|
|
281
|
+
*
|
|
282
|
+
* Creates a new trial state and optionally attaches behavioral capture
|
|
283
|
+
* to the target element.
|
|
284
|
+
*
|
|
285
|
+
* @param config - Trial configuration
|
|
286
|
+
* @throws Error if a trial is already in progress
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```typescript
|
|
290
|
+
* recorder.startTrial({
|
|
291
|
+
* trialType: "survey-text",
|
|
292
|
+
* targetElement: document.querySelector("textarea"),
|
|
293
|
+
* });
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
startTrial(config: TrialConfig): void;
|
|
297
|
+
/**
|
|
298
|
+
* Ends the current trial and returns the completed trial data.
|
|
299
|
+
*
|
|
300
|
+
* @param response - Optional response information
|
|
301
|
+
* @param platformData - Optional platform-specific trial data
|
|
302
|
+
* @returns The completed trial
|
|
303
|
+
* @throws Error if no trial is in progress
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* const trial = recorder.endTrial({
|
|
308
|
+
* type: "text",
|
|
309
|
+
* value: "User response",
|
|
310
|
+
* characterCount: 13,
|
|
311
|
+
* wordCount: 2,
|
|
312
|
+
* });
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
endTrial(response?: ResponseInfo, platformData?: Record<string, unknown>): CompletedTrial;
|
|
316
|
+
/**
|
|
317
|
+
* Attaches behavioral capture to an element for the current trial.
|
|
318
|
+
*
|
|
319
|
+
* This can be called after startTrial() if the target element becomes
|
|
320
|
+
* available later (e.g., dynamically created elements).
|
|
321
|
+
*
|
|
322
|
+
* @param element - The DOM element to attach to
|
|
323
|
+
* @throws Error if no trial is in progress
|
|
324
|
+
* @throws Error if capture is already attached
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* recorder.startTrial({ trialType: "survey" });
|
|
329
|
+
* // Later, when the element is available:
|
|
330
|
+
* recorder.attachCapture(document.querySelector("textarea")!);
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
attachCapture(element: HTMLElement, startTime?: number): void;
|
|
334
|
+
/**
|
|
335
|
+
* Detaches behavioral capture without ending the trial.
|
|
336
|
+
*
|
|
337
|
+
* Useful when the target element is removed before the trial ends.
|
|
338
|
+
*/
|
|
339
|
+
detachCapture(): void;
|
|
340
|
+
/**
|
|
341
|
+
* Exports the session data in SlopitSession format.
|
|
342
|
+
*
|
|
343
|
+
* @returns Complete session data
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* const session = recorder.exportSession();
|
|
348
|
+
* await fetch("/api/sessions", {
|
|
349
|
+
* method: "POST",
|
|
350
|
+
* body: JSON.stringify(session),
|
|
351
|
+
* });
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
exportSession(): SlopitSession;
|
|
355
|
+
/**
|
|
356
|
+
* Gets the current trial state.
|
|
357
|
+
*
|
|
358
|
+
* @returns Current trial state or null if no trial is in progress
|
|
359
|
+
*/
|
|
360
|
+
getCurrentTrial(): TrialState | null;
|
|
361
|
+
/**
|
|
362
|
+
* Gets all completed trials.
|
|
363
|
+
*
|
|
364
|
+
* @returns Array of completed trials
|
|
365
|
+
*/
|
|
366
|
+
getTrials(): CompletedTrial[];
|
|
367
|
+
/**
|
|
368
|
+
* Checks if a trial is currently in progress.
|
|
369
|
+
*
|
|
370
|
+
* @returns True if a trial is in progress
|
|
371
|
+
*/
|
|
372
|
+
isTrialInProgress(): boolean;
|
|
373
|
+
/**
|
|
374
|
+
* Gets the session ID.
|
|
375
|
+
*
|
|
376
|
+
* @returns The session identifier
|
|
377
|
+
*/
|
|
378
|
+
getSessionId(): string;
|
|
379
|
+
/**
|
|
380
|
+
* Resets the recorder for a new session.
|
|
381
|
+
*
|
|
382
|
+
* Clears all trials and generates a new session ID.
|
|
383
|
+
*/
|
|
384
|
+
reset(): void;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* MutationObserver-based element discovery utilities.
|
|
389
|
+
*
|
|
390
|
+
* @module
|
|
391
|
+
*/
|
|
392
|
+
/**
|
|
393
|
+
* Options for discovering an element in the DOM.
|
|
394
|
+
*/
|
|
395
|
+
interface DiscoverElementOptions {
|
|
396
|
+
/**
|
|
397
|
+
* CSS selector for the element to find.
|
|
398
|
+
*/
|
|
399
|
+
selector: string;
|
|
400
|
+
/**
|
|
401
|
+
* Container element or document to search within.
|
|
402
|
+
*/
|
|
403
|
+
container: HTMLElement | Document;
|
|
404
|
+
/**
|
|
405
|
+
* Timeout in milliseconds before giving up.
|
|
406
|
+
*
|
|
407
|
+
* @default 5000
|
|
408
|
+
*/
|
|
409
|
+
timeout?: number;
|
|
410
|
+
/**
|
|
411
|
+
* Callback invoked when the element is found.
|
|
412
|
+
*
|
|
413
|
+
* @param element - The discovered element
|
|
414
|
+
*/
|
|
415
|
+
onFound: (element: HTMLElement) => void;
|
|
416
|
+
/**
|
|
417
|
+
* Optional callback invoked if the element is not found within the timeout.
|
|
418
|
+
*/
|
|
419
|
+
onTimeout?: () => void;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Discovers an element in the DOM using a MutationObserver.
|
|
423
|
+
*
|
|
424
|
+
* If the element already exists, the callback is invoked immediately.
|
|
425
|
+
* Otherwise, a MutationObserver watches for DOM changes until the element
|
|
426
|
+
* appears or the timeout is reached.
|
|
427
|
+
*
|
|
428
|
+
* @param options - Discovery options
|
|
429
|
+
* @returns Cleanup function to disconnect the observer and cancel the timeout
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```typescript
|
|
433
|
+
* const cleanup = discoverElement({
|
|
434
|
+
* selector: "textarea.response-input",
|
|
435
|
+
* container: document.getElementById("trial-container")!,
|
|
436
|
+
* timeout: 3000,
|
|
437
|
+
* onFound: (element) => {
|
|
438
|
+
* capture.attach(element);
|
|
439
|
+
* },
|
|
440
|
+
* onTimeout: () => {
|
|
441
|
+
* console.warn("Target element not found within timeout");
|
|
442
|
+
* },
|
|
443
|
+
* });
|
|
444
|
+
*
|
|
445
|
+
* // Later, to cancel observation:
|
|
446
|
+
* cleanup();
|
|
447
|
+
* ```
|
|
448
|
+
*/
|
|
449
|
+
declare function discoverElement(options: DiscoverElementOptions): () => void;
|
|
450
|
+
/**
|
|
451
|
+
* Options for discovering multiple elements in the DOM.
|
|
452
|
+
*/
|
|
453
|
+
interface DiscoverElementsOptions {
|
|
454
|
+
/**
|
|
455
|
+
* CSS selector for elements to find.
|
|
456
|
+
*/
|
|
457
|
+
selector: string;
|
|
458
|
+
/**
|
|
459
|
+
* Container element or document to search within.
|
|
460
|
+
*/
|
|
461
|
+
container: HTMLElement | Document;
|
|
462
|
+
/**
|
|
463
|
+
* Minimum number of elements required.
|
|
464
|
+
*
|
|
465
|
+
* @default 1
|
|
466
|
+
*/
|
|
467
|
+
minCount?: number;
|
|
468
|
+
/**
|
|
469
|
+
* Timeout in milliseconds before giving up.
|
|
470
|
+
*
|
|
471
|
+
* @default 5000
|
|
472
|
+
*/
|
|
473
|
+
timeout?: number;
|
|
474
|
+
/**
|
|
475
|
+
* Callback invoked when the required number of elements are found.
|
|
476
|
+
*
|
|
477
|
+
* @param elements - The discovered elements
|
|
478
|
+
*/
|
|
479
|
+
onFound: (elements: HTMLElement[]) => void;
|
|
480
|
+
/**
|
|
481
|
+
* Optional callback invoked if the required elements are not found within the timeout.
|
|
482
|
+
*
|
|
483
|
+
* @param found - Elements that were found before timeout
|
|
484
|
+
*/
|
|
485
|
+
onTimeout?: (found: HTMLElement[]) => void;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Discovers multiple elements in the DOM using a MutationObserver.
|
|
489
|
+
*
|
|
490
|
+
* Waits until the minimum number of matching elements are present.
|
|
491
|
+
*
|
|
492
|
+
* @param options - Discovery options
|
|
493
|
+
* @returns Cleanup function to disconnect the observer and cancel the timeout
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```typescript
|
|
497
|
+
* const cleanup = discoverElements({
|
|
498
|
+
* selector: "input[type='text']",
|
|
499
|
+
* container: document.body,
|
|
500
|
+
* minCount: 3,
|
|
501
|
+
* onFound: (inputs) => {
|
|
502
|
+
* inputs.forEach((input) => capture.attach(input));
|
|
503
|
+
* },
|
|
504
|
+
* });
|
|
505
|
+
* ```
|
|
506
|
+
*/
|
|
507
|
+
declare function discoverElements(options: DiscoverElementsOptions): () => void;
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Common export utilities for creating SlopitSession data.
|
|
511
|
+
*
|
|
512
|
+
* @module
|
|
513
|
+
*/
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Options for creating session metadata.
|
|
517
|
+
*/
|
|
518
|
+
interface CreateSessionMetadataOptions extends SessionMetadata {
|
|
519
|
+
/**
|
|
520
|
+
* Session start time in milliseconds (Unix timestamp).
|
|
521
|
+
* If not provided, the current time is used.
|
|
522
|
+
*/
|
|
523
|
+
startTime?: number;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Creates the base structure for a SlopitSession.
|
|
527
|
+
*
|
|
528
|
+
* This function builds the non-trial portions of a session, including
|
|
529
|
+
* session ID, platform info, environment, and timing.
|
|
530
|
+
*
|
|
531
|
+
* @param platform - Platform information
|
|
532
|
+
* @param options - Session metadata options
|
|
533
|
+
* @returns Partial session with metadata populated
|
|
534
|
+
*
|
|
535
|
+
* @example
|
|
536
|
+
* ```typescript
|
|
537
|
+
* const sessionBase = createSessionMetadata(
|
|
538
|
+
* { name: "jspsych", version: "8.0.0", adapterVersion: "0.1.0" },
|
|
539
|
+
* { participantId: "P001", studyId: "study-123" }
|
|
540
|
+
* );
|
|
541
|
+
* ```
|
|
542
|
+
*/
|
|
543
|
+
declare function createSessionMetadata(platform: PlatformInfo, options?: CreateSessionMetadataOptions): Omit<SlopitSession, "trials" | "globalEvents" | "timing"> & {
|
|
544
|
+
timing: {
|
|
545
|
+
startTime: number;
|
|
546
|
+
};
|
|
547
|
+
};
|
|
548
|
+
/**
|
|
549
|
+
* Options for completing a session.
|
|
550
|
+
*/
|
|
551
|
+
interface CompleteSessionOptions {
|
|
552
|
+
/**
|
|
553
|
+
* End time in milliseconds (Unix timestamp).
|
|
554
|
+
* If not provided, the current time is used.
|
|
555
|
+
*/
|
|
556
|
+
endTime?: number;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Completes a session by adding timing and global events.
|
|
560
|
+
*
|
|
561
|
+
* @param sessionBase - The partial session from createSessionMetadata
|
|
562
|
+
* @param trials - Array of completed trials
|
|
563
|
+
* @param options - Completion options
|
|
564
|
+
* @returns Complete SlopitSession
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```typescript
|
|
568
|
+
* const session = completeSession(sessionBase, trials, { endTime: Date.now() });
|
|
569
|
+
* ```
|
|
570
|
+
*/
|
|
571
|
+
declare function completeSession(sessionBase: Omit<SlopitSession, "trials" | "globalEvents" | "timing"> & {
|
|
572
|
+
timing: {
|
|
573
|
+
startTime: number;
|
|
574
|
+
};
|
|
575
|
+
}, trials: SlopitTrial[], options?: CompleteSessionOptions): SlopitSession;
|
|
576
|
+
/**
|
|
577
|
+
* Trial data used for conversion.
|
|
578
|
+
*/
|
|
579
|
+
interface TrialConversionData {
|
|
580
|
+
/**
|
|
581
|
+
* Unique trial identifier.
|
|
582
|
+
*/
|
|
583
|
+
trialId: string;
|
|
584
|
+
/**
|
|
585
|
+
* Zero-indexed trial position.
|
|
586
|
+
*/
|
|
587
|
+
trialIndex: number;
|
|
588
|
+
/**
|
|
589
|
+
* Trial type identifier.
|
|
590
|
+
*/
|
|
591
|
+
trialType?: string | undefined;
|
|
592
|
+
/**
|
|
593
|
+
* Trial start time in milliseconds.
|
|
594
|
+
*/
|
|
595
|
+
startTime: number;
|
|
596
|
+
/**
|
|
597
|
+
* Trial end time in milliseconds.
|
|
598
|
+
*/
|
|
599
|
+
endTime: number;
|
|
600
|
+
/**
|
|
601
|
+
* Stimulus information.
|
|
602
|
+
*/
|
|
603
|
+
stimulus?: StimulusInfo | undefined;
|
|
604
|
+
/**
|
|
605
|
+
* Response information.
|
|
606
|
+
*/
|
|
607
|
+
response?: ResponseInfo | undefined;
|
|
608
|
+
/**
|
|
609
|
+
* Behavioral capture data.
|
|
610
|
+
*/
|
|
611
|
+
behavioral?: BehavioralData | undefined;
|
|
612
|
+
/**
|
|
613
|
+
* Flags from capture.
|
|
614
|
+
*/
|
|
615
|
+
flags?: CaptureFlag[] | undefined;
|
|
616
|
+
/**
|
|
617
|
+
* Platform-specific data.
|
|
618
|
+
*/
|
|
619
|
+
platformData?: Record<string, unknown> | undefined;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Converts trial data to the SlopitTrial format.
|
|
623
|
+
*
|
|
624
|
+
* @param data - Trial data to convert
|
|
625
|
+
* @returns SlopitTrial object
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* ```typescript
|
|
629
|
+
* const trial = convertTrialToSlopit({
|
|
630
|
+
* trialId: "trial-001",
|
|
631
|
+
* trialIndex: 0,
|
|
632
|
+
* trialType: "survey-text",
|
|
633
|
+
* startTime: 1700000000000,
|
|
634
|
+
* endTime: 1700000010000,
|
|
635
|
+
* response: { type: "text", value: "Hello", characterCount: 5, wordCount: 1 },
|
|
636
|
+
* behavioral: capture.getData(),
|
|
637
|
+
* flags: capture.getFlags(),
|
|
638
|
+
* });
|
|
639
|
+
* ```
|
|
640
|
+
*/
|
|
641
|
+
declare function convertTrialToSlopit(data: TrialConversionData): SlopitTrial;
|
|
642
|
+
/**
|
|
643
|
+
* Converts a CompletedTrial to a SlopitTrial.
|
|
644
|
+
*
|
|
645
|
+
* @param trial - Completed trial to convert
|
|
646
|
+
* @returns SlopitTrial object
|
|
647
|
+
*/
|
|
648
|
+
declare function convertCompletedTrialToSlopit(trial: CompletedTrial): SlopitTrial;
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Creates a response info object from text input.
|
|
652
|
+
*
|
|
653
|
+
* @param text - The text response
|
|
654
|
+
* @returns ResponseInfo object with word and character counts
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* ```typescript
|
|
658
|
+
* const response = createTextResponse("Hello world");
|
|
659
|
+
* // { type: "text", value: "Hello world", characterCount: 11, wordCount: 2 }
|
|
660
|
+
* ```
|
|
661
|
+
*/
|
|
662
|
+
declare function createTextResponse(text: string): ResponseInfo;
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Shared test utilities for adapter testing.
|
|
666
|
+
*
|
|
667
|
+
* @module
|
|
668
|
+
*/
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Creates a mock textarea element.
|
|
672
|
+
*
|
|
673
|
+
* @param id - Optional element ID
|
|
674
|
+
* @returns HTMLTextAreaElement suitable for testing
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```typescript
|
|
678
|
+
* const textarea = createMockTextarea("response-input");
|
|
679
|
+
* document.body.appendChild(textarea);
|
|
680
|
+
* capture.attach(textarea);
|
|
681
|
+
* ```
|
|
682
|
+
*/
|
|
683
|
+
declare function createMockTextarea(id?: string): HTMLTextAreaElement;
|
|
684
|
+
/**
|
|
685
|
+
* Creates a mock input element.
|
|
686
|
+
*
|
|
687
|
+
* @param id - Optional element ID
|
|
688
|
+
* @param type - Input type (defaults to "text")
|
|
689
|
+
* @returns HTMLInputElement suitable for testing
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* ```typescript
|
|
693
|
+
* const input = createMockInput("user-name", "text");
|
|
694
|
+
* document.body.appendChild(input);
|
|
695
|
+
* ```
|
|
696
|
+
*/
|
|
697
|
+
declare function createMockInput(id?: string, type?: string): HTMLInputElement;
|
|
698
|
+
/**
|
|
699
|
+
* Options for simulating a keystroke.
|
|
700
|
+
*/
|
|
701
|
+
interface SimulateKeystrokeOptions {
|
|
702
|
+
/**
|
|
703
|
+
* Physical key code (e.g., "KeyA", "Enter").
|
|
704
|
+
*/
|
|
705
|
+
code?: string;
|
|
706
|
+
/**
|
|
707
|
+
* Modifier key states.
|
|
708
|
+
*/
|
|
709
|
+
modifiers?: Partial<ModifierState>;
|
|
710
|
+
/**
|
|
711
|
+
* Whether to include the keyup event.
|
|
712
|
+
*
|
|
713
|
+
* @default true
|
|
714
|
+
*/
|
|
715
|
+
includeKeyUp?: boolean;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Simulates a keystroke on an element.
|
|
719
|
+
*
|
|
720
|
+
* Dispatches both keydown and keyup events unless includeKeyUp is false.
|
|
721
|
+
*
|
|
722
|
+
* @param element - Target element
|
|
723
|
+
* @param key - Key value (e.g., "a", "Enter", "Backspace")
|
|
724
|
+
* @param options - Simulation options
|
|
725
|
+
*
|
|
726
|
+
* @example
|
|
727
|
+
* ```typescript
|
|
728
|
+
* simulateKeystroke(textarea, "a", { code: "KeyA" });
|
|
729
|
+
* simulateKeystroke(textarea, "A", { code: "KeyA", modifiers: { shift: true } });
|
|
730
|
+
* simulateKeystroke(textarea, "Enter", { code: "Enter" });
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
733
|
+
declare function simulateKeystroke(element: HTMLElement, key: string, options?: SimulateKeystrokeOptions): void;
|
|
734
|
+
/**
|
|
735
|
+
* Simulates typing a string character by character.
|
|
736
|
+
*
|
|
737
|
+
* @param element - Target element
|
|
738
|
+
* @param text - Text to type
|
|
739
|
+
* @param delayMs - Delay between keystrokes in milliseconds (default 50)
|
|
740
|
+
* @returns Promise that resolves when typing is complete
|
|
741
|
+
*
|
|
742
|
+
* @example
|
|
743
|
+
* ```typescript
|
|
744
|
+
* await simulateTyping(textarea, "Hello world");
|
|
745
|
+
* ```
|
|
746
|
+
*/
|
|
747
|
+
declare function simulateTyping(element: HTMLElement, text: string, delayMs?: number): Promise<void>;
|
|
748
|
+
/**
|
|
749
|
+
* Options for simulating a paste event.
|
|
750
|
+
*/
|
|
751
|
+
interface SimulatePasteOptions {
|
|
752
|
+
/**
|
|
753
|
+
* Whether the paste should be prevented/blocked.
|
|
754
|
+
*
|
|
755
|
+
* @default false
|
|
756
|
+
*/
|
|
757
|
+
blocked?: boolean;
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Simulates a paste event on an element.
|
|
761
|
+
*
|
|
762
|
+
* @param element - Target element
|
|
763
|
+
* @param text - Text to paste
|
|
764
|
+
* @param options - Simulation options
|
|
765
|
+
*
|
|
766
|
+
* @example
|
|
767
|
+
* ```typescript
|
|
768
|
+
* simulatePaste(textarea, "Pasted content");
|
|
769
|
+
* simulatePaste(textarea, "Blocked paste", { blocked: true });
|
|
770
|
+
* ```
|
|
771
|
+
*/
|
|
772
|
+
declare function simulatePaste(element: HTMLElement, text: string, options?: SimulatePasteOptions): void;
|
|
773
|
+
/**
|
|
774
|
+
* Simulates a copy event on an element.
|
|
775
|
+
*
|
|
776
|
+
* @param element - Target element
|
|
777
|
+
* @param text - Text being copied (for verification purposes)
|
|
778
|
+
*
|
|
779
|
+
* @example
|
|
780
|
+
* ```typescript
|
|
781
|
+
* simulateCopy(textarea, "Selected text");
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
784
|
+
declare function simulateCopy(element: HTMLElement, text: string): void;
|
|
785
|
+
/**
|
|
786
|
+
* Simulates focus and blur events on an element.
|
|
787
|
+
*
|
|
788
|
+
* @param element - Target element
|
|
789
|
+
*
|
|
790
|
+
* @example
|
|
791
|
+
* ```typescript
|
|
792
|
+
* simulateFocusBlur(textarea); // Focus, then blur
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
declare function simulateFocusBlur(element: HTMLElement): void;
|
|
796
|
+
/**
|
|
797
|
+
* Simulates a focus event on an element.
|
|
798
|
+
*
|
|
799
|
+
* @param element - Target element
|
|
800
|
+
*/
|
|
801
|
+
declare function simulateFocus(element: HTMLElement): void;
|
|
802
|
+
/**
|
|
803
|
+
* Simulates a blur event on an element.
|
|
804
|
+
*
|
|
805
|
+
* @param element - Target element
|
|
806
|
+
*/
|
|
807
|
+
declare function simulateBlur(element: HTMLElement): void;
|
|
808
|
+
/**
|
|
809
|
+
* Simulates a mouse click on an element.
|
|
810
|
+
*
|
|
811
|
+
* @param element - Target element
|
|
812
|
+
* @param x - X coordinate
|
|
813
|
+
* @param y - Y coordinate
|
|
814
|
+
*/
|
|
815
|
+
declare function simulateClick(element: HTMLElement, x?: number, y?: number): void;
|
|
816
|
+
/**
|
|
817
|
+
* Simulates mouse movement on an element.
|
|
818
|
+
*
|
|
819
|
+
* @param element - Target element
|
|
820
|
+
* @param x - X coordinate
|
|
821
|
+
* @param y - Y coordinate
|
|
822
|
+
*/
|
|
823
|
+
declare function simulateMouseMove(element: HTMLElement, x: number, y: number): void;
|
|
824
|
+
/**
|
|
825
|
+
* Creates mock behavioral data for testing.
|
|
826
|
+
*
|
|
827
|
+
* @returns BehavioralData with sample events and metrics
|
|
828
|
+
*
|
|
829
|
+
* @example
|
|
830
|
+
* ```typescript
|
|
831
|
+
* const mockData = createMockBehavioralData();
|
|
832
|
+
* expect(mockData.keystrokes).toHaveLength(3);
|
|
833
|
+
* ```
|
|
834
|
+
*/
|
|
835
|
+
declare function createMockBehavioralData(): BehavioralData;
|
|
836
|
+
/**
|
|
837
|
+
* Mock DataTransfer class for clipboard events.
|
|
838
|
+
*/
|
|
839
|
+
declare class MockDataTransfer {
|
|
840
|
+
private data;
|
|
841
|
+
setData(format: string, data: string): void;
|
|
842
|
+
getData(format: string): string;
|
|
843
|
+
clearData(format?: string): void;
|
|
844
|
+
get types(): string[];
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Mock ClipboardEvent class.
|
|
848
|
+
*/
|
|
849
|
+
declare class MockClipboardEvent extends Event {
|
|
850
|
+
clipboardData: MockDataTransfer;
|
|
851
|
+
constructor(type: string, init?: {
|
|
852
|
+
clipboardData?: MockDataTransfer;
|
|
853
|
+
bubbles?: boolean;
|
|
854
|
+
cancelable?: boolean;
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Utility function for creating a delay.
|
|
859
|
+
*
|
|
860
|
+
* @param ms - Delay in milliseconds
|
|
861
|
+
* @returns Promise that resolves after the delay
|
|
862
|
+
*/
|
|
863
|
+
declare function delay(ms: number): Promise<void>;
|
|
864
|
+
/**
|
|
865
|
+
* Waits for a condition to become true.
|
|
866
|
+
*
|
|
867
|
+
* @param condition - Function that returns true when the condition is met
|
|
868
|
+
* @param timeout - Maximum time to wait in milliseconds
|
|
869
|
+
* @param interval - Polling interval in milliseconds
|
|
870
|
+
* @returns Promise that resolves when the condition is met
|
|
871
|
+
* @throws Error if timeout is reached
|
|
872
|
+
*
|
|
873
|
+
* @example
|
|
874
|
+
* ```typescript
|
|
875
|
+
* await waitFor(() => document.querySelector(".loaded") !== null);
|
|
876
|
+
* ```
|
|
877
|
+
*/
|
|
878
|
+
declare function waitFor(condition: () => boolean, timeout?: number, interval?: number): Promise<void>;
|
|
879
|
+
|
|
880
|
+
export { type AdapterConfig, type AdapterData, BaseRecorder, type CompleteSessionOptions, type CompletedTrial, type CreateSessionMetadataOptions, type DiscoverElementOptions, type DiscoverElementsOptions, MockClipboardEvent, MockDataTransfer, type RecorderConfig, type SessionMetadata, type SimulateKeystrokeOptions, type SimulatePasteOptions, type TrialConfig, type TrialConversionData, type TrialState, completeSession, convertCompletedTrialToSlopit, convertTrialToSlopit, createMockBehavioralData, createMockInput, createMockTextarea, createSessionMetadata, createTextResponse, delay, discoverElement, discoverElements, simulateBlur, simulateClick, simulateCopy, simulateFocus, simulateFocusBlur, simulateKeystroke, simulateMouseMove, simulatePaste, simulateTyping, waitFor };
|