Skip to content

Commit

Permalink
chore: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKid committed Feb 24, 2025
1 parent 0a8e4a0 commit 2a14ef7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/image-alt-text/opportunityHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ const getImageSuggestionIdentifier = (suggestion) => `${suggestion.pageUrl}/${su
*/
export async function syncAltTextSuggestions({ opportunity, newSuggestions, log }) {
const existingSuggestions = await opportunity.getSuggestions();
log.info(`existingSuggestions: ${JSON.stringify(existingSuggestions)}`);

// eslint-disable-next-line max-len
const ignoredSuggestions = existingSuggestions.filter((s) => s.status === SuggestionModel.STATUSES.SKIPPED);
const ignoredSuggestions = existingSuggestions.filter((sug) => {
log.info(`sug ${sug}`);
const parsedSuggestion = JSON.parse(JSON.stringify(sug));
log.info(`parsedSuggestion: ${JSON.stringify(parsedSuggestion)}`);
log.info(`parsedSuggestion.rank: ${parsedSuggestion.rank}`);
return parsedSuggestion.status === SuggestionModel.STATUSES.SKIPPED;
});
log.info(`Ignored suggestions: ${JSON.stringify(ignoredSuggestions)}`);
const ignoredSuggestionIds = ignoredSuggestions.map((s) => s.data.recommendations[0].id);
log.info(`Ignored suggestion ids: ${JSON.stringify(ignoredSuggestionIds)}`);

// Remove existing suggestions that were not ignored
await Promise.all(existingSuggestions
.filter((suggestion) => !ignoredSuggestionIds.includes(suggestion.id))
.map((suggestion) => suggestion.remove()));

const suggestionsToAdd = newSuggestions.filter((s) => !ignoredSuggestionIds.includes(s.id));
log.info(`suggestionsToAdd: ${JSON.stringify(suggestionsToAdd)}`);

// Add new suggestions to oppty
if (isNonEmptyArray(suggestionsToAdd)) {
Expand Down
4 changes: 2 additions & 2 deletions test/audits/image-alt-text/opportunity-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ describe('Image Alt Text Opportunity Handler', () => {
{
id: 'suggestion-1',
status: SuggestionModel.STATUSES.SKIPPED,
data: { recommendations: [{ id: 'suggestion-1' }] }, // Add data.id to match handler's expectation
data: { recommendations: [{ id: 'suggestion-1' }] },
remove: sinon.stub().resolves(),
},
{
id: 'suggestion-2',
status: 'NEW',
data: { recommendations: [{ id: 'suggestion-2' }] }, // Add data.id to match handler's expectation
data: { recommendations: [{ id: 'suggestion-2' }] },
remove: sinon.stub().resolves(),
},
];
Expand Down

0 comments on commit 2a14ef7

Please sign in to comment.