|
| 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