@vestra-player/sdk 1.9.0 → 1.10.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 (48) hide show
  1. package/CHANGELOG.md +29 -7
  2. package/bin/vestra-plugin.mjs +89 -8
  3. package/capability-scan.json +18 -0
  4. package/dist/api-requests.d.ts +4 -3
  5. package/dist/api-requests.js +10 -3
  6. package/dist/condition-table.d.ts +1889 -0
  7. package/dist/condition-table.js +562 -0
  8. package/dist/docs-tree.json.gz +0 -0
  9. package/dist/i18n-tools.d.ts +7 -0
  10. package/dist/i18n-tools.js +66 -3
  11. package/dist/player-runtime.d.ts +3 -0
  12. package/dist/player-runtime.js +3 -0
  13. package/dist/plugin/connection.d.ts +12 -1
  14. package/dist/plugin/connection.js +58 -1
  15. package/dist/plugin/index.d.ts +7 -0
  16. package/dist/plugin/index.js +13 -0
  17. package/dist/plugin/mpv.js +39 -5
  18. package/dist/plugin/player/events.d.ts +14 -5
  19. package/dist/plugin/player/events.js +19 -3
  20. package/dist/plugin/player.d.ts +16 -0
  21. package/dist/plugin/player.js +77 -1
  22. package/dist/plugin/scope.d.ts +4 -1
  23. package/dist/plugin/scope.js +8 -0
  24. package/dist/plugin/testing/commands.js +18 -22
  25. package/dist/plugin/testing/index.d.ts +107 -0
  26. package/dist/plugin/testing/index.js +168 -21
  27. package/dist/plugin/testing/media.d.ts +6 -0
  28. package/dist/plugin/testing/media.js +15 -2
  29. package/dist/plugin/testing/source-events.d.ts +28 -0
  30. package/dist/plugin/testing/source-events.js +66 -0
  31. package/dist/plugin/testing/transport.js +5 -0
  32. package/dist/plugin/testing/triggers.d.ts +82 -0
  33. package/dist/plugin/testing/triggers.js +448 -0
  34. package/dist/plugin/timeline.d.ts +3 -3
  35. package/dist/plugin/timeline.js +3 -3
  36. package/dist/plugin/triggers.d.ts +55 -0
  37. package/dist/plugin/triggers.js +233 -0
  38. package/dist/plugin/types/base.d.ts +37 -1
  39. package/dist/plugin/types/commands.d.ts +374 -1
  40. package/dist/plugin/types/playback.d.ts +257 -9
  41. package/dist/plugin/when.d.ts +78 -4
  42. package/dist/plugin/when.js +182 -14
  43. package/dist/slint-version.d.ts +7 -0
  44. package/dist/slint-version.js +7 -0
  45. package/dist/wire.d.ts +15 -0
  46. package/dist/wire.js +11 -0
  47. package/llms.txt +4 -4
  48. package/package.json +1 -1
@@ -20,8 +20,13 @@ import type { MotionMode, MotionReason, RenderingMode, RenderingPreference, Rend
20
20
  import type { MediaJob } from "../../media.ts";
21
21
  import type { LanguagePreference, Locale, Plugin, StopReason, TemplateArgs, TextValue, ThemeId } from "../types.ts";
22
22
  import { LOCALES, LIST_SEPARATORS, TEXT_LIMITS } from "../../i18n.ts";
23
+ import type { KeptTrigger } from "./triggers.ts";
24
+ import { TRIGGER_LIMITS } from "../triggers.ts";
23
25
  /** Throw one of these from `host.handle(...)` to simulate a definite refusal. */
24
26
  export { HostError };
27
+ /** The player's limits on triggers (1.10.0), which the test host enforces as well. */
28
+ export { TRIGGER_LIMITS };
29
+ export type { KeptTrigger };
25
30
  /** The player's command caps, which the test host enforces as well. */
26
31
  export { COMMAND_LIMITS, MENU_IDS };
27
32
  /** The icons and limits of bottom-bar buttons, which the test host checks as well. */
@@ -3201,6 +3206,108 @@ export declare class TestHost implements Transport {
3201
3206
  * ```
3202
3207
  */
3203
3208
  get keepsRunning(): string[];
3209
+ /**
3210
+ * The trigger conditions the player keeps for this plugin
3211
+ * (`plugin.trigger().firesOn`), in the wire form, with their sequence
3212
+ * numbers and whether each holds now. They stay through
3213
+ * {@link TestHost.park}; {@link TestHost.upgrade} clears the unnamed ones.
3214
+ *
3215
+ * @since 1.10.0
3216
+ */
3217
+ get triggers(): readonly KeptTrigger[];
3218
+ /**
3219
+ * The test host's wall clock (Unix milliseconds), which `when.time`
3220
+ * conditions read. It starts at the real time; it moves only with
3221
+ * {@link TestHost.advance}, or when a test sets it.
3222
+ *
3223
+ * @since 1.10.0
3224
+ */
3225
+ get now(): number;
3226
+ /**
3227
+ * Set the clock to `value` (Unix milliseconds). Only the clock moves:
3228
+ * the kept conditions see the new time at their next evaluation, on
3229
+ * {@link TestHost.advance} or a state the test pushes.
3230
+ */
3231
+ set now(value: number);
3232
+ /**
3233
+ * Move the clock by `ms` milliseconds and, while the source plays
3234
+ * (`phase` `ready`, not paused), the position by `rate × ms`. Each
3235
+ * position boundary and time bound a kept condition waits for is passed
3236
+ * in its order: an `enter` or `leave` runs with the clock and the position
3237
+ * where the player would call it, and the test does not wait. The new
3238
+ * position is then pushed as `source.state`.
3239
+ *
3240
+ * @example A silence is entered 2 s before it starts
3241
+ * The source plays from 0, and the plugin's position condition has a
3242
+ * lead of 2 s before a range at 10 s. Advancing 8 s of clock plays 8 s
3243
+ * of media, so the trigger's `enter` runs on the way, at 8 s.
3244
+ * ```ts
3245
+ * import assert from "node:assert/strict";
3246
+ * declare const host: TestHost;
3247
+ * // ---cut---
3248
+ * host.setSource({ phase: "ready", duration: 60, position: 0 });
3249
+ * // … the plugin gave when.position.in([{ start: 10, end: 12 }], { lead: 2 })
3250
+ * await host.advance(8_000); // its enter ran at 8 s
3251
+ * ```
3252
+ *
3253
+ * @throws `TypeError` (rejects) `ms` is not a number of 0 or more.
3254
+ * @since 1.10.0
3255
+ */
3256
+ advance(ms: number): Promise<void>;
3257
+ /**
3258
+ * The player puts the plugin in standby: it is stopped (reason
3259
+ * `idle`); its trigger conditions and the wake events it listens to are
3260
+ * kept. A condition that flips, or a wake event, starts it again
3261
+ * (`activate()` runs anew) and delivers what waited, in order; the
3262
+ * conditions given again by `activate()` change nothing.
3263
+ *
3264
+ * @throws `Error` without a code (rejects) The plugin was not started
3265
+ * with {@link startPlugin}.
3266
+ *
3267
+ * @example A trigger condition outlives standby
3268
+ * The test puts a plugin that armed one trigger in standby, as the
3269
+ * player does when nothing uses it, and checks that the player still
3270
+ * keeps the condition that will start the plugin again.
3271
+ * ```ts
3272
+ * import assert from "node:assert/strict";
3273
+ * declare const host: TestHost;
3274
+ * // ---cut---
3275
+ * await host.park(); // stopped with reason "idle"
3276
+ * assert.equal(host.parked, true);
3277
+ * assert.equal(host.triggers.length, 1);
3278
+ * ```
3279
+ *
3280
+ * @since 1.10.0
3281
+ */
3282
+ park(): Promise<void>;
3283
+ /** The plugin is in standby ({@link TestHost.park}) and not started again yet. */
3284
+ get parked(): boolean;
3285
+ /**
3286
+ * Another version replaces the plugin: it is stopped (reason
3287
+ * `upgrade`), the conditions of its unnamed triggers are cleared, the
3288
+ * named ones kept, and it starts again with `activate` (the new
3289
+ * version's code; default: the same). A name the new version does not
3290
+ * declare is dropped when it starts.
3291
+ *
3292
+ * @throws `Error` without a code (rejects) The plugin was not started
3293
+ * with {@link startPlugin}.
3294
+ *
3295
+ * @example A named sleep timer survives an update
3296
+ * The test arms the plugin's sleep timer from its command, replaces the
3297
+ * plugin with the same code as a new version, and checks that the
3298
+ * player still keeps the condition of the trigger named `sleep`.
3299
+ * ```ts
3300
+ * import assert from "node:assert/strict";
3301
+ * declare const host: TestHost;
3302
+ * // ---cut---
3303
+ * await host.command("in45").run();
3304
+ * await host.upgrade(); // the same activate, as the new version
3305
+ * assert.ok(host.triggers.some((kept) => kept.trigger === "sleep"));
3306
+ * ```
3307
+ *
3308
+ * @since 1.10.0
3309
+ */
3310
+ upgrade(activate?: (plugin: Plugin) => unknown): Promise<void>;
3204
3311
  }
3205
3312
  /**
3206
3313
  * Activate a plugin against a test host, the way the player's runner does:
@@ -74,6 +74,7 @@ import { CAPABILITIES_REMOVED } from "../../manifest.js";
74
74
  import { MESSAGE_LIMIT } from "../../protocol.js";
75
75
  import { OSD_LIMITS } from "../osd.js";
76
76
  import { TIMELINE_LIMITS } from "../timeline.js";
77
+ import { PLAYER_SLINT } from "../../slint-version.js";
77
78
  import { SECRET_LIMITS, SESSION_STORAGE_LIMITS } from "../services.js";
78
79
  import { SUBTITLE_LIMITS } from "../subtitles.js";
79
80
  import { BINDING_INPUTS, canonical } from "../bindings.js";
@@ -93,12 +94,17 @@ import { mountOther, unmountOther, mounted, setTimelineRanges, setTimelineLayer,
93
94
  import { osdHandlers } from "./osd.js";
94
95
  import { mpvHandlers, setMpvProperty } from "./mpv.js";
95
96
  import { packageDirectories } from "./slint-types.js";
97
+ import { quitEvent } from "./source-events.js";
96
98
  import { failStream, jobsHandlers } from "./jobs.js";
97
99
  import { motionState, upscalingState, failVideoRender, failVideoShaders, upscalingDecoding, processingHandlers, } from "./processing.js";
98
100
  import { sourceIntent, startResolution, setSource, mediaHandlers, initialSource, } from "./media.js";
99
101
  import { drop, setMainWindow, servicesHandlers, sortedJson, } from "./services.js";
102
+ import { advance, keptTriggers, park, triggerState, triggersHandlers, upgrade, } from "./triggers.js";
103
+ import { TRIGGER_LIMITS } from "../triggers.js";
100
104
  /** Throw one of these from `host.handle(...)` to simulate a definite refusal. */
101
105
  export { HostError };
106
+ /** The player's limits on triggers (1.10.0), which the test host enforces as well. */
107
+ export { TRIGGER_LIMITS };
102
108
  /** The player's command caps, which the test host enforces as well. */
103
109
  export { COMMAND_LIMITS, MENU_IDS };
104
110
  /** The icons and limits of bottom-bar buttons, which the test host checks as well. */
@@ -1547,7 +1553,7 @@ export class TestHost {
1547
1553
  components: {
1548
1554
  mpv: "mpv 0.41.0",
1549
1555
  libav: { ffmpeg: "7.1" },
1550
- slint: "1.18.1",
1556
+ slint: PLAYER_SLINT,
1551
1557
  },
1552
1558
  };
1553
1559
  /**
@@ -2789,7 +2795,125 @@ export class TestHost {
2789
2795
  get keepsRunning() {
2790
2796
  return keepsRunning(this, this.#inner);
2791
2797
  }
2798
+ // ------------------------------------------------------------ triggers
2799
+ /**
2800
+ * The trigger conditions the player keeps for this plugin
2801
+ * (`plugin.trigger().firesOn`), in the wire form, with their sequence
2802
+ * numbers and whether each holds now. They stay through
2803
+ * {@link TestHost.park}; {@link TestHost.upgrade} clears the unnamed ones.
2804
+ *
2805
+ * @since 1.10.0
2806
+ */
2807
+ get triggers() {
2808
+ return keptTriggers(this);
2809
+ }
2810
+ /**
2811
+ * The test host's wall clock (Unix milliseconds), which `when.time`
2812
+ * conditions read. It starts at the real time; it moves only with
2813
+ * {@link TestHost.advance}, or when a test sets it.
2814
+ *
2815
+ * @since 1.10.0
2816
+ */
2817
+ get now() {
2818
+ return triggerState(this).now;
2819
+ }
2820
+ /**
2821
+ * Set the clock to `value` (Unix milliseconds). Only the clock moves:
2822
+ * the kept conditions see the new time at their next evaluation, on
2823
+ * {@link TestHost.advance} or a state the test pushes.
2824
+ */
2825
+ set now(value) {
2826
+ triggerState(this).now = value;
2827
+ }
2828
+ /**
2829
+ * Move the clock by `ms` milliseconds and, while the source plays
2830
+ * (`phase` `ready`, not paused), the position by `rate × ms`. Each
2831
+ * position boundary and time bound a kept condition waits for is passed
2832
+ * in its order: an `enter` or `leave` runs with the clock and the position
2833
+ * where the player would call it, and the test does not wait. The new
2834
+ * position is then pushed as `source.state`.
2835
+ *
2836
+ * @example A silence is entered 2 s before it starts
2837
+ * The source plays from 0, and the plugin's position condition has a
2838
+ * lead of 2 s before a range at 10 s. Advancing 8 s of clock plays 8 s
2839
+ * of media, so the trigger's `enter` runs on the way, at 8 s.
2840
+ * ```ts
2841
+ * import assert from "node:assert/strict";
2842
+ * declare const host: TestHost;
2843
+ * // ---cut---
2844
+ * host.setSource({ phase: "ready", duration: 60, position: 0 });
2845
+ * // … the plugin gave when.position.in([{ start: 10, end: 12 }], { lead: 2 })
2846
+ * await host.advance(8_000); // its enter ran at 8 s
2847
+ * ```
2848
+ *
2849
+ * @throws `TypeError` (rejects) `ms` is not a number of 0 or more.
2850
+ * @since 1.10.0
2851
+ */
2852
+ advance(ms) {
2853
+ return advance(this, this.#inner, ms);
2854
+ }
2855
+ /**
2856
+ * The player puts the plugin in standby: it is stopped (reason
2857
+ * `idle`); its trigger conditions and the wake events it listens to are
2858
+ * kept. A condition that flips, or a wake event, starts it again
2859
+ * (`activate()` runs anew) and delivers what waited, in order; the
2860
+ * conditions given again by `activate()` change nothing.
2861
+ *
2862
+ * @throws `Error` without a code (rejects) The plugin was not started
2863
+ * with {@link startPlugin}.
2864
+ *
2865
+ * @example A trigger condition outlives standby
2866
+ * The test puts a plugin that armed one trigger in standby, as the
2867
+ * player does when nothing uses it, and checks that the player still
2868
+ * keeps the condition that will start the plugin again.
2869
+ * ```ts
2870
+ * import assert from "node:assert/strict";
2871
+ * declare const host: TestHost;
2872
+ * // ---cut---
2873
+ * await host.park(); // stopped with reason "idle"
2874
+ * assert.equal(host.parked, true);
2875
+ * assert.equal(host.triggers.length, 1);
2876
+ * ```
2877
+ *
2878
+ * @since 1.10.0
2879
+ */
2880
+ park() {
2881
+ return park(this, this.#inner);
2882
+ }
2883
+ /** The plugin is in standby ({@link TestHost.park}) and not started again yet. */
2884
+ get parked() {
2885
+ return triggerState(this).parked;
2886
+ }
2887
+ /**
2888
+ * Another version replaces the plugin: it is stopped (reason
2889
+ * `upgrade`), the conditions of its unnamed triggers are cleared, the
2890
+ * named ones kept, and it starts again with `activate` (the new
2891
+ * version's code; default: the same). A name the new version does not
2892
+ * declare is dropped when it starts.
2893
+ *
2894
+ * @throws `Error` without a code (rejects) The plugin was not started
2895
+ * with {@link startPlugin}.
2896
+ *
2897
+ * @example A named sleep timer survives an update
2898
+ * The test arms the plugin's sleep timer from its command, replaces the
2899
+ * plugin with the same code as a new version, and checks that the
2900
+ * player still keeps the condition of the trigger named `sleep`.
2901
+ * ```ts
2902
+ * import assert from "node:assert/strict";
2903
+ * declare const host: TestHost;
2904
+ * // ---cut---
2905
+ * await host.command("in45").run();
2906
+ * await host.upgrade(); // the same activate, as the new version
2907
+ * assert.ok(host.triggers.some((kept) => kept.trigger === "sleep"));
2908
+ * ```
2909
+ *
2910
+ * @since 1.10.0
2911
+ */
2912
+ upgrade(activate) {
2913
+ return upgrade(this, this.#inner, activate);
2914
+ }
2792
2915
  #defaults = {
2916
+ ...triggersHandlers(this, this.#inner),
2793
2917
  ...transportHandlers(this, this.#inner),
2794
2918
  ...commandsHandlers(this, this.#inner),
2795
2919
  ...builtinHandlers(this, this.#inner),
@@ -2913,26 +3037,42 @@ export async function startPlugin(activate, options = {}) {
2913
3037
  mkdirSync(data, { recursive: true });
2914
3038
  if (host.fileRoots.length === 0)
2915
3039
  host.fileRoots = [directory, data];
2916
- const instance = await createPlugin(host, manifest, directory, data,
2917
- // Like the player: protocol additions it takes (`app.features` set on
2918
- // the host replaces them, e.g. `[]` for an older player's behaviour).
2919
- { features: [PATCH_FEATURE], ...host.app },
2920
- // Unlike the player's runner, which logs a refused command declaration
2921
- // and activates the rest: a plugin's tests show the refusal.
2922
- { strictDeclarations: true });
2923
- await activate(instance.plugin);
2924
- // Like the player: a position or resolver another plugin holds is not an
2925
- // activation failure.
2926
- for (;;) {
2927
- try {
2928
- await instance.activated();
2929
- break;
2930
- }
2931
- catch (error) {
2932
- if (!(error instanceof Error && takenErrors.has(error)))
2933
- throw error;
3040
+ // One run of the plugin; `park` and `upgrade` (1.10.0) start another.
3041
+ const run = async (entry) => {
3042
+ const started = await createPlugin(host, manifest, directory, data,
3043
+ // Like the player: protocol additions it takes (`app.features` set on
3044
+ // the host replaces them, e.g. `[]` for an older player's behaviour).
3045
+ { features: [PATCH_FEATURE], ...host.app },
3046
+ // Unlike the player's runner, which logs a refused command declaration
3047
+ // and activates the rest: a plugin's tests show the refusal.
3048
+ { strictDeclarations: true });
3049
+ await started.activate(entry);
3050
+ // Like the player: a position or resolver another plugin holds is not an
3051
+ // activation failure.
3052
+ for (;;) {
3053
+ try {
3054
+ await started.activated();
3055
+ break;
3056
+ }
3057
+ catch (error) {
3058
+ if (!(error instanceof Error && takenErrors.has(error)))
3059
+ throw error;
3060
+ }
2934
3061
  }
2935
- }
3062
+ return started;
3063
+ };
3064
+ let instance = await run(activate);
3065
+ const triggers = triggerState(host);
3066
+ let code = activate;
3067
+ triggers.start = async (entry) => {
3068
+ if (entry)
3069
+ code = entry;
3070
+ instance = await run(code);
3071
+ };
3072
+ triggers.stop = async (reason) => {
3073
+ await instance.dispose({ reason });
3074
+ await host.settle();
3075
+ };
2936
3076
  await host.settle();
2937
3077
  tellResidency(host);
2938
3078
  // Zero declared parameters (`stop.length === 0`) and a `()` overload, so
@@ -2945,7 +3085,14 @@ export async function startPlugin(activate, options = {}) {
2945
3085
  const reason = typeof first === "object" && first !== null && "reason" in first
2946
3086
  ? first.reason
2947
3087
  : undefined;
2948
- await instance.dispose({ reason });
3088
+ // Like the player: at a quit a running plugin hears its source end.
3089
+ const quit = reason === "quit" ? quitEvent(host.source) : null;
3090
+ if (quit) {
3091
+ host.emit("source.ended", structuredClone(quit));
3092
+ await host.settle();
3093
+ }
3094
+ if (!triggers.parked)
3095
+ await instance.dispose({ reason });
2949
3096
  await host.settle();
2950
3097
  host.close();
2951
3098
  };
@@ -2,6 +2,12 @@ import type { Json } from "../../wire.ts";
2
2
  import type { SourceState } from "../../source.ts";
3
3
  import type { TestHost } from "./index.ts";
4
4
  import type { Handler, Internals } from "./internals.ts";
5
+ /**
6
+ * Broadcast the source state `host.source` became from `previous`, then
7
+ * the discrete source events it adds (`source.opening`, `source.ready`,
8
+ * `source.ended`), as the player sends them after it.
9
+ */
10
+ export declare function emitSource(host: TestHost, previous: SourceState, reload?: boolean): void;
5
11
  export declare function initialSource(): SourceState;
6
12
  export declare const readyCapabilities: {
7
13
  pause: boolean;
@@ -13,6 +13,17 @@ import { isTextValue } from "../../i18n.js";
13
13
  import { taken } from "./transport.js";
14
14
  import { declaredText, hostText } from "./i18n.js";
15
15
  import { timelineChanged } from "./layout.js";
16
+ import { sourceEvents } from "./source-events.js";
17
+ /**
18
+ * Broadcast the source state `host.source` became from `previous`, then
19
+ * the discrete source events it adds (`source.opening`, `source.ready`,
20
+ * `source.ended`), as the player sends them after it.
21
+ */
22
+ export function emitSource(host, previous, reload = false) {
23
+ host.emit("source.state", host.source);
24
+ for (const [name, data] of sourceEvents(previous, host.source, reload))
25
+ host.emit(name, structuredClone(data));
26
+ }
16
27
  export function initialSource() {
17
28
  return {
18
29
  kind: "media",
@@ -145,12 +156,13 @@ export function sourceIntent(host, inner, intent) {
145
156
  inner.selected = Number(intent.producerId);
146
157
  if (intent.kind === "open" || intent.kind === "close")
147
158
  inner.selected = null;
159
+ const previous = host.source;
148
160
  host.source = next;
149
161
  host.subtitles.source = {
150
162
  generation: next.stamp.generation,
151
163
  phase: next.phase,
152
164
  };
153
- host.emit("source.state", host.source);
165
+ emitSource(host, previous, intent.kind === "restart");
154
166
  cancelResolutions(host, inner);
155
167
  return { ticket: { stamp: next.stamp, cycle: next.cycle, owner: 1 } };
156
168
  }
@@ -389,6 +401,7 @@ export function setSource(host, inner, patch) {
389
401
  // (the player reads this as the end arrives).
390
402
  if (patch.phase === "ended" && host.source.phase !== "ended")
391
403
  inner.endKeepsPause = inner.stepped || host.source.paused;
404
+ const previous = host.source;
392
405
  host.source = {
393
406
  ...host.source,
394
407
  ...patch,
@@ -400,7 +413,7 @@ export function setSource(host, inner, patch) {
400
413
  generation: host.source.stamp.generation,
401
414
  phase: host.source.phase,
402
415
  };
403
- host.emit("source.state", host.source);
416
+ emitSource(host, previous);
404
417
  // Like the player (reference §6.3): loading shows the backdrop.
405
418
  if (patch.phase === "loading" && host.backdrop && !host.backdrop.effective)
406
419
  host.setBackdropEffective(true);
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The discrete source events of the test host: `source.opening`,
3
+ * `source.ready` and `source.ended`, derived from two consecutive source
4
+ * states by the player's rules (player `src/host/src/source_events.rs`;
5
+ * the cases of its tests are repeated in test/source-events.test.ts).
6
+ *
7
+ * A playthrough is one load of a source (`stamp.generation`, `cycle`); it
8
+ * is in playback while `loading` or `ready`. One that leaves playback gets
9
+ * `ended` (its last state: the state that says it ended, or the last one
10
+ * before a replacement or a close); a new one gets `opening`, then
11
+ * `ready`. A new cycle of the same source that is not loading is a seek
12
+ * away from the end: it plays on, nothing opens. The test host's restart
13
+ * goes to `ready` at once, so it says it is a new load (`reload`).
14
+ *
15
+ * Internal to the SDK; not exported from `@vestra-player/sdk/testing`.
16
+ */
17
+ import type { SourceState } from "../../source.ts";
18
+ export type SourceEventName = "source.opening" | "source.ready" | "source.ended";
19
+ /** The events `next` adds to `previous`, in order. */
20
+ export declare function sourceEvents(previous: SourceState, next: SourceState, reload?: boolean): [SourceEventName, unknown][];
21
+ /**
22
+ * `source.ended` with `reason: "quit"` for a source in playback (the
23
+ * player quits; only a running plugin gets it).
24
+ */
25
+ export declare function quitEvent(state: SourceState): {
26
+ state: SourceState;
27
+ reason: string;
28
+ } | null;
@@ -0,0 +1,66 @@
1
+ const inPlayback = (s) => s.source !== null && (s.phase === "loading" || s.phase === "ready");
2
+ const samePlaythrough = (a, b) => a.stamp.generation === b.stamp.generation && a.cycle === b.cycle;
3
+ /** `source.ended`'s data for a playthrough that ended in `state`. */
4
+ function endedIn(state) {
5
+ const reason = state.terminalReason;
6
+ if (state.phase === "error")
7
+ return reason !== null && typeof reason === "object"
8
+ ? { state, reason: "error", error: reason.failed }
9
+ : { state, reason: "error" };
10
+ return {
11
+ state,
12
+ reason: reason === "natural" || reason === "replaced" ? reason : "stopped",
13
+ };
14
+ }
15
+ /** The events `next` adds to `previous`, in order. */
16
+ export function sourceEvents(previous, next, reload = false) {
17
+ const events = [];
18
+ const open = next.source !== null;
19
+ const same = samePlaythrough(previous, next);
20
+ const resumed = !reload &&
21
+ previous.source !== null &&
22
+ previous.stamp.generation === next.stamp.generation &&
23
+ previous.cycle !== next.cycle &&
24
+ previous.phase === "ended" &&
25
+ next.phase !== "loading";
26
+ const begins = open && !same && !resumed;
27
+ if (inPlayback(previous) && !(same && inPlayback(next))) {
28
+ if (same && open)
29
+ events.push(["source.ended", endedIn(next)]);
30
+ else if (!open && next.stamp.generation === previous.stamp.generation)
31
+ events.push(["source.ended", { state: previous, reason: "stopped" }]);
32
+ else
33
+ events.push(["source.ended", { state: previous, reason: "replaced" }]);
34
+ }
35
+ if (begins) {
36
+ events.push([
37
+ "source.opening",
38
+ {
39
+ state: next,
40
+ previous: previous.source !== null
41
+ ? { sourceId: previous.stamp.generation }
42
+ : null,
43
+ },
44
+ ]);
45
+ if (next.phase === "ready" || next.phase === "ended")
46
+ events.push(["source.ready", { state: next }]);
47
+ if (next.phase === "ended" || next.phase === "error")
48
+ events.push(["source.ended", endedIn(next)]);
49
+ }
50
+ else if (same && previous.phase === "loading" && open) {
51
+ if (next.phase === "ready" || next.phase === "ended")
52
+ events.push(["source.ready", { state: next }]);
53
+ // Loading straight to its end: ready, then ended.
54
+ const at = events.findIndex(([name]) => name === "source.ended");
55
+ if (next.phase === "ended" && at >= 0)
56
+ events.push(...events.splice(at, 1));
57
+ }
58
+ return events;
59
+ }
60
+ /**
61
+ * `source.ended` with `reason: "quit"` for a source in playback (the
62
+ * player quits; only a running plugin gets it).
63
+ */
64
+ export function quitEvent(state) {
65
+ return inPlayback(state) ? { state, reason: "quit" } : null;
66
+ }
@@ -9,6 +9,7 @@
9
9
  import { rmSync } from "node:fs";
10
10
  import { HostError } from "../../wire.js";
11
11
  import { WAKE_EVENTS } from "../connection.js";
12
+ import { hearEvent } from "./triggers.js";
12
13
  export const tick = () => new Promise((resolve) => setImmediate(resolve));
13
14
  /**
14
15
  * Refusals for a position or resolver another plugin holds: not activation
@@ -49,6 +50,10 @@ export function on(_host, inner, event, listener) {
49
50
  }
50
51
  /** Implements `TestHost.emit`. */
51
52
  export function emit(host, inner, event, data) {
53
+ // Trigger conditions first (the player evaluates them before it sends
54
+ // the state); a parked plugin hears nothing, a wake event wakes it.
55
+ if (hearEvent(host, inner, event, data))
56
+ return;
52
57
  if ([
53
58
  "source.state",
54
59
  "playback.backend",
@@ -0,0 +1,82 @@
1
+ import type { Json } from "../../wire.ts";
2
+ import { matchedRange } from "../when.ts";
3
+ import type { ConditionFacts, ConditionJson } from "../when.ts";
4
+ import type { TestHost } from "./index.ts";
5
+ import type { Handler, Internals } from "./internals.ts";
6
+ /**
7
+ * One kept trigger condition, as `TestHost.triggers` lists it.
8
+ *
9
+ * @since 1.10.0
10
+ * @category Testing
11
+ */
12
+ export interface KeptTrigger {
13
+ /** The trigger: its name, or its place among the unnamed ones. */
14
+ readonly trigger: number | string;
15
+ /** The condition in the wire form. */
16
+ readonly when: Json;
17
+ /** Its sequence number. */
18
+ readonly seq: number;
19
+ /** It holds now ("entered"). */
20
+ readonly entered: boolean;
21
+ }
22
+ interface Kept {
23
+ trigger: number | string;
24
+ when: ConditionJson;
25
+ seq: number;
26
+ /** The source its position list belongs to. */
27
+ bound: number | null;
28
+ /** Entered, with the range it is in. */
29
+ entered: {
30
+ range: number | null;
31
+ } | null;
32
+ /** The run that gave it. */
33
+ run: number;
34
+ }
35
+ /** What the player keeps for the plugin's triggers. */
36
+ declare class TriggerState {
37
+ readonly kept: Map<string, Kept>;
38
+ seq: number;
39
+ /** The run (process generation): a park, a wake and an upgrade start one. */
40
+ run: number;
41
+ /** How many unnamed triggers this version declared. */
42
+ unnamed: number | null;
43
+ /** The virtual wall clock (Unix ms). */
44
+ now: number;
45
+ parked: boolean;
46
+ /** The wake events the plugin listened to when it was parked. */
47
+ wakes: Set<string>;
48
+ /** What waits for the next start: (merge key, event, data). */
49
+ backlog: [string, string, unknown][];
50
+ waking: Promise<void> | undefined;
51
+ /** The backlog is delivered now (at the woken plugin's ready). */
52
+ flushing: boolean;
53
+ /** Starts the plugin again (`startPlugin`); `entry`: other code (an upgrade). */
54
+ start: ((entry?: (plugin: never) => unknown) => Promise<void>) | undefined;
55
+ /** Stops the running plugin with a reason. */
56
+ stop: ((reason: string) => Promise<void>) | undefined;
57
+ /** Evaluating: no evaluation inside another. */
58
+ busy: boolean;
59
+ }
60
+ export declare function triggerState(host: TestHost): TriggerState;
61
+ /** The facts the player's conditions read, from the host. */
62
+ export declare function conditionFacts(host: TestHost, state?: TriggerState): ConditionFacts;
63
+ /** Evaluates `only` (or every kept condition) and sends what flipped (T1, T10). */
64
+ export declare function evaluateTriggers(host: TestHost, inner: Internals, only?: Kept): void;
65
+ /**
66
+ * `emit` of `event`: the trigger conditions are evaluated first for the
67
+ * player's own state changes; a wake event for a parked plugin that listened
68
+ * to it is held and wakes it (`true`: not delivered now).
69
+ */
70
+ export declare function hearEvent(host: TestHost, inner: Internals, event: string, data: unknown): boolean;
71
+ /** `TestHost.triggers`. */
72
+ export declare function keptTriggers(host: TestHost): KeptTrigger[];
73
+ /** `TestHost.advance`: the clock and, while playing, the position move by `ms`. */
74
+ export declare function advance(host: TestHost, inner: Internals, ms: number): Promise<void>;
75
+ /** `TestHost.park`: the player puts the plugin in standby. */
76
+ export declare function park(host: TestHost, _inner: Internals): Promise<void>;
77
+ /** `TestHost.upgrade`: another version replaces the plugin (§4.2). */
78
+ export declare function upgrade(host: TestHost, _inner: Internals, entry?: (plugin: never) => unknown): Promise<void>;
79
+ /** The default handlers of this area's wire methods, by method name. */
80
+ export declare function triggersHandlers(host: TestHost, inner: Internals): Record<string, Handler>;
81
+ /** `matchedRange` for tests of the host's position lists. */
82
+ export { matchedRange };