;;; test-pearl-format.el --- Tests for org entry rendering -*- lexical-binding: t; -*- ;; Copyright (C) 2026 Craig Jennings ;; Author: Craig Jennings ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Tests for the org renderer: `pearl--format-issue-as-org-entry' (a ;; normalized issue plist -> heading + LINEAR-* drawer + body description), ;; `pearl--description-to-org-body' (the interim heading guard), and ;; `pearl--build-org-content'. Issues come from the shared fixtures via ;; `pearl--normalize-issue', so the renderer is exercised on the same ;; shapes production hands it. ;;; Code: (require 'test-bootstrap (expand-file-name "test-bootstrap.el")) (require 'testutil-fixtures (expand-file-name "testutil-fixtures.el")) (defmacro test-pearl--with-default-mapping (&rest body) "Run BODY. Kept as a thin wrapper: the renderer slugifies the state name now, so these tests no longer need a state mapping bound." (declare (indent 0)) `(progn ,@body)) (defun test-pearl--norm-full () "A normalized fully-populated issue." (pearl--normalize-issue (testutil-linear-fixture-issue-full))) (defun test-pearl--norm-bare () "A normalized issue with null/missing optional fields." (pearl--normalize-issue (testutil-linear-fixture-issue-null-fields))) ;;; format-issue-as-org-entry (ert-deftest test-pearl-format-issue-full-renders-heading-and-drawer () "A full issue renders the heading and the namespaced LINEAR-* drawer." (test-pearl--with-default-mapping (let ((out (pearl--format-issue-as-org-entry (test-pearl--norm-full)))) (should (string-match-p "^\\*\\* IN-PROGRESS \\[#B\\] ENG-42: Fix the Thing :@craig:bug:backend:$" out)) (should (string-match-p "^:LINEAR-ID: +uuid-1$" out)) (should (string-match-p "^:LINEAR-IDENTIFIER: +ENG-42$" out)) (should (string-match-p "^:LINEAR-STATE-NAME: +In Progress$" out)) (should (string-match-p "^:LINEAR-TEAM-NAME: +Engineering$" out)) (should (string-match-p "^:LINEAR-PROJECT-NAME: +Platform$" out)) (should (string-match-p "^:LINEAR-ASSIGNEE-NAME: +Craig$" out)) (should (string-match-p "^:LINEAR-LABELS: +\\[bug, backend\\]$" out)) (should (string-match-p "^:LINEAR-DESC-SHA256: +[0-9a-f]\\{64\\}$" out)) (should (string-match-p "^:LINEAR-TITLE-SHA256: +[0-9a-f]\\{64\\}$" out)) (should (string-match-p "^:END:$" out))))) ;;; synced baselines for structured fields (save-model v2) (ert-deftest test-pearl-format-issue-writes-synced-baselines () "The renderer writes a synced baseline per structured field, equal to its live value at fetch." (test-pearl--with-default-mapping (let ((out (pearl--format-issue-as-org-entry (test-pearl--norm-full)))) ;; priority: numeric baseline beside the cookie (should (string-match-p "^:LINEAR-PRIORITY: +2$" out)) ;; state: live id and synced baseline both present and equal (should (string-match-p "^:LINEAR-STATE-ID: +state-1$" out)) (should (string-match-p "^:LINEAR-STATE-ID-SYNCED: +state-1$" out)) ;; assignee: live id and synced baseline equal (should (string-match-p "^:LINEAR-ASSIGNEE-ID: +user-1$" out)) (should (string-match-p "^:LINEAR-ASSIGNEE-ID-SYNCED: +user-1$" out)) ;; labels: live id set and synced baseline equal, in fetched order (should (string-match-p "^:LINEAR-LABEL-IDS: +lbl-1 lbl-2$" out)) (should (string-match-p "^:LINEAR-LABEL-IDS-SYNCED: +lbl-1 lbl-2$" out))))) (ert-deftest test-pearl-format-issue-bare-empty-baselines () "Null/missing structured fields render empty id baselines without error." (test-pearl--with-default-mapping (let ((out (pearl--format-issue-as-org-entry (test-pearl--norm-bare)))) (should (string-match-p "^:LINEAR-LABEL-IDS: +$" out)) (should (string-match-p "^:LINEAR-LABEL-IDS-SYNCED: +$" out))))) (ert-deftest test-pearl-format-issue-description-in-body-not-property () "The description renders as body text, not a :DESCRIPTION: property." (test-pearl--with-default-mapping (let ((out (pearl--format-issue-as-org-entry (test-pearl--norm-full)))) (should-not (string-match-p ":DESCRIPTION:" out)) (should (string-match-p "Line one" out)) (should (string-match-p "Line two" out))))) (ert-deftest test-pearl-format-issue-bare-empty-optionals () "Null/missing optional fields render as empty values, and the body is empty." (test-pearl--with-default-mapping (let ((out (pearl--format-issue-as-org-entry (test-pearl--norm-bare)))) (should (string-match-p "^\\*\\* TODO ENG-7: Bare Issue$" out)) (should (string-match-p "^:LINEAR-PROJECT-NAME: +$" out)) (should (string-match-p "^:LINEAR-ASSIGNEE-NAME: +$" out)) (should (string-match-p "^:LINEAR-LABELS: +\\[\\]$" out)) ;; null description -> nothing after :END: (should (string-match-p ":END:\n\\'" out))))) (ert-deftest test-pearl-format-issue-strips-brackets-from-title () "Square brackets in the title are stripped so org parsing stays sane." (test-pearl--with-default-mapping (let ((out (pearl--format-issue-as-org-entry '(:id "u" :identifier "ENG-1" :title "Fix [URGENT] bug" :priority 1 :state (:name "Todo"))))) (should (string-match-p "^\\*\\* TODO \\[#A\\] ENG-1: Fix URGENT bug$" out)) ;; the title provenance hash is of the displayed (stripped, verbatim) title ;; without the identifier prefix, so a later no-op title sync matches the ;; heading and never clobbers brackets or pushes the prefix (should (string-match-p (format "^:LINEAR-TITLE-SHA256: +%s$" (secure-hash 'sha256 "Fix URGENT bug")) out))))) ;;; build-org-content (ert-deftest test-pearl-build-org-content-empty-issues-header-only () "With no issues the content is the file header plus the empty parent, no entries." (test-pearl--with-default-mapping (let ((out (pearl--build-org-content '()))) (should (string-match-p "^#\\+title:" out)) (should-not (string-match-p "^\\*\\* " out))))) (ert-deftest test-pearl-build-org-content-no-hardcoded-filetags () "The header carries no hardcoded =#+filetags= (a personal value used to leak in)." (test-pearl--with-default-mapping (let ((out (pearl--build-org-content '()))) (should-not (string-match-p "#\\+filetags" out)) (should-not (string-match-p "twai" out))))) (ert-deftest test-pearl-build-org-content-renders-view-parent-heading () "Issues nest under a single top-level heading named after the view, so they sort together (org-sort on the parent) instead of being orphan headings." (test-pearl--with-default-mapping (let ((out (pearl--build-org-content (list (test-pearl--norm-full)) '(:type filter :name "My open issues" :filter nil)))) (should (string-match-p "^\\* My open issues$" out)) ;; the parent precedes the issue, which renders one level deeper (should (< (string-match "^\\* My open issues$" out) (string-match "^\\*\\* IN-PROGRESS" out)))))) (ert-deftest test-pearl-build-org-content-startup-show2levels () "The page opens folded to the parent and issue headings; Comments (level 3) and all bodies stay folded, so a fresh view isn't a wall of open issues." (test-pearl--with-default-mapping (let ((out (pearl--build-org-content '()))) (should (string-match-p "^#\\+STARTUP: show2levels$" out)) (should-not (string-match-p "^#\\+STARTUP: show3levels$" out)) (should-not (string-match-p "^#\\+STARTUP: overview$" out))))) (ert-deftest test-pearl-build-org-content-no-shared-file-id () "The file header carries no hardcoded org :ID: drawer. Asserts against an :ID: property specifically rather than any :PROPERTIES: drawer: the folded help header legitimately carries a :VISIBILITY: drawer, so a blanket :PROPERTIES: check would catch that instead of an actual id leak." (test-pearl--with-default-mapping (let ((out (pearl--build-org-content '()))) (should-not (string-match-p "a12acb12" out)) (should-not (string-match-p "^:ID:" out))))) (ert-deftest test-pearl-build-org-content-includes-each-issue () "Each issue contributes one heading to the rendered content." (test-pearl--with-default-mapping (let ((out (pearl--build-org-content (list (test-pearl--norm-full) (test-pearl--norm-bare))))) (should (string-match-p "^\\*\\* IN-PROGRESS \\[#B\\] ENG-42: Fix the Thing :@craig:bug:backend:$" out)) (should (string-match-p "^\\*\\* TODO ENG-7: Bare Issue$" out))))) ;;; --restore-page-visibility (defun test-pearl--line-visible-p (re) "Non-nil when the line matching RE from point-min is not folded away." (save-excursion (goto-char (point-min)) (and (re-search-forward re nil t) (not (invisible-p (line-beginning-position)))))) (ert-deftest test-pearl-restore-page-visibility-folds-bodies-keeps-headings () "After a repopulation the page folds to headings: parent and issues stay visible while property drawers fold away." (test-pearl--with-default-mapping (let ((pearl-fold-after-update t)) (with-temp-buffer (insert (pearl--build-org-content (list (test-pearl--norm-full)))) (org-mode) (org-fold-show-all) (pearl--restore-page-visibility) (should (test-pearl--line-visible-p "^\\* ")) (should (test-pearl--line-visible-p "^\\*\\* IN-PROGRESS")) (should-not (test-pearl--line-visible-p "^:LINEAR-ID:")))))) (ert-deftest test-pearl-restore-page-visibility-noop-when-disabled () "With `pearl-fold-after-update' nil the buffer is left fully expanded." (test-pearl--with-default-mapping (let ((pearl-fold-after-update nil)) (with-temp-buffer (insert (pearl--build-org-content (list (test-pearl--norm-full)))) (org-mode) (org-fold-show-all) (pearl--restore-page-visibility) (should (test-pearl--line-visible-p "^:LINEAR-ID:")))))) ;;; --hide-or-tidy-drawers (optional org-tidy integration) ;; org-tidy isn't a test dependency, so declare its mode var special here; the ;; `let' bindings below then bind it dynamically where `bound-and-true-p' reads. (defvar org-tidy-mode) (ert-deftest test-pearl-hide-or-tidy-uses-org-tidy-when-active () "With org-tidy-mode on, drawer hiding re-tidies via `org-tidy-buffer'." (let ((tidied nil) (org-tidy-mode t)) (cl-letf (((symbol-function 'org-tidy-buffer) (lambda () (setq tidied t))) ((symbol-function 'pearl--hide-all-drawers) (lambda () (error "should not native-fold when org-tidy is on")))) (pearl--hide-or-tidy-drawers)) (should tidied))) (ert-deftest test-pearl-hide-or-tidy-folds-natively-when-inactive () "With org-tidy off, drawer hiding falls back to the native fold." (let ((folded nil) (org-tidy-mode nil)) (cl-letf (((symbol-function 'pearl--hide-all-drawers) (lambda () (setq folded t))) ((symbol-function 'org-tidy-buffer) (lambda () (error "should not tidy when org-tidy is off")))) (pearl--hide-or-tidy-drawers)) (should folded))) (provide 'test-pearl-format) ;;; test-pearl-format.el ends here