aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/files_test.go
blob: b17a4f63dcfbae841cd9ed4e513be7127ffecf75 (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
// 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)
	}
}