diff options
| author | Christopher Warner <cwarner@kernelcode.com> | 2026-10-10 18:42:38 -0400 |
|---|---|---|
| committer | Christopher Warner <cwarner@kernelcode.com> | 2026-10-10 18:42:38 -0400 |
| commit | 78862ba855c6e66cac64a86c25dca6149d8fb2fd (patch) | |
| tree | 8259c5337ba7ef0e08ea25f720770ac79510573c /scripts/lib/note-helpers.mjs | |
| parent | 50b9b2b4c0db6a5e1d9dd511d062d99ae2e11dd3 (diff) | |
| download | iphone-notes-78862ba855c6e66cac64a86c25dca6149d8fb2fd.tar.gz iphone-notes-78862ba855c6e66cac64a86c25dca6149d8fb2fd.tar.bz2 iphone-notes-78862ba855c6e66cac64a86c25dca6149d8fb2fd.tar.xz iphone-notes-78862ba855c6e66cac64a86c25dca6149d8fb2fd.zip | |
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 <cursoragent@cursor.com>
Diffstat (limited to 'scripts/lib/note-helpers.mjs')
| -rw-r--r-- | scripts/lib/note-helpers.mjs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/scripts/lib/note-helpers.mjs b/scripts/lib/note-helpers.mjs new file mode 100644 index 0000000..5dd43a0 --- /dev/null +++ b/scripts/lib/note-helpers.mjs @@ -0,0 +1,88 @@ +/** + * Pure helpers for icloud-note (unit-tested; no Playwright). + */ + +/** Join title + body the way Apple Notes expects (first line = title). */ +export function formatNoteText(title, body) { + const t = title == null ? "" : String(title); + const b = body == null ? "" : String(body); + return [t, b].filter((s) => s.length > 0).join("\n\n"); +} + +/** True when a URL is clearly an Apple auth/sign-in page. */ +export function isAppleAuthUrl(url) { + return /idmsa\.apple\.com|appleid\.apple\.com/i.test(String(url || "")); +} + +/** + * Heuristic "logged out" detection from page signals (mirrors pageLooksLoggedOut). + * @param {{ url?: string, bodyText?: string, hasPasswordField?: boolean, hasSignInControl?: boolean, hasNotesApp?: boolean }} s + */ +export function looksLoggedOut(s = {}) { + if (isAppleAuthUrl(s.url)) return true; + const t = String(s.bodyText || "").slice(0, 4000); + if (/Sign in to iCloud/i.test(t)) return true; + if (s.hasPasswordField) return true; + if (s.hasSignInControl && !s.hasNotesApp) return true; + return false; +} + +/** Resolve XDG-style config paths (overridable via env for tests). */ +export function resolveConfigPaths(env = process.env, homedir = "") { + const home = + homedir || + env.HOME || + env.USERPROFILE || + "/tmp"; + const configDir = + env.ICLOUD_NOTES_CONFIG || `${home}/.config/icloud-notes`; + return { + configDir, + profileDir: `${configDir}/browser-profile`, + statePath: `${configDir}/storage-state.json`, + }; +} + +/** + * Parse create CLI flags (subset of node:util parseArgs shape). + * @returns {{ title: string, bodyFile?: string, body?: string, positionals: string[] }} + */ +export function parseCreateArgv(argv) { + const out = { title: "", body: undefined, bodyFile: undefined, positionals: [] }; + for (let i = 0; i < argv.length; i++) { + const a = argv[i]; + if (a === "--title") { + out.title = String(argv[++i] ?? ""); + } else if (a === "--body") { + out.body = String(argv[++i] ?? ""); + } else if (a === "--body-file") { + out.bodyFile = String(argv[++i] ?? ""); + } else if (a.startsWith("-")) { + throw new Error(`Unknown option: ${a}`); + } else { + out.positionals.push(a); + } + } + if (!out.title && out.positionals[0]) { + out.title = out.positionals[0]; + } + return out; +} + +/** Validate create has something to send. */ +export function requireCreateContent(title, body) { + const t = title == null ? "" : String(title); + const b = body == null ? "" : String(body).replace(/\s+$/, ""); + if (!t && !b) { + throw new Error("Provide --title and/or --body / --body-file / stdin"); + } + return { title: t, body: b }; +} + +/** Conventional Commits 1.0.0 subject line (first line of message). */ +export function isConventionalCommitSubject(subject) { + const line = String(subject || "").trim(); + return /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-zA-Z0-9._/-]+\))?!?: .+/.test( + line, + ); +} |
