@@ -3,6 +3,7 @@ import { createSingleMetric } from '@aws-github-runner/aws-powertools-util';
33import { MetricUnit } from '@aws-lambda-powertools/metrics' ;
44import { metricGitHubAppRateLimit } from './rate-limit' ;
55import { describe , it , expect , beforeEach , vi } from 'vitest' ;
6+ import { getParameter } from '@aws-github-runner/aws-ssm-util' ;
67
78process . env . PARAMETER_GITHUB_APP_ID_NAME = 'test' ;
89vi . mock ( '@aws-github-runner/aws-ssm-util' , async ( ) => {
@@ -78,4 +79,27 @@ describe('metricGitHubAppRateLimit', () => {
7879
7980 expect ( createSingleMetric ) . not . toHaveBeenCalled ( ) ;
8081 } ) ;
82+
83+ it ( 'should cache GitHub App ID and only call getParameter once' , async ( ) => {
84+ // Reset modules to clear the appIdPromise cache
85+ vi . resetModules ( ) ;
86+ const { metricGitHubAppRateLimit : freshMetricFunction } = await import ( './rate-limit' ) ;
87+
88+ process . env . ENABLE_METRIC_GITHUB_APP_RATE_LIMIT = 'true' ;
89+ const headers : ResponseHeaders = {
90+ 'x-ratelimit-remaining' : '10' ,
91+ 'x-ratelimit-limit' : '60' ,
92+ } ;
93+
94+ const mockGetParameter = vi . mocked ( getParameter ) ;
95+ mockGetParameter . mockClear ( ) ;
96+
97+ await freshMetricFunction ( headers ) ;
98+ await freshMetricFunction ( headers ) ;
99+ await freshMetricFunction ( headers ) ;
100+
101+ // getParameter should only be called once due to caching
102+ expect ( mockGetParameter ) . toHaveBeenCalledTimes ( 1 ) ;
103+ expect ( mockGetParameter ) . toHaveBeenCalledWith ( process . env . PARAMETER_GITHUB_APP_ID_NAME ) ;
104+ } ) ;
81105} ) ;
0 commit comments