aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin-debug-show-log.el
blob: e90b2e96ed51c635a52c0aa91f7164131384bc86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
;;; test-wttrin-debug-show-log.el --- Tests for wttrin-debug-show-log -*- lexical-binding: t; -*-

;; Copyright (C) 2025-2026 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin-debug-show-log.  Verifies the *wttrin-debug-log*
;; buffer is created with the expected header, entry rendering, and the
;; reversed (oldest-first) ordering used for human reading.

;;; Code:

(require 'ert)
(require 'wttrin)
(require 'testutil-wttrin)

(require 'wttrin-debug
         (expand-file-name "wttrin-debug.el"
                           (file-name-directory (locate-library "wttrin"))))

;;; Setup and Teardown

(defun test-wttrin-debug-show-log-setup ()
  "Setup for show-log tests."
  (when (get-buffer "*wttrin-debug-log*")
    (kill-buffer "*wttrin-debug-log*")))

(defun test-wttrin-debug-show-log-teardown ()
  "Teardown for show-log tests."
  (when (get-buffer "*wttrin-debug-log*")
    (kill-buffer "*wttrin-debug-log*")))

;;; Normal Cases

(ert-deftest test-wttrin-debug-show-log-normal-creates-display-buffer ()
  "Show-log creates the *wttrin-debug-log* buffer."
  (test-wttrin-debug-show-log-setup)
  (unwind-protect
      (let ((wttrin--debug-log nil))
        (wttrin-debug-show-log)
        (should (get-buffer "*wttrin-debug-log*")))
    (test-wttrin-debug-show-log-teardown)))

(ert-deftest test-wttrin-debug-show-log-normal-header-shows-entry-count ()
  "Header reflects the number of entries currently in the log."
  (test-wttrin-debug-show-log-setup)
  (unwind-protect
      (let ((wttrin--debug-log '(("00:00:03.000" . "c")
                                 ("00:00:02.000" . "b")
                                 ("00:00:01.000" . "a"))))
        (wttrin-debug-show-log)
        (with-current-buffer "*wttrin-debug-log*"
          (should (string-match-p "Total entries: 3" (buffer-string)))))
    (test-wttrin-debug-show-log-teardown)))

(ert-deftest test-wttrin-debug-show-log-normal-renders-entries-with-timestamps ()
  "Each entry appears as [timestamp] message in the display."
  (test-wttrin-debug-show-log-setup)
  (unwind-protect
      (let ((wttrin--debug-log '(("12:34:56.789" . "hello world"))))
        (wttrin-debug-show-log)
        (with-current-buffer "*wttrin-debug-log*"
          (should (string-match-p "\\[12:34:56\\.789\\] hello world"
                                  (buffer-string)))))
    (test-wttrin-debug-show-log-teardown)))

(ert-deftest test-wttrin-debug-show-log-normal-entries-rendered-oldest-first ()
  "Storage is newest-first (push); display reverses to oldest-first for reading."
  (test-wttrin-debug-show-log-setup)
  (unwind-protect
      (let ((wttrin--debug-log '(("00:00:03.000" . "third")
                                 ("00:00:02.000" . "second")
                                 ("00:00:01.000" . "first"))))
        (wttrin-debug-show-log)
        (with-current-buffer "*wttrin-debug-log*"
          (let* ((contents (buffer-string))
                 (pos-first (string-match "first" contents))
                 (pos-second (string-match "second" contents))
                 (pos-third (string-match "third" contents)))
            (should pos-first)
            (should pos-second)
            (should pos-third)
            (should (< pos-first pos-second))
            (should (< pos-second pos-third)))))
    (test-wttrin-debug-show-log-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin-debug-show-log-boundary-empty-log-shows-placeholder ()
  "Empty log renders the \"No log entries yet\" placeholder."
  (test-wttrin-debug-show-log-setup)
  (unwind-protect
      (let ((wttrin--debug-log nil))
        (wttrin-debug-show-log)
        (with-current-buffer "*wttrin-debug-log*"
          (should (string-match-p "(No log entries yet)" (buffer-string)))
          (should (string-match-p "Total entries: 0" (buffer-string)))))
    (test-wttrin-debug-show-log-teardown)))

(provide 'test-wttrin-debug-show-log)
;;; test-wttrin-debug-show-log.el ends here