import { FeatureRow , isValidFeatureMetricsReport , postFeatureMetrics } from './feature_metrics' ; import { InsertableTable } from './infrastructure/table' ; import { DailyFeatureMetricsReport } from './model' ; class FakeFeaturesTable implements InsertableTable < FeatureRow > { public rows : FeatureRow [ ] | undefined ; async insert ( rows ) { this . rows = rows ; } } describe ( 'postFeatureMetrics' , ( ) => { it ( 'correctly inserts feature metrics rows' , async ( ) => { const table = new FakeFeaturesTable ( ) ; const report = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : false } } ; await postFeatureMetrics ( table , report ) ; const rows = [ { serverId : report . serverId , serverVersion : report . serverVersion , timestamp : new Date ( report . timestampUtcMs ) . toISOString ( ) , dataLimit : report . dataLimit } ] ; expect ( table . rows ) . toEqual ( rows ) ; } ) ; } ) ; describe ( 'isValidFeatureMetricsReport' , ( ) => { it ( 'returns true for valid report' , ( ) => { const report = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( report ) ) . toBeTruthy ( ) ; } ) ; it ( 'returns true for valid report with per-key data limit count' , ( ) => { const report = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : true , perKeyLimitCount : 1 } } ; expect ( isValidFeatureMetricsReport ( report ) ) . toBeTruthy ( ) ; } ) ; it ( 'returns false for report with negative per-key data limit count' , ( ) => { const report = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : true , perKeyLimitCount : - 1 } } ; expect ( isValidFeatureMetricsReport ( report ) ) . toBeFalsy ( ) ; } ) ; it ( 'returns false for missing report' , ( ) => { expect ( isValidFeatureMetricsReport ( undefined ) ) . toBeFalsy ( ) ; } ) ; it ( 'returns false for incorrect report field types' , ( ) => { const invalidReport = { serverId : 1234 , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( invalidReport ) ) . toBeFalsy ( ) ; const invalidReport2 = { serverId : 'id' , serverVersion : 1010 , timestampUtcMs : 123456 , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( invalidReport2 ) ) . toBeFalsy ( ) ; const invalidReport3 = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : '123' , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( invalidReport3 ) ) . toBeFalsy ( ) ; const invalidReport4 = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : 'enabled' } ; expect ( isValidFeatureMetricsReport ( invalidReport4 ) ) . toBeFalsy ( ) ; const invalidReport5 = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : 'true' } } ; expect ( isValidFeatureMetricsReport ( invalidReport5 ) ) . toBeFalsy ( ) ; } ) ; it ( 'returns false for missing report fields' , ( ) => { const invalidReport = { serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( invalidReport ) ) . toBeFalsy ( ) ; const invalidReport2 = { serverId : 'id' , timestampUtcMs : 123456 , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( invalidReport2 ) ) . toBeFalsy ( ) ; const invalidReport3 = { serverId : 'id' , serverVersion : '0.0.0' , dataLimit : { enabled : true } } ; expect ( isValidFeatureMetricsReport ( invalidReport3 ) ) . toBeFalsy ( ) ; const invalidReport4 = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , } ; expect ( isValidFeatureMetricsReport ( invalidReport4 ) ) . toBeFalsy ( ) ; const invalidReport5 = { serverId : 'id' , serverVersion : '0.0.0' , timestampUtcMs : 123456 , dataLimit : { } } ; expect ( isValidFeatureMetricsReport ( invalidReport5 ) ) . toBeFalsy ( ) ; } ) ; } ) ;