Skip to content

Commit cb7c35e

Browse files
authored
fix: extend post-completion protection to skip/halt/rewind (#133)
1 parent cc9703d commit cb7c35e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/engine.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ async function replayState(
316316
return res;
317317
}
318318
async function cancel(key?: unknown) {
319+
if (complete) {
320+
throw new Error(
321+
"Cannot perform a cancel operation after the conversation has completed, are you missing an `await`?",
322+
);
323+
}
319324
canceled = true;
320325
interrupted = true;
321326
message = key;

test/engine.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
assert,
55
assertEquals,
66
assertGreater,
7+
assertRejects,
78
assertSpyCall,
89
assertSpyCalls,
910
describe,
@@ -387,4 +388,33 @@ describe("ReplayEngine", () => {
387388
assertSpyCalls(builder, 2);
388389
assertEquals(i, 0);
389390
});
391+
it("should throw error if the controls are used after the replay has finished", async () => {
392+
let controls: ReplayControls | undefined;
393+
const engine = new ReplayEngine((c) => {
394+
controls = c;
395+
});
396+
const res = await engine.play();
397+
assertEquals(res.type, "returned");
398+
assertRejects(
399+
async () => {
400+
await controls?.action(() => 0, "");
401+
},
402+
Error,
403+
"missing an `await`",
404+
);
405+
assertRejects(
406+
async () => {
407+
await controls?.cancel();
408+
},
409+
Error,
410+
"missing an `await`",
411+
);
412+
assertRejects(
413+
async () => {
414+
await controls?.interrupt("");
415+
},
416+
Error,
417+
"missing an `await`",
418+
);
419+
});
390420
});

0 commit comments

Comments
 (0)