Skip to content

Commit 8022cbd

Browse files
authored
Merge pull request #252 from orionrobots/copilot/fix-244
Add fallback thumbnails for posts without images on front page
2 parents 838a0c8 + 2253a80 commit 8022cbd

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/thumbnails.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,21 @@ function has_thumbnail(post) {
3636

3737
async function thumbnail_for_post(post) {
3838
const thumbnailUrl = getPostThumbnailPath(post);
39+
let imageSrc;
40+
3941
if(thumbnailUrl == undefined) {
40-
return "";
42+
// Use favicon.png as fallback when no thumbnail is defined
43+
imageSrc = "favicon.png";
44+
} else {
45+
imageSrc = stripLeadingSlash(thumbnailUrl);
46+
if ( !fs.existsSync(imageSrc)) {
47+
// Use favicon.png as fallback when thumbnail file doesn't exist
48+
imageSrc = "favicon.png";
49+
}
4150
}
42-
const imageSrc = stripLeadingSlash(thumbnailUrl);
51+
4352
if ( !fs.existsSync(imageSrc)) {
53+
// If even favicon.png doesn't exist, return empty (shouldn't happen)
4454
return "";
4555
} else {
4656
// console.log("Generating thumbnail for " + imageSrc);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Feature: Recent Posts Thumbnails
2+
3+
Scenario: All recent posts should have thumbnail images
4+
Given the Staging site is started
5+
When the index page is visited
6+
Then the recent posts list should be present
7+
And each recent post should have a picture tag with an img element

tests/staging/step_definitions/website_steps.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,53 @@ Then('the Logo should be displayed in the top left corner', async function () {
7676
throw new Error('Logo (favicon.png) is not displayed in a nav element');
7777
}
7878
});
79+
80+
Then('the recent posts list should be present', async function () {
81+
if (!page) {
82+
throw new Error('Page not initialized. Make sure previous steps are executed first.');
83+
}
84+
85+
// Check that the recent posts section exists with its heading
86+
try {
87+
const recentPostsHeading = await page.locator('h2:has-text("Recent Posts")');
88+
await recentPostsHeading.waitFor({ state: 'visible', timeout: 10000 });
89+
90+
// Check that the posts list exists
91+
const postsList = await page.locator('ul.posts');
92+
await postsList.waitFor({ state: 'visible', timeout: 10000 });
93+
} catch (error) {
94+
throw new Error('Recent posts list is not present on the page');
95+
}
96+
});
97+
98+
Then('each recent post should have a picture tag with an img element', async function () {
99+
if (!page) {
100+
throw new Error('Page not initialized. Make sure previous steps are executed first.');
101+
}
102+
103+
// Find all post items in the recent posts list
104+
const postItems = await page.locator('ul.posts li.post').all();
105+
106+
if (postItems.length === 0) {
107+
throw new Error('No recent posts found in the posts list');
108+
}
109+
110+
// Check each post item has a picture tag with img element
111+
for (let i = 0; i < postItems.length; i++) {
112+
const postItem = postItems[i];
113+
114+
try {
115+
// Look for either a picture tag with img, or just an img tag within the media-left link
116+
const imageElement = await postItem.locator('a.media-left picture img, a.media-left img').first();
117+
await imageElement.waitFor({ state: 'visible', timeout: 5000 });
118+
119+
// Verify the img element has a valid src attribute
120+
const src = await imageElement.getAttribute('src');
121+
if (!src || src.trim() === '') {
122+
throw new Error(`Post ${i + 1} has an img element but no src attribute`);
123+
}
124+
} catch (error) {
125+
throw new Error(`Post ${i + 1} does not have a picture tag with img element: ${error.message}`);
126+
}
127+
}
128+
});

0 commit comments

Comments
 (0)