@serenity-js/core 3.42.1 → 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 +20 -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 +4 -4
  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
@@ -0,0 +1,255 @@
1
+ import { ensure, isDefined, property } from 'tiny-types';
2
+ import { ConfigurationError, LogicError, RaiseErrors } from '../errors/index.js';
3
+ import { ActorEntersStage, ActorSpotlighted, } from '../events/index.js';
4
+ import { CorrelationId } from '../model/index.js';
5
+ import { Actor, ScheduleWork } from '../screenplay/index.js';
6
+ /**
7
+ * Manages the lifecycle of [actors](https://serenity-js.org/api/core/class/Actor/) on the [stage](https://serenity-js.org/api/core/class/Stage/),
8
+ * including their creation, retrieval, and tracking of which actors are in the foreground (scene-scoped)
9
+ * versus background (test run-scoped).
10
+ *
11
+ * The `ActorLifecycleManager` is responsible for:
12
+ * - Instantiating and caching actors via the configured [cast](https://serenity-js.org/api/core/class/Cast/)
13
+ * - Tracking which actor is currently in the spotlight (active)
14
+ * - Managing the focus area (foreground vs background) where new actors are created
15
+ * - Providing access to actors for dismissal when scenes or test runs finish
16
+ *
17
+ * ## Default behaviour
18
+ *
19
+ * By default, actors created before the actual test scenario starts, e.g. in beforeAll hooks, are placed in the `'background'` focus area.
20
+ * When a [`SceneStarts`](https://serenity-js.org/api/core-events/class/SceneStarts/) event is announced,
21
+ * the focus switches to `'foreground'`. When a [`SceneFinishes`](https://serenity-js.org/api/core-events/class/SceneFinishes/)
22
+ * event is announced, foreground actors are dismissed, their abilities [discarded](https://serenity-js.org/api/core/interface/Discardable/)
23
+ * and focus returns to `'background'`.
24
+ *
25
+ * ## Custom lifecycle management
26
+ *
27
+ * Test runner adapters like [`@serenity-js/playwright-test`](https://serenity-js.org/api/playwright-test/),
28
+ * where test execution and reporting happen in separate processes, can inject a custom `ActorLifecycleManager` instance
29
+ * to control actor lifecycle programmatically.
30
+ *
31
+ * ```typescript
32
+ * const actorLifecycleManager = new ActorLifecycleManager(cast, clock, interactionTimeout);
33
+ * const serenity = new Serenity(clock, cueTimeout, actorLifecycleManager);
34
+ *
35
+ * // At the start of each test:
36
+ * actorLifecycleManager.switchFocus('foreground');
37
+ * ```
38
+ *
39
+ * ## Learn more
40
+ * - [`Stage`](https://serenity-js.org/api/core/class/Stage/)
41
+ * - [`Cast`](https://serenity-js.org/api/core/class/Cast/)
42
+ * - [`Actor`](https://serenity-js.org/api/core/class/Actor/)
43
+ *
44
+ * @group Stage
45
+ */
46
+ export class ActorLifecycleManager {
47
+ cast;
48
+ clock;
49
+ interactionTimeout;
50
+ /**
51
+ * The most recent actor referenced via the {@apilink actor} method
52
+ */
53
+ currentActor;
54
+ /**
55
+ * The scene in which the spotlight was last set.
56
+ * Used to detect when the spotlight shifts to a different scene context.
57
+ */
58
+ currentActorScene = new CorrelationId('unknown');
59
+ currentFocusValue = 'background';
60
+ actors = {
61
+ 'foreground': new Map(),
62
+ 'background': new Map(),
63
+ };
64
+ stage;
65
+ constructor(cast, clock, interactionTimeout) {
66
+ this.cast = cast;
67
+ this.clock = clock;
68
+ this.interactionTimeout = interactionTimeout;
69
+ }
70
+ /**
71
+ * Configures the manager with new settings.
72
+ *
73
+ * @param options - Configuration options
74
+ * @param options.interactionTimeout - The maximum time to wait for an interaction to complete
75
+ */
76
+ configure({ interactionTimeout }) {
77
+ this.interactionTimeout = interactionTimeout;
78
+ }
79
+ /**
80
+ * Associates this manager with a [`Stage`](https://serenity-js.org/api/core/class/Stage/) instance.
81
+ *
82
+ * This method is called automatically by the `Stage` during construction.
83
+ * It establishes the bidirectional relationship between the manager and the stage,
84
+ * allowing the manager to emit [domain events](https://serenity-js.org/api/core-events/class/DomainEvent/)
85
+ * when actors enter the stage or are spotlighted.
86
+ *
87
+ * @param stage - The Stage instance to associate with this manager
88
+ */
89
+ assignTo(stage) {
90
+ this.stage = stage;
91
+ }
92
+ /**
93
+ * Configures the manager to use the provided [cast](https://serenity-js.org/api/core/class/Cast/) for preparing actors.
94
+ *
95
+ * @param actors - The cast to use for preparing new actors
96
+ *
97
+ * @throws [`ConfigurationError`](https://serenity-js.org/api/core/class/ConfigurationError/)
98
+ * If the provided cast is not defined or doesn't have a `prepare` method
99
+ */
100
+ engage(actors) {
101
+ this.cast = ensure('actors', actors, isDefined(), property('prepare', isDefined()));
102
+ }
103
+ /**
104
+ * Returns the current [cast](https://serenity-js.org/api/core/class/Cast/) used for preparing actors.
105
+ *
106
+ * @returns The currently configured cast
107
+ */
108
+ currentCast() {
109
+ return this.cast;
110
+ }
111
+ /**
112
+ * Instantiates a new [`Actor`](https://serenity-js.org/api/core/class/Actor/) or fetches an existing one
113
+ * identified by their name if they've already been instantiated.
114
+ *
115
+ * When a new actor is instantiated, an [`ActorEntersStage`](https://serenity-js.org/api/core-events/class/ActorEntersStage/)
116
+ * event is announced. When the spotlight shifts to a different actor (or the same actor in a different scene),
117
+ * an [`ActorSpotlighted`](https://serenity-js.org/api/core-events/class/ActorSpotlighted/) event is announced.
118
+ *
119
+ * Actors are first looked up in the `'background'` focus area, then in `'foreground'`.
120
+ * New actors are always created in the current focus area.
121
+ *
122
+ * @param name - Case-sensitive name of the Actor, e.g. `Alice`
123
+ * @returns The actor with the given name
124
+ */
125
+ actor(name) {
126
+ if (!this.existingActorCalled(name)) {
127
+ const actor = this.prepareActor(new Actor(name, this.stage, [
128
+ new RaiseErrors(this.stage),
129
+ new ScheduleWork(this.clock, this.interactionTimeout)
130
+ ]));
131
+ this.actors[this.currentFocusValue].set(actor.name, actor);
132
+ this.stage.announce(new ActorEntersStage(this.stage.currentSceneId(), actor.toJSON(), this.stage.currentTime()));
133
+ }
134
+ const previousActorInSpotlight = this.currentActor;
135
+ const previousSceneOfSpotlightedActor = this.currentActorScene;
136
+ this.currentActor = this.existingActorCalled(name);
137
+ this.currentActorScene = this.stage.currentSceneId();
138
+ const spotlightShifted = this.currentActor !== previousActorInSpotlight
139
+ || !this.stage.currentSceneId().equals(previousSceneOfSpotlightedActor);
140
+ if (spotlightShifted) {
141
+ this.stage.announce(new ActorSpotlighted(this.stage.currentSceneId(), this.currentActor.toJSON(), this.stage.currentTime()));
142
+ }
143
+ return this.currentActor;
144
+ }
145
+ prepareActor(actor) {
146
+ let preparedActor;
147
+ try {
148
+ preparedActor = this.cast.prepare(actor);
149
+ }
150
+ catch (error) {
151
+ throw new ConfigurationError(`${this.typeOf(this.cast)} encountered a problem when preparing actor "${actor.name}" for stage`, error);
152
+ }
153
+ if (!(preparedActor instanceof Actor)) {
154
+ throw new ConfigurationError(`Instead of a new instance of actor "${actor.name}", ${this.typeOf(this.cast)} returned ${preparedActor}`);
155
+ }
156
+ return preparedActor;
157
+ }
158
+ typeOf(cast) {
159
+ return cast.constructor === Object
160
+ ? 'Cast'
161
+ : cast.constructor.name;
162
+ }
163
+ existingActorCalled(name) {
164
+ return this.actors['background'].has(name)
165
+ ? this.actors['background'].get(name)
166
+ : this.actors['foreground'].get(name);
167
+ }
168
+ /**
169
+ * Returns `true` if there is an [`Actor`](https://serenity-js.org/api/core/class/Actor/) in the spotlight, `false` otherwise.
170
+ *
171
+ * @returns `true` if an actor is currently spotlighted
172
+ */
173
+ hasActorInTheSpotlight() {
174
+ return Boolean(this.currentActor);
175
+ }
176
+ /**
177
+ * Returns the last [`Actor`](https://serenity-js.org/api/core/class/Actor/) instantiated
178
+ * via [`actor`](https://serenity-js.org/api/core/class/ActorLifecycleManager/#actor).
179
+ *
180
+ * @returns The currently spotlighted actor
181
+ *
182
+ * @throws [`LogicError`](https://serenity-js.org/api/core/class/LogicError/)
183
+ * If no [`Actor`](https://serenity-js.org/api/core/class/Actor/) has been activated yet
184
+ */
185
+ actorInTheSpotlight() {
186
+ if (!this.currentActor) {
187
+ throw new LogicError(`There is no actor in the spotlight yet. Make sure you instantiate one with stage.actor(actorName) before calling this method.`);
188
+ }
189
+ return this.currentActor;
190
+ }
191
+ /**
192
+ * Switches the focus to the specified stage area.
193
+ *
194
+ * Actors created after this call will be added to the specified area.
195
+ * This method is typically called automatically by the [`Stage`](https://serenity-js.org/api/core/class/Stage/)
196
+ * in response to [`SceneStarts`](https://serenity-js.org/api/core-events/class/SceneStarts/) and
197
+ * [`SceneFinishes`](https://serenity-js.org/api/core-events/class/SceneFinishes/) events.
198
+ *
199
+ * Test runner adapters can also call this method directly to control actor lifecycle
200
+ * when scene events are not available (e.g., in Playwright Test where the reporter
201
+ * runs in a separate process).
202
+ *
203
+ * @param focus - The focus area to switch to: `'foreground'` for scene-scoped actors,
204
+ * `'background'` for test run-scoped actors
205
+ */
206
+ switchFocus(focus) {
207
+ this.currentFocusValue = focus;
208
+ }
209
+ /**
210
+ * Returns the current focus area.
211
+ *
212
+ * @returns The current focus: `'foreground'` or `'background'`
213
+ */
214
+ currentFocus() {
215
+ return this.currentFocusValue;
216
+ }
217
+ /**
218
+ * Returns all actors in the specified focus area.
219
+ *
220
+ * This method is used by the [`Stage`](https://serenity-js.org/api/core/class/Stage/) to retrieve
221
+ * actors for dismissal when scenes or test runs finish.
222
+ *
223
+ * @param focus - The focus area to retrieve actors from
224
+ * @returns An array of actors in the specified focus area
225
+ */
226
+ actorsIn(focus) {
227
+ return Array.from(this.actors[focus].values());
228
+ }
229
+ /**
230
+ * Clears the spotlight if the current actor is in the specified focus area.
231
+ *
232
+ * This ensures that after actors in a focus area are dismissed, the spotlight
233
+ * doesn't reference a dismissed actor.
234
+ *
235
+ * @param focus - The focus area to check
236
+ */
237
+ clearSpotlightIfIn(focus) {
238
+ const actors = this.actorsIn(focus);
239
+ if (actors.includes(this.currentActor)) {
240
+ this.currentActor = undefined;
241
+ }
242
+ }
243
+ /**
244
+ * Clears all actors from the specified focus area.
245
+ *
246
+ * This method is called by the [`Stage`](https://serenity-js.org/api/core/class/Stage/) after
247
+ * actors have been dismissed to remove them from the internal tracking maps.
248
+ *
249
+ * @param focus - The focus area to clear
250
+ */
251
+ clearActorsIn(focus) {
252
+ this.actors[focus].clear();
253
+ }
254
+ }
255
+ //# sourceMappingURL=ActorLifecycleManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ActorLifecycleManager.js","sourceRoot":"","sources":["../../src/stage/ActorLifecycleManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,M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AC,IAAI,CAAC,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,sBAAsB;QACzB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;;;OAQG;IACI,mBAAmB;QACtB,IAAI,CAAE,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,MAAM,IAAI,UAAU,CAAC,+HAA+H,CAAC,CAAC;QAC1J,CAAC;QAED,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,WAAW,CAAC,KAAiB;QACzB,IAAI,CAAC,iBAAiB,GAAG,KAAK,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,YAAY;QACR,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAClC,CAAC;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,KAAiB;QACtB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IACnD,CAAC;IAED;;;;;;;OAOG;IACH,kBAAkB,CAAC,KAAiB;QAEhC,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEpC,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;YACrC,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC;QAClC,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,KAAiB;QAC3B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,CAAC;IAC/B,CAAC;CACJ"}
@@ -2,9 +2,11 @@ import type { SerenityConfig } from '../config/index.js';
2
2
  import { ErrorFactory, type ErrorOptions, type RuntimeError } from '../errors/index.js';
3
3
  import { type DomainEvent, type EmitsDomainEvents } from '../events/index.js';
4
4
  import { type ActivityDetails, CorrelationId, type CorrelationIdFactory } from '../model/index.js';
5
- import { Actor, type Clock, type Duration, type Timestamp } from '../screenplay/index.js';
6
- import type { ListensToDomainEvents } from '../stage/index.js';
5
+ import type { Actor } from '../screenplay/index.js';
6
+ import type { Clock, Duration, Timestamp } from '../screenplay/index.js';
7
+ import { ActorLifecycleManager } from './ActorLifecycleManager.js';
7
8
  import type { Cast } from './Cast.js';
9
+ import type { ListensToDomainEvents } from './ListensToDomainEvents.js';
8
10
  import type { StageManager } from './StageManager.js';
9
11
  /**
10
12
  * Stage is the place where [actors](https://serenity-js.org/api/core/class/Actor/) perform.
@@ -23,44 +25,28 @@ import type { StageManager } from './StageManager.js';
23
25
  * @group Stage
24
26
  */
25
27
  export declare class Stage implements EmitsDomainEvents {
26
- private cast;
27
- private manager;
28
+ private readonly manager;
28
29
  private errors;
29
30
  private readonly clock;
30
- private interactionTimeout;
31
31
  private readonly sceneIdFactory;
32
32
  static readonly unknownSceneId: CorrelationId;
33
- /**
34
- * Actors instantiated after the scene has started,
35
- * who will be dismissed when the scene finishes.
36
- */
37
- private actorsOnFrontStage;
38
- /**
39
- * Actors instantiated before the scene has started,
40
- * who will be dismissed when the test run finishes.
41
- */
42
- private actorsOnBackstage;
43
- private actorsOnStage;
44
- /**
45
- * The most recent actor referenced via the [`Actor`](https://serenity-js.org/api/core/class/Actor/) method
46
- */
47
- private actorInTheSpotlight;
48
- /**
49
- * The scene in which the spotlight was last set.
50
- * Used to detect when the spotlight shifts to a different scene context.
51
- */
52
- private sceneOfSpotlightedActor;
53
33
  private currentActivity;
54
34
  private currentScene;
35
+ private readonly actorLifecycleManager;
55
36
  /**
56
- * @param cast
57
- * @param manager
58
- * @param errors
59
- * @param clock
60
- * @param interactionTimeout
61
- * @param sceneIdFactory
62
- */
63
- constructor(cast: Cast, manager: StageManager, errors: ErrorFactory, clock: Clock, interactionTimeout: Duration, sceneIdFactory?: CorrelationIdFactory);
37
+ * Creates a new Stage instance.
38
+ *
39
+ * @param cast - The default cast to use for preparing actors
40
+ * @param manager - The stage manager responsible for notifying listeners of domain events
41
+ * @param errors - Factory for creating runtime errors with proper context
42
+ * @param clock - Clock used for timestamping domain events
43
+ * @param interactionTimeout - Default timeout for actor interactions
44
+ * @param sceneIdFactory - Factory for creating scene correlation IDs
45
+ * @param actorLifecycleManager - Optional custom ActorLifecycleManager instance.
46
+ * When provided, allows test runner adapters to control actor lifecycle programmatically.
47
+ * If not provided, a default manager is created.
48
+ */
49
+ constructor(cast: Cast, manager: StageManager, errors: ErrorFactory, clock: Clock, interactionTimeout: Duration, sceneIdFactory?: CorrelationIdFactory, actorLifecycleManager?: ActorLifecycleManager);
64
50
  configure(options: Pick<SerenityConfig, 'actors' | 'cueTimeout' | 'interactionTimeout' | 'diffFormatter'>): void;
65
51
  /**
66
52
  * An alias for [`Stage.actor`](https://serenity-js.org/api/core/class/Stage/#actor)
@@ -97,7 +83,7 @@ export declare class Stage implements EmitsDomainEvents {
97
83
  engage(actors: Cast): void;
98
84
  /**
99
85
  * Assigns listeners to be notified of [Serenity/JS domain events](https://serenity-js.org/api/core-events/class/DomainEvent/)
100
- * emitted via [`Stage.announce`](https://serenity-js.org/api/core/class/Stage/#announce).s
86
+ * emitted via [`Stage.announce`](https://serenity-js.org/api/core/class/Stage/#announce).
101
87
  *
102
88
  * @param listeners
103
89
  */
@@ -110,6 +96,8 @@ export declare class Stage implements EmitsDomainEvents {
110
96
  */
111
97
  announce(...events: Array<DomainEvent>): void;
112
98
  private announceSingle;
99
+ private notifyOfStageExit;
100
+ private dismissActorsIn;
113
101
  /**
114
102
  * Returns current time. This method should be used whenever
115
103
  * [`DomainEvent`](https://serenity-js.org/api/core-events/class/DomainEvent/) objects are instantiated by you programmatically.
@@ -161,9 +149,5 @@ export declare class Stage implements EmitsDomainEvents {
161
149
  */
162
150
  waitForNextCue(): Promise<void>;
163
151
  createError<RE extends RuntimeError>(errorType: new (...args: any[]) => RE, options: ErrorOptions): RE;
164
- private instantiatedActorCalled;
165
- private notifyOfStageExit;
166
- private dismiss;
167
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AAK,EAAE,EAAE,OAAO,EAAE,YAAY,GAAG,EAAE;CAMzG"}
@@ -1,8 +1,8 @@
1
- import { ensure, isDefined, property } from 'tiny-types';
2
- import { ConfigurationError, ErrorFactory, LogicError, RaiseErrors } from '../errors/index.js';
3
- import { ActorEntersStage, ActorSpotlighted, ActorStageExitAttempted, ActorStageExitCompleted, ActorStageExitFailed, ActorStageExitStarts, SceneFinishes, SceneStarts, TestRunFinishes } from '../events/index.js';
1
+ import { ensure, isDefined } from 'tiny-types';
2
+ import { ErrorFactory, LogicError } from '../errors/index.js';
3
+ import { ActorStageExitAttempted, ActorStageExitCompleted, ActorStageExitFailed, ActorStageExitStarts, SceneFinishes, SceneStarts, TestRunFinishes } from '../events/index.js';
4
4
  import { CorrelationId, Name } from '../model/index.js';
5
- import { Actor, ScheduleWork } from '../screenplay/index.js';
5
+ import { ActorLifecycleManager } from './ActorLifecycleManager.js';
6
6
  /**
7
7
  * Stage is the place where [actors](https://serenity-js.org/api/core/class/Actor/) perform.
8
8
  *
@@ -20,49 +20,31 @@ import { Actor, ScheduleWork } from '../screenplay/index.js';
20
20
  * @group Stage
21
21
  */
22
22
  export class Stage {
23
- cast;
24
23
  manager;
25
24
  errors;
26
25
  clock;
27
- interactionTimeout;
28
26
  sceneIdFactory;
29
27
  static unknownSceneId = new CorrelationId('unknown');
30
- /**
31
- * Actors instantiated after the scene has started,
32
- * who will be dismissed when the scene finishes.
33
- */
34
- actorsOnFrontStage = new Map();
35
- /**
36
- * Actors instantiated before the scene has started,
37
- * who will be dismissed when the test run finishes.
38
- */
39
- actorsOnBackstage = new Map();
40
- actorsOnStage = this.actorsOnBackstage;
41
- /**
42
- * The most recent actor referenced via the [`Actor`](https://serenity-js.org/api/core/class/Actor/) method
43
- */
44
- actorInTheSpotlight = undefined;
45
- /**
46
- * The scene in which the spotlight was last set.
47
- * Used to detect when the spotlight shifts to a different scene context.
48
- */
49
- sceneOfSpotlightedActor = undefined;
50
28
  currentActivity = undefined;
51
29
  currentScene = Stage.unknownSceneId;
30
+ actorLifecycleManager;
52
31
  /**
53
- * @param cast
54
- * @param manager
55
- * @param errors
56
- * @param clock
57
- * @param interactionTimeout
58
- * @param sceneIdFactory
59
- */
60
- constructor(cast, manager, errors, clock, interactionTimeout, sceneIdFactory = CorrelationId) {
61
- this.cast = cast;
32
+ * Creates a new Stage instance.
33
+ *
34
+ * @param cast - The default cast to use for preparing actors
35
+ * @param manager - The stage manager responsible for notifying listeners of domain events
36
+ * @param errors - Factory for creating runtime errors with proper context
37
+ * @param clock - Clock used for timestamping domain events
38
+ * @param interactionTimeout - Default timeout for actor interactions
39
+ * @param sceneIdFactory - Factory for creating scene correlation IDs
40
+ * @param actorLifecycleManager - Optional custom ActorLifecycleManager instance.
41
+ * When provided, allows test runner adapters to control actor lifecycle programmatically.
42
+ * If not provided, a default manager is created.
43
+ */
44
+ constructor(cast, manager, errors, clock, interactionTimeout, sceneIdFactory = CorrelationId, actorLifecycleManager) {
62
45
  this.manager = manager;
63
46
  this.errors = errors;
64
47
  this.clock = clock;
65
- this.interactionTimeout = interactionTimeout;
66
48
  this.sceneIdFactory = sceneIdFactory;
67
49
  ensure('Cast', cast, isDefined());
68
50
  ensure('StageManager', manager, isDefined());
@@ -70,11 +52,15 @@ export class Stage {
70
52
  ensure('Clock', clock, isDefined());
71
53
  ensure('interactionTimeout', interactionTimeout, isDefined());
72
54
  ensure('sceneIdFactory', sceneIdFactory, isDefined());
55
+ this.actorLifecycleManager = actorLifecycleManager ?? new ActorLifecycleManager(cast, this.clock, interactionTimeout);
56
+ this.actorLifecycleManager.assignTo(this);
73
57
  }
74
58
  configure(options) {
75
- this.interactionTimeout = options.interactionTimeout || this.interactionTimeout;
59
+ if (options.interactionTimeout) {
60
+ this.actorLifecycleManager.configure({ interactionTimeout: options.interactionTimeout });
61
+ }
76
62
  if (options.actors) {
77
- this.engage(options.actors);
63
+ this.actorLifecycleManager.engage(options.actors);
78
64
  }
79
65
  if (options.cueTimeout) {
80
66
  this.manager.configure({ cueTimeout: options.cueTimeout });
@@ -99,34 +85,7 @@ export class Stage {
99
85
  * Case-sensitive name of the Actor, e.g. `Alice`
100
86
  */
101
87
  actor(name) {
102
- if (!this.instantiatedActorCalled(name)) {
103
- let actor;
104
- try {
105
- const newActor = new Actor(name, this, [
106
- new RaiseErrors(this),
107
- new ScheduleWork(this.clock, this.interactionTimeout)
108
- ]);
109
- actor = this.cast.prepare(newActor);
110
- }
111
- catch (error) {
112
- throw new ConfigurationError(`${this.typeOf(this.cast)} encountered a problem when preparing actor "${name}" for stage`, error);
113
- }
114
- if (!(actor instanceof Actor)) {
115
- throw new ConfigurationError(`Instead of a new instance of actor "${name}", ${this.typeOf(this.cast)} returned ${actor}`);
116
- }
117
- this.actorsOnStage.set(name, actor);
118
- this.announce(new ActorEntersStage(this.currentScene, actor.toJSON()));
119
- }
120
- const previousActorInSpotlight = this.actorInTheSpotlight;
121
- const previousSceneOfSpotlightedActor = this.sceneOfSpotlightedActor;
122
- this.actorInTheSpotlight = this.instantiatedActorCalled(name);
123
- this.sceneOfSpotlightedActor = this.currentScene;
124
- const spotlightShifted = this.actorInTheSpotlight !== previousActorInSpotlight
125
- || !this.currentScene.equals(previousSceneOfSpotlightedActor);
126
- if (spotlightShifted) {
127
- this.announce(new ActorSpotlighted(this.currentScene, this.actorInTheSpotlight.toJSON()));
128
- }
129
- return this.actorInTheSpotlight;
88
+ return this.actorLifecycleManager.actor(name);
130
89
  }
131
90
  /**
132
91
  * Returns the last [`Actor`](https://serenity-js.org/api/core/class/Actor/) instantiated via [`Stage.actor`](https://serenity-js.org/api/core/class/Stage/#actor).
@@ -136,16 +95,13 @@ export class Stage {
136
95
  * If no [`Actor`](https://serenity-js.org/api/core/class/Actor/) has been activated yet
137
96
  */
138
97
  theActorInTheSpotlight() {
139
- if (!this.actorInTheSpotlight) {
140
- throw new LogicError(`There is no actor in the spotlight yet. Make sure you instantiate one with stage.actor(actorName) before calling this method.`);
141
- }
142
- return this.actorInTheSpotlight;
98
+ return this.actorLifecycleManager.actorInTheSpotlight();
143
99
  }
144
100
  /**
145
101
  * Returns `true` if there is an [`Actor`](https://serenity-js.org/api/core/class/Actor/) in the spotlight, `false` otherwise.
146
102
  */
147
103
  theShowHasStarted() {
148
- return !!this.actorInTheSpotlight;
104
+ return this.actorLifecycleManager.hasActorInTheSpotlight();
149
105
  }
150
106
  /**
151
107
  * Configures the Stage to prepare [actors](https://serenity-js.org/api/core/class/Actor/)
@@ -154,11 +110,11 @@ export class Stage {
154
110
  * @param actors
155
111
  */
156
112
  engage(actors) {
157
- this.cast = ensure('actors', actors, isDefined(), property('prepare', isDefined()));
113
+ this.actorLifecycleManager.engage(actors);
158
114
  }
159
115
  /**
160
116
  * Assigns listeners to be notified of [Serenity/JS domain events](https://serenity-js.org/api/core-events/class/DomainEvent/)
161
- * emitted via [`Stage.announce`](https://serenity-js.org/api/core/class/Stage/#announce).s
117
+ * emitted via [`Stage.announce`](https://serenity-js.org/api/core/class/Stage/#announce).
162
118
  *
163
119
  * @param listeners
164
120
  */
@@ -178,26 +134,52 @@ export class Stage {
178
134
  }
179
135
  announceSingle(event) {
180
136
  if (event instanceof SceneStarts) {
181
- this.actorsOnStage = this.actorsOnFrontStage;
137
+ this.actorLifecycleManager.switchFocus('foreground');
182
138
  }
183
139
  if (event instanceof SceneFinishes || event instanceof TestRunFinishes) {
184
- this.notifyOfStageExit(this.currentSceneId());
140
+ this.notifyOfStageExit(this.actorLifecycleManager.currentFocus());
185
141
  }
186
142
  this.manager.notifyOf(event);
187
143
  if (event instanceof SceneFinishes) {
188
- this.dismiss(this.actorsOnStage);
189
- this.actorsOnStage = this.actorsOnBackstage;
144
+ this.dismissActorsIn('foreground');
145
+ this.actorLifecycleManager.switchFocus('background');
190
146
  }
191
147
  if (event instanceof TestRunFinishes) {
192
- this.dismiss(this.actorsOnStage);
148
+ this.dismissActorsIn('background');
149
+ }
150
+ }
151
+ notifyOfStageExit(focus) {
152
+ for (const actor of this.actorLifecycleManager.actorsIn(focus)) {
153
+ this.announce(new ActorStageExitStarts(this.currentSceneId(), actor.toJSON(), this.currentTime()));
154
+ }
155
+ }
156
+ async dismissActorsIn(focus) {
157
+ const actors = this.actorLifecycleManager.actorsIn(focus);
158
+ this.actorLifecycleManager.clearSpotlightIfIn(focus);
159
+ // Wait for the Photographer to finish taking any screenshots
160
+ await this.manager.waitForAsyncOperationsToComplete();
161
+ const actorsToDismiss = new Map(actors.map(actor => [actor, CorrelationId.create()]));
162
+ for (const [actor, correlationId] of actorsToDismiss) {
163
+ this.announce(new ActorStageExitAttempted(correlationId, new Name(actor.name), this.currentTime()));
164
+ }
165
+ // Try to dismiss each actor
166
+ for (const [actor, correlationId] of actorsToDismiss) {
167
+ try {
168
+ await actor.dismiss();
169
+ this.announce(new ActorStageExitCompleted(correlationId, new Name(actor.name), this.currentTime()));
170
+ }
171
+ catch (error) {
172
+ this.announce(new ActorStageExitFailed(error, correlationId, this.currentTime()));
173
+ }
193
174
  }
175
+ this.actorLifecycleManager.clearActorsIn(focus);
194
176
  }
195
177
  /**
196
178
  * Returns current time. This method should be used whenever
197
179
  * [`DomainEvent`](https://serenity-js.org/api/core-events/class/DomainEvent/) objects are instantiated by you programmatically.
198
180
  */
199
181
  currentTime() {
200
- return this.manager.currentTime();
182
+ return this.clock.now();
201
183
  }
202
184
  /**
203
185
  * Generates and remembers a `CorrelationId`
@@ -268,43 +250,5 @@ export class Stage {
268
250
  ...options,
269
251
  });
270
252
  }
271
- instantiatedActorCalled(name) {
272
- return this.actorsOnBackstage.has(name)
273
- ? this.actorsOnBackstage.get(name)
274
- : this.actorsOnFrontStage.get(name);
275
- }
276
- notifyOfStageExit(sceneId) {
277
- for (const actor of this.actorsOnStage.values()) {
278
- this.announce(new ActorStageExitStarts(sceneId, actor.toJSON(), this.currentTime()));
279
- }
280
- }
281
- async dismiss(activeActors) {
282
- const actors = Array.from(activeActors.values());
283
- if (actors.includes(this.actorInTheSpotlight)) {
284
- this.actorInTheSpotlight = undefined;
285
- }
286
- // Wait for the Photographer to finish taking any screenshots
287
- await this.manager.waitForAsyncOperationsToComplete();
288
- const actorsToDismiss = new Map(actors.map(actor => [actor, CorrelationId.create()]));
289
- for (const [actor, correlationId] of actorsToDismiss) {
290
- this.announce(new ActorStageExitAttempted(correlationId, new Name(actor.name), this.currentTime()));
291
- }
292
- // Try to dismiss each actor
293
- for (const [actor, correlationId] of actorsToDismiss) {
294
- try {
295
- await actor.dismiss();
296
- this.announce(new ActorStageExitCompleted(correlationId, new Name(actor.name), this.currentTime()));
297
- }
298
- catch (error) {
299
- this.announce(new ActorStageExitFailed(error, correlationId, this.currentTime()));
300
- }
301
- }
302
- activeActors.clear();
303
- }
304
- typeOf(cast) {
305
- return cast.constructor === Object
306
- ? 'Cast'
307
- : cast.constructor.name;
308
- }
309
253
  }
310
254
  //# sourceMappingURL=Stage.js.map