From 78862ba855c6e66cac64a86c25dca6149d8fb2fd Mon Sep 17 00:00:00 2001 From: Christopher Warner Date: Sat, 10 Oct 2026 18:42:38 -0400 Subject: test: cover helpers, dry-run CLI, and conventional commits Extract pure note helpers for unit tests, add ICLOUD_NOTES_DRY_RUN so create and status can be exercised without Chromium, and gate git history on Conventional Commits 1.0.0 subjects. Co-authored-by: Cursor --- tests/helpers.test.mjs | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/helpers.test.mjs (limited to 'tests/helpers.test.mjs') diff --git a/tests/helpers.test.mjs b/tests/helpers.test.mjs new file mode 100644 index 0000000..38742d8 --- /dev/null +++ b/tests/helpers.test.mjs @@ -0,0 +1,113 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { + formatNoteText, + isAppleAuthUrl, + looksLoggedOut, + resolveConfigPaths, + parseCreateArgv, + requireCreateContent, + isConventionalCommitSubject, +} from "../scripts/lib/note-helpers.mjs"; + +describe("formatNoteText", () => { + it("joins title and body with a blank line", () => { + assert.equal(formatNoteText("Hi", "body"), "Hi\n\nbody"); + }); + it("title only", () => { + assert.equal(formatNoteText("Hi", ""), "Hi"); + }); + it("body only", () => { + assert.equal(formatNoteText("", "body"), "body"); + }); + it("ignores nullish", () => { + assert.equal(formatNoteText(null, "x"), "x"); + }); +}); + +describe("isAppleAuthUrl / looksLoggedOut", () => { + it("detects idmsa / appleid URLs", () => { + assert.equal(isAppleAuthUrl("https://idmsa.apple.com/appleauth/auth"), true); + assert.equal(isAppleAuthUrl("https://appleid.apple.com/"), true); + assert.equal(isAppleAuthUrl("https://www.icloud.com/notes/"), false); + }); + it("detects sign-in page signals", () => { + assert.equal( + looksLoggedOut({ bodyText: "Sign in to iCloud to continue" }), + true, + ); + assert.equal(looksLoggedOut({ hasPasswordField: true }), true); + assert.equal( + looksLoggedOut({ hasSignInControl: true, hasNotesApp: false }), + true, + ); + assert.equal( + looksLoggedOut({ hasSignInControl: true, hasNotesApp: true }), + false, + ); + assert.equal(looksLoggedOut({ url: "https://www.icloud.com/notes/" }), false); + }); +}); + +describe("resolveConfigPaths", () => { + it("uses ICLOUD_NOTES_CONFIG when set", () => { + const p = resolveConfigPaths({ ICLOUD_NOTES_CONFIG: "/tmp/icloud-test" }); + assert.equal(p.configDir, "/tmp/icloud-test"); + assert.equal(p.statePath, "/tmp/icloud-test/storage-state.json"); + assert.equal(p.profileDir, "/tmp/icloud-test/browser-profile"); + }); + it("defaults under homedir/.config/icloud-notes", () => { + const p = resolveConfigPaths({}, "/home/me"); + assert.equal(p.configDir, "/home/me/.config/icloud-notes"); + }); +}); + +describe("parseCreateArgv / requireCreateContent", () => { + it("parses flags", () => { + const a = parseCreateArgv(["--title", "T", "--body", "B"]); + assert.equal(a.title, "T"); + assert.equal(a.body, "B"); + }); + it("positional title", () => { + const a = parseCreateArgv(["MyTitle"]); + assert.equal(a.title, "MyTitle"); + }); + it("body-file flag", () => { + const a = parseCreateArgv(["--title", "T", "--body-file", "/tmp/x"]); + assert.equal(a.bodyFile, "/tmp/x"); + }); + it("rejects unknown options", () => { + assert.throws(() => parseCreateArgv(["--headed"]), /Unknown option/); + }); + it("requireCreateContent throws when empty", () => { + assert.throws(() => requireCreateContent("", ""), /Provide --title/); + }); + it("requireCreateContent trims body trailing whitespace", () => { + const r = requireCreateContent("T", "hi\n\n"); + assert.equal(r.body, "hi"); + }); +}); + +describe("isConventionalCommitSubject", () => { + it("accepts Conventional Commits subjects", () => { + assert.equal( + isConventionalCommitSubject( + "feat: add icloud.com/notes helper and agent skill", + ), + true, + ); + assert.equal( + isConventionalCommitSubject( + "fix: run create/status headless without a visible browser", + ), + true, + ); + assert.equal(isConventionalCommitSubject("feat(notes): add dry-run"), true); + assert.equal(isConventionalCommitSubject("feat!: breaking"), true); + }); + it("rejects non-conventional subjects", () => { + assert.equal(isConventionalCommitSubject("Added tests"), false); + assert.equal(isConventionalCommitSubject("feat add stuff"), false); + assert.equal(isConventionalCommitSubject("FIX: caps type"), false); + }); +}); -- cgit v1.2.3