@@ -8,6 +8,7 @@ import assert from 'node:assert';
88import { describe , it } from 'node:test' ;
99
1010import {
11+ applyToExistingMetrics ,
1112 generateToolMetrics ,
1213 validateEnumHomogeneity ,
1314} from '../../src/telemetry/toolMetricsUtils.js' ;
@@ -80,4 +81,183 @@ describe('toolMetricsUtils', () => {
8081 assert . strictEqual ( metrics [ 0 ] . args [ 0 ] . argType , 'string' ) ;
8182 } ) ;
8283 } ) ;
84+
85+ describe ( 'applyToExistingMetrics' , ( ) => {
86+ it ( 'should return the same metrics if existing and update are the same' , ( ) => {
87+ const existing = [ { name : 'foo' , args : [ ] } ] ;
88+ const update = [ { name : 'foo' , args : [ ] } ] ;
89+ const result = applyToExistingMetrics ( existing , update ) ;
90+ const expected = [ { name : 'foo' , args : [ ] } ] ;
91+ assert . deepStrictEqual ( result , expected ) ;
92+ } ) ;
93+
94+ it ( 'should append new entries to the end of the array' , ( ) => {
95+ const existing = [ { name : 'foo' , args : [ ] } ] ;
96+ const update = [
97+ { name : 'foo' , args : [ ] } ,
98+ { name : 'bar' , args : [ ] } ,
99+ ] ;
100+ const result = applyToExistingMetrics ( existing , update ) ;
101+ const expected = [
102+ { name : 'foo' , args : [ ] } ,
103+ { name : 'bar' , args : [ ] } ,
104+ ] ;
105+ assert . deepStrictEqual ( result , expected ) ;
106+ } ) ;
107+
108+ it ( 'should mark missing entries as deprecated and preserve their order' , ( ) => {
109+ const existing = [
110+ { name : 'foo' , args : [ ] } ,
111+ { name : 'bar' , args : [ ] } ,
112+ ] ;
113+ const update = [ { name : 'foo' , args : [ ] } ] ;
114+ const result = applyToExistingMetrics ( existing , update ) ;
115+ const expected = [
116+ { name : 'foo' , args : [ ] } ,
117+ { name : 'bar' , args : [ ] , isDeprecated : true } ,
118+ ] ;
119+ assert . deepStrictEqual ( result , expected ) ;
120+ } ) ;
121+
122+ it ( 'should handle adding new entries and deprecating old ones simultaneously' , ( ) => {
123+ const existing = [
124+ { name : 'foo' , args : [ ] } ,
125+ { name : 'bar' , args : [ ] } ,
126+ ] ;
127+ const update = [
128+ { name : 'bar' , args : [ ] } ,
129+ { name : 'baz' , args : [ ] } ,
130+ ] ;
131+ const result = applyToExistingMetrics ( existing , update ) ;
132+ const expected = [
133+ { name : 'foo' , args : [ ] , isDeprecated : true } ,
134+ { name : 'bar' , args : [ ] } ,
135+ { name : 'baz' , args : [ ] } ,
136+ ] ;
137+ assert . deepStrictEqual ( result , expected ) ;
138+ } ) ;
139+
140+ it ( 'should append new arguments to the back' , ( ) => {
141+ const existing = [
142+ { name : 'foo' , args : [ { name : 'arg_a' , argType : 'string' } ] } ,
143+ ] ;
144+ const update = [
145+ {
146+ name : 'foo' ,
147+ args : [
148+ { name : 'arg_a' , argType : 'string' } ,
149+ { name : 'arg_b' , argType : 'string' } ,
150+ ] ,
151+ } ,
152+ ] ;
153+ const result = applyToExistingMetrics ( existing , update ) ;
154+ const expected = [
155+ {
156+ name : 'foo' ,
157+ args : [
158+ { name : 'arg_a' , argType : 'string' } ,
159+ { name : 'arg_b' , argType : 'string' } ,
160+ ] ,
161+ } ,
162+ ] ;
163+ assert . deepStrictEqual ( result , expected ) ;
164+ } ) ;
165+
166+ it ( 'should mark removed arguments as deprecated' , ( ) => {
167+ const existing = [
168+ {
169+ name : 'foo' ,
170+ args : [
171+ { name : 'arg_a' , argType : 'string' } ,
172+ { name : 'arg_b' , argType : 'string' } ,
173+ ] ,
174+ } ,
175+ ] ;
176+ const update = [
177+ { name : 'foo' , args : [ { name : 'arg_a' , argType : 'string' } ] } ,
178+ ] ;
179+ const result = applyToExistingMetrics ( existing , update ) ;
180+ const expected = [
181+ {
182+ name : 'foo' ,
183+ args : [
184+ { name : 'arg_a' , argType : 'string' } ,
185+ { name : 'arg_b' , argType : 'string' , isDeprecated : true } ,
186+ ] ,
187+ } ,
188+ ] ;
189+ assert . deepStrictEqual ( result , expected ) ;
190+ } ) ;
191+
192+ it ( 'should not change args if they are the same' , ( ) => {
193+ const existing = [
194+ { name : 'foo' , args : [ { name : 'arg_a' , argType : 'string' } ] } ,
195+ ] ;
196+ const update = [
197+ { name : 'foo' , args : [ { name : 'arg_a' , argType : 'string' } ] } ,
198+ ] ;
199+ const result = applyToExistingMetrics ( existing , update ) ;
200+ const expected = [
201+ { name : 'foo' , args : [ { name : 'arg_a' , argType : 'string' } ] } ,
202+ ] ;
203+ assert . deepStrictEqual ( result , expected ) ;
204+ } ) ;
205+
206+ it ( 'should handle adding and removing arguments simultaneously' , ( ) => {
207+ const existing = [
208+ {
209+ name : 'foo' ,
210+ args : [
211+ { name : 'arg_a' , argType : 'string' } ,
212+ { name : 'arg_b' , argType : 'string' } ,
213+ ] ,
214+ } ,
215+ ] ;
216+ const update = [
217+ {
218+ name : 'foo' ,
219+ args : [
220+ { name : 'arg_b' , argType : 'string' } ,
221+ { name : 'arg_c' , argType : 'string' } ,
222+ ] ,
223+ } ,
224+ ] ;
225+ const result = applyToExistingMetrics ( existing , update ) ;
226+ const expected = [
227+ {
228+ name : 'foo' ,
229+ args : [
230+ { name : 'arg_a' , argType : 'string' , isDeprecated : true } ,
231+ { name : 'arg_b' , argType : 'string' } ,
232+ { name : 'arg_c' , argType : 'string' } ,
233+ ] ,
234+ } ,
235+ ] ;
236+ assert . deepStrictEqual ( result , expected ) ;
237+ } ) ;
238+
239+ it ( 'should handle tool and argument changes simultaneously' , ( ) => {
240+ const existing = [
241+ { name : 'foo' , args : [ { name : 'arg_a' , argType : 'string' } ] } ,
242+ { name : 'bar' , args : [ ] } ,
243+ ] ;
244+ const update = [
245+ { name : 'foo' , args : [ { name : 'arg_b' , argType : 'string' } ] } ,
246+ { name : 'baz' , args : [ ] } ,
247+ ] ;
248+ const result = applyToExistingMetrics ( existing , update ) ;
249+ const expected = [
250+ {
251+ name : 'foo' ,
252+ args : [
253+ { name : 'arg_a' , argType : 'string' , isDeprecated : true } ,
254+ { name : 'arg_b' , argType : 'string' } ,
255+ ] ,
256+ } ,
257+ { name : 'bar' , args : [ ] , isDeprecated : true } ,
258+ { name : 'baz' , args : [ ] } ,
259+ ] ;
260+ assert . deepStrictEqual ( result , expected ) ;
261+ } ) ;
262+ } ) ;
83263} ) ;
0 commit comments