// @vitest-environment jsdom import { describe, expect, test } from "vitest"; import { parseWordpressExport } from "./parseWordpressExport"; const item = (fields: string) => `${fields}`; const wxr = (items: string) => ` My blog https://blog.example.com 1.2 https://blog.example.com https://blog.example.com ${items} `; const post = (o: { id: string; type?: string; status?: string; slug?: string; date?: string; dateGmt?: string; html?: string; password?: string; extra?: string; }) => item(` <![CDATA[Post ${o.id}]]> https://blog.example.com/${o.slug ?? `post-${o.id}`}/ https://blog.example.com/?p=${o.id} Body

"}]]>
${o.id} ${o.extra ?? ""}`); const attachment = (id: string, url: string, size = "") => item(` ${id} `); describe("parseWordpressExport", () => { test("reads the site and its posts and pages, oldest first", () => { const ex = parseWordpressExport( wxr( post({ id: "2", dateGmt: "2026-01-01 00:00:00" }) + post({ id: "1", type: "page", slug: "about" }) + post({ id: "3", type: "nav_menu_item" }) + post({ id: "4", status: "trash" }) + post({ id: "5", status: "auto-draft" }), ), ); expect(ex.siteTitle).toBe("My blog"); expect(ex.siteUrl).toBe("https://blog.example.com"); expect(ex.posts.map((p) => [p.id, p.type, p.slug])).toEqual([ ["1", "page", "about"], ["2", "post", "post-2"], ]); expect(ex.posts[0]).toMatchObject({ title: "Post 1", status: "publish", date: "2025-07-08T12:30:00.000Z", link: "https://blog.example.com/about/", guid: "https://blog.example.com/?p=1", html: "

Body

", }); }); test("drafts without a GMT date or slug fall back to the local date and id", () => { const ex = parseWordpressExport( wxr( post({ id: "7", status: "draft", slug: "", dateGmt: "0000-00-00 00:00:00", date: "2025-05-01 09:00:00", }), ), ); expect(ex.posts[0]).toMatchObject({ slug: "7", date: "2025-05-01T09:00:00.000Z", }); }); test("categories then tags, without Uncategorized or duplicates", () => { const ex = parseWordpressExport( wxr( post({ id: "1", extra: ` `, }), ), ); expect(ex.posts[0].tags).toEqual(["Foodie", "Miso"]); }); test("resolves the featured image and the attachments the body uses", () => { const ex = parseWordpressExport( wxr( attachment( "11", "https://blog.example.com/wp-content/uploads/a.png", 'a:6:{s:5:"width";i:750;s:6:"height";i:1000;s:4:"file";s:5:"a.png";s:5:"sizes";a:1:{s:6:"medium";a:3:{s:4:"file";s:13:"a-225x300.png";s:5:"width";i:225;s:6:"height";i:300;}}}', ) + attachment( "12", "https://blog.example.com/wp-content/uploads/b.png", ) + attachment( "13", "https://blog.example.com/wp-content/uploads/c.png", ) + post({ id: "1", html: `[gallery ids="12, 13"]`, extra: ``, }), ), ); const p = ex.posts[0]; expect(p.featureImage).toEqual({ url: "https://blog.example.com/wp-content/uploads/a.png", width: 750, height: 1000, }); expect(Object.keys(p.attachments).sort()).toEqual(["11", "12", "13"]); expect(p.attachments["12"]).toEqual({ url: "https://blog.example.com/wp-content/uploads/b.png", width: null, height: null, }); }); test("password-protected posts are flagged", () => { const ex = parseWordpressExport( wxr(post({ id: "1", password: "hunter2" }) + post({ id: "2" })), ); expect(ex.posts.map((p) => p.passwordProtected)).toEqual([true, false]); }); test("rejects files that aren't WXR", () => { expect(() => parseWordpressExport("{}")).toThrow(/valid XML/); expect(() => parseWordpressExport(`x`), ).toThrow(/WXR/); }); });