@@ -11,7 +11,10 @@ import { resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
1111import { afterEach , describe , expect , it , vi } from "vitest" ;
1212import { stateMigrations } from "../doctor-contract-api.js" ;
1313import { resolveTelegramBotInfoCachePath } from "./bot-info-cache.js" ;
14- import { resolveTelegramMessageCachePath } from "./message-cache-persistence.js" ;
14+ import {
15+ resolveTelegramMessageCachePath ,
16+ resolveTelegramMessageCachePersistentScopeKey ,
17+ } from "./message-cache-persistence.js" ;
1518import { detectTelegramLegacyStateMigrations } from "./state-migrations.js" ;
1619import {
1720 resolveTopicNameCacheNamespace ,
@@ -47,6 +50,181 @@ afterEach(() => {
4750} ) ;
4851
4952describe ( "telegram state migrations" , ( ) => {
53+ it ( "does not require a migration owner when multi-agent startup has no legacy artifacts" , async ( ) => {
54+ const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
55+ const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
56+ try {
57+ const cfg = {
58+ agents : {
59+ ownership : "explicit" ,
60+ entries : { main : { } , ops : { } , research : { } } ,
61+ } ,
62+ } as OpenClawConfig ;
63+
64+ await expect ( detectTelegramLegacyStateMigrations ( { cfg, env } ) ) . resolves . toEqual ( [ ] ) ;
65+ } finally {
66+ await rm ( dir , { recursive : true , force : true } ) ;
67+ }
68+ } ) ;
69+
70+ it ( "uses the materialized Telegram binding as legacy-state owner after H2 normalization" , async ( ) => {
71+ const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
72+ const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
73+ const legacyStorePath = path . join ( dir , "sessions" , "sessions.json" ) ;
74+ const messageCachePath = resolveTelegramMessageCachePath ( legacyStorePath ) ;
75+ const sentMessagePath = `${ legacyStorePath } .telegram-sent-messages.json` ;
76+ const topicNamePath = resolveTopicNameCachePath ( legacyStorePath ) ;
77+ const ownerStorePath = resolveStorePath ( undefined , { env, agentId : "main" } ) ;
78+ const ownerMessagePath = resolveTelegramMessageCachePath ( ownerStorePath ) ;
79+ const ownerTopicNamespace = resolveTopicNameCacheNamespace (
80+ resolveTopicNameCacheScope ( ownerStorePath ) ,
81+ ) ;
82+ try {
83+ await mkdir ( path . dirname ( legacyStorePath ) , { recursive : true } ) ;
84+ await writeFile ( messageCachePath , JSON . stringify ( [ persistedCacheEntry ( 51 , "bound owner" ) ] ) ) ;
85+ await writeFile ( sentMessagePath , JSON . stringify ( { 7 : { 51 : Date . now ( ) } } ) ) ;
86+ await writeFile (
87+ topicNamePath ,
88+ JSON . stringify ( { "7:51" : { name : "Bound owner" , updatedAt : Date . now ( ) } } ) ,
89+ ) ;
90+
91+ const cfg = {
92+ agents : {
93+ ownership : "explicit" ,
94+ entries : { main : { } , ops : { } , research : { } } ,
95+ } ,
96+ bindings : [ { agentId : "main" , match : { channel : "telegram" , accountId : "*" } } ] ,
97+ } as OpenClawConfig ;
98+ const plans = await detectTelegramLegacyStateMigrations ( { cfg, env } ) ;
99+ const messagePlan = plans . find ( ( plan ) => plan . sourcePath === messageCachePath ) ;
100+ const sentPlan = plans . find ( ( plan ) => plan . sourcePath === sentMessagePath ) ;
101+ const topicPlan = plans . find ( ( plan ) => plan . sourcePath === topicNamePath ) ;
102+
103+ expect ( messagePlan ) . toMatchObject ( {
104+ kind : "plugin-state-import" ,
105+ scopeKey : resolveTelegramMessageCachePersistentScopeKey ( ownerMessagePath ) ,
106+ } ) ;
107+ expect ( topicPlan ) . toMatchObject ( {
108+ kind : "plugin-state-import" ,
109+ namespace : ownerTopicNamespace ,
110+ } ) ;
111+ if ( ! sentPlan || sentPlan . kind !== "plugin-state-import" ) {
112+ throw new Error ( "expected Telegram sent-message import plan" ) ;
113+ }
114+ expect ( ( await sentPlan . readEntries ( ) ) [ 0 ] ?. value ) . toMatchObject ( {
115+ scopeKey : createHash ( "sha256" ) . update ( ownerStorePath , "utf8" ) . digest ( "hex" ) . slice ( 0 , 24 ) ,
116+ } ) ;
117+ } finally {
118+ await rm ( dir , { recursive : true , force : true } ) ;
119+ }
120+ } ) ;
121+
122+ it ( "retains raw legacy default-marker ownership during rollback compatibility" , async ( ) => {
123+ const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
124+ const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
125+ const legacyStorePath = path . join ( dir , "sessions" , "sessions.json" ) ;
126+ const sentMessagePath = `${ legacyStorePath } .telegram-sent-messages.json` ;
127+ const ownerStorePath = resolveStorePath ( undefined , { env, agentId : "ops" } ) ;
128+ try {
129+ await mkdir ( path . dirname ( legacyStorePath ) , { recursive : true } ) ;
130+ await writeFile ( sentMessagePath , JSON . stringify ( { 7 : { 52 : Date . now ( ) } } ) ) ;
131+
132+ const cfg = {
133+ agents : { list : [ { id : "main" } , { id : "ops" , default : true } ] } ,
134+ } as OpenClawConfig ;
135+ const plans = await detectTelegramLegacyStateMigrations ( { cfg, env } ) ;
136+ const sentPlan = plans . find ( ( plan ) => plan . sourcePath === sentMessagePath ) ;
137+ if ( ! sentPlan || sentPlan . kind !== "plugin-state-import" ) {
138+ throw new Error ( "expected Telegram sent-message import plan" ) ;
139+ }
140+ expect ( ( await sentPlan . readEntries ( ) ) [ 0 ] ?. value ) . toMatchObject ( {
141+ scopeKey : createHash ( "sha256" ) . update ( ownerStorePath , "utf8" ) . digest ( "hex" ) . slice ( 0 , 24 ) ,
142+ } ) ;
143+ } finally {
144+ await rm ( dir , { recursive : true , force : true } ) ;
145+ }
146+ } ) ;
147+
148+ it ( "fails closed when legacy Telegram state has no explicit multi-agent owner" , async ( ) => {
149+ const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
150+ const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
151+ const legacyStorePath = path . join ( dir , "sessions" , "sessions.json" ) ;
152+ const sentMessagePath = `${ legacyStorePath } .telegram-sent-messages.json` ;
153+ try {
154+ await mkdir ( path . dirname ( legacyStorePath ) , { recursive : true } ) ;
155+ await writeFile ( sentMessagePath , JSON . stringify ( { 7 : { 53 : Date . now ( ) } } ) ) ;
156+
157+ const cfg = {
158+ agents : { ownership : "explicit" , entries : { main : { } , ops : { } , research : { } } } ,
159+ } as OpenClawConfig ;
160+ await expect ( detectTelegramLegacyStateMigrations ( { cfg, env } ) ) . rejects . toMatchObject ( {
161+ name : "AgentSelectionRequiredError" ,
162+ code : "AGENT_SELECTION_REQUIRED" ,
163+ } ) ;
164+ } finally {
165+ await rm ( dir , { recursive : true , force : true } ) ;
166+ }
167+ } ) ;
168+
169+ it ( "fails closed when global legacy state spans multiple Telegram route owners" , async ( ) => {
170+ const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
171+ const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
172+ const legacyStorePath = path . join ( dir , "sessions" , "sessions.json" ) ;
173+ const sentMessagePath = `${ legacyStorePath } .telegram-sent-messages.json` ;
174+ try {
175+ await mkdir ( path . dirname ( legacyStorePath ) , { recursive : true } ) ;
176+ await writeFile ( sentMessagePath , JSON . stringify ( { 7 : { 54 : Date . now ( ) } } ) ) ;
177+
178+ const cfg = {
179+ agents : { ownership : "explicit" , entries : { main : { } , ops : { } } } ,
180+ channels : {
181+ telegram : {
182+ accounts : {
183+ primary : { botToken : "123456:primary" } ,
184+ alerts : { botToken : "123456:alerts" } ,
185+ } ,
186+ } ,
187+ } ,
188+ bindings : [
189+ { agentId : "main" , match : { channel : "telegram" , accountId : "primary" } } ,
190+ { agentId : "ops" , match : { channel : "telegram" , accountId : "alerts" } } ,
191+ ] ,
192+ } as OpenClawConfig ;
193+ await expect ( detectTelegramLegacyStateMigrations ( { cfg, env } ) ) . rejects . toThrow (
194+ / ^ L e g a c y T e l e g r a m s t a t e h a s m u l t i p l e r o u t e d o w n e r s \( (?: m a i n , o p s | o p s , m a i n ) \) / ,
195+ ) ;
196+ } finally {
197+ await rm ( dir , { recursive : true , force : true } ) ;
198+ }
199+ } ) ;
200+
201+ it ( "imports an account-scoped topic cache without requiring a global migration owner" , async ( ) => {
202+ const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
203+ const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
204+ const opsStorePath = resolveStorePath ( undefined , { env, agentId : "ops" } ) ;
205+ const topicNamePath = resolveTopicNameCachePath ( opsStorePath ) ;
206+ try {
207+ await mkdir ( path . dirname ( topicNamePath ) , { recursive : true } ) ;
208+ await writeFile (
209+ topicNamePath ,
210+ JSON . stringify ( { "7:55" : { name : "Ops" , updatedAt : Date . now ( ) } } ) ,
211+ ) ;
212+
213+ const cfg = {
214+ agents : { ownership : "explicit" , entries : { main : { } , ops : { } } } ,
215+ channels : { telegram : { accounts : { ops : { botToken : "123456:ops" } } } } ,
216+ } as OpenClawConfig ;
217+ const plans = await detectTelegramLegacyStateMigrations ( { cfg, env } ) ;
218+
219+ expect ( plans . find ( ( plan ) => plan . sourcePath === topicNamePath ) ) . toMatchObject ( {
220+ kind : "plugin-state-import" ,
221+ namespace : resolveTopicNameCacheNamespace ( resolveTopicNameCacheScope ( opsStorePath ) ) ,
222+ } ) ;
223+ } finally {
224+ await rm ( dir , { recursive : true , force : true } ) ;
225+ }
226+ } ) ;
227+
50228 it ( "detects legacy bot-info cache import" , async ( ) => {
51229 const dir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-telegram-state-migration-" ) ) ;
52230 const env = { ...process . env , OPENCLAW_STATE_DIR : dir } ;
0 commit comments