@vnejs/plugins.scenario.fastforward 0.2.0 → 0.2.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.
@@ -1,6 +1,10 @@
1
1
  import "@vnejs/contracts.core.scenario";
2
2
  import { ModuleCore } from "@vnejs/module.core";
3
3
  const STORAGE_VALUE = 1;
4
+ const toVisitKey = (key) => {
5
+ const sep = key.lastIndexOf("/");
6
+ return sep === -1 ? key : key.slice(sep + 1);
7
+ };
4
8
  export class FastforwardVisits extends ModuleCore {
5
9
  name = "fastforward.visits";
6
10
  visits = new Set();
@@ -13,12 +17,14 @@ export class FastforwardVisits extends ModuleCore {
13
17
  };
14
18
  init = async () => {
15
19
  await this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.CHECK_VISITED, value: this.PARAMS.FASTFORWARD.DEFAULT_CHECK_VISITED });
16
- this.visits = new Set(Object.keys((await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name }))));
20
+ const stored = (await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name }));
21
+ this.visits = new Set(Object.keys(stored).map(toVisitKey));
17
22
  };
18
23
  onExec = async ({ module, block, line, args = {}, keywords = [] } = {}) => {
19
24
  const key = (await this.emitOne(this.EVENTS.VENDORS.HASH, this.state));
25
+ const group = this.state.label || this.globalState.scenario.label;
20
26
  this.visits.add(key);
21
- this.emit(this.EVENTS.STORAGE.SET, { module: this.name, key, value: STORAGE_VALUE });
27
+ this.emit(this.EVENTS.STORAGE.SET, { module: this.name, group, key, value: STORAGE_VALUE });
22
28
  this.setState({ module, block, line, args, keywords, label: this.globalState.scenario.label });
23
29
  };
24
30
  onVisitCheck = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.scenario.fastforward",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,6 +8,12 @@ import type { FastforwardPluginConstants, FastforwardPluginEvents, FastforwardPl
8
8
 
9
9
  const STORAGE_VALUE = 1;
10
10
 
11
+ const toVisitKey = (key: string) => {
12
+ const sep = key.lastIndexOf("/");
13
+
14
+ return sep === -1 ? key : key.slice(sep + 1);
15
+ };
16
+
11
17
  export class FastforwardVisits extends ModuleCore<
12
18
  FastforwardPluginEvents,
13
19
  FastforwardPluginConstants,
@@ -32,14 +38,17 @@ export class FastforwardVisits extends ModuleCore<
32
38
  init = async () => {
33
39
  await this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.FASTFORWARD.CHECK_VISITED, value: this.PARAMS.FASTFORWARD.DEFAULT_CHECK_VISITED });
34
40
 
35
- this.visits = new Set(Object.keys((await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name })) as Record<string, number>));
41
+ const stored = (await this.emitOne(this.EVENTS.STORAGE.GET_ALL, { module: this.name })) as Record<string, number>;
42
+
43
+ this.visits = new Set(Object.keys(stored).map(toVisitKey));
36
44
  };
37
45
 
38
46
  onExec = async ({ module, block, line, args = {}, keywords = [] }: ScenarioExecPayload = {}) => {
39
47
  const key = (await this.emitOne(this.EVENTS.VENDORS.HASH, this.state)) as string;
48
+ const group = this.state.label || this.globalState.scenario.label;
40
49
 
41
50
  this.visits.add(key);
42
- this.emit(this.EVENTS.STORAGE.SET, { module: this.name, key, value: STORAGE_VALUE });
51
+ this.emit(this.EVENTS.STORAGE.SET, { module: this.name, group, key, value: STORAGE_VALUE });
43
52
 
44
53
  this.setState({ module, block, line, args, keywords, label: this.globalState.scenario.label });
45
54
  };
@@ -0,0 +1,72 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { SUBSCRIBE_EVENTS as SCENARIO_EVENTS } from "@vnejs/contracts.core.scenario";
4
+ import { SUBSCRIBE_EVENTS as SETTINGS_EVENTS } from "@vnejs/contracts.core.settings";
5
+ import { SUBSCRIBE_EVENTS as STORAGE_EVENTS } from "@vnejs/contracts.core.storage";
6
+ import { SETTINGS_KEYS } from "@vnejs/contracts.scenario.fastforward";
7
+ import { SUBSCRIBE_EVENTS as VENDORS_EVENTS } from "@vnejs/contracts.core.vendors";
8
+ import { spyEvent, stubEvent, stubSilentEvent } from "@vnejs/test-utils";
9
+
10
+ import { FastforwardVisits } from "../modules/visits.js";
11
+ import { createFastforwardTestModule, registerFastforwardPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
12
+
13
+ describe("FastforwardVisits", () => {
14
+ beforeEach(() => {
15
+ registerFastforwardPluginVne();
16
+ });
17
+
18
+ it("init loads visit hashes from GET_ALL keys that include group", async () => {
19
+ const { module, observer } = createFastforwardTestModule(FastforwardVisits, { subscribe: false });
20
+
21
+ stubSilentEvent(observer, SETTINGS_EVENTS.INIT);
22
+ stubEvent(observer, STORAGE_EVENTS.GET_ALL, () => ({ "intro/hash-a": 1, "outro/hash-b": 1 }));
23
+
24
+ await (module as FastforwardVisits).init();
25
+
26
+ expect((module as FastforwardVisits).visits).toEqual(new Set(["hash-a", "hash-b"]));
27
+ });
28
+
29
+ it("EXEC stores visit under scenario label group", async () => {
30
+ const { module, observer } = createFastforwardTestModule(FastforwardVisits, {
31
+ state: { scenario: { label: "intro", curLine: 0 } },
32
+ });
33
+ const storageSet = spyEvent(observer, STORAGE_EVENTS.SET);
34
+
35
+ stubEvent(observer, VENDORS_EVENTS.HASH, () => "visit-hash");
36
+ (module as FastforwardVisits).setState({ label: "intro", args: {}, keywords: [] });
37
+
38
+ await observer.emit(SCENARIO_EVENTS.EXEC, { module: "text", block: 0, line: "say hi", args: {}, keywords: [] });
39
+
40
+ expect((module as FastforwardVisits).visits.has("visit-hash")).toBe(true);
41
+ expect(storageSet).toHaveBeenCalledExactlyOnceWith({
42
+ module: "fastforward.visits",
43
+ group: "intro",
44
+ key: "visit-hash",
45
+ value: 1,
46
+ });
47
+ });
48
+
49
+ it("CHECK allows visit when hash is known", async () => {
50
+ const { module, observer } = createFastforwardTestModule(FastforwardVisits);
51
+
52
+ (module as FastforwardVisits).shared.settings[SETTINGS_KEYS.CHECK_VISITED] = true;
53
+ (module as FastforwardVisits).visits.add("known-hash");
54
+ stubEvent(observer, VENDORS_EVENTS.HASH, () => "known-hash");
55
+
56
+ const [result] = await observer.emit(SUBSCRIBE_EVENTS.CHECK);
57
+
58
+ expect(result).toEqual({ isAllowed: true, source: "fastforward.visits" });
59
+ });
60
+
61
+ it("CLEAR empties visits and RM_ALL by module", async () => {
62
+ const { module, observer } = createFastforwardTestModule(FastforwardVisits);
63
+ const rmAll = spyEvent(observer, STORAGE_EVENTS.RM_ALL);
64
+
65
+ (module as FastforwardVisits).visits.add("hash");
66
+
67
+ await observer.emit(SUBSCRIBE_EVENTS.CLEAR);
68
+
69
+ expect((module as FastforwardVisits).visits.size).toBe(0);
70
+ expect(rmAll).toHaveBeenCalledExactlyOnceWith({ module: "fastforward.visits" });
71
+ });
72
+ });