Skip to content

Commit d44fd70

Browse files
authored
fix: add code import as step audit (#1649)
Please ensure your pull request adheres to the following guidelines: - [ ] make sure to link the related issues in this description - [ ] when merging / squashing, make sure the fixed issue references are visible in the commits, for easy compilation of release notes - [ ] If data sources for any opportunity has been updated/added, please update the [wiki](https://wiki.corp.adobe.com/display/AEMSites/Data+Sources+for+Opportunities) for same opportunity. ## Related Issues Thanks for contributing!
1 parent f71deea commit d44fd70

File tree

5 files changed

+761
-480
lines changed

5 files changed

+761
-480
lines changed

src/accessibility/handler.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,19 @@ export function createProcessAccessibilityOpportunitiesWithDevice(deviceType) {
413413
};
414414
}
415415

416+
export async function codeImportStep(context) {
417+
const {
418+
log, site,
419+
} = context;
420+
421+
log.info(`[A11yAudit] [Site Id: ${site.getId()}] starting code import step`);
422+
423+
return {
424+
type: 'code',
425+
siteId: site.getId(),
426+
};
427+
}
428+
416429
export default new AuditBuilder()
417430
.withUrlResolver((site) => site.resolveFinalURL())
418431
.addStep(
@@ -425,5 +438,6 @@ export default new AuditBuilder()
425438
scrapeAccessibilityData,
426439
AUDIT_STEP_DESTINATIONS.CONTENT_SCRAPER,
427440
)
441+
.addStep('codeImport', codeImportStep, AUDIT_STEP_DESTINATIONS.IMPORT_WORKER)
428442
.addStep('processAccessibilityOpportunities', processAccessibilityOpportunities)
429443
.build();

src/accessibility/utils/data-processing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ export async function sendRunImportMessage(
984984
* @param {Object} context - The context object containing log, s3Client, env
985985
* @returns {Promise<Object|null>} Object containing codeBucket and codePath, or null if should skip
986986
*/
987-
async function getCodeInfo(site, opportunityType, context) {
987+
export async function getCodeInfo(site, opportunityType, context) {
988988
const { log, s3Client, env } = context;
989989
const siteId = site.getId();
990990
const deliveryType = site.getDeliveryType();

src/accessibility/utils/generate-individual-opportunities.js

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import {
2121
successCriteriaLinks, accessibilityOpportunitiesMap, URL_SOURCE_SEPARATOR, issueTypesForCodeFix,
2222
} from './constants.js';
23-
import { getAuditData } from './data-processing.js';
23+
import { getAuditData, getCodeInfo } from './data-processing.js';
2424
import { processSuggestionsForMystique } from '../guidance-utils/mystique-data-processing.js';
2525
import { isAuditEnabledForSite } from '../../common/audit-utils.js';
2626
import { saveMystiqueValidationMetricsToS3, saveOpptyWithRetry } from './scrape-utils.js';
@@ -173,81 +173,45 @@ async function sendMystiqueMessage({
173173
log,
174174
context,
175175
}) {
176+
// Create base message
177+
const message = createDirectMystiqueMessage({
178+
url,
179+
issuesList,
180+
opportunity,
181+
siteId,
182+
auditId,
183+
deliveryType,
184+
aggregationKey,
185+
});
186+
176187
// Check if code fix flow is enabled for this site
177188
const autoFixEnabled = await isAuditEnabledForSite('a11y-mystique-auto-fix', context.site, context);
178189
const useCodeFixFlow = autoFixEnabled && shouldUseCodeFixFlow(issuesList);
179190

191+
// Add code info if code fix flow is enabled
180192
if (useCodeFixFlow) {
181-
const forwardPayload = createMystiqueForwardPayload({
182-
url,
183-
issuesList,
184-
opportunity,
185-
aggregationKey,
186-
siteId,
187-
auditId,
188-
deliveryType,
189-
});
193+
const codeInfo = await getCodeInfo(context.site, 'accessibility', context);
194+
if (codeInfo?.codeBucket && codeInfo?.codePath) {
195+
message.data.codeBucket = codeInfo.codeBucket;
196+
message.data.codePath = codeInfo.codePath;
197+
}
198+
}
190199

191-
const message = {
192-
type: 'code',
193-
siteId,
194-
allowCache: true,
195-
data: {},
196-
forward: {
197-
queue: env.QUEUE_SPACECAT_TO_MYSTIQUE,
198-
payload: forwardPayload,
199-
},
200+
try {
201+
await sqs.sendMessage(env.QUEUE_SPACECAT_TO_MYSTIQUE, message);
202+
return {
203+
success: true,
204+
url,
200205
};
201-
202-
try {
203-
await sqs.sendMessage(env.IMPORT_WORKER_QUEUE_URL, message);
204-
log.info(
205-
`[A11yIndividual] Sent message to import worker for code fix and forwarding to Mystique for url ${url}`,
206-
);
207-
return {
208-
success: true,
209-
url,
210-
};
211-
} catch (error) {
212-
log.error(
213-
`[A11yIndividual][A11yProcessingError] Failed to send message to import worker for url ${url} with error: ${error.message}`,
214-
);
215-
return {
216-
success: false,
217-
url,
218-
error: error.message,
219-
};
220-
}
221-
} else {
222-
// Legacy flow: Send directly to Mystique without code injection
223-
const message = createDirectMystiqueMessage({
206+
} catch (error) {
207+
log.error(
208+
`[A11yIndividual][A11yProcessingError] Failed to send message to Mystique for url ${url} with error: ${error.message}`,
209+
);
210+
return {
211+
success: false,
224212
url,
225-
issuesList,
226-
opportunity,
227-
siteId,
228-
auditId,
229-
deliveryType,
230-
});
231-
232-
try {
233-
await sqs.sendMessage(env.QUEUE_SPACECAT_TO_MYSTIQUE, message);
234-
log.info(
235-
`[A11yIndividual] Sent message directly to Mystique (legacy flow) for url ${url}`,
236-
);
237-
return {
238-
success: true,
239-
url,
240-
};
241-
} catch (error) {
242-
log.error(
243-
`[A11yIndividual][A11yProcessingError] Failed to send message to Mystique for url ${url}, message: ${JSON.stringify(message, null, 2)} with error: ${error.message}`,
244-
);
245-
return {
246-
success: false,
247-
url,
248-
error: error.message,
249-
};
250-
}
213+
error: error.message,
214+
};
251215
}
252216
}
253217

test/audits/accessibility.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('Accessibility Audit Handler', () => {
3030
let processAccessibilityOpportunities;
3131
let createProcessAccessibilityOpportunitiesWithDevice;
3232
let processImportStep;
33+
let codeImportStep;
3334
let getUrlsForAuditStub;
3435
let aggregateAccessibilityDataStub;
3536
let generateReportOpportunitiesStub;
@@ -106,6 +107,7 @@ describe('Accessibility Audit Handler', () => {
106107
processAccessibilityOpportunities = accessibilityModule.processAccessibilityOpportunities;
107108
createProcessAccessibilityOpportunitiesWithDevice = accessibilityModule.createProcessAccessibilityOpportunitiesWithDevice;
108109
processImportStep = accessibilityModule.processImportStep;
110+
codeImportStep = accessibilityModule.codeImportStep;
109111
});
110112

111113
afterEach(() => {
@@ -1482,6 +1484,83 @@ describe('Accessibility Audit Handler', () => {
14821484

14831485
});
14841486

1487+
describe('codeImportStep', () => {
1488+
it('should return correct step result with type and siteId', async () => {
1489+
// Arrange
1490+
const context = {
1491+
log: mockContext.log,
1492+
site: mockSite,
1493+
};
1494+
1495+
// Act
1496+
const result = await codeImportStep(context);
1497+
1498+
// Assert
1499+
expect(result).to.deep.equal({
1500+
type: 'code',
1501+
siteId: 'test-site-id',
1502+
});
1503+
});
1504+
1505+
it('should log the correct info message', async () => {
1506+
// Arrange
1507+
const context = {
1508+
log: mockContext.log,
1509+
site: mockSite,
1510+
};
1511+
1512+
// Act
1513+
await codeImportStep(context);
1514+
1515+
// Assert
1516+
expect(mockContext.log.info).to.have.been.calledWith(
1517+
'[A11yAudit] [Site Id: test-site-id] starting code import step',
1518+
);
1519+
});
1520+
1521+
it('should call site.getId() twice', async () => {
1522+
// Arrange
1523+
const context = {
1524+
log: mockContext.log,
1525+
site: mockSite,
1526+
};
1527+
1528+
// Reset the stub to track calls
1529+
mockSite.getId.resetHistory();
1530+
1531+
// Act
1532+
await codeImportStep(context);
1533+
1534+
// Assert
1535+
expect(mockSite.getId).to.have.been.calledTwice;
1536+
});
1537+
1538+
it('should handle different site IDs correctly', async () => {
1539+
// Arrange
1540+
const customSiteId = 'different-site-id';
1541+
const customMockSite = {
1542+
getId: sandbox.stub().returns(customSiteId),
1543+
getBaseURL: sandbox.stub().returns('https://different.com'),
1544+
};
1545+
const context = {
1546+
log: mockContext.log,
1547+
site: customMockSite,
1548+
};
1549+
1550+
// Reset log history to check specific calls
1551+
mockContext.log.info.resetHistory();
1552+
1553+
// Act
1554+
const result = await codeImportStep(context);
1555+
1556+
// Assert
1557+
expect(result.siteId).to.equal(customSiteId);
1558+
expect(mockContext.log.info).to.have.been.calledWith(
1559+
`[A11yAudit] [Site Id: ${customSiteId}] starting code import step`,
1560+
);
1561+
});
1562+
});
1563+
14851564
describe('createProcessAccessibilityOpportunitiesWithDevice', () => {
14861565
beforeEach(() => {
14871566
// Reset context to include AWS_ENV for tests

0 commit comments

Comments
 (0)