@vnejs/bridge.plugins.scenario.choose.plugins.scenario.qte 0.0.0-stage → 0.2.7

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 @@
1
+ import "@vnejs/contracts.scenario.choose";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import "@vnejs/contracts.scenario.choose";
2
+ import { regPlugin } from "@vnejs/registry";
3
+ import { BridgePluginChoosePluginQte } from "./modules/bridge_plugin_choose_plugin_qte.js";
4
+ const PLUGIN_NAME = "BRIDGE_PLUGIN_CHOOSE_PLUGIN_QTE";
5
+ regPlugin(PLUGIN_NAME, {}, [BridgePluginChoosePluginQte]);
@@ -0,0 +1,18 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { ChooseOption, ChooseOptionReplaceHandler } from "@vnejs/contracts.scenario.choose";
3
+ import type { QteResultPayload } from "@vnejs/contracts.scenario.qte";
4
+ import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "../types.js";
5
+ declare module "@vnejs/contracts.scenario.choose" {
6
+ interface ChooseOption {
7
+ qte?: string;
8
+ }
9
+ }
10
+ export declare class BridgePluginChoosePluginQte extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
11
+ name: string;
12
+ pendingResolve: ((success: boolean) => void) | null;
13
+ subscribe: () => void;
14
+ init: () => Promise<import("@vnejs/registry").EmitResults<"vne:choose:option_replace_reg">> | undefined;
15
+ onOptionReplace: ChooseOptionReplaceHandler;
16
+ onQteResult: ({ success }?: QteResultPayload) => void;
17
+ resolveFailIndex: (index: number, options: ChooseOption[]) => number;
18
+ }
@@ -0,0 +1,46 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ const LOGS_ACTIONS = {
3
+ MISSING: "missing",
4
+ MISSING_FALLBACK: "missing_fallback",
5
+ };
6
+ export class BridgePluginChoosePluginQte extends ModuleCore {
7
+ name = "bridge_plugin_choose_plugin_qte";
8
+ pendingResolve = null;
9
+ subscribe = () => {
10
+ this.on(this.EVENTS.QTE.RESULT, this.onQteResult);
11
+ };
12
+ init = () => this.emit(this.EVENTS.CHOOSE.OPTION_REPLACE_REG, { handler: this.onOptionReplace });
13
+ onOptionReplace = async ({ index = 0, options = [] } = {}) => {
14
+ const option = options[index];
15
+ const qteId = option?.qte;
16
+ if (typeof qteId !== "string" || !qteId)
17
+ return index;
18
+ const item = this.PARAMS.QTE.ITEMS[qteId];
19
+ if (!item) {
20
+ this.emit(this.EVENTS.LOGS.EMIT, { action: LOGS_ACTIONS.MISSING, id: qteId });
21
+ return this.resolveFailIndex(index, options);
22
+ }
23
+ const success = await new Promise((resolve) => {
24
+ this.pendingResolve = resolve;
25
+ this.emit(this.EVENTS.QTE.EMIT, { id: qteId, item, skipScenario: true });
26
+ });
27
+ if (success)
28
+ return index;
29
+ return this.resolveFailIndex(index, options);
30
+ };
31
+ onQteResult = ({ success = false } = {}) => {
32
+ if (!this.pendingResolve)
33
+ return;
34
+ const resolve = this.pendingResolve;
35
+ this.pendingResolve = null;
36
+ resolve(!!success);
37
+ };
38
+ resolveFailIndex = (index, options) => {
39
+ const fallbackIndex = options.findIndex((option) => option.keywords?.includes(this.CONST.QTE.LINE_KEYWORDS.QTE_FALLBACK));
40
+ if (fallbackIndex < 0) {
41
+ this.emit(this.EVENTS.LOGS.EMIT, { action: LOGS_ACTIONS.MISSING_FALLBACK, index });
42
+ return index;
43
+ }
44
+ return fallbackIndex;
45
+ };
46
+ }
@@ -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 { Constants as QteConstants, 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 & Record<QtePluginName, QteConstants>;
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 CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@vnejs/bridge.plugins.scenario.choose.plugins.scenario.qte",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.7",
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.2.0",
34
+ "@vnejs/contracts.scenario.choose": "~0.2.0",
35
+ "@vnejs/contracts.scenario.qte": "~0.2.0",
36
+ "@vnejs/registry": "~0.2.0"
37
+ },
38
+ "devDependencies": {
39
+ "@vnejs/configs.ts-common": "~0.2.0",
40
+ "@vnejs/configs.vitest": "~0.2.0",
41
+ "@vnejs/test-utils": "~0.2.0",
42
+ "@vnejs/contracts.scenario.choose": "~0.2.0",
43
+ "@vnejs/contracts.scenario.qte": "~0.2.0",
44
+ "@vnejs/module.core": "~0.2.0",
45
+ "@vnejs/registry": "~0.2.0"
46
+ }
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ import "@vnejs/contracts.scenario.choose";
2
+
3
+ import { regPlugin } from "@vnejs/registry";
4
+
5
+ import { BridgePluginChoosePluginQte } from "./modules/bridge_plugin_choose_plugin_qte.js";
6
+
7
+ const PLUGIN_NAME = "BRIDGE_PLUGIN_CHOOSE_PLUGIN_QTE";
8
+
9
+ regPlugin(PLUGIN_NAME, {}, [BridgePluginChoosePluginQte]);
@@ -0,0 +1,70 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { ChooseOption, ChooseOptionReplaceHandler } from "@vnejs/contracts.scenario.choose";
3
+ import type { QteResultPayload } from "@vnejs/contracts.scenario.qte";
4
+
5
+ import type { PluginConstants, PluginEvents, PluginParams, PluginSettings } from "../types.js";
6
+
7
+ const LOGS_ACTIONS = {
8
+ MISSING: "missing",
9
+ MISSING_FALLBACK: "missing_fallback",
10
+ } as const;
11
+
12
+ declare module "@vnejs/contracts.scenario.choose" {
13
+ interface ChooseOption {
14
+ qte?: string;
15
+ }
16
+ }
17
+
18
+ export class BridgePluginChoosePluginQte extends ModuleCore<PluginEvents, PluginConstants, PluginSettings, PluginParams> {
19
+ name = "bridge_plugin_choose_plugin_qte";
20
+
21
+ pendingResolve: ((success: boolean) => void) | null = null;
22
+
23
+ subscribe = () => {
24
+ this.on(this.EVENTS.QTE.RESULT, this.onQteResult);
25
+ };
26
+
27
+ init = () => this.emit(this.EVENTS.CHOOSE.OPTION_REPLACE_REG, { handler: this.onOptionReplace });
28
+
29
+ onOptionReplace: ChooseOptionReplaceHandler = async ({ index = 0, options = [] } = {}) => {
30
+ const option = options[index];
31
+ const qteId = option?.qte;
32
+
33
+ if (typeof qteId !== "string" || !qteId) return index;
34
+
35
+ const item = this.PARAMS.QTE.ITEMS[qteId];
36
+
37
+ if (!item) {
38
+ this.emit(this.EVENTS.LOGS.EMIT, { action: LOGS_ACTIONS.MISSING, id: qteId });
39
+ return this.resolveFailIndex(index, options);
40
+ }
41
+
42
+ const success = await new Promise<boolean>((resolve) => {
43
+ this.pendingResolve = resolve;
44
+ this.emit(this.EVENTS.QTE.EMIT, { id: qteId, item, skipScenario: true });
45
+ });
46
+
47
+ if (success) return index;
48
+
49
+ return this.resolveFailIndex(index, options);
50
+ };
51
+
52
+ onQteResult = ({ success = false }: QteResultPayload = {}) => {
53
+ if (!this.pendingResolve) return;
54
+
55
+ const resolve = this.pendingResolve;
56
+ this.pendingResolve = null;
57
+ resolve(!!success);
58
+ };
59
+
60
+ resolveFailIndex = (index: number, options: ChooseOption[]) => {
61
+ const fallbackIndex = options.findIndex((option) => option.keywords?.includes(this.CONST.QTE.LINE_KEYWORDS.QTE_FALLBACK));
62
+
63
+ if (fallbackIndex < 0) {
64
+ this.emit(this.EVENTS.LOGS.EMIT, { action: LOGS_ACTIONS.MISSING_FALLBACK, index });
65
+ return index;
66
+ }
67
+
68
+ return fallbackIndex;
69
+ };
70
+ }
@@ -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 { BridgePluginChoosePluginQte } from "../modules/bridge_plugin_choose_plugin_qte.js";
7
+ import { CHOOSE_EVENTS, createCompatibilityTestModule, QTE_EVENTS, QTE_LINE_KEYWORDS, QTE_PARAMS, registerCompatibilityVne } from "./setup.js";
8
+
9
+ const OPTIONS: ChooseOption[] = [
10
+ { index: 10, text: "With QTE", qte: "easy" },
11
+ { index: 20, text: "Fallback", keywords: [QTE_LINE_KEYWORDS.QTE_FALLBACK] },
12
+ ];
13
+
14
+ const ITEM = { start: 50, size: 40, duration: 2500 };
15
+
16
+ describe("BridgePluginChoosePluginQte", () => {
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(BridgePluginChoosePluginQte, { subscribe: false });
24
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
25
+
26
+ await (module as BridgePluginChoosePluginQte).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(BridgePluginChoosePluginQte, { 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 BridgePluginChoosePluginQte).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(BridgePluginChoosePluginQte);
48
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
49
+ const emit = spyEvent(observer, QTE_EVENTS.EMIT);
50
+
51
+ await (module as BridgePluginChoosePluginQte).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(BridgePluginChoosePluginQte);
66
+ const replaceReg = spyEvent(observer, CHOOSE_EVENTS.OPTION_REPLACE_REG);
67
+
68
+ await (module as BridgePluginChoosePluginQte).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(BridgePluginChoosePluginQte);
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 BridgePluginChoosePluginQte).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(BridgePluginChoosePluginQte, { 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", keywords: [QTE_LINE_KEYWORDS.QTE_FALLBACK] },
102
+ ];
103
+
104
+ await (module as BridgePluginChoosePluginQte).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,22 @@
1
+ import { PARAMS as CHOOSE_PARAMS, PLUGIN_NAME as CHOOSE, SUBSCRIBE_EVENTS as CHOOSE_EVENTS } from "@vnejs/contracts.scenario.choose";
2
+ import {
3
+ CONSTANTS as QTE_CONSTANTS,
4
+ LINE_KEYWORDS as QTE_LINE_KEYWORDS,
5
+ PARAMS as QTE_PARAMS,
6
+ PLUGIN_NAME as QTE,
7
+ SUBSCRIBE_EVENTS as QTE_EVENTS,
8
+ } from "@vnejs/contracts.scenario.qte";
9
+ import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin } from "@vnejs/test-utils";
10
+
11
+ export const registerCompatibilityVne = () => {
12
+ registerCoreVne();
13
+ registerVnePlugin(CHOOSE, { events: CHOOSE_EVENTS, params: CHOOSE_PARAMS });
14
+ registerVnePlugin(QTE, { events: QTE_EVENTS, params: QTE_PARAMS, constants: QTE_CONSTANTS });
15
+ };
16
+
17
+ export const createCompatibilityTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
18
+ ModuleClass: T,
19
+ options: Parameters<typeof baseCreateTestModule>[1] = {},
20
+ ) => baseCreateTestModule(ModuleClass, options);
21
+
22
+ export { CHOOSE_EVENTS, QTE_EVENTS, QTE_LINE_KEYWORDS, QTE_PARAMS };
package/src/types.ts ADDED
@@ -0,0 +1,16 @@
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
+ Constants as QteConstants,
5
+ Params as QteParams,
6
+ PluginName as QtePluginName,
7
+ SubscribeEvents as QteSubscribeEvents,
8
+ } from "@vnejs/contracts.scenario.qte";
9
+
10
+ export type PluginEvents = ModuleCoreEvents & Record<ChoosePluginName, ChooseSubscribeEvents> & Record<QtePluginName, QteSubscribeEvents>;
11
+
12
+ export type PluginConstants = ModuleCoreConstants & Record<QtePluginName, QteConstants>;
13
+
14
+ export type PluginSettings = ModuleCoreSettings;
15
+
16
+ export type PluginParams = ModuleCoreParams & Record<QtePluginName, QteParams>;
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts"],
8
+ "exclude": ["dist", "node_modules", "src/tests"]
9
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.