import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import delay from "delay";
import { render } from "ink-testing-library";
import { serialize as serializeAutomation } from "../sync/automations.js";
import { serialize } from "../sync/emails.js";
import { serialize as serializeSnippet } from "../sync/snippets.js";
import { writeSyncState } from "../sync/state.js";
import { jsonResponse, mockFetch } from "../test-helpers.js";
import Push from "./push.js";
describe("push", () => {
let tempDir: string;
let emailsDir: string;
let originalFetch: typeof fetch;
beforeEach(async () => {
tempDir = await mkdtemp(path.join(tmpdir(), "push-test-"));
emailsDir = path.join(tempDir, "emails");
originalFetch = globalThis.fetch;
await mkdir(emailsDir, { recursive: true });
await writeSyncState(tempDir, { syncedImages: {} });
await writeFile(
path.join(tempDir, "newsletter.json"),
JSON.stringify({ id: "nl_1", name: "Test" }),
);
});
afterEach(async () => {
globalThis.fetch = originalFetch;
if (tempDir) {
await rm(tempDir, { recursive: true, force: true });
}
});
function renderPush() {
render(
,
);
return delay(500);
}
test("should upload new images and replace relative paths with absolute URLs", async () => {
const mediaDir = path.join(tempDir, "media");
await mkdir(mediaDir, { recursive: true });
await writeFile(
path.join(emailsDir, "test-post.md"),
serialize({
id: "email_1",
subject: "Test Post",
slug: "test-post",
body: "Here is  in my post",
}),
);
await writeFile(
path.join(mediaDir, "photo.png"),
Buffer.from("real-png-data"),
);
const pushedEmails: any[] = [];
mockFetch(
async (request, url) => {
if (url.pathname === "/images" && request.method === "POST") {
const formData = await request.formData();
const file = formData.get("image") as File;
expect(file).toBeTruthy();
expect(file.name).toBe("photo.png");
return jsonResponse(
{
id: "img_uploaded",
image: "https://assets.buttondown.email/images/photo.png",
creation_date: "2025-01-01",
},
201,
);
}
},
async (request, url) => {
if (url.pathname.includes("/emails/") && request.method === "PATCH") {
const body = await request.json();
pushedEmails.push(body);
return jsonResponse({});
}
},
);
await renderPush();
expect(pushedEmails).toHaveLength(1);
expect(pushedEmails[0].body).toContain(
"https://assets.buttondown.email/images/photo.png",
);
expect(pushedEmails[0].body).not.toContain("../media/photo.png");
const state = JSON.parse(
await readFile(path.join(tempDir, ".buttondown.json"), "utf8"),
);
expect(state.syncedImages.img_uploaded).toEqual({
id: "img_uploaded",
localPath: "media/photo.png",
url: "https://assets.buttondown.email/images/photo.png",
filename: "photo.png",
});
});
test("should skip upload for already-synced images", async () => {
const mediaDir = path.join(tempDir, "media");
await mkdir(mediaDir, { recursive: true });
await writeFile(
path.join(emailsDir, "reuse-post.md"),
serialize({
id: "email_2",
subject: "Reuse Post",
slug: "reuse-post",
body: "Reusing  again",
}),
);
await writeFile(path.join(mediaDir, "photo.png"), Buffer.from("png-data"));
await writeSyncState(tempDir, {
syncedImages: {
img_existing: {
id: "img_existing",
localPath: path.join(tempDir, "media", "photo.png"),
url: "https://assets.buttondown.email/images/photo.png",
filename: "photo.png",
},
},
});
let imageUploadCount = 0;
const pushedEmails: any[] = [];
mockFetch(
(request, url) => {
if (url.pathname === "/images" && request.method === "POST") {
imageUploadCount++;
return jsonResponse(
{
id: "img_new",
image: "https://example.com/new.png",
creation_date: "2025-01-01",
},
201,
);
}
},
async (request, url) => {
if (url.pathname.includes("/emails/") && request.method === "PATCH") {
const body = await request.json();
pushedEmails.push(body);
return jsonResponse({});
}
},
);
await renderPush();
expect(imageUploadCount).toBe(0);
expect(pushedEmails).toHaveLength(1);
expect(pushedEmails[0].body).toContain(
"https://assets.buttondown.email/images/photo.png",
);
});
test("should not push unchanged emails", async () => {
await writeFile(
path.join(emailsDir, "unchanged.md"),
serialize({
id: "email_unchanged",
subject: "Unchanged Post",
slug: "unchanged",
body: "Same content as remote",
}),
);
await writeFile(
path.join(emailsDir, "changed.md"),
serialize({
id: "email_changed",
subject: "Changed Post",
slug: "changed",
body: "Updated locally",
}),
);
const pushedEmailIds: string[] = [];
mockFetch(
(request, url) => {
if (url.pathname === "/emails" && request.method === "GET") {
return jsonResponse({
results: [
{
id: "email_unchanged",
subject: "Unchanged Post",
slug: "unchanged",
body: "Same content as remote",
},
{
id: "email_changed",
subject: "Changed Post",
slug: "changed",
body: "Old content from remote",
},
],
count: 2,
});
}
},
(request, url) => {
if (url.pathname.includes("/emails/") && request.method === "PATCH") {
const id = url.pathname.split("/emails/")[1];
pushedEmailIds.push(id);
return jsonResponse({});
}
},
);
await renderPush();
expect(pushedEmailIds).toHaveLength(1);
expect(pushedEmailIds[0]).toBe("email_changed");
});
test("should preserve the remote editor mode when the local file has none", async () => {
await writeFile(
path.join(emailsDir, "plain-post.md"),
serialize({
id: "email_3",
subject: "Plain Post",
slug: "plain-post",
body: "Just text, no images here.",
}),
);
const pushedEmails: any[] = [];
mockFetch(
(request, url) => {
if (url.pathname === "/emails" && request.method === "GET") {
return jsonResponse({
results: [
{
id: "email_3",
subject: "Plain Post",
slug: "plain-post",
body: "Old content.",
},
],
count: 1,
});
}
},
async (request, url) => {
if (url.pathname.includes("/emails/") && request.method === "PATCH") {
const body = await request.json();
pushedEmails.push(body);
return jsonResponse({});
}
},
);
await renderPush();
expect(pushedEmails).toHaveLength(1);
expect(pushedEmails[0].body).toBe(
"Just text, no images here.",
);
});
test("should push the declared editor mode from frontmatter", async () => {
await writeFile(
path.join(emailsDir, "fancy-post.md"),
serialize({
id: "email_4",
subject: "Fancy Post",
slug: "fancy-post",
body: "
HTML content
",
}),
);
const pushedEmails: any[] = [];
mockFetch(async (request, url) => {
if (url.pathname.includes("/emails/") && request.method === "PATCH") {
const body = await request.json();
pushedEmails.push(body);
return jsonResponse({});
}
});
await renderPush();
expect(pushedEmails).toHaveLength(1);
expect(pushedEmails[0].body).toBe(
"HTML content
",
);
});
test("should not push unchanged automations", async () => {
const automationsDir = path.join(tempDir, "automations");
await mkdir(automationsDir, { recursive: true });
const unchangedAutomation = {
id: "auto_unchanged",
name: "Unchanged Automation",
status: "active" as const,
trigger: "subscriber.created" as const,
actions: [
{ type: "send_email" as const, metadata: {} as Record },
],
filters: { filters: [], groups: [], predicate: "and" as const },
metadata: {} as Record,
};
const changedAutomation = {
id: "auto_changed",
name: "Changed Automation",
status: "active" as const,
trigger: "subscriber.created" as const,
actions: [
{ type: "send_email" as const, metadata: {} as Record },
],
filters: { filters: [], groups: [], predicate: "and" as const },
metadata: {} as Record,
};
await writeFile(
path.join(automationsDir, "unchanged-automation.json"),
serializeAutomation(unchangedAutomation),
);
await writeFile(
path.join(automationsDir, "changed-automation.json"),
serializeAutomation({
...changedAutomation,
status: "inactive" as const,
}),
);
const patchedAutomationIds: string[] = [];
mockFetch(
(request, url) => {
if (url.pathname === "/automations" && request.method === "GET") {
return jsonResponse({
results: [unchangedAutomation, changedAutomation],
count: 2,
});
}
},
async (request, url) => {
if (
url.pathname.includes("/automations/") &&
request.method === "PATCH"
) {
const id = url.pathname.split("/automations/")[1];
patchedAutomationIds.push(id);
return jsonResponse({});
}
},
);
await renderPush();
expect(patchedAutomationIds).toHaveLength(1);
expect(patchedAutomationIds[0]).toBe("auto_changed");
});
test("should not push unchanged snippets", async () => {
const snippetsDir = path.join(tempDir, "snippets");
await mkdir(snippetsDir, { recursive: true });
const unchangedSnippet = {
id: "snip_unchanged",
name: "Unchanged Snippet",
identifier: "unchanged-snippet",
content: "Same content as remote",
};
const changedSnippet = {
id: "snip_changed",
name: "Changed Snippet",
identifier: "changed-snippet",
content: "Old content from remote",
};
await writeFile(
path.join(snippetsDir, "unchanged-snippet.md"),
serializeSnippet(unchangedSnippet),
);
await writeFile(
path.join(snippetsDir, "changed-snippet.md"),
serializeSnippet({
...changedSnippet,
content: "Updated locally",
}),
);
const patchedSnippetIds: string[] = [];
mockFetch(
(request, url) => {
if (url.pathname === "/snippets" && request.method === "GET") {
return jsonResponse({
results: [unchangedSnippet, changedSnippet],
count: 2,
});
}
},
async (request, url) => {
if (url.pathname.includes("/snippets/") && request.method === "PATCH") {
const id = url.pathname.split("/snippets/")[1];
patchedSnippetIds.push(id);
return jsonResponse({});
}
},
);
await renderPush();
expect(patchedSnippetIds).toHaveLength(1);
expect(patchedSnippetIds[0]).toBe("snip_changed");
});
test("should not push unchanged newsletter", async () => {
const newsletter = { id: "nl_1", name: "Test" };
await writeFile(
path.join(tempDir, "newsletter.json"),
JSON.stringify(newsletter),
);
let newsletterPatched = false;
mockFetch((request, url) => {
if (url.pathname === "/newsletters" && request.method === "GET") {
return jsonResponse({
results: [newsletter],
count: 1,
});
}
if (
url.pathname.includes("/newsletters/") &&
request.method === "PATCH"
) {
newsletterPatched = true;
return jsonResponse({});
}
});
await renderPush();
expect(newsletterPatched).toBe(false);
});
});