@serenity-js/core 3.42.2 → 3.43.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/esm/Serenity.d.ts +3 -1
  3. package/esm/Serenity.d.ts.map +1 -1
  4. package/esm/Serenity.js +4 -3
  5. package/esm/Serenity.js.map +1 -1
  6. package/esm/screenplay/Actor.d.ts +1 -1
  7. package/esm/screenplay/Actor.d.ts.map +1 -1
  8. package/esm/screenplay/Actor.js +5 -6
  9. package/esm/screenplay/Actor.js.map +1 -1
  10. package/esm/screenplay/abilities/Discardable.d.ts +3 -2
  11. package/esm/screenplay/abilities/Discardable.d.ts.map +1 -1
  12. package/esm/screenplay/abilities/Discardable.js +25 -1
  13. package/esm/screenplay/abilities/Discardable.js.map +1 -1
  14. package/esm/screenplay/abilities/Initialisable.d.ts +4 -3
  15. package/esm/screenplay/abilities/Initialisable.d.ts.map +1 -1
  16. package/esm/screenplay/abilities/Initialisable.js +22 -1
  17. package/esm/screenplay/abilities/Initialisable.js.map +1 -1
  18. package/esm/stage/ActorLifecycleManager.d.ts +191 -0
  19. package/esm/stage/ActorLifecycleManager.d.ts.map +1 -0
  20. package/esm/stage/ActorLifecycleManager.js +255 -0
  21. package/esm/stage/ActorLifecycleManager.js.map +1 -0
  22. package/esm/stage/Stage.d.ts +22 -38
  23. package/esm/stage/Stage.d.ts.map +1 -1
  24. package/esm/stage/Stage.js +61 -117
  25. package/esm/stage/Stage.js.map +1 -1
  26. package/esm/stage/StageManager.d.ts +2 -4
  27. package/esm/stage/StageManager.d.ts.map +1 -1
  28. package/esm/stage/StageManager.js +0 -5
  29. package/esm/stage/StageManager.js.map +1 -1
  30. package/esm/stage/index.d.ts +1 -0
  31. package/esm/stage/index.d.ts.map +1 -1
  32. package/esm/stage/index.js +1 -0
  33. package/esm/stage/index.js.map +1 -1
  34. package/lib/Serenity.d.ts +3 -1
  35. package/lib/Serenity.d.ts.map +1 -1
  36. package/lib/Serenity.js +4 -3
  37. package/lib/Serenity.js.map +1 -1
  38. package/lib/screenplay/Actor.d.ts +1 -1
  39. package/lib/screenplay/Actor.d.ts.map +1 -1
  40. package/lib/screenplay/Actor.js +4 -5
  41. package/lib/screenplay/Actor.js.map +1 -1
  42. package/lib/screenplay/abilities/Discardable.d.ts +3 -2
  43. package/lib/screenplay/abilities/Discardable.d.ts.map +1 -1
  44. package/lib/screenplay/abilities/Discardable.js +27 -0
  45. package/lib/screenplay/abilities/Discardable.js.map +1 -1
  46. package/lib/screenplay/abilities/Initialisable.d.ts +4 -3
  47. package/lib/screenplay/abilities/Initialisable.d.ts.map +1 -1
  48. package/lib/screenplay/abilities/Initialisable.js +24 -0
  49. package/lib/screenplay/abilities/Initialisable.js.map +1 -1
  50. package/lib/stage/ActorLifecycleManager.d.ts +191 -0
  51. package/lib/stage/ActorLifecycleManager.d.ts.map +1 -0
  52. package/lib/stage/ActorLifecycleManager.js +259 -0
  53. package/lib/stage/ActorLifecycleManager.js.map +1 -0
  54. package/lib/stage/Stage.d.ts +22 -38
  55. package/lib/stage/Stage.d.ts.map +1 -1
  56. package/lib/stage/Stage.js +57 -113
  57. package/lib/stage/Stage.js.map +1 -1
  58. package/lib/stage/StageManager.d.ts +2 -4
  59. package/lib/stage/StageManager.d.ts.map +1 -1
  60. package/lib/stage/StageManager.js +0 -5
  61. package/lib/stage/StageManager.js.map +1 -1
  62. package/lib/stage/index.d.ts +1 -0
  63. package/lib/stage/index.d.ts.map +1 -1
  64. package/lib/stage/index.js +1 -0
  65. package/lib/stage/index.js.map +1 -1
  66. package/package.json +3 -3
  67. package/src/Serenity.ts +5 -2
  68. package/src/screenplay/Actor.ts +6 -9
  69. package/src/screenplay/abilities/Discardable.ts +7 -2
  70. package/src/screenplay/abilities/Initialisable.ts +10 -3
  71. package/src/stage/ActorLifecycleManager.ts +314 -0
  72. package/src/stage/Stage.ts +87 -165
  73. package/src/stage/StageManager.ts +3 -7
  74. package/src/stage/index.ts +1 -0
@@ -1,17 +1,13 @@
1
- import { ensure, isDefined, property } from 'tiny-types';
1
+ import { ensure, isDefined } from 'tiny-types';
2
2
 
3
3
  import type { SerenityConfig } from '../config/index.js';
4
4
  import {
5
- ConfigurationError,
6
5
  ErrorFactory,
7
6
  type ErrorOptions,
8
7
  LogicError,
9
- RaiseErrors,
10
8
  type RuntimeError
11
9
  } from '../errors/index.js';
12
10
  import {
13
- ActorEntersStage,
14
- ActorSpotlighted,
15
11
  ActorStageExitAttempted,
16
12
  ActorStageExitCompleted,
17
13
  ActorStageExitFailed,
@@ -23,9 +19,11 @@ import {
23
19
  TestRunFinishes
24
20
  } from '../events/index.js';
25
21
  import { type ActivityDetails, CorrelationId, type CorrelationIdFactory, Name } from '../model/index.js';
26
- import { Actor, type Clock, type Duration, ScheduleWork, type Timestamp } from '../screenplay/index.js';
27
- import type { ListensToDomainEvents } from '../stage/index.js';
22
+ import type { Actor} from '../screenplay/index.js';
23
+ import type { Clock, Duration, Timestamp } from '../screenplay/index.js';
24
+ import { ActorLifecycleManager, type StageFocus } from './ActorLifecycleManager.js';
28
25
  import type { Cast } from './Cast.js';
26
+ import type { ListensToDomainEvents } from './ListensToDomainEvents.js';
29
27
  import type { StageManager } from './StageManager.js';
30
28
 
31
29
  /**
@@ -48,50 +46,33 @@ export class Stage implements EmitsDomainEvents {
48
46
 
49
47
  public static readonly unknownSceneId = new CorrelationId('unknown')
50
48
 
51
- /**
52
- * Actors instantiated after the scene has started,
53
- * who will be dismissed when the scene finishes.
54
- */
55
- private actorsOnFrontStage: Map<string, Actor> = new Map<string, Actor>();
56
-
57
- /**
58
- * Actors instantiated before the scene has started,
59
- * who will be dismissed when the test run finishes.
60
- */
61
- private actorsOnBackstage: Map<string, Actor> = new Map<string, Actor>();
62
-
63
- private actorsOnStage: Map<string, Actor> = this.actorsOnBackstage;
64
-
65
- /**
66
- * The most recent actor referenced via the [`Actor`](https://serenity-js.org/api/core/class/Actor/) method
67
- */
68
- private actorInTheSpotlight: Actor = undefined;
69
-
70
- /**
71
- * The scene in which the spotlight was last set.
72
- * Used to detect when the spotlight shifts to a different scene context.
73
- */
74
- private sceneOfSpotlightedActor: CorrelationId = undefined;
75
-
76
49
  private currentActivity: { id: CorrelationId, details: ActivityDetails } = undefined;
77
50
 
78
51
  private currentScene: CorrelationId = Stage.unknownSceneId;
79
52
 
53
+ private readonly actorLifecycleManager: ActorLifecycleManager
54
+
80
55
  /**
81
- * @param cast
82
- * @param manager
83
- * @param errors
84
- * @param clock
85
- * @param interactionTimeout
86
- * @param sceneIdFactory
56
+ * Creates a new Stage instance.
57
+ *
58
+ * @param cast - The default cast to use for preparing actors
59
+ * @param manager - The stage manager responsible for notifying listeners of domain events
60
+ * @param errors - Factory for creating runtime errors with proper context
61
+ * @param clock - Clock used for timestamping domain events
62
+ * @param interactionTimeout - Default timeout for actor interactions
63
+ * @param sceneIdFactory - Factory for creating scene correlation IDs
64
+ * @param actorLifecycleManager - Optional custom ActorLifecycleManager instance.
65
+ * When provided, allows test runner adapters to control actor lifecycle programmatically.
66
+ * If not provided, a default manager is created.
87
67
  */
88
68
  constructor(
89
- private cast: Cast,
90
- private manager: StageManager,
69
+ cast: Cast,
70
+ private readonly manager: StageManager,
91
71
  private errors: ErrorFactory,
92
72
  private readonly clock: Clock,
93
- private interactionTimeout: Duration,
73
+ interactionTimeout: Duration,
94
74
  private readonly sceneIdFactory: CorrelationIdFactory = CorrelationId,
75
+ actorLifecycleManager?: ActorLifecycleManager,
95
76
  ) {
96
77
  ensure('Cast', cast, isDefined());
97
78
  ensure('StageManager', manager, isDefined());
@@ -99,13 +80,18 @@ export class Stage implements EmitsDomainEvents {
99
80
  ensure('Clock', clock, isDefined());
100
81
  ensure('interactionTimeout', interactionTimeout, isDefined());
101
82
  ensure('sceneIdFactory', sceneIdFactory, isDefined());
83
+
84
+ this.actorLifecycleManager = actorLifecycleManager ?? new ActorLifecycleManager(cast, this.clock, interactionTimeout);
85
+ this.actorLifecycleManager.assignTo(this);
102
86
  }
103
87
 
104
88
  configure(options: Pick<SerenityConfig, 'actors' | 'cueTimeout' | 'interactionTimeout' | 'diffFormatter'>): void {
105
- this.interactionTimeout = options.interactionTimeout || this.interactionTimeout;
89
+ if (options.interactionTimeout) {
90
+ this.actorLifecycleManager.configure({ interactionTimeout: options.interactionTimeout });
91
+ }
106
92
 
107
93
  if (options.actors) {
108
- this.engage(options.actors);
94
+ this.actorLifecycleManager.engage(options.actors);
109
95
  }
110
96
 
111
97
  if (options.cueTimeout) {
@@ -134,52 +120,7 @@ export class Stage implements EmitsDomainEvents {
134
120
  * Case-sensitive name of the Actor, e.g. `Alice`
135
121
  */
136
122
  actor(name: string): Actor {
137
- if (! this.instantiatedActorCalled(name)) {
138
- let actor;
139
- try {
140
- const newActor = new Actor(name, this, [
141
- new RaiseErrors(this),
142
- new ScheduleWork(this.clock, this.interactionTimeout)
143
- ]);
144
-
145
- actor = this.cast.prepare(newActor);
146
- }
147
- catch (error) {
148
- throw new ConfigurationError(`${ this.typeOf(this.cast) } encountered a problem when preparing actor "${ name }" for stage`, error);
149
- }
150
-
151
- if (! (actor instanceof Actor)) {
152
- throw new ConfigurationError(`Instead of a new instance of actor "${ name }", ${ this.typeOf(this.cast) } returned ${ actor }`);
153
- }
154
-
155
- this.actorsOnStage.set(name, actor);
156
-
157
- this.announce(
158
- new ActorEntersStage(
159
- this.currentScene,
160
- actor.toJSON(),
161
- )
162
- )
163
- }
164
-
165
- const previousActorInSpotlight = this.actorInTheSpotlight;
166
- const previousSceneOfSpotlightedActor = this.sceneOfSpotlightedActor;
167
- this.actorInTheSpotlight = this.instantiatedActorCalled(name);
168
- this.sceneOfSpotlightedActor = this.currentScene;
169
-
170
- const spotlightShifted = this.actorInTheSpotlight !== previousActorInSpotlight
171
- || ! this.currentScene.equals(previousSceneOfSpotlightedActor);
172
-
173
- if (spotlightShifted) {
174
- this.announce(
175
- new ActorSpotlighted(
176
- this.currentScene,
177
- this.actorInTheSpotlight.toJSON(),
178
- )
179
- );
180
- }
181
-
182
- return this.actorInTheSpotlight;
123
+ return this.actorLifecycleManager.actor(name);
183
124
  }
184
125
 
185
126
  /**
@@ -190,18 +131,14 @@ export class Stage implements EmitsDomainEvents {
190
131
  * If no [`Actor`](https://serenity-js.org/api/core/class/Actor/) has been activated yet
191
132
  */
192
133
  theActorInTheSpotlight(): Actor {
193
- if (! this.actorInTheSpotlight) {
194
- throw new LogicError(`There is no actor in the spotlight yet. Make sure you instantiate one with stage.actor(actorName) before calling this method.`);
195
- }
196
-
197
- return this.actorInTheSpotlight;
134
+ return this.actorLifecycleManager.actorInTheSpotlight();
198
135
  }
199
136
 
200
137
  /**
201
138
  * Returns `true` if there is an [`Actor`](https://serenity-js.org/api/core/class/Actor/) in the spotlight, `false` otherwise.
202
139
  */
203
140
  theShowHasStarted(): boolean {
204
- return !! this.actorInTheSpotlight;
141
+ return this.actorLifecycleManager.hasActorInTheSpotlight();
205
142
  }
206
143
 
207
144
  /**
@@ -211,12 +148,12 @@ export class Stage implements EmitsDomainEvents {
211
148
  * @param actors
212
149
  */
213
150
  engage(actors: Cast): void {
214
- this.cast = ensure('actors', actors, isDefined(), property('prepare', isDefined()));
151
+ this.actorLifecycleManager.engage(actors);
215
152
  }
216
153
 
217
154
  /**
218
155
  * Assigns listeners to be notified of [Serenity/JS domain events](https://serenity-js.org/api/core-events/class/DomainEvent/)
219
- * emitted via [`Stage.announce`](https://serenity-js.org/api/core/class/Stage/#announce).s
156
+ * emitted via [`Stage.announce`](https://serenity-js.org/api/core/class/Stage/#announce).
220
157
  *
221
158
  * @param listeners
222
159
  */
@@ -238,24 +175,70 @@ export class Stage implements EmitsDomainEvents {
238
175
 
239
176
  private announceSingle(event: DomainEvent): void {
240
177
  if (event instanceof SceneStarts) {
241
- this.actorsOnStage = this.actorsOnFrontStage;
178
+ this.actorLifecycleManager.switchFocus('foreground');
242
179
  }
243
180
 
244
181
  if (event instanceof SceneFinishes || event instanceof TestRunFinishes) {
245
- this.notifyOfStageExit(this.currentSceneId());
182
+ this.notifyOfStageExit(this.actorLifecycleManager.currentFocus());
246
183
  }
247
184
 
248
185
  this.manager.notifyOf(event);
249
186
 
250
187
  if (event instanceof SceneFinishes) {
251
- this.dismiss(this.actorsOnStage);
252
-
253
- this.actorsOnStage = this.actorsOnBackstage;
188
+ this.dismissActorsIn('foreground');
189
+ this.actorLifecycleManager.switchFocus('background');
254
190
  }
255
191
 
256
192
  if (event instanceof TestRunFinishes) {
257
- this.dismiss(this.actorsOnStage);
193
+ this.dismissActorsIn('background');
194
+ }
195
+ }
196
+
197
+ private notifyOfStageExit(focus: StageFocus): void {
198
+ for (const actor of this.actorLifecycleManager.actorsIn(focus)) {
199
+ this.announce(new ActorStageExitStarts(
200
+ this.currentSceneId(),
201
+ actor.toJSON(),
202
+ this.currentTime(),
203
+ ));
204
+ }
205
+ }
206
+
207
+ private async dismissActorsIn(focus: StageFocus): Promise<void> {
208
+ const actors = this.actorLifecycleManager.actorsIn(focus);
209
+
210
+ this.actorLifecycleManager.clearSpotlightIfIn(focus);
211
+
212
+ // Wait for the Photographer to finish taking any screenshots
213
+ await this.manager.waitForAsyncOperationsToComplete();
214
+
215
+ const actorsToDismiss = new Map<Actor, CorrelationId>(actors.map(actor => [ actor, CorrelationId.create() ]));
216
+
217
+ for (const [ actor, correlationId ] of actorsToDismiss) {
218
+ this.announce(new ActorStageExitAttempted(
219
+ correlationId,
220
+ new Name(actor.name),
221
+ this.currentTime(),
222
+ ));
223
+ }
224
+
225
+ // Try to dismiss each actor
226
+ for (const [ actor, correlationId ] of actorsToDismiss) {
227
+ try {
228
+ await actor.dismiss();
229
+
230
+ this.announce(new ActorStageExitCompleted(correlationId, new Name(actor.name), this.currentTime()));
231
+ }
232
+ catch (error) {
233
+ this.announce(new ActorStageExitFailed(
234
+ error,
235
+ correlationId,
236
+ this.currentTime()
237
+ ));
238
+ }
258
239
  }
240
+
241
+ this.actorLifecycleManager.clearActorsIn(focus);
259
242
  }
260
243
 
261
244
  /**
@@ -263,7 +246,7 @@ export class Stage implements EmitsDomainEvents {
263
246
  * [`DomainEvent`](https://serenity-js.org/api/core-events/class/DomainEvent/) objects are instantiated by you programmatically.
264
247
  */
265
248
  currentTime(): Timestamp {
266
- return this.manager.currentTime();
249
+ return this.clock.now();
267
250
  }
268
251
 
269
252
  /**
@@ -343,65 +326,4 @@ export class Stage implements EmitsDomainEvents {
343
326
  ...options,
344
327
  });
345
328
  }
346
-
347
- private instantiatedActorCalled(name: string): Actor | undefined {
348
- return this.actorsOnBackstage.has(name)
349
- ? this.actorsOnBackstage.get(name)
350
- : this.actorsOnFrontStage.get(name)
351
- }
352
-
353
- private notifyOfStageExit(sceneId: CorrelationId): void {
354
- for (const actor of this.actorsOnStage.values()) {
355
- this.announce(new ActorStageExitStarts(
356
- sceneId,
357
- actor.toJSON(),
358
- this.currentTime(),
359
- ));
360
- }
361
- }
362
-
363
- private async dismiss(activeActors: Map<string, Actor>): Promise<void> {
364
- const actors = Array.from(activeActors.values());
365
-
366
- if (actors.includes(this.actorInTheSpotlight)) {
367
- this.actorInTheSpotlight = undefined;
368
- }
369
-
370
- // Wait for the Photographer to finish taking any screenshots
371
- await this.manager.waitForAsyncOperationsToComplete();
372
-
373
- const actorsToDismiss = new Map<Actor, CorrelationId>(actors.map(actor => [actor, CorrelationId.create()]));
374
-
375
- for (const [ actor, correlationId ] of actorsToDismiss) {
376
- this.announce(new ActorStageExitAttempted(
377
- correlationId,
378
- new Name(actor.name),
379
- this.currentTime(),
380
- ));
381
- }
382
-
383
- // Try to dismiss each actor
384
- for (const [ actor, correlationId ] of actorsToDismiss) {
385
- try {
386
- await actor.dismiss();
387
-
388
- this.announce(new ActorStageExitCompleted(correlationId, new Name(actor.name), this.currentTime()));
389
- }
390
- catch (error) {
391
- this.announce(new ActorStageExitFailed(
392
- error,
393
- correlationId,
394
- this.currentTime()
395
- ));
396
- }
397
- }
398
-
399
- activeActors.clear();
400
- }
401
-
402
- private typeOf(cast: Cast): string {
403
- return cast.constructor === Object
404
- ? 'Cast'
405
- : cast.constructor.name;
406
- }
407
329
  }
@@ -1,17 +1,17 @@
1
1
  import type { DomainEvent } from '../events/index.js';
2
2
  import { AsyncOperationAttempted, AsyncOperationCompleted, AsyncOperationFailed } from '../events/index.js';
3
3
  import type { CorrelationId, Description, Name } from '../model/index.js';
4
- import type { Clock, Duration, TellsTime, Timestamp } from '../screenplay/index.js';
4
+ import type { Clock, Duration, Timestamp } from '../screenplay/index.js';
5
5
  import type { ListensToDomainEvents } from '../stage/index.js';
6
6
 
7
7
  /**
8
8
  * @group Stage
9
9
  */
10
- export class StageManager implements TellsTime {
10
+ export class StageManager {
11
11
  private readonly subscribers: ListensToDomainEvents[] = [];
12
12
  private readonly wip: WIP;
13
13
 
14
- constructor(private cueTimeout: Duration, private readonly clock: Clock) {
14
+ constructor(private cueTimeout: Duration, clock: Clock) {
15
15
  this.wip = new WIP(cueTimeout, clock);
16
16
  }
17
17
 
@@ -70,10 +70,6 @@ export class StageManager implements TellsTime {
70
70
  throw new Error(this.wip.descriptionOfTimedOutOperations());
71
71
  }
72
72
  }
73
-
74
- currentTime(): Timestamp {
75
- return this.clock.now();
76
- }
77
73
  }
78
74
 
79
75
  /**
@@ -1,3 +1,4 @@
1
+ export * from './ActorLifecycleManager.js';
1
2
  export * from './Cast.js';
2
3
  export * from './crew/index.js';
3
4
  export * from './Extras.js';