Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

93 lines
2.8 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { portableTextToProsemirror } from "../../../src/content/converters/portable-text-to-prosemirror.js";
import type { PortableTextBlock } from "../../../src/content/converters/types.js";
describe("Image blocks without asset wrapper", () => {
it("does not crash when an image block has url at the top level instead of inside asset", () => {
// This is the format that can originate from migrations or third-party imports
// (e.g. Ghost → Portable Text). Without the fix, accessing block.asset.url
// throws TypeError: Cannot read properties of undefined (reading 'url').
const blocks: PortableTextBlock[] = [
{
_type: "block",
_key: "b1",
style: "normal",
children: [{ _type: "span", _key: "s1", text: "Before image", marks: [] }],
markDefs: [],
},
{
_type: "image",
_key: "img1",
url: "https://example.com/photo.jpg",
alt: "A photo without asset wrapper",
} as unknown as PortableTextBlock,
{
_type: "block",
_key: "b2",
style: "normal",
children: [{ _type: "span", _key: "s2", text: "After image", marks: [] }],
markDefs: [],
},
];
const result = portableTextToProsemirror(blocks);
expect(result.type).toBe("doc");
expect(result.content).toHaveLength(3);
});
it("extracts src and alt from top-level url when asset is missing", () => {
const blocks: PortableTextBlock[] = [
{
_type: "image",
_key: "img1",
url: "https://example.com/photo.jpg",
alt: "A test image",
} as unknown as PortableTextBlock,
];
const result = portableTextToProsemirror(blocks);
const imageNode = result.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.src).toBe("https://example.com/photo.jpg");
expect(imageNode.attrs?.alt).toBe("A test image");
});
it("handles image block with neither asset nor url gracefully", () => {
const blocks: PortableTextBlock[] = [
{
_type: "image",
_key: "img1",
} as unknown as PortableTextBlock,
];
const result = portableTextToProsemirror(blocks);
const imageNode = result.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.src).toBe("");
expect(imageNode.attrs?.alt).toBe("");
});
it("still converts well-formed image blocks with asset wrapper correctly", () => {
const blocks: PortableTextBlock[] = [
{
_type: "image",
_key: "img1",
asset: { _ref: "media-123", url: "https://example.com/photo.jpg" },
alt: "A proper image",
},
];
const result = portableTextToProsemirror(blocks);
const imageNode = result.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.src).toBe("https://example.com/photo.jpg");
expect(imageNode.attrs?.alt).toBe("A proper image");
expect(imageNode.attrs?.mediaId).toBe("media-123");
});
});