@vnejs/compatibility.scenario.choose-with-scenario.qte 0.1.1

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.
@@ -0,0 +1,20 @@
1
+ import "@vnejs/contracts.scenario.choose";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+ import type { ChooseOption, ChooseOptionReplaceHandler } from "@vnejs/contracts.scenario.choose";
4
+ import type { QteResultPayload } from "@vnejs/contracts.scenario.qte";
5
+ import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "./types.js";
6
+ declare module "@vnejs/contracts.scenario.choose" {
7
+ interface ChooseOption {
8
+ qte?: string;
9
+ qte_fallback?: boolean;
10
+ }
11
+ }
12
+ export declare class CompatibilityChooseQte extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
13
+ name: string;
14
+ pendingResolve: ((success: boolean) => void) | null;
15
+ init: () => Promise<unknown[]> | undefined;
16
+ subscribe: () => void;
17
+ onQteResult: ({ success }?: QteResultPayload) => void;
18
+ onOptionReplace: ChooseOptionReplaceHandler;
19
+ resolveFailIndex: (index: number, options: ChooseOption[]) => number;
20
+ }
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ import "@vnejs/contracts.scenario.choose";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+ import { regModule } from "@vnejs/shared";
4
+ export class CompatibilityChooseQte extends ModuleCore {
5
+ name = "compatibility.scenario.choose-with-scenario.qte";
6
+ pendingResolve = null;
7
+ init = () => this.emit(this.EVENTS.CHOOSE.OPTION_REPLACE_REG, {
8
+ handler: this.onOptionReplace,
9
+ });
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.QTE.RESULT, this.onQteResult);
12
+ };
13
+ onQteResult = ({ success = false } = {}) => {
14
+ if (!this.pendingResolve)
15
+ return;
16
+ const resolve = this.pendingResolve;
17
+ this.pendingResolve = null;
18
+ resolve(!!success);
19
+ };
20
+ onOptionReplace = async ({ index = 0, options = [] } = {}) => {
21
+ const option = options[index];
22
+ const qteId = option?.qte;
23
+ if (typeof qteId !== "string" || !qteId)
24
+ return index;
25
+ const item = this.PARAMS.QTE.ITEMS[qteId];
26
+ if (!item) {
27
+ this.emitLog({ action: "missing", id: qteId });
28
+ return this.resolveFailIndex(index, options);
29
+ }
30
+ const success = await new Promise((resolve) => {
31
+ this.pendingResolve = resolve;
32
+ void this.emit(this.EVENTS.QTE.EMIT, { id: qteId, item, skipScenario: true });
33
+ });
34
+ if (success)
35
+ return index;
36
+ return this.resolveFailIndex(index, options);
37
+ };
38
+ resolveFailIndex = (index, options) => {
39
+ const fallbackIndex = options.findIndex((option) => option.qte_fallback === true);
40
+ if (fallbackIndex < 0) {
41
+ this.emitLog({ action: "missing_fallback", index });
42
+ return index;
43
+ }
44
+ return fallbackIndex;
45
+ };
46
+ }
47
+ regModule(CompatibilityChooseQte);
@@ -0,0 +1,7 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ import type { PluginName as ChoosePluginName, SubscribeEvents as ChooseSubscribeEvents } from "@vnejs/contracts.scenario.choose";
3
+ import type { Params as QteParams, PluginName as QtePluginName, SubscribeEvents as QteSubscribeEvents } from "@vnejs/contracts.scenario.qte";
4
+ export type PluginEvents = ModuleCoreEvents & Record<ChoosePluginName, ChooseSubscribeEvents> & Record<QtePluginName, QteSubscribeEvents>;
5
+ export type PluginConstants = ModuleCoreConstants;
6
+ export type PluginSettings = ModuleCoreSettings;
7
+ export type PluginParams = ModuleCoreParams & Record<QtePluginName, QteParams>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@vnejs/compatibility.scenario.choose-with-scenario.qte",
3
+ "version": "0.1.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "tsconfig.json"
19
+ ],
20
+ "scripts": {
21
+ "test": "npx @vnejs/monorepo test",
22
+ "build": "npx @vnejs/monorepo package",
23
+ "publish:major:plugin": "npm run publish:major",
24
+ "publish:minor:plugin": "npm run publish:minor",
25
+ "publish:patch:plugin": "npm run publish:patch",
26
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
27
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
28
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
29
+ },
30
+ "author": "",
31
+ "license": "ISC",
32
+ "peerDependencies": {
33
+ "@vnejs/module.core": "~0.1.0",
34
+ "@vnejs/contracts.scenario.choose": "~0.1.0",
35
+ "@vnejs/contracts.scenario.qte": "~0.1.0",
36
+ "@vnejs/shared": "~0.1.0"
37
+ },
38
+ "devDependencies": {
39
+ "@vnejs/configs.ts-common": "~0.1.0",
40
+ "@vnejs/configs.vitest": "~0.1.0",
41
+ "@vnejs/test-utils": "~0.1.0",
42
+ "@vnejs/contracts.scenario.choose": "~0.1.0",
43
+ "@vnejs/contracts.scenario.qte": "~0.1.0",
44
+ "@vnejs/module.core": "~0.1.0",
45
+ "@vnejs/shared": "~0.1.0"
46
+ }
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,79 @@
1
+ import "@vnejs/contracts.scenario.choose";
2
+
3
+ import { ModuleCore } from "@vnejs/module.core";
4
+ import { regModule } from "@vnejs/shared";
5
+ import type {
6
+ ChooseOption,
7
+ ChooseOptionReplaceHandler,
8
+ ChooseOptionReplacePayload,
9
+ ChooseOptionReplaceRegPayload,
10
+ } from "@vnejs/contracts.scenario.choose";
11
+ import type { QteEmitPayload, QteResultPayload } from "@vnejs/contracts.scenario.qte";
12
+
13
+ import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "./types.js";
14
+
15
+ declare module "@vnejs/contracts.scenario.choose" {
16
+ interface ChooseOption {
17
+ qte?: string;
18
+ qte_fallback?: boolean;
19
+ }
20
+ }
21
+
22
+ export class CompatibilityChooseQte extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
23
+ name = "compatibility.scenario.choose-with-scenario.qte";
24
+
25
+ pendingResolve: ((success: boolean) => void) | null = null;
26
+
27
+ init = () =>
28
+ this.emit(this.EVENTS.CHOOSE.OPTION_REPLACE_REG, {
29
+ handler: this.onOptionReplace,
30
+ } satisfies ChooseOptionReplaceRegPayload);
31
+
32
+ subscribe = () => {
33
+ this.on(this.EVENTS.QTE.RESULT, this.onQteResult);
34
+ };
35
+
36
+ onQteResult = ({ success = false }: QteResultPayload = {}) => {
37
+ if (!this.pendingResolve) return;
38
+
39
+ const resolve = this.pendingResolve;
40
+ this.pendingResolve = null;
41
+ resolve(!!success);
42
+ };
43
+
44
+ onOptionReplace: ChooseOptionReplaceHandler = async ({ index = 0, options = [] }: ChooseOptionReplacePayload = {}) => {
45
+ const option = options[index];
46
+ const qteId = option?.qte;
47
+
48
+ if (typeof qteId !== "string" || !qteId) return index;
49
+
50
+ const item = this.PARAMS.QTE.ITEMS[qteId];
51
+
52
+ if (!item) {
53
+ this.emitLog({ action: "missing", id: qteId });
54
+ return this.resolveFailIndex(index, options);
55
+ }
56
+
57
+ const success = await new Promise<boolean>((resolve) => {
58
+ this.pendingResolve = resolve;
59
+ void this.emit(this.EVENTS.QTE.EMIT, { id: qteId, item, skipScenario: true } satisfies QteEmitPayload);
60
+ });
61
+
62
+ if (success) return index;
63
+
64
+ return this.resolveFailIndex(index, options);
65
+ };
66
+
67
+ resolveFailIndex = (index: number, options: ChooseOption[]) => {
68
+ const fallbackIndex = options.findIndex((option) => option.qte_fallback === true);
69
+
70
+ if (fallbackIndex < 0) {
71
+ this.emitLog({ action: "missing_fallback", index });
72
+ return index;
73
+ }
74
+
75
+ return fallbackIndex;
76
+ };
77
+ }
78
+
79
+ regModule(CompatibilityChooseQte);
@@ -0,0 +1,112 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { spyEvent } from "@vnejs/test-utils";
4
+ import type { ChooseOption, ChooseOptionReplaceHandler } from "@vnejs/contracts.scenario.choose";
5
+
6
+ import { CompatibilityChooseQte } from "../index.js";
7
+ import { CHOOSE_EVENTS, createCompatibilityTestModule, QTE_EVENTS, QTE_PARAMS, registerCompatibilityVne } from "./setup.js";
8
+
9
+ const OPTIONS: ChooseOption[] = [
10
+ { index: 10, text: "With QTE", qte: "easy" },
11
+ { index: 20, text: "Fallback", qte_fallback: true },
12
+ ];
13
+
14
+ const ITEM = { start: 50, size: 40, duration: 2500 };
15
+
16
+ describe("CompatibilityChooseQte", () => {
17
+ beforeEach(() => {
18
+ registerCompatibilityVne();
19
+ Object.assign(QTE_PARAMS.ITEMS, { easy: ITEM });
20
+ });
21
+
22
+ it("init registers OPTION_REPLACE handler", async () => {
23
+ const { module, observer } = createCompatibilityTestModule(CompatibilityChooseQte, { subscribe: false });
24
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
25
+
26
+ await (module as CompatibilityChooseQte).init();
27
+
28
+ expect(replaceReg).toHaveBeenCalledOnce();
29
+ expect(replaceReg.mock.calls[0]?.[0]?.handler).toBeTypeOf("function");
30
+ });
31
+
32
+ it("returns same index when option has no qte", async () => {
33
+ const { module, observer } = createCompatibilityTestModule(CompatibilityChooseQte, { subscribe: false });
34
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
35
+ const emit = spyEvent(observer, QTE_EVENTS.EMIT);
36
+
37
+ await (module as CompatibilityChooseQte).init();
38
+
39
+ const handler = replaceReg.mock.calls[0]?.[0]?.handler as ChooseOptionReplaceHandler;
40
+ const result = await handler({ index: 1, options: OPTIONS });
41
+
42
+ expect(result).toBe(1);
43
+ expect(emit).not.toHaveBeenCalled();
44
+ });
45
+
46
+ it("success keeps selected index", async () => {
47
+ const { module, observer } = createCompatibilityTestModule(CompatibilityChooseQte);
48
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
49
+ const emit = spyEvent(observer, QTE_EVENTS.EMIT);
50
+
51
+ await (module as CompatibilityChooseQte).init();
52
+
53
+ const handler = replaceReg.mock.calls[0]?.[0]?.handler as ChooseOptionReplaceHandler;
54
+ const pending = handler({ index: 0, options: OPTIONS });
55
+
56
+ await Promise.resolve();
57
+ expect(emit).toHaveBeenCalledExactlyOnceWith({ id: "easy", item: ITEM, skipScenario: true });
58
+
59
+ await observer.emit(QTE_EVENTS.RESULT, { success: true });
60
+
61
+ expect(await pending).toBe(0);
62
+ });
63
+
64
+ it("fail remaps to qte_fallback option", async () => {
65
+ const { module, observer } = createCompatibilityTestModule(CompatibilityChooseQte);
66
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
67
+
68
+ await (module as CompatibilityChooseQte).init();
69
+
70
+ const handler = replaceReg.mock.calls[0]?.[0]?.handler as ChooseOptionReplaceHandler;
71
+ const pending = handler({ index: 0, options: OPTIONS });
72
+
73
+ await Promise.resolve();
74
+ await observer.emit(QTE_EVENTS.RESULT, { success: false });
75
+
76
+ expect(await pending).toBe(1);
77
+ });
78
+
79
+ it("fail without fallback keeps selected index", async () => {
80
+ const { module, observer } = createCompatibilityTestModule(CompatibilityChooseQte);
81
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
82
+ const options: ChooseOption[] = [{ index: 10, text: "With QTE", qte: "easy" }];
83
+
84
+ await (module as CompatibilityChooseQte).init();
85
+
86
+ const handler = replaceReg.mock.calls[0]?.[0]?.handler as ChooseOptionReplaceHandler;
87
+ const pending = handler({ index: 0, options });
88
+
89
+ await Promise.resolve();
90
+ await observer.emit(QTE_EVENTS.RESULT, { success: false });
91
+
92
+ expect(await pending).toBe(0);
93
+ });
94
+
95
+ it("missing item fails without opening QTE emit of item", async () => {
96
+ const { module, observer } = createCompatibilityTestModule(CompatibilityChooseQte, { subscribe: false });
97
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
98
+ const emit = spyEvent(observer, QTE_EVENTS.EMIT);
99
+ const options: ChooseOption[] = [
100
+ { index: 10, text: "With QTE", qte: "missing" },
101
+ { index: 20, text: "Fallback", qte_fallback: true },
102
+ ];
103
+
104
+ await (module as CompatibilityChooseQte).init();
105
+
106
+ const handler = replaceReg.mock.calls[0]?.[0]?.handler as ChooseOptionReplaceHandler;
107
+ const result = await handler({ index: 0, options });
108
+
109
+ expect(result).toBe(1);
110
+ expect(emit).not.toHaveBeenCalled();
111
+ });
112
+ });
@@ -0,0 +1,16 @@
1
+ import { PARAMS as CHOOSE_PARAMS, PLUGIN_NAME as CHOOSE, SUBSCRIBE_EVENTS as CHOOSE_EVENTS } from "@vnejs/contracts.scenario.choose";
2
+ import { PARAMS as QTE_PARAMS, PLUGIN_NAME as QTE, SUBSCRIBE_EVENTS as QTE_EVENTS } from "@vnejs/contracts.scenario.qte";
3
+ import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin } from "@vnejs/test-utils";
4
+
5
+ export const registerCompatibilityVne = () => {
6
+ registerCoreVne();
7
+ registerVnePlugin(CHOOSE, { events: CHOOSE_EVENTS, params: CHOOSE_PARAMS });
8
+ registerVnePlugin(QTE, { events: QTE_EVENTS, params: QTE_PARAMS });
9
+ };
10
+
11
+ export const createCompatibilityTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
12
+ ModuleClass: T,
13
+ options: Parameters<typeof baseCreateTestModule>[1] = {},
14
+ ) => baseCreateTestModule(ModuleClass, options);
15
+
16
+ export { CHOOSE_EVENTS, QTE_EVENTS, QTE_PARAMS };
package/src/types.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ import type { PluginName as ChoosePluginName, SubscribeEvents as ChooseSubscribeEvents } from "@vnejs/contracts.scenario.choose";
3
+ import type {
4
+ Params as QteParams,
5
+ PluginName as QtePluginName,
6
+ SubscribeEvents as QteSubscribeEvents,
7
+ } from "@vnejs/contracts.scenario.qte";
8
+
9
+ export type PluginEvents = ModuleCoreEvents &
10
+ Record<ChoosePluginName, ChooseSubscribeEvents> &
11
+ Record<QtePluginName, QteSubscribeEvents>;
12
+
13
+ export type PluginConstants = ModuleCoreConstants;
14
+
15
+ export type PluginSettings = ModuleCoreSettings;
16
+
17
+ export type PluginParams = ModuleCoreParams & Record<QtePluginName, QteParams>;
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": [
8
+ "src/**/*.ts"
9
+ ],
10
+ "exclude": [
11
+ "dist",
12
+ "node_modules",
13
+ "src/tests"
14
+ ]
15
+ }