diff --git a/src/lib/onboard/machine/runtime.test.ts b/src/lib/onboard/machine/runtime.test.ts index 7b26269541..f098ba0dc3 100644 --- a/src/lib/onboard/machine/runtime.test.ts +++ b/src/lib/onboard/machine/runtime.test.ts @@ -185,23 +185,28 @@ describe("OnboardRuntime", () => { expect(policiesHarness.getSession().machine.state).toBe("policies"); }); - it("completes from post_verify and emits completion events", async () => { - const { runtime, events, getSession } = createHarness(sessionInState("post_verify")); + it("transitions through finalizing and post_verify before completion", async () => { + const { runtime, events, getSession } = createHarness(sessionInState("finalizing")); + await runtime.transition("post_verify"); await runtime.complete({ sandboxName: "my-assistant" }); expect(getSession()).toMatchObject({ status: "complete", resumable: false, sandboxName: "my-assistant", - machine: { state: "complete", revision: 8 }, + machine: { state: "complete", revision: 9 }, }); expect(events.map((event) => event.type)).toEqual([ + "state.exited", + "state.entered", "context.updated", "state.completed", "state.entered", "onboard.completed", ]); + expect(events[0]).toMatchObject({ state: "finalizing" }); + expect(events[1]).toMatchObject({ state: "post_verify" }); }); it("emits skipped and repair events without mutating durable state", async () => { diff --git a/src/lib/onboard/runtime-boundary.ts b/src/lib/onboard/runtime-boundary.ts index 45e017c367..f71d72cec6 100644 --- a/src/lib/onboard/runtime-boundary.ts +++ b/src/lib/onboard/runtime-boundary.ts @@ -91,6 +91,15 @@ export class OnboardRuntimeBoundary { } async recordSessionComplete(updates: SessionUpdates = {}): Promise { - return this.getRuntime().completeSession(updates); + const runtime = this.getRuntime(); + const current = await runtime.session(); + if (current.machine.state === "finalizing") { + await runtime.transition("post_verify"); + return runtime.complete(updates); + } + if (current.machine.state === "post_verify") { + return runtime.complete(updates); + } + return runtime.completeSession(updates); } }