@@ -3,8 +3,7 @@ import { describe, expect, it, vi } from "vitest";
33import type { OpenClawConfig } from "../config/config.js" ;
44import {
55 createChannelOnboardingPostWriteHook ,
6- createChannelOnboardingPostWriteHookCollector ,
7- runCollectedChannelOnboardingPostWriteHooks ,
6+ createChannelSetupTransaction ,
87} from "./onboard-channels.js" ;
98import { createExitThrowingRuntime } from "./test-wizard-helpers.js" ;
109
@@ -20,8 +19,8 @@ describe("setupChannels post-write hooks", () => {
2019 const adapter = {
2120 afterConfigWritten,
2221 } ;
23- const collector = createChannelOnboardingPostWriteHookCollector ( ) ;
2422 const runtime = createExitThrowingRuntime ( ) ;
23+ const transaction = createChannelSetupTransaction ( { runtime } ) ;
2524 const hook = createChannelOnboardingPostWriteHook ( {
2625 accountId : "acct-1" ,
2726 adapter,
@@ -32,16 +31,13 @@ describe("setupChannels post-write hooks", () => {
3231 if ( ! hook ) {
3332 throw new Error ( "expected post-write hook" ) ;
3433 }
35- collector . collect ( hook ) ;
34+ transaction . onPostWriteHook ( hook ) ;
3635
3736 expect ( afterConfigWritten ) . not . toHaveBeenCalled ( ) ;
3837
39- await runCollectedChannelOnboardingPostWriteHooks ( {
40- hooks : collector . drain ( ) ,
41- cfg,
42- runtime,
43- } ) ;
38+ const committed = await transaction . commit ( cfg , async ( ) => cfg ) ;
4439
40+ expect ( committed ) . toBe ( cfg ) ;
4541 expect ( afterConfigWritten ) . toHaveBeenCalledWith ( {
4642 previousCfg,
4743 cfg,
@@ -52,24 +48,35 @@ describe("setupChannels post-write hooks", () => {
5248
5349 it ( "logs onboarding post-write hook failures without aborting" , async ( ) => {
5450 const runtime = createExitThrowingRuntime ( ) ;
55-
56- await runCollectedChannelOnboardingPostWriteHooks ( {
57- hooks : [
58- {
59- channel : "telegram" ,
60- accountId : "acct-1" ,
61- run : async ( ) => {
62- throw new Error ( "hook failed" ) ;
63- } ,
64- } ,
65- ] ,
66- cfg : { } as OpenClawConfig ,
67- runtime,
51+ const transaction = createChannelSetupTransaction ( { runtime } ) ;
52+ transaction . onPostWriteHook ( {
53+ channel : "telegram" ,
54+ accountId : "acct-1" ,
55+ run : async ( ) => {
56+ throw new Error ( "hook failed" ) ;
57+ } ,
6858 } ) ;
6959
60+ await transaction . commit ( { } as OpenClawConfig , async ( config ) => config ) ;
61+
7062 expect ( runtime . error ) . toHaveBeenCalledWith (
7163 'Channel telegram post-setup warning for "acct-1": hook failed' ,
7264 ) ;
7365 expect ( runtime . exit ) . not . toHaveBeenCalled ( ) ;
7466 } ) ;
67+
68+ it ( "does not run hooks when config persistence fails" , async ( ) => {
69+ const runtime = createExitThrowingRuntime ( ) ;
70+ const hook = vi . fn ( ) ;
71+ const transaction = createChannelSetupTransaction ( { runtime } ) ;
72+ transaction . onPostWriteHook ( { channel : "matrix" , accountId : "ops" , run : hook } ) ;
73+
74+ await expect (
75+ transaction . commit ( { } as OpenClawConfig , async ( ) => {
76+ throw new Error ( "write failed" ) ;
77+ } ) ,
78+ ) . rejects . toThrow ( "write failed" ) ;
79+
80+ expect ( hook ) . not . toHaveBeenCalled ( ) ;
81+ } ) ;
7582} ) ;
0 commit comments