@@ -47,6 +47,17 @@ const MOCK_METRIC_SOURCE: Omit<Extract<TSource, { kind: 'metric' }>, 'id'> = {
4747 } ,
4848} ;
4949
50+ const MOCK_PROMQL_SOURCE : Omit < Extract < TSource , { kind : 'promql' } > , 'id' > = {
51+ kind : SourceKind . Promql ,
52+ name : 'Test PromQL Source' ,
53+ connection : new Types . ObjectId ( ) . toString ( ) ,
54+ from : {
55+ databaseName : 'test_db' ,
56+ tableName : 'metrics_ts' ,
57+ } ,
58+ timestampValueExpression : 'timestamp' ,
59+ } ;
60+
5061const createTestConnection = ( team : Types . ObjectId , id : string ) =>
5162 Connection . create ( {
5263 _id : id ,
@@ -63,6 +74,7 @@ const getLoggedInAgent = async (server: ReturnType<typeof getServer>) => {
6374 await Promise . all ( [
6475 createTestConnection ( result . team . _id , MOCK_SOURCE . connection ) ,
6576 createTestConnection ( result . team . _id , MOCK_METRIC_SOURCE . connection ) ,
77+ createTestConnection ( result . team . _id , MOCK_PROMQL_SOURCE . connection ) ,
6678 ] ) ;
6779
6880 return result ;
@@ -803,6 +815,92 @@ describe('sources router', () => {
803815 } ) ;
804816 } ) ;
805817
818+ describe ( 'minAutoGranularity on a PromQL source' , ( ) => {
819+ it ( 'POST / - creates a PromQL source with minAutoGranularity and it round-trips' , async ( ) => {
820+ const { agent } = await getLoggedInAgent ( server ) ;
821+
822+ const response = await agent
823+ . post ( '/sources' )
824+ . send ( {
825+ ...MOCK_PROMQL_SOURCE ,
826+ minAutoGranularity : '1 minute' ,
827+ } )
828+ . expect ( 200 ) ;
829+
830+ expect ( response . body . minAutoGranularity ) . toBe ( '1 minute' ) ;
831+
832+ const sources = await Source . find ( { } ) . lean ( ) ;
833+ expect ( sources ) . toHaveLength ( 1 ) ;
834+ const persisted = sources [ 0 ] ;
835+ if ( persisted ?. kind !== SourceKind . Promql ) {
836+ expect ( persisted ?. kind ) . toBe ( SourceKind . Promql ) ;
837+ throw new Error ( 'Source is not a PromQL source' ) ;
838+ }
839+ expect ( persisted . minAutoGranularity ) . toBe ( '1 minute' ) ;
840+ } ) ;
841+
842+ it ( "POST / - the form's \"No minimum\" value ('') persists as unset, not as an empty string" , async ( ) => {
843+ const { agent } = await getLoggedInAgent ( server ) ;
844+
845+ const response = await agent
846+ . post ( '/sources' )
847+ . send ( {
848+ ...MOCK_PROMQL_SOURCE ,
849+ minAutoGranularity : '' ,
850+ } )
851+ . expect ( 200 ) ;
852+
853+ expect ( response . body ) . not . toHaveProperty ( 'minAutoGranularity' ) ;
854+
855+ const sources = await Source . find ( { } ) . lean ( ) ;
856+ expect ( sources ) . toHaveLength ( 1 ) ;
857+ expect ( sources [ 0 ] ) . not . toHaveProperty ( 'minAutoGranularity' ) ;
858+ } ) ;
859+
860+ it ( 'PUT /:id - updates minAutoGranularity' , async ( ) => {
861+ const { agent, team } = await getLoggedInAgent ( server ) ;
862+
863+ const promqlSource = await Source . create ( {
864+ ...MOCK_PROMQL_SOURCE ,
865+ minAutoGranularity : '1 minute' ,
866+ team : team . _id ,
867+ } ) ;
868+
869+ await agent
870+ . put ( `/sources/${ promqlSource . _id } ` )
871+ . send ( {
872+ id : promqlSource . _id . toString ( ) ,
873+ ...MOCK_PROMQL_SOURCE ,
874+ minAutoGranularity : '5 minute' ,
875+ } )
876+ . expect ( 200 ) ;
877+
878+ const updatedSource = await Source . findById ( promqlSource . _id ) . lean ( ) ;
879+ expect ( updatedSource ) . toHaveProperty ( 'minAutoGranularity' , '5 minute' ) ;
880+ } ) ;
881+
882+ it ( 'PUT /:id - removes minAutoGranularity when omitted from the update payload' , async ( ) => {
883+ const { agent, team } = await getLoggedInAgent ( server ) ;
884+
885+ const promqlSource = await Source . create ( {
886+ ...MOCK_PROMQL_SOURCE ,
887+ minAutoGranularity : '1 minute' ,
888+ team : team . _id ,
889+ } ) ;
890+
891+ await agent
892+ . put ( `/sources/${ promqlSource . _id } ` )
893+ . send ( {
894+ id : promqlSource . _id . toString ( ) ,
895+ ...MOCK_PROMQL_SOURCE ,
896+ } )
897+ . expect ( 200 ) ;
898+
899+ const updatedSource = await Source . findById ( promqlSource . _id ) . lean ( ) ;
900+ expect ( updatedSource ) . not . toHaveProperty ( 'minAutoGranularity' ) ;
901+ } ) ;
902+ } ) ;
903+
806904 it ( 'DELETE /:id - deletes a source' , async ( ) => {
807905 const { agent, team } = await getLoggedInAgent ( server ) ;
808906
0 commit comments