summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rwxr-xr-xtests/cli.test.sh37
-rwxr-xr-xtests/conventional-commits.test.sh18
-rw-r--r--tests/helpers.test.mjs113
-rwxr-xr-xtests/run.sh5
4 files changed, 25 insertions, 148 deletions
diff --git a/tests/cli.test.sh b/tests/cli.test.sh
index d3f8d88..6aeef87 100755
--- a/tests/cli.test.sh
+++ b/tests/cli.test.sh
@@ -3,7 +3,9 @@
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
-CLI=(node "$ROOT/scripts/icloud-note.mjs")
+cd "$ROOT"
+cargo build -q
+CLI=("$ROOT/target/debug/icloud-note")
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
export ICLOUD_NOTES_CONFIG="$TMP/cfg"
@@ -13,14 +15,17 @@ fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok: $*"; }
# --- help ---
-for flag in help -h --help; do
- out="$("${CLI[@]}" "$flag" 2>&1)" || fail "help ($flag) exited non-zero"
- [[ "$out" == *"icloud-note create"* ]] || fail "help missing create ($flag)"
-done
-out="$("${CLI[@]}" help 2>&1)"
+out="$("${CLI[@]}" 2>&1)" || fail "no-args help failed"
+[[ "$out" == *"icloud-note create"* ]] || fail "help missing create: $out"
[[ "$out" == *"headless"* ]] || fail "help should say create/status are headless"
[[ "$out" != *"--headed"* ]] || fail "help must not advertise --headed"
[[ "$out" == *"$ICLOUD_NOTES_CONFIG"* ]] || fail "help should show ICLOUD_NOTES_CONFIG path"
+set +e
+out_h="$("${CLI[@]}" -h 2>&1)"
+code_h=$?
+set -e
+[[ "$code_h" -eq 0 ]] || fail "-h exited $code_h"
+[[ "$out_h" == *"create"* || "$out_h" == *"Create"* ]] || fail "-h missing create"
pass "help"
# --- unknown ---
@@ -29,7 +34,6 @@ err="$("${CLI[@]}" nosuchcmd 2>&1)"
code=$?
set -e
[[ "$code" -ne 0 ]] || fail "unknown command should fail"
-[[ "$err" == *"Unknown command"* ]] || fail "unknown command message"
pass "unknown command"
# --- create validation ---
@@ -38,10 +42,10 @@ err="$("${CLI[@]}" create 2>&1)"
code=$?
set -e
[[ "$code" -ne 0 ]] || fail "create without content should fail"
-[[ "$err" == *"Provide --title"* ]] || fail "create empty message"
+[[ "$err" == *"Provide --title"* ]] || fail "create empty message: $err"
pass "create requires content"
-# --- create dry-run (no browser) ---
+# --- create dry-run ---
out="$("${CLI[@]}" create --title "T" --body "hello" 2>&1)" || fail "create dry-run failed"
[[ "$out" == *'dry-run: would create note title="T"'* ]] || fail "create dry-run output: $out"
[[ "$out" == *"chars="* ]] || fail "create dry-run missing chars"
@@ -71,11 +75,10 @@ set -e
[[ "$err" == *"No saved session"* ]] || fail "status empty message: $err"
pass "status dry-run without session"
-mkdir -p "$ICLOUD_NOTES_CONFIG"
-echo '{"cookies":[]}' > "$ICLOUD_NOTES_CONFIG/storage-state.json"
-out="$("${CLI[@]}" status 2>&1)" || fail "status dry-run with state failed"
+mkdir -p "$ICLOUD_NOTES_CONFIG/browser-profile/Default"
+out="$("${CLI[@]}" status 2>&1)" || fail "status dry-run with profile failed"
[[ "$out" == *"dry-run status"* ]] || fail "status dry-run ok: $out"
-pass "status dry-run with session file"
+pass "status dry-run with session profile"
# --- login dry-run must not open a browser ---
set +e
@@ -86,7 +89,7 @@ set -e
[[ "$err" == *"dry-run"* ]] || fail "login dry-run message: $err"
pass "login dry-run refuses browser"
-# --- login without display (real path, no dry-run) ---
+# --- login without display ---
unset ICLOUD_NOTES_DRY_RUN
set +e
err="$(env -u DISPLAY -u WAYLAND_DISPLAY ICLOUD_NOTES_CONFIG="$ICLOUD_NOTES_CONFIG" \
@@ -97,13 +100,11 @@ set -e
[[ "$err" == *"graphical session"* ]] || fail "login DISPLAY message: $err"
pass "login requires graphical session"
-# --- setup script present ---
[[ -x "$ROOT/scripts/setup.sh" ]] || fail "setup.sh not executable"
pass "setup.sh executable"
-# --- skill / docs mention headless ---
rg -q 'headless' "$ROOT/SKILL.md" || fail "SKILL.md should mention headless"
-rg -q 'headless' "$ROOT/README.md" || fail "README.md should mention headless"
-pass "docs mention headless"
+rg -q 'Rust|cargo' "$ROOT/README.md" || fail "README should mention Rust"
+pass "docs"
echo "All cli tests passed."
diff --git a/tests/conventional-commits.test.sh b/tests/conventional-commits.test.sh
index 042b819..12c1f46 100755
--- a/tests/conventional-commits.test.sh
+++ b/tests/conventional-commits.test.sh
@@ -8,7 +8,6 @@ cd "$ROOT"
fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok: $*"; }
-# Same rule as scripts/lib/note-helpers.mjs isConventionalCommitSubject
RE='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-zA-Z0-9._/-]+\))?!?: .+'
mapfile -t SUBJECTS < <(git log --format=%s)
@@ -25,18 +24,9 @@ for s in "${SUBJECTS[@]}"; do
done
[[ "$bad" -eq 0 ]] || fail "one or more commits are not Conventional Commits"
-# Unit helper agrees with git history
-node --input-type=module -e '
-import { isConventionalCommitSubject } from "./scripts/lib/note-helpers.mjs";
-import { execSync } from "node:child_process";
-const subjects = execSync("git log --format=%s", { encoding: "utf8" })
- .trim().split("\n").filter(Boolean);
-for (const s of subjects) {
- if (!isConventionalCommitSubject(s)) {
- console.error("helper rejected:", s);
- process.exit(1);
- }
-}
-'
+# Cross-check with Rust helper unit tests
+out="$(cargo test -q --lib helpers::tests::conventional_subjects -- --exact 2>&1)"
+echo "$out"
+echo "$out" | rg -q '1 passed' || fail "Rust conventional_subjects test did not run/pass"
echo "All conventional-commit checks passed."
diff --git a/tests/helpers.test.mjs b/tests/helpers.test.mjs
deleted file mode 100644
index 38742d8..0000000
--- a/tests/helpers.test.mjs
+++ /dev/null
@@ -1,113 +0,0 @@
-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);
- });
-});
diff --git a/tests/run.sh b/tests/run.sh
index 6d845df..f34fd3e 100755
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -1,11 +1,10 @@
#!/usr/bin/env bash
-# Full test suite for iphone-notes.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
-echo "== helpers (node:test) =="
-node --test tests/helpers.test.mjs
+echo "== cargo test =="
+cargo test -q
echo "== cli =="
./tests/cli.test.sh