import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import { collapseLinkedImagesToTextLinks, convertClipboardHtmlToMarkdown, convertHtmlIslandsInMarkdown, normalizePastedNoteMarkdown, resolveNoteClipboardPaste, serializeSafeHtmlImage, shouldInterceptResolvedNotePaste, shouldInsertClipboardTextAsMarkdown, } from "./noteClipboardPaste.ts"; const CATTY_PASTE = `--- Screenshot 2026-07-02 at 22 51 24 --- # 🔥 Catty Agent — Your IT Ops AI Partner > 🚀 **Boost your IT ops daily work with AI power.** Catty Agent is the built-in AI assistant that understands your servers, executes commands, and handles complex multi-host operations — all through natural conversation. ### 🔥 What can Catty Agent do? - 🚀 **Natural language server management** — just tell it what you need, no more memorizing commands - 🔥 **Real-time server diagnostics** — check status, inspect logs, monitor resources through conversation `; test("turndown converts pure html clipboard", () => { const html = `

Runbook

Restart sshd on prod.

shot `; const md = convertClipboardHtmlToMarkdown(html); assert.match(md, /^# Runbook/m); assert.match(md, /\*\*sshd\*\*/); assert.match(md, /\[docs\]\(https:\/\/example\.com\)/); assert.match(md, /!\[shot\]\(https:\/\/example\.com\/a\.png\)/); }); test("screenshot images keep width and height attributes", () => { const md = convertHtmlIslandsInMarkdown(CATTY_PASTE); assert.match(md, /^# 🔥 Catty Agent/m); assert.match( md, /]*src="https:\/\/github\.com\/user-attachments\/assets\/3116165d-623a-4d3a-a28a-914befb9b72d"/, ); assert.match(md, /width="3142"/); assert.match(md, /height="1764"/); assert.match(md, /alt="Screenshot 2026-07-02 at 22 51 24"/); assert.doesNotMatch(md, /\\#/); assert.doesNotMatch(md, / { assert.equal( serializeSafeHtmlImage({ src: "https://example.com/a.png", alt: "shot", }), "![shot](https://example.com/a.png)", ); assert.match( serializeSafeHtmlImage({ src: "https://example.com/a.png", alt: "shot", width: 3142, height: 1764, }), /]*width="3142"[^>]*height="1764"[^>]*\/>/, ); assert.match( serializeSafeHtmlImage({ src: "https://cdn.ko-fi.com/cdn/kofi3.png?v=2", alt: "Support on Ko-fi", width: 150, }), /width="150"/, ); }); test("linked badge images collapse to clean text links", () => { const source = [ "[![GitHub Release](https://img.shields.io/github/v/release/binaricat/Netcatty)](https://github.com/binaricat/Netcatty/releases/latest)", "", "[ ", "![Platform](https://img.shields.io/badge/Platform-macOS-blue)", " ](#)", "", "[", 'Support on Ko-fi', "](https://ko-fi.com/binaricat)", ].join("\n"); const md = collapseLinkedImagesToTextLinks(source); assert.match(md, /\[GitHub Release\]\(https:\/\/github\.com\/binaricat\/Netcatty\/releases\/latest\)/); assert.match(md, /\[Platform\]\(#\)/); assert.match(md, /\[Support on Ko-fi\]\(https:\/\/ko-fi\.com\/binaricat\)/); assert.doesNotMatch(md, /!\[GitHub Release\]/); assert.doesNotMatch(md, /^\s*\]\(/m); }); test("normalize removes orphan link closers but keeps image dimensions", () => { const messy = [ "Intro", "](https://example.com/orphan)", 'wide', "Done", ].join("\n"); const md = normalizePastedNoteMarkdown(messy); assert.doesNotMatch(md, /\]\(https:\/\/example\.com\/orphan\)/); assert.match(md, /src="https:\/\/example\.com\/w\.png"/); assert.match(md, /width="2000"/); assert.match(md, /height="1000"/); }); test("resolve pastes Catty-style mixed markdown+html with image sizes", () => { const payload = resolveNoteClipboardPaste({ plainText: CATTY_PASTE, htmlText: "", }); assert.ok(payload.kind === "html-converted" || payload.kind === "markdown"); assert.equal( shouldInterceptResolvedNotePaste({ editorMode: "edit", pasteInsideCodeBlock: false, payload, }), true, ); assert.match(payload.text, /^# 🔥 Catty Agent/m); assert.match(payload.text, /width="3142"/); assert.match(payload.text, /height="1764"/); }); test("repo README paste collapses shields badges without debris", () => { const readmeHead = readFileSync(new URL("../../README.md", import.meta.url), "utf8").slice(0, 2200); const payload = resolveNoteClipboardPaste({ plainText: readmeHead, htmlText: "" }); assert.ok(payload.text.length > 50); assert.doesNotMatch(payload.text, /^\s*\]\([^)\n]+\)\s*$/m); // Large screenshot keeps dimensions in source. assert.match(payload.text, /width="3142"/); assert.match(payload.text, /height="1764"/); }); test("resolve uses full turndown for browser StartFragment html", () => { const payload = resolveNoteClipboardPaste({ plainText: "flat text without structure", htmlText: `

From browser

Hello world

x `, }); assert.equal(payload.kind, "html-converted"); assert.match(payload.text, /^# From browser/m); assert.match(payload.text, /\*\*world\*\*/); assert.match(payload.text, /src="https:\/\/cdn\.example\.com\/x\.png"/); assert.match(payload.text, /width="2000"/); assert.match(payload.text, /height="1000"/); }); test("resolve uses structured plain markdown when html is absent", () => { const payload = resolveNoteClipboardPaste({ plainText: "# From .md file\n\n- item", htmlText: "", }); assert.equal(payload.kind, "markdown"); assert.match(payload.text, /# From \.md file/); }); test("plain unstructured text is not intercepted", () => { assert.equal(shouldInsertClipboardTextAsMarkdown("hello world"), false); const payload = resolveNoteClipboardPaste({ plainText: "hello world", htmlText: "", }); assert.equal(payload.kind, "plain"); assert.equal( shouldInterceptResolvedNotePaste({ editorMode: "edit", pasteInsideCodeBlock: false, payload, }), false, ); });