Files
remanso-jetstream/publish.test.ts
Julien Calixte 9f2a34554b feat: blob images
2026-02-08 15:12:41 +01:00

86 lines
2.6 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import { stripUnpublishedLinks, extractLocalImages } from "./publish";
describe("stripUnpublishedLinks", () => {
const isUnpublished = (href: string) =>
!href.startsWith("http://") && !href.startsWith("https://");
test("strips links to unpublished notes", () => {
const md = "Check out [my draft](draft-note) for details.";
expect(stripUnpublishedLinks(md, isUnpublished)).toBe(
"Check out my draft for details.",
);
});
test("keeps links to published URLs", () => {
const md = "Visit [Google](https://google.com) today.";
expect(stripUnpublishedLinks(md, isUnpublished)).toBe(md);
});
test("strips multiple unpublished links", () => {
const md = "See [note A](note-a) and [note B](note-b).";
expect(stripUnpublishedLinks(md, isUnpublished)).toBe(
"See note A and note B.",
);
});
test("handles mixed links", () => {
const md =
"Read [docs](https://docs.example.com) and [my note](local-note).";
expect(stripUnpublishedLinks(md, isUnpublished)).toBe(
"Read [docs](https://docs.example.com) and my note.",
);
});
test("does not strip image references", () => {
const md = "![alt text](./images/photo.png)";
expect(stripUnpublishedLinks(md, isUnpublished)).toBe(md);
});
test("handles empty link text", () => {
const md = "[](unpublished-ref)";
expect(stripUnpublishedLinks(md, isUnpublished)).toBe("");
});
});
describe("extractLocalImages", () => {
test("extracts local image paths", () => {
const md = "![photo](./images/photo.png)";
const result = extractLocalImages(md);
expect(result).toEqual([
{ full: "![photo](./images/photo.png)", alt: "photo", path: "./images/photo.png" },
]);
});
test("skips URL images", () => {
const md = "![logo](https://example.com/logo.png)";
expect(extractLocalImages(md)).toEqual([]);
});
test("extracts multiple local images", () => {
const md = `
![a](./a.png)
Some text
![b](../assets/b.jpg)
![c](https://cdn.example.com/c.webp)
`;
const result = extractLocalImages(md);
expect(result).toHaveLength(2);
expect(result[0].path).toBe("./a.png");
expect(result[1].path).toBe("../assets/b.jpg");
});
test("handles images with empty alt", () => {
const md = "![](./no-alt.png)";
const result = extractLocalImages(md);
expect(result).toEqual([
{ full: "![](./no-alt.png)", alt: "", path: "./no-alt.png" },
]);
});
test("handles http:// URLs (case insensitive)", () => {
const md = "![img](HTTP://example.com/img.png)";
expect(extractLocalImages(md)).toEqual([]);
});
});