aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/files_test.go
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-15 13:48:31 +0200
committerDanilo M. <danix@danix.xyz>2026-09-15 13:48:31 +0200
commitdbecac7041605a5dc8dd88109f71ea00a5692faa (patch)
treec3ef95c8e30d6ee1bc6027cc61488db5a7e514b0 /internal/notify/files_test.go
parent79c064efe75aa201584a9d618cbde950574f0ca5 (diff)
downloadnotifyd-dbecac7041605a5dc8dd88109f71ea00a5692faa.tar.gz
notifyd-dbecac7041605a5dc8dd88109f71ea00a5692faa.zip
feat: publish the queue and history atomically
Both files are written whole through a temporary file and a rename, so a renderer never reads a half-written value. An empty queue is [] rather than null, because the renderer parses it as an array.
Diffstat (limited to 'internal/notify/files_test.go')
-rw-r--r--internal/notify/files_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/internal/notify/files_test.go b/internal/notify/files_test.go
new file mode 100644
index 0000000..b17a4f6
--- /dev/null
+++ b/internal/notify/files_test.go
@@ -0,0 +1,57 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// 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.
+
+package notify
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestPublishWritesBothFiles(t *testing.T) {
+ dir := t.TempDir()
+ live := []Popup{{ID: 1, App: "a", Urgency: Normal, Created: 1, Expires: 2}}
+ history := []Popup{{ID: 2, App: "b", Urgency: Low, Created: 3}}
+ if err := Publish(dir, live, history); err != nil {
+ t.Fatalf("Publish: %v", err)
+ }
+ var gotLive []Popup
+ data, err := os.ReadFile(filepath.Join(dir, "queue.json"))
+ if err != nil {
+ t.Fatalf("read queue: %v", err)
+ }
+ if err := json.Unmarshal(data, &gotLive); err != nil {
+ t.Fatalf("queue not JSON: %v", err)
+ }
+ if len(gotLive) != 1 || gotLive[0].ID != 1 {
+ t.Errorf("queue = %+v, want one id 1", gotLive)
+ }
+ if _, err := os.Stat(filepath.Join(dir, "history.json")); err != nil {
+ t.Errorf("history.json missing: %v", err)
+ }
+}
+
+func TestPublishEmptyIsAnEmptyArray(t *testing.T) {
+ dir := t.TempDir()
+ if err := Publish(dir, nil, nil); err != nil {
+ t.Fatalf("Publish: %v", err)
+ }
+ data, _ := os.ReadFile(filepath.Join(dir, "queue.json"))
+ var got []Popup
+ if err := json.Unmarshal(data, &got); err != nil {
+ t.Fatalf("empty queue not JSON array: %v (%s)", err, data)
+ }
+ if string(data) != "[]" {
+ t.Errorf("empty queue encoded as %q, want []", data)
+ }
+}