From 0358b97a8188b42a72e70ead081bfa220cb31f27 Mon Sep 17 00:00:00 2001 From: edithatogo <15080672+edithatogo@users.noreply.github.com> Date: Fri, 21 Aug 2026 09:20:49 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20tests=20for=20draft=20URL?= =?UTF-8?q?=20resolution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/publish/draft-url.test.ts | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/publish/draft-url.test.ts diff --git a/src/publish/draft-url.test.ts b/src/publish/draft-url.test.ts new file mode 100644 index 0000000..6020981 --- /dev/null +++ b/src/publish/draft-url.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict"; +import { describe, it } from "vitest"; +import { resolveDraftEditorUrl } from "./draft-url.js"; + +describe("resolveDraftEditorUrl", () => { + it("returns the original url if draftId is undefined", () => { + assert.equal( + resolveDraftEditorUrl("https://example.substack.com/publish/post", undefined), + "https://example.substack.com/publish/post", + ); + }); + + it("returns empty string if draftId is undefined and draftUrl is empty", () => { + assert.equal(resolveDraftEditorUrl("", undefined), ""); + }); + + it("returns empty string if draftUrl is empty", () => { + assert.equal(resolveDraftEditorUrl("", "12345"), ""); + }); + + it("returns the original url if it already ends with the draftId", () => { + assert.equal( + resolveDraftEditorUrl("https://example.substack.com/publish/post/12345", "12345"), + "https://example.substack.com/publish/post/12345", + ); + }); + + it("returns the original url if it already ends with the draftId and has trailing slash", () => { + assert.equal( + resolveDraftEditorUrl("https://example.substack.com/publish/post/12345/", "12345"), + "https://example.substack.com/publish/post/12345/", + ); + }); + + it("replaces existing numeric draft ID with the new one", () => { + assert.equal( + resolveDraftEditorUrl("https://example.substack.com/publish/post/999", "12345"), + "https://example.substack.com/publish/post/12345", + ); + }); + + it("appends draftId if it is missing", () => { + assert.equal( + resolveDraftEditorUrl("https://example.substack.com/publish/post", "12345"), + "https://example.substack.com/publish/post/12345", + ); + }); + + it("handles urls with trailing slash", () => { + assert.equal( + resolveDraftEditorUrl("https://example.substack.com/publish/post/", "12345"), + "https://example.substack.com/publish/post/12345", + ); + }); + + it("preserves query parameters and hashes (catch block logic)", () => { + // URL without protocol to trigger the catch block + assert.equal( + resolveDraftEditorUrl("example.substack.com/publish/post?foo=bar#baz", "12345"), + "example.substack.com/publish/post/12345?foo=bar#baz", + ); + }); + + it("preserves query parameters and hashes when replacing draftId (catch block logic)", () => { + // URL without protocol to trigger the catch block + assert.equal( + resolveDraftEditorUrl("example.substack.com/publish/post/999?foo=bar#baz", "12345"), + "example.substack.com/publish/post/12345?foo=bar#baz", + ); + }); + + it("preserves query parameters and hashes when draftId is already correct (catch block logic)", () => { + // URL without protocol to trigger the catch block + assert.equal( + resolveDraftEditorUrl("example.substack.com/publish/post/12345?foo=bar#baz", "12345"), + "example.substack.com/publish/post/12345?foo=bar#baz", + ); + }); +});