Skip to content

Commit edd19b7

Browse files
committed
add tests for UserInterruptionsManagerImplementation
1 parent e801ad2 commit edd19b7

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

v-next/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
3434
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
3535
"test": "glob --cmd=\"node --import tsx/esm --test\" \"test/**/*.ts\"",
36+
"test:only": "glob --cmd=\"node --import tsx/esm --test --test-only\" \"test/**/*.ts\"",
3637
"test:github": "glob --cmd=\"node --import tsx/esm --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout\" \"test/**/*.ts\"",
3738
"test:coverage": "c8 --reporter html --reporter text --all --src src glob --cmd=\"node --import tsx/esm --test\" \"test/**/*.ts\"",
3839
"pretest": "pnpm build",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { AsyncMutex } from "../../../src/internal/async-mutex.js";
5+
6+
describe("AsyncMutex", () => {
7+
it("should run a function exclusively", async () => {
8+
const mutex = new AsyncMutex();
9+
10+
let running = 0;
11+
let maxRunning = 0;
12+
13+
async function run() {
14+
running++;
15+
maxRunning = Math.max(maxRunning, running);
16+
await new Promise((resolve) => setTimeout(resolve, 10));
17+
running--;
18+
}
19+
20+
await Promise.all([
21+
mutex.excluiveRun(run),
22+
mutex.excluiveRun(run),
23+
mutex.excluiveRun(run),
24+
]);
25+
26+
assert.strictEqual(maxRunning, 1);
27+
});
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { HookManagerImplementation } from "../../../src/internal/hook-manager.js";
5+
import { UserInterruptionManagerImplementation } from "../../../src/internal/user-interruptions.js";
6+
import { UserInterruptionHooks } from "../../../src/types/hooks.js";
7+
8+
describe("UserInterruptionManager", () => {
9+
describe("displayMessage", () => {
10+
it("Should call a dynamic handler with a given message from an interruptor", async () => {
11+
const hookManager = new HookManagerImplementation([]);
12+
const userInterruptionManager = new UserInterruptionManagerImplementation(
13+
hookManager,
14+
);
15+
hookManager.setContext({} as any);
16+
17+
let called = false;
18+
let givenInterruptor: string = "";
19+
let givenMessage: string = "";
20+
21+
const handlers: Partial<UserInterruptionHooks> = {
22+
async displayMessage(_context, interruptor, message) {
23+
called = true;
24+
givenInterruptor = interruptor;
25+
givenMessage = message;
26+
},
27+
};
28+
29+
hookManager.registerHandlers("userInterruptions", handlers);
30+
31+
await userInterruptionManager.displayMessage(
32+
"test-interruptor",
33+
"test-message",
34+
);
35+
36+
assert(called);
37+
assert.equal(givenInterruptor, "test-interruptor");
38+
assert.equal(givenMessage, "test-message");
39+
});
40+
});
41+
42+
describe("requestInput", () => {
43+
it("Should call a dynamic handler with a given input description from an interruptor", async () => {
44+
const hookManager = new HookManagerImplementation([]);
45+
const userInterruptionManager = new UserInterruptionManagerImplementation(
46+
hookManager,
47+
);
48+
hookManager.setContext({} as any);
49+
50+
let called = false;
51+
let givenInterruptor: string = "";
52+
let givenInputDescription: string = "";
53+
54+
const handlers: Partial<UserInterruptionHooks> = {
55+
async requestInput(_context, interruptor, inputDescription) {
56+
called = true;
57+
givenInterruptor = interruptor;
58+
givenInputDescription = inputDescription;
59+
return "test-input";
60+
},
61+
};
62+
63+
hookManager.registerHandlers("userInterruptions", handlers);
64+
65+
const input = await userInterruptionManager.requestInput(
66+
"test-interruptor",
67+
"test-input-description",
68+
);
69+
70+
assert(called);
71+
assert.equal(givenInterruptor, "test-interruptor");
72+
assert.equal(givenInputDescription, "test-input-description");
73+
assert.equal(input, "test-input");
74+
});
75+
});
76+
77+
describe("requestSecretInput", () => {
78+
it("Should call a dynamic handler with a given input description from an interruptor", async () => {
79+
const hookManager = new HookManagerImplementation([]);
80+
const userInterruptionManager = new UserInterruptionManagerImplementation(
81+
hookManager,
82+
);
83+
hookManager.setContext({} as any);
84+
85+
let called = false;
86+
let givenInterruptor: string = "";
87+
let givenInputDescription: string = "";
88+
89+
const handlers: Partial<UserInterruptionHooks> = {
90+
async requestSecretInput(_context, interruptor, inputDescription) {
91+
called = true;
92+
givenInterruptor = interruptor;
93+
givenInputDescription = inputDescription;
94+
return "test-secret-input";
95+
},
96+
};
97+
98+
hookManager.registerHandlers("userInterruptions", handlers);
99+
100+
const input = await userInterruptionManager.requestSecretInput(
101+
"test-interruptor",
102+
"test-input-description",
103+
);
104+
105+
assert(called);
106+
assert.equal(givenInterruptor, "test-interruptor");
107+
assert.equal(givenInputDescription, "test-input-description");
108+
assert.equal(input, "test-secret-input");
109+
});
110+
});
111+
});

0 commit comments

Comments
 (0)