@vnejs/plugins.scenario.random 0.1.15 → 0.1.17
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.
- package/dist/modules/random.d.ts +3 -1
- package/dist/modules/random.js +13 -2
- package/package.json +9 -4
- package/src/modules/random.ts +17 -2
- package/src/tests/random.test.ts +32 -0
- package/src/tests/setup.ts +17 -0
- package/tsconfig.json +1 -1
package/dist/modules/random.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { ModuleCore } from "@vnejs/module.core";
|
|
|
2
2
|
import type { RandomPluginConstants, RandomPluginEvents, RandomPluginParams, RandomPluginSettings } from "../types.js";
|
|
3
3
|
export declare class Random extends ModuleCore<RandomPluginEvents, RandomPluginConstants, RandomPluginSettings, RandomPluginParams> {
|
|
4
4
|
name: string;
|
|
5
|
-
|
|
5
|
+
optionPrefix: string;
|
|
6
|
+
init: () => void;
|
|
6
7
|
onLineExec: () => Promise<void>;
|
|
8
|
+
onOptionLineExec: () => Promise<void>;
|
|
7
9
|
}
|
package/dist/modules/random.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
import { getOptionExecShift } from "@vnejs/helpers";
|
|
1
2
|
import { ModuleCore } from "@vnejs/module.core";
|
|
2
3
|
export class Random extends ModuleCore {
|
|
3
4
|
name = "random";
|
|
4
|
-
|
|
5
|
+
optionPrefix = `% ${this.name}.option`;
|
|
6
|
+
init = () => {
|
|
7
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.name, handler: this.onLineExec });
|
|
8
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: `${this.name}.option`, handler: this.onOptionLineExec });
|
|
9
|
+
};
|
|
5
10
|
onLineExec = async () => {
|
|
6
11
|
const { label, curLine } = this.globalState.scenario;
|
|
7
12
|
const options = [];
|
|
@@ -9,7 +14,7 @@ export class Random extends ModuleCore {
|
|
|
9
14
|
let indexShift = 1;
|
|
10
15
|
let curLineObj = lines[curLine + indexShift];
|
|
11
16
|
while (curLineObj && (curLineObj.indent ?? 0) > (lines[curLine].indent ?? 0)) {
|
|
12
|
-
if (curLineObj.line.startsWith(
|
|
17
|
+
if (curLineObj.line.startsWith(this.optionPrefix) && curLineObj.indent === (lines[curLine].indent ?? 0) + 2)
|
|
13
18
|
options.push(curLine + indexShift);
|
|
14
19
|
indexShift++;
|
|
15
20
|
curLineObj = lines[curLine + indexShift];
|
|
@@ -19,4 +24,10 @@ export class Random extends ModuleCore {
|
|
|
19
24
|
this.globalState.scenario.curLine = options[index] + 1;
|
|
20
25
|
this.emitNext();
|
|
21
26
|
};
|
|
27
|
+
onOptionLineExec = async () => {
|
|
28
|
+
const { label, curLine } = this.globalState.scenario;
|
|
29
|
+
const lines = (await this.emitOne(this.EVENTS.SCENARIO.LINES_GET, { label }));
|
|
30
|
+
this.globalState.scenario.curLine = curLine + getOptionExecShift(lines, curLine);
|
|
31
|
+
this.emitNext();
|
|
32
|
+
};
|
|
22
33
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.scenario.random",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"tsconfig.json"
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
|
-
"test": "
|
|
21
|
+
"test": "npx @vnejs/monorepo test",
|
|
22
22
|
"build": "npx @vnejs/monorepo package",
|
|
23
23
|
"publish:major:plugin": "npm run publish:major",
|
|
24
24
|
"publish:minor:plugin": "npm run publish:minor",
|
|
@@ -30,13 +30,18 @@
|
|
|
30
30
|
"author": "",
|
|
31
31
|
"license": "ISC",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@vnejs/contracts.scenario.random": "~0.1.0"
|
|
33
|
+
"@vnejs/contracts.scenario.random": "~0.1.0",
|
|
34
|
+
"@vnejs/helpers": "~0.1.0"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
37
|
+
"@vnejs/helpers": "~0.1.0",
|
|
36
38
|
"@vnejs/module.core": "~0.1.0",
|
|
37
39
|
"@vnejs/shared": "~0.1.0"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
|
-
"@vnejs/configs.ts-common": "~0.1.0"
|
|
42
|
+
"@vnejs/configs.ts-common": "~0.1.0",
|
|
43
|
+
"@vnejs/configs.vitest": "~0.1.0",
|
|
44
|
+
"@vnejs/helpers": "~0.1.0",
|
|
45
|
+
"@vnejs/test-utils": "~0.1.0"
|
|
41
46
|
}
|
|
42
47
|
}
|
package/src/modules/random.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getOptionExecShift } from "@vnejs/helpers";
|
|
1
2
|
import { ModuleCore } from "@vnejs/module.core";
|
|
2
3
|
|
|
3
4
|
import type { RandomPluginConstants, RandomPluginEvents, RandomPluginParams, RandomPluginSettings } from "../types.js";
|
|
@@ -6,7 +7,12 @@ import type { ScenarioLine } from "../utils/random.js";
|
|
|
6
7
|
export class Random extends ModuleCore<RandomPluginEvents, RandomPluginConstants, RandomPluginSettings, RandomPluginParams> {
|
|
7
8
|
name = "random";
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
optionPrefix = `% ${this.name}.option`;
|
|
11
|
+
|
|
12
|
+
init = () => {
|
|
13
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: this.name, handler: this.onLineExec });
|
|
14
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { block: `${this.name}.option`, handler: this.onOptionLineExec });
|
|
15
|
+
};
|
|
10
16
|
|
|
11
17
|
onLineExec = async () => {
|
|
12
18
|
const { label, curLine } = this.globalState.scenario;
|
|
@@ -17,7 +23,7 @@ export class Random extends ModuleCore<RandomPluginEvents, RandomPluginConstants
|
|
|
17
23
|
let curLineObj = lines[curLine + indexShift];
|
|
18
24
|
|
|
19
25
|
while (curLineObj && (curLineObj.indent ?? 0) > (lines[curLine].indent ?? 0)) {
|
|
20
|
-
if (curLineObj.line.startsWith(
|
|
26
|
+
if (curLineObj.line.startsWith(this.optionPrefix) && curLineObj.indent === (lines[curLine].indent ?? 0) + 2) options.push(curLine + indexShift);
|
|
21
27
|
|
|
22
28
|
indexShift++;
|
|
23
29
|
curLineObj = lines[curLine + indexShift];
|
|
@@ -31,4 +37,13 @@ export class Random extends ModuleCore<RandomPluginEvents, RandomPluginConstants
|
|
|
31
37
|
|
|
32
38
|
this.emitNext();
|
|
33
39
|
};
|
|
40
|
+
|
|
41
|
+
onOptionLineExec = async () => {
|
|
42
|
+
const { label, curLine } = this.globalState.scenario;
|
|
43
|
+
const lines = (await this.emitOne(this.EVENTS.SCENARIO.LINES_GET, { label })) as ScenarioLine[];
|
|
44
|
+
|
|
45
|
+
this.globalState.scenario.curLine = curLine + getOptionExecShift(lines, curLine);
|
|
46
|
+
|
|
47
|
+
this.emitNext();
|
|
48
|
+
};
|
|
34
49
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { SUBSCRIBE_EVENTS as SCENARIO_EVENTS } from "@vnejs/contracts.core.scenario";
|
|
4
|
+
import { spyEvent, stubEvent } from "@vnejs/test-utils";
|
|
5
|
+
|
|
6
|
+
import { Random } from "../modules/random.js";
|
|
7
|
+
import { createRandomTestModule, registerRandomPluginVne } from "./setup.js";
|
|
8
|
+
|
|
9
|
+
describe("Random", () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
registerRandomPluginVne();
|
|
12
|
+
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("onLineExec picks random option and advances scenario", async () => {
|
|
16
|
+
const { module, observer, state } = createRandomTestModule(Random, { subscribe: false });
|
|
17
|
+
const next = spyEvent(observer, SCENARIO_EVENTS.NEXT);
|
|
18
|
+
|
|
19
|
+
stubEvent(observer, SCENARIO_EVENTS.LINES_GET, () => [
|
|
20
|
+
{ line: "% random", indent: 0 },
|
|
21
|
+
{ line: "% random.option A", indent: 2 },
|
|
22
|
+
{ line: "% random.option B", indent: 2 },
|
|
23
|
+
|
|
24
|
+
{ line: "next", indent: 0 },
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
await (module as Random).onLineExec();
|
|
28
|
+
|
|
29
|
+
expect(state.scenario.curLine).toBe(2);
|
|
30
|
+
expect(next).toHaveBeenCalledOnce();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PLUGIN_NAME } from "@vnejs/contracts.scenario.random";
|
|
2
|
+
import { createCoreGlobalState, createTestModule, registerCoreVne } from "@vnejs/test-utils";
|
|
3
|
+
|
|
4
|
+
export const registerRandomPluginVne = () => {
|
|
5
|
+
registerCoreVne();
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const createRandomTestModule = <T extends Parameters<typeof createTestModule>[0]>(
|
|
9
|
+
ModuleClass: T,
|
|
10
|
+
options: Parameters<typeof createTestModule>[1] = {},
|
|
11
|
+
) =>
|
|
12
|
+
createTestModule(ModuleClass, {
|
|
13
|
+
state: createCoreGlobalState({ scenario: { label: "main", curLine: 0 } }),
|
|
14
|
+
...options,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export { PLUGIN_NAME };
|
package/tsconfig.json
CHANGED