22import { buildApprovalResolutionRef } from "openclaw/plugin-sdk/approval-reference-runtime" ;
33import { describe , expect , it } from "vitest" ;
44import { parseTelegramApprovalCallbackData } from "./approval-callback-data.js" ;
5- import { buildTelegramPresentationButtons , resolveTelegramInlineButtons } from "./button-types.js" ;
5+ import {
6+ buildTelegramPresentationButtons ,
7+ resolveTelegramInlineButtons ,
8+ type TelegramButtonBuildOptions ,
9+ } from "./button-types.js" ;
610import { describeTelegramInteractiveButtonBehavior } from "./button-types.test-helpers.js" ;
711import {
812 buildTelegramOpaqueCallbackData ,
@@ -11,6 +15,24 @@ import {
1115
1216describeTelegramInteractiveButtonBehavior ( ) ;
1317
18+ function questionButtonOptions (
19+ questions : ReadonlyArray < { questionId : string ; optionValues : readonly string [ ] } > ,
20+ ) : TelegramButtonBuildOptions {
21+ return {
22+ questionOptionIndices : new Map (
23+ questions . map (
24+ ( { questionId, optionValues } ) =>
25+ [
26+ questionId ,
27+ new Map (
28+ optionValues . map ( ( value , index ) => [ value . trim ( ) . toLowerCase ( ) , index ] as const ) ,
29+ ) ,
30+ ] as const ,
31+ ) ,
32+ ) ,
33+ } ;
34+ }
35+
1436describe ( "buildTelegramInteractiveButtons callback limits" , ( ) => {
1537 it ( "drops buttons whose callback payload exceeds Telegram limits" , ( ) => {
1638 expect (
@@ -56,23 +78,186 @@ describe("buildTelegramPresentationButtons", () => {
5678
5779 it ( "encodes question buttons by record id and option index" , ( ) => {
5880 const questionId = "ask_0123456789abcdef0123456789abcdef" ;
81+ expect (
82+ buildTelegramPresentationButtons (
83+ {
84+ blocks : [
85+ {
86+ type : "buttons" ,
87+ buttons : [ "Staging" , "Production" ] . map ( ( label ) => ( {
88+ label,
89+ action : { type : "question" as const , questionId, optionValue : label } ,
90+ } ) ) ,
91+ } ,
92+ ] ,
93+ } ,
94+ questionButtonOptions ( [ { questionId, optionValues : [ "Staging" , "Production" ] } ] ) ,
95+ ) ,
96+ ) . toEqual ( [
97+ [
98+ { text : "Staging" , callback_data : `tgq1:${ questionId } :0` , style : undefined } ,
99+ { text : "Production" , callback_data : `tgq1:${ questionId } :1` , style : undefined } ,
100+ ] ,
101+ ] ) ;
102+ } ) ;
103+
104+ it ( "drops question buttons when authoritative Gateway option order is unavailable" , ( ) => {
105+ const questionId = "ask_0123456789abcdef0123456789abcdef" ;
106+
59107 expect (
60108 buildTelegramPresentationButtons ( {
61109 blocks : [
62110 {
63111 type : "buttons" ,
64- buttons : [ "Staging" , "Production" ] . map ( ( label ) => ( {
65- label,
66- action : { type : "question" as const , questionId, optionValue : label } ,
67- } ) ) ,
112+ buttons : [
113+ {
114+ label : "Production" ,
115+ action : {
116+ type : "question" as const ,
117+ questionId,
118+ optionValue : "Production" ,
119+ } ,
120+ } ,
121+ ] ,
68122 } ,
69123 ] ,
70124 } ) ,
71- ) . toEqual ( [
72- [
73- { text : "Staging" , callback_data : `tgq1:${ questionId } :0` , style : undefined } ,
74- { text : "Production" , callback_data : `tgq1:${ questionId } :1` , style : undefined } ,
75- ] ,
125+ ) . toBeUndefined ( ) ;
126+ } ) ;
127+
128+ it ( "keeps question option indices independent and stable across presentation blocks" , ( ) => {
129+ const firstQuestionId = "ask_0123456789abcdef0123456789abcdef" ;
130+ const secondQuestionId = "ask_fedcba9876543210fedcba9876543210" ;
131+ const questionButton = ( questionId : string , optionValue : string ) => ( {
132+ label : optionValue ,
133+ action : { type : "question" as const , questionId, optionValue } ,
134+ } ) ;
135+
136+ const rows = buildTelegramPresentationButtons (
137+ {
138+ blocks : [
139+ {
140+ type : "buttons" ,
141+ buttons : [
142+ questionButton ( firstQuestionId , "東京" ) ,
143+ questionButton ( firstQuestionId , "Déployer" ) ,
144+ ] ,
145+ } ,
146+ {
147+ type : "buttons" ,
148+ buttons : [
149+ questionButton ( secondQuestionId , "東京" ) ,
150+ questionButton ( secondQuestionId , "Production" ) ,
151+ ] ,
152+ } ,
153+ {
154+ type : "buttons" ,
155+ buttons : [
156+ questionButton ( firstQuestionId , "東京" ) ,
157+ questionButton ( firstQuestionId , "Production 🚀" ) ,
158+ ] ,
159+ } ,
160+ ] ,
161+ } ,
162+ questionButtonOptions ( [
163+ {
164+ questionId : firstQuestionId ,
165+ optionValues : [ "東京" , "Déployer" , "Production 🚀" ] ,
166+ } ,
167+ { questionId : secondQuestionId , optionValues : [ "東京" , "Production" ] } ,
168+ ] ) ,
169+ ) ;
170+
171+ expect ( rows ?. map ( ( row ) => row . map ( ( button ) => button . callback_data ) ) ) . toEqual ( [
172+ [ `tgq1:${ firstQuestionId } :0` , `tgq1:${ firstQuestionId } :1` ] ,
173+ [ `tgq1:${ secondQuestionId } :0` , `tgq1:${ secondQuestionId } :1` ] ,
174+ [ `tgq1:${ firstQuestionId } :0` , `tgq1:${ firstQuestionId } :2` ] ,
175+ ] ) ;
176+ } ) ;
177+
178+ it ( "maps repeated rendered question values to their canonical Gateway option indices" , ( ) => {
179+ const questionId = "ask_0123456789abcdef0123456789abcdef" ;
180+ const button = ( label : string ) => ( {
181+ label,
182+ action : { type : "question" as const , questionId, optionValue : label } ,
183+ } ) ;
184+
185+ const rows = buildTelegramPresentationButtons (
186+ {
187+ blocks : [
188+ { type : "buttons" , buttons : [ button ( "A" ) , button ( "A" ) ] } ,
189+ { type : "buttons" , buttons : [ button ( "B" ) , button ( "C" ) ] } ,
190+ ] ,
191+ } ,
192+ questionButtonOptions ( [ { questionId, optionValues : [ "A" , "B" , "C" ] } ] ) ,
193+ ) ;
194+
195+ expect ( rows ?. flatMap ( ( row ) => row . map ( ( entry ) => entry . callback_data ) ) ) . toEqual ( [
196+ `tgq1:${ questionId } :0` ,
197+ `tgq1:${ questionId } :0` ,
198+ `tgq1:${ questionId } :1` ,
199+ `tgq1:${ questionId } :2` ,
200+ ] ) ;
201+ } ) ;
202+
203+ it ( "normalizes repeated question values with the Gateway trim and lowercase contract" , ( ) => {
204+ const questionId = "ask_0123456789abcdef0123456789abcdef" ;
205+ const questionButton = ( optionValue : string ) => ( {
206+ label : optionValue ,
207+ action : { type : "question" as const , questionId, optionValue } ,
208+ } ) ;
209+
210+ const rows = buildTelegramPresentationButtons (
211+ {
212+ blocks : [
213+ { type : "buttons" , buttons : [ questionButton ( " Deploy " ) , questionButton ( "deploy" ) ] } ,
214+ { type : "buttons" , buttons : [ questionButton ( "Production" ) ] } ,
215+ ] ,
216+ } ,
217+ questionButtonOptions ( [ { questionId, optionValues : [ " Deploy " , "Production" ] } ] ) ,
218+ ) ;
219+
220+ expect ( rows ?. flatMap ( ( row ) => row . map ( ( entry ) => entry . callback_data ) ) ) . toEqual ( [
221+ `tgq1:${ questionId } :0` ,
222+ `tgq1:${ questionId } :0` ,
223+ `tgq1:${ questionId } :1` ,
224+ ] ) ;
225+ } ) ;
226+
227+ it ( "does not consume a question option position when callback encoding fails" , ( ) => {
228+ const validQuestionId = "ask_0123456789abcdef0123456789abcdef" ;
229+ const invalidQuestionId = "not-a-gateway-question" ;
230+ const rows = buildTelegramPresentationButtons (
231+ {
232+ blocks : [
233+ {
234+ type : "buttons" ,
235+ buttons : [
236+ {
237+ label : "Invalid" ,
238+ action : {
239+ type : "question" as const ,
240+ questionId : invalidQuestionId ,
241+ optionValue : "Invalid" ,
242+ } ,
243+ } ,
244+ {
245+ label : "Valid" ,
246+ action : {
247+ type : "question" as const ,
248+ questionId : validQuestionId ,
249+ optionValue : "Valid" ,
250+ } ,
251+ } ,
252+ ] ,
253+ } ,
254+ ] ,
255+ } ,
256+ questionButtonOptions ( [ { questionId : validQuestionId , optionValues : [ "Valid" , "Other" ] } ] ) ,
257+ ) ;
258+
259+ expect ( rows ) . toEqual ( [
260+ [ { text : "Valid" , callback_data : `tgq1:${ validQuestionId } :0` , style : undefined } ] ,
76261 ] ) ;
77262 } ) ;
78263
0 commit comments