Skip to content

Commit

Permalink
fix: set dev source for psi (#117)
Browse files Browse the repository at this point in the history
* chore: migrate renovate config

* chore: remove esmock

* fix: set psi x-source header with env

* fix: test
  • Loading branch information
solaris007 committed Feb 2, 2024
1 parent f50efe9 commit 3537fda
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/lhs/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,17 @@ function initServices(config, log = console) {
psiApiBaseUrl,
sqs,
dataAccess,
environment,
} = config;

const psiClient = PSIClient({ apiKey: psiApiKey, apiBaseUrl: psiApiBaseUrl }, log);
const psiClient = PSIClient(
{
apiKey: psiApiKey,
apiBaseUrl: psiApiBaseUrl,
environment,
},
log,
);

return {
dataAccess,
Expand Down Expand Up @@ -346,6 +354,7 @@ export default async function audit(message, context) {
psiApiBaseUrl,
sqs,
dataAccess,
environment: context.func.version === 'v1' ? 'prod' : 'dev',
}, log);

const startTime = process.hrtime();
Expand Down
5 changes: 3 additions & 2 deletions src/support/psi-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const PSI_STRATEGIES = [PSI_STRATEGY_MOBILE, PSI_STRATEGY_DESKTOP];
* @return {PSIClient} - The PSI client.
*/
function PSIClient(config, log = console) {
const { apiKey, apiBaseUrl } = config;
const { apiKey, apiBaseUrl, environment } = config;

if (!isValidUrl(apiBaseUrl)) {
throw new Error(`Invalid PSI API Base URL: ${apiBaseUrl}`);
Expand Down Expand Up @@ -72,7 +72,8 @@ function PSIClient(config, log = console) {
const performPSICheck = async (baseURL, strategy) => {
try {
const apiURL = getPSIApiUrl(baseURL, strategy);
const response = await fetch(apiURL, { headers: { 'x-source': 'spacecat' } });
const xSource = `spacecat-${environment}`;
const response = await fetch(apiURL, { headers: { 'x-source': xSource } });
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
Expand Down
4 changes: 4 additions & 0 deletions test/audits/lhs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ describe('LHS Audit', () => {
};
context = {
log: mockLog,
func: {
version: 'ci',
},
runtime: {
region: 'us-east-1',
},
Expand Down Expand Up @@ -276,6 +279,7 @@ describe('LHS Audit', () => {
.get('/?url=https%3A%2F%2Fadobe.com%2F&strategy=mobile')
.reply(200, psiResult);

context.func.version = 'v1';
context.sqs.sendMessage.rejects(new Error('SQS Error'));

await audit(auditQueueMessage, context);
Expand Down
3 changes: 2 additions & 1 deletion test/support/psi-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('PSIClient', () => {
const config = {
apiKey: 'testApiKey',
apiBaseUrl: 'https://example.com',
environment: 'dev',
};

beforeEach(() => {
Expand Down Expand Up @@ -79,7 +80,7 @@ describe('PSIClient', () => {
it('fetches PSI data successfully', async () => {
nock('https://example.com')
.get('/')
.matchHeader('x-source', 'spacecat')
.matchHeader('x-source', 'spacecat-dev')
.query(true)
.reply(200, { lighthouseResult: { score: 0.9 } });

Expand Down

0 comments on commit 3537fda

Please sign in to comment.