terry-staging #12

Merged
terry merged 13 commits from terry-staging into main 2026-05-30 10:45:30 +00:00
2 changed files with 101 additions and 6 deletions
Showing only changes of commit 5f7c4eea62 - Show all commits

View File

@@ -0,0 +1,93 @@
import { describe, expect, it } from "vitest";
import type { Post } from "../types/post";
import { postToResource } from "./postResourceAdapter";
const basePost: Post = {
id: "post-1",
postType: "link",
categoryId: 1,
categorySlug: "official-announcement",
language: "zh",
title: "Link post",
text: "https://example.com/article",
attachments: [],
isRecommended: true,
publishedAt: "2026-05-30T00:00:00Z",
};
describe("postToResource", () => {
it("uses link preview images as the resource cover when there is no attachment", () => {
const resource = postToResource(
{
...basePost,
linkPreview: {
url: "https://example.com/article",
canonicalUrl: "https://example.com/article",
siteName: "Example",
title: "Example article",
description: "Example description",
imageUrl: "https://example.com/preview.jpg",
},
},
"zh-TW",
);
expect(resource.coverImage).toBe("https://example.com/preview.jpg");
expect(resource.previewUrl).toBe("https://example.com/preview.jpg");
});
it("uses document thumbnails before poster URLs", () => {
const resource = postToResource(
{
...basePost,
postType: "pdf",
attachments: [
{
id: "att-1",
kind: "document",
url: "/uploads/file.pdf",
mime: "application/pdf",
filename: "file.pdf",
sizeBytes: 123,
posterUrl: "/uploads/file.pdf",
thumbnailUrl: "/uploads/thumb.jpg",
},
],
},
"zh-TW",
);
expect(resource.coverImage).toBe("/uploads/thumb.jpg");
expect(resource.previewUrl).toBe("/uploads/thumb.jpg");
});
it("keeps an attachment thumbnail ahead of a link preview image", () => {
const resource = postToResource(
{
...basePost,
attachments: [
{
id: "att-1",
kind: "image",
url: "/uploads/full.jpg",
mime: "image/jpeg",
filename: "full.jpg",
sizeBytes: 123,
thumbnailUrl: "/uploads/thumb.jpg",
},
],
linkPreview: {
url: "https://example.com/article",
canonicalUrl: "https://example.com/article",
siteName: "Example",
title: "Example article",
description: "Example description",
imageUrl: "https://example.com/preview.jpg",
},
},
"zh-TW",
);
expect(resource.coverImage).toBe("/uploads/thumb.jpg");
});
});

View File

@@ -24,12 +24,13 @@ function inferType(post: Post, att: Attachment | undefined): string {
return "text"; return "text";
} }
function coverFor(att: Attachment | undefined) { function coverFor(post: Post, att: Attachment | undefined) {
if (!att) return ""; const linkPreviewImage = post.linkPreview?.imageUrl || "";
if (!att) return linkPreviewImage;
if (att.kind === "image" || att.mime.startsWith("image/")) { if (att.kind === "image" || att.mime.startsWith("image/")) {
return att.thumbnailUrl || att.url; return att.thumbnailUrl || att.url || linkPreviewImage;
} }
return att.posterUrl || att.thumbUrl || att.thumbnailUrl || ""; return att.thumbnailUrl || att.posterUrl || att.thumbUrl || linkPreviewImage;
} }
export function postToResource( export function postToResource(
@@ -49,9 +50,10 @@ export function postToResource(
categoryId: post.categoryId, categoryId: post.categoryId,
categorySlug: post.categorySlug, categorySlug: post.categorySlug,
categoryName: category?.name || post.categorySlug, categoryName: category?.name || post.categorySlug,
coverImage: coverFor(first), coverImage: coverFor(post, first),
fileUrl: first?.url, fileUrl: first?.url,
previewUrl: first?.posterUrl || first?.thumbnailUrl, previewUrl:
first?.thumbnailUrl || first?.posterUrl || post.linkPreview?.imageUrl,
externalUrl: undefined, externalUrl: undefined,
bodyText: postDisplayText(post, lang), bodyText: postDisplayText(post, lang),
badgeLabel: post.isRecommended ? "Recommended" : undefined, badgeLabel: post.isRecommended ? "Recommended" : undefined,