@@ -29,6 +29,48 @@ type CronStoreWrite = {
2929 jobs : Array < { id : string ; delivery : { channel : string ; to : string } } > ;
3030} ;
3131
32+ const scopedTargetWritebackCases = [
33+ {
34+ name : "channel Direct Messages topic" ,
35+ rawTarget : "@mychannel:direct-topic:77" ,
36+ matchingTarget : "t.me/MyChannel:direct-topic:77" ,
37+ resolvedTarget : "-100123:direct-topic:77" ,
38+ unmatchedTargets : [
39+ "@mychannel" ,
40+ "@mychannel:direct-topic:88" ,
41+ "@mychannel:topic:77" ,
42+ "@mychannel:77" ,
43+ "@otherchannel:direct-topic:77" ,
44+ ] ,
45+ } ,
46+ {
47+ name : "explicit forum topic" ,
48+ rawTarget : "@mychannel:topic:77" ,
49+ matchingTarget : "t.me/MyChannel:77" ,
50+ resolvedTarget : "-100123:topic:77" ,
51+ unmatchedTargets : [ "@mychannel" , "@mychannel:direct-topic:77" , "@mychannel:topic:88" ] ,
52+ } ,
53+ {
54+ name : "shorthand forum topic" ,
55+ rawTarget : "@mychannel:77" ,
56+ matchingTarget : "t.me/MyChannel:topic:77" ,
57+ resolvedTarget : "-100123:77" ,
58+ unmatchedTargets : [ "@mychannel" , "@mychannel:direct-topic:77" , "@mychannel:88" ] ,
59+ } ,
60+ {
61+ name : "unthreaded target" ,
62+ rawTarget : "t.me/mychannel" ,
63+ matchingTarget : "@MyChannel" ,
64+ resolvedTarget : "-100123" ,
65+ unmatchedTargets : [
66+ "@mychannel:direct-topic:77" ,
67+ "@mychannel:direct-topic:88" ,
68+ "@mychannel:topic:77" ,
69+ "@mychannel:77" ,
70+ ] ,
71+ } ,
72+ ] as const ;
73+
3274vi . mock ( "openclaw/plugin-sdk/config-mutation" , async ( ) => {
3375 const actual = await vi . importActual < typeof import ( "openclaw/plugin-sdk/config-mutation" ) > (
3476 "openclaw/plugin-sdk/config-mutation" ,
@@ -251,33 +293,58 @@ export function installMaybePersistResolvedTelegramTargetTests(params?: {
251293 ] ) ;
252294 } ) ;
253295
254- it ( "preserves topic suffix style in writeback target" , async ( ) => {
296+ it . each (
297+ scopedTargetWritebackCases . flatMap ( ( testCase ) =>
298+ ( [ "config" , "cron" ] as const ) . map ( ( surface ) => ( {
299+ name : testCase . name ,
300+ surface,
301+ testCase,
302+ } ) ) ,
303+ ) ,
304+ ) ( "rewrites only matching $name $surface targets" , async ( { surface, testCase } ) => {
305+ const unmatchedAccounts = Object . fromEntries (
306+ testCase . unmatchedTargets . map ( ( target , index ) => [ `other${ index } ` , { defaultTo : target } ] ) ,
307+ ) ;
255308 readConfigFileSnapshotForWrite . mockResolvedValue ( {
256309 snapshot : {
257310 config : {
258311 channels : {
259312 telegram : {
260- defaultTo : "t.me/mychannel:topic:9" ,
313+ defaultTo : testCase . matchingTarget ,
314+ accounts : unmatchedAccounts ,
261315 } ,
262316 } ,
263317 } ,
264318 } ,
265319 writeOptions : { } ,
266320 } ) ;
267- loadCronStore . mockResolvedValue ( { version : 1 , jobs : [ ] } ) ;
321+ loadCronStore . mockResolvedValue ( {
322+ version : 1 ,
323+ jobs : [ testCase . matchingTarget , ...testCase . unmatchedTargets ] . map ( ( target , index ) => ( {
324+ id : String ( index ) ,
325+ delivery : { channel : "telegram" , to : target } ,
326+ } ) ) ,
327+ } ) ;
268328
269329 await maybePersistResolvedTelegramTarget ( {
270330 cfg : { } as OpenClawConfig ,
271- rawTarget : "t.me/mychannel:topic:9" ,
331+ rawTarget : testCase . rawTarget ,
272332 resolvedChatId : "-100123" ,
273333 gatewayClientScopes : undefined ,
274334 trustedInternalWriteback : true ,
275335 } ) ;
276336
277- expect ( writeConfigFile ) . toHaveBeenCalledTimes ( 1 ) ;
278- const [ writtenConfig , writeOptions ] = requireWriteConfigCall ( ) ;
279- expect ( writtenConfig . channels ?. telegram ?. defaultTo ) . toBe ( "-100123:topic:9" ) ;
280- expect ( writeOptions ) . toEqual ( { } ) ;
337+ const persistedTargets =
338+ surface === "config"
339+ ? [
340+ requireWriteConfigCall ( ) [ 0 ] . channels ?. telegram ?. defaultTo ,
341+ ...Object . values ( requireWriteConfigCall ( ) [ 0 ] . channels ?. telegram ?. accounts ?? { } ) . map (
342+ ( account ) => account . defaultTo ,
343+ ) ,
344+ ]
345+ : requireSaveCronStoreCall ( ) [ 1 ] . jobs . map ( ( job ) => job . delivery . to ) ;
346+
347+ expect ( persistedTargets ) . toEqual ( [ testCase . resolvedTarget , ...testCase . unmatchedTargets ] ) ;
281348 } ) ;
282349
283350 it ( "matches username targets case-insensitively" , async ( ) => {
0 commit comments