import { describe, it, expect } from "vitest"; import { parse } from "./parser"; import { defaultTheme } from "./default-theme"; describe("content parser", () => { it("returns doc with empty paragraph if no content is provided", () => { const content = parse("", [], defaultTheme); expect(content).toEqual({ type: "doc", content: [{ type: "paragraph", content: [] }], }); }); it("properly maps new links to hard breaks", () => { const content = parse("Hello\nWorld\n\n\n\r\n\rTest", [], defaultTheme); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [{ type: "text", text: "Hello" }] }, { type: "paragraph" }, { type: "paragraph", content: [{ type: "text", text: "World" }] }, { type: "paragraph" }, { type: "paragraph" }, { type: "paragraph" }, { type: "paragraph" }, { type: "paragraph" }, { type: "paragraph", content: [{ type: "text", text: "Test" }] }, ], }); }); it("returns doc with text pargraph if only text is provided", () => { const content = parse("Hello šŸŒŽ", [], defaultTheme); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [{ type: "text", text: "Hello šŸŒŽ" }], }, ], }); }); it("maps ens references to mentions", () => { const content = parse( "Hello @0x1234567890123456789012345678901234567890", [ { type: "ens", address: "0x1234567890123456789012345678901234567890", name: "John Doe", avatarUrl: null, url: "", position: { start: 6, end: 49, }, }, ], defaultTheme, ); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "Hello " }, { type: "mention", attrs: { type: "ens", address: "0x1234567890123456789012345678901234567890", name: "John Doe", url: "", avatarUrl: null, value: "0x1234567890123456789012345678901234567890", }, }, ], }, ], }); }); it("maps farcaster references to mentions", () => { const content = parse( "Hello @0x1234567890123456789012345678901234567890", [ { type: "farcaster", address: "0x1234567890123456789012345678901234567890", fid: 1234567890, pfpUrl: null, displayName: null, url: "", username: "username", fname: "username.fcast.id", position: { start: 6, end: 49, }, }, ], defaultTheme, ); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "Hello " }, { type: "mention", attrs: { type: "farcaster", address: "0x1234567890123456789012345678901234567890", fid: 1234567890, fname: "username.fcast.id", displayName: null, username: "username", pfpUrl: null, url: "", value: "0x1234567890123456789012345678901234567890", }, }, ], }, ], }); }); it("maps erc20 references to mentions", () => { const content = parse( "Hello $0x1234567890123456789012345678901234567890", [ { type: "erc20", address: "0x1234567890123456789012345678901234567890", name: "John Doe", symbol: "JDOE", chainId: 1, position: { start: 6, end: 49, }, logoURI: null, decimals: 18, chains: [ { chainId: 1, caip: "eip155:1/erc20:0x1234567890123456789012345678901234567890", }, ], }, ], defaultTheme, ); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "Hello " }, { type: "mention", attrs: { type: "erc20", address: "0x1234567890123456789012345678901234567890", name: "John Doe", symbol: "JDOE", chainId: 1, caip19: "eip155:1/erc20:0x1234567890123456789012345678901234567890", decimals: 18, logoURI: null, value: "eip155:1/erc20:0x1234567890123456789012345678901234567890", }, }, ], }, ], }); }); it("maps urls to links", () => { const content = parse( "šŸŒŽ https://example.com/test šŸ’» nehehe http://localhost:3000", [], defaultTheme, ); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "šŸŒŽ " }, { type: "text", marks: [ { type: "link", attrs: { href: "https://example.com/test", class: "cursor-pointer underline", rel: "noopener noreferrer", target: "_blank", }, }, ], text: "https://example.com/test", }, { type: "text", text: " šŸ’» nehehe " }, { type: "text", marks: [ { type: "link", attrs: { href: "http://localhost:3000", class: "cursor-pointer underline", rel: "noopener noreferrer", target: "_blank", }, }, ], text: "http://localhost:3000", }, ], }, ], }); }); it("parses unicode characters", () => { const content = parse( "šŸ‘€ what is šŸŽ¶ luc.eth this $USDC šŸ’» @0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d.", [ { type: "ens", address: "0x225f137127d9067788314bc7fcc1f36746a3c3B5", name: "luc.eth", avatarUrl: "https://ipfs.io/ipfs/bafkreifnrjhkl7ccr2ifwn2n7ap6dh2way25a6w5x2szegvj5pt4b5nvfu", url: "https://app.ens.domains/luc.eth", position: { start: 14, end: 21, }, }, { type: "erc20", address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", name: "USD Coin", symbol: "USDC", logoURI: "https://tokens.1inch.io/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png", decimals: 18, chainId: 1, chains: [ { chainId: 1, caip: "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", }, ], position: { start: 27, end: 32, }, }, { type: "farcaster", address: "0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", fid: 341794, fname: "mskr.fcast.id", url: "https://farcaster.xyz/mskr", username: "mskr", displayName: "mskr", pfpUrl: "https://i.imgur.com/DyoLsDd.jpg", position: { start: 38, end: 81, }, }, ], defaultTheme, ); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "šŸ‘€ what is šŸŽ¶ " }, { type: "mention", attrs: { type: "ens", address: "0x225f137127d9067788314bc7fcc1f36746a3c3B5", name: "luc.eth", url: "https://app.ens.domains/luc.eth", avatarUrl: "https://ipfs.io/ipfs/bafkreifnrjhkl7ccr2ifwn2n7ap6dh2way25a6w5x2szegvj5pt4b5nvfu", value: "0x225f137127d9067788314bc7fcc1f36746a3c3B5", }, }, { type: "text", text: " this " }, { type: "mention", attrs: { type: "erc20", address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", name: "USD Coin", symbol: "USDC", chainId: 1, decimals: 18, logoURI: "https://tokens.1inch.io/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png", caip19: "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", value: "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", }, }, { type: "text", text: " šŸ’» " }, { type: "mention", attrs: { type: "farcaster", address: "0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", fid: 341794, fname: "mskr.fcast.id", displayName: "mskr", username: "mskr", pfpUrl: "https://i.imgur.com/DyoLsDd.jpg", url: "https://farcaster.xyz/mskr", value: "0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", }, }, { type: "text", text: "." }, ], }, ], }); }); it("correctly parses complex text with unicode characters", () => { const content = parse( "Hello šŸŒŽ @0x1234567890123456789012345678901234567890 šŸ’»\n\nhttps://example.com\nā¤ļø @0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", [ { type: "ens", address: "0x1234567890123456789012345678901234567890", name: "John Doe", avatarUrl: null, url: "", position: { start: 9, end: 52, }, }, { type: "farcaster", address: "0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", fid: 341794, fname: "mskr.fcast.id", url: "https://farcaster.xyz/mskr", username: "mskr", displayName: "mskr", pfpUrl: "https://i.imgur.com/DyoLsDd.jpg", position: { start: 80, end: 123, }, }, ], defaultTheme, ); expect(content).toEqual({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "Hello šŸŒŽ " }, { type: "mention", attrs: { type: "ens", address: "0x1234567890123456789012345678901234567890", name: "John Doe", url: "", avatarUrl: null, value: "0x1234567890123456789012345678901234567890", }, }, { type: "text", text: " šŸ’»" }, ], }, { type: "paragraph" }, { type: "paragraph" }, { type: "paragraph", content: [ { marks: [ { type: "link", attrs: { href: "https://example.com", class: "cursor-pointer underline", rel: "noopener noreferrer", target: "_blank", }, }, ], type: "text", text: "https://example.com", }, ], }, { type: "paragraph" }, { type: "paragraph", content: [ { type: "text", text: "ā¤ļø " }, { type: "mention", attrs: { type: "farcaster", address: "0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", fid: 341794, fname: "mskr.fcast.id", displayName: "mskr", username: "mskr", pfpUrl: "https://i.imgur.com/DyoLsDd.jpg", url: "https://farcaster.xyz/mskr", value: "0x78397D9D185D3a57D01213CBe3Ec1EbAC3EEc77d", }, }, ], }, ], }); }); });