Skip to content

Commit ac9641f

Browse files
authored
fix: call onExit when conversation.halt without conversation.wait (#145)
1 parent 9d1374b commit ac9641f

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/plugin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ export function conversations<OC extends Context, C extends Context>(
499499
}
500500

501501
const index: ConversationIndex<OC, C> = new Map();
502+
const exit = options.onExit !== undefined
503+
? async (name: string) => {
504+
await options.onExit?.(name, ctx);
505+
}
506+
: undefined;
502507
async function enter(id: string, ...args: unknown[]) {
503508
const entry = index.get(id);
504509
if (entry === undefined) {
@@ -516,18 +521,17 @@ export function conversations<OC extends Context, C extends Context>(
516521
api: ctx.api,
517522
me: ctx.me,
518523
};
524+
const onHalt = async () => {
525+
await exit?.(id);
526+
};
519527
return await enterConversation(builder, base, {
520528
args,
521529
ctx,
522530
plugins,
531+
onHalt,
523532
maxMillisecondsToWait,
524533
});
525534
}
526-
const exit = options.onExit !== undefined
527-
? async (name: string) => {
528-
await options.onExit?.(name, ctx);
529-
}
530-
: undefined;
531535
function isParallel(name: string) {
532536
return index.get(name)?.parallel ?? true;
533537
}

test/plugin.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,55 @@ describe("createConversation", () => {
363363
assertSpyCalls(onExit, 1);
364364
assertSpyCallArg(onExit, 0, 0, "convo");
365365
});
366+
it("should call onExit upon halt without wait", async () => {
367+
const onExit = spy(() => {});
368+
const mw = new Composer<TestContext>();
369+
let i = 0;
370+
let j = 0;
371+
mw.use(
372+
conversations({ onExit }),
373+
createConversation(async (convo) => {
374+
i++;
375+
await convo.halt();
376+
j++;
377+
}, { id: "convo" }),
378+
async (ctx) => {
379+
await ctx.conversation.enter("convo");
380+
},
381+
);
382+
const up = { message: { chat: { id: 0 } } };
383+
await mw.middleware()(mkctx(up), next);
384+
await mw.middleware()(mkctx(up), next);
385+
assertEquals(i, 2);
386+
assertEquals(j, 0);
387+
assertSpyCalls(onExit, 2); // enter, halt, enter, halt
388+
assertSpyCallArg(onExit, 0, 0, "convo");
389+
});
390+
it("should call onExit upon halt after wait", async () => {
391+
const onExit = spy(() => {});
392+
const mw = new Composer<TestContext>();
393+
let i = 0;
394+
let j = 0;
395+
mw.use(
396+
conversations({ onExit }),
397+
createConversation(async (convo) => {
398+
i++;
399+
await convo.wait();
400+
await convo.halt();
401+
j++;
402+
}, { id: "convo" }),
403+
async (ctx) => {
404+
await ctx.conversation.enter("convo");
405+
},
406+
);
407+
const up = { message: { chat: { id: 0 } } };
408+
await mw.middleware()(mkctx(up), next);
409+
await mw.middleware()(mkctx(up), next);
410+
assertEquals(i, 2);
411+
assertEquals(j, 0);
412+
assertSpyCalls(onExit, 1); // enter, wait, resume, halt
413+
assertSpyCallArg(onExit, 0, 0, "convo");
414+
});
366415
it("should continue if default wait timeouts do not kick in", async () => {
367416
const onExit = spy(() => {});
368417
const mw = new Composer<TestContext>();

0 commit comments

Comments
 (0)