Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
feat: add primary page domain checks
Browse files Browse the repository at this point in the history
  • Loading branch information
simone-amadio-acn authored and tensor5 committed Sep 21, 2023
1 parent 236a702 commit fa41bce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
58 changes: 37 additions & 21 deletions src/audits/municipality/menuAudit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import lighthouse from "lighthouse";
import { primaryMenuItems } from "../../storage/municipality/menuItems";
import {
checkOrder,
getPageElementDataAttribute,
getPageElementDataAttribute, isInternalRedirectUrl,
loadPageData,
missingMenuItems,
} from "../../utils/utils";
import { auditDictionary } from "../../storage/auditDictionary";
import { MenuItem } from "../../types/menuItem";
import { getRandomFirstLevelPages } from "../../utils/municipality/utils";

const Audit = lighthouse.Audit;

Expand Down Expand Up @@ -89,13 +90,11 @@ class LoadAudit extends lighthouse.Audit {
wrong_order_menu_voices: "",
});

const $: CheerioAPI = await loadPageData(url);
const firstLevelPages = await getRandomFirstLevelPages(url, false);

const foundMenuElements = await getPageElementDataAttribute(
$,
'[data-element="main-navigation"]',
"> li > a"
);
const foundMenuElements = firstLevelPages.map((page) => {
return page.linkName;
});

results[0].found_menu_voices = foundMenuElements.join(", ");

Expand Down Expand Up @@ -144,22 +143,39 @@ class LoadAudit extends lighthouse.Audit {
result: "Voce di menù",
found_menu_voices: "Link trovato",
missing_menu_voices: "Pagina associata corretta",
wrong_order_menu_voices: "Pagina esterna",
wrong_order_menu_voices: "Pagina interna al dominio",
});

results.push({
subItems: {
type: "subitems",
items: [
{
menu_voice: "Amministrazione",
inspected_page: "www.google.com",
correct_associated_page: "No",
external: "Sì",
},
],
},
});
for(const page of firstLevelPages){
const isInternal = await isInternalRedirectUrl(url, page.linkUrl)
let isCorrectlyAssociated = false;

if(isInternal){
const $ = await loadPageData(page.linkUrl);
const pageName = $('[data-element="page-name"]').text().trim() ?? "";
if(pageName.length > 0 && pageName.toLowerCase() === page.linkName.toLowerCase()){
isCorrectlyAssociated = true;
}
}

if(!isInternal || !isCorrectlyAssociated){
score = 0;
}

const item = {
menu_voice: page.linkName,
inspected_page: page.linkUrl,
correct_associated_page: isCorrectlyAssociated ? 'Sì' : 'No',
external: isInternal ? 'Sì' : 'No',
};

results.push({
subItems: {
type: "subitems",
items: [item]
},
});
}

return {
score: score,
Expand Down
9 changes: 4 additions & 5 deletions src/utils/municipality/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ const getRandomFirstLevelPagesUrl = async (
url: string,
numberOfPages = 1
): Promise<string[]> => {
const pagesUrls: string[] = [];

const pages = await getRandomFirstLevelPages(url, true);

for (const page of pages) {
pagesUrls.push(page.linkUrl);
}
const pagesUrls = pages.map((page) => {
return page.linkUrl;
});

return getRandomNString(pagesUrls, numberOfPages);
};
Expand Down Expand Up @@ -884,4 +882,5 @@ export {
getButtonUrl,
isDrupal,
getPages,
getRandomFirstLevelPages,
};
2 changes: 0 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,6 @@ const isInternalRedirectUrl = async (
return false;
}

console.log(res?.url(), baseUrl);

const data = await page.content();

await page.goto("about:blank");
Expand Down

0 comments on commit fa41bce

Please sign in to comment.