ScuttleBot

scuttlebot / internal / config / history_test.go
Blame History Raw 105 lines
1
package config
2
3
import (
4
"os"
5
"path/filepath"
6
"testing"
7
"time"
8
)
9
10
func TestSnapshotAndPrune(t *testing.T) {
11
dir := t.TempDir()
12
histDir := filepath.Join(dir, "history")
13
configPath := filepath.Join(dir, "scuttlebot.yaml")
14
15
// No-op when config file doesn't exist yet.
16
if err := SnapshotConfig(histDir, configPath); err != nil {
17
t.Fatalf("SnapshotConfig (no file): %v", err)
18
}
19
20
// Write a config file and snapshot it.
21
if err := os.WriteFile(configPath, []byte("bridge:\n enabled: true\n"), 0o600); err != nil {
22
t.Fatal(err)
23
}
24
if err := SnapshotConfig(histDir, configPath); err != nil {
25
t.Fatalf("SnapshotConfig: %v", err)
26
}
27
28
entries, err := ListHistory(histDir, "scuttlebot.yaml")
29
if err != nil {
30
t.Fatal(err)
31
}
32
if len(entries) != 1 {
33
t.Fatalf("want 1 snapshot, got %d", len(entries))
34
}
35
if entries[0].Size == 0 {
36
t.Error("snapshot size should be non-zero")
37
}
38
if entries[0].Timestamp.IsZero() {
39
t.Error("snapshot timestamp should be set")
40
}
41
}
42
43
func TestPruneHistory(t *testing.T) {
44
dir := t.TempDir()
45
base := "scuttlebot.yaml"
46
keep := 3
47
48
// Write 5 snapshot files with distinct timestamps.
49
for i := range 5 {
50
stamp := time.Now().Add(time.Duration(i) * time.Second).Format(historyTimestampFormat)
51
name := filepath.Join(dir, base+"."+stamp)
52
if err := os.WriteFile(name, []byte("v"), 0o600); err != nil {
53
t.Fatal(err)
54
}
55
// Ensure distinct mtime ordering.
56
time.Sleep(2 * time.Millisecond)
57
}
58
59
if err := PruneHistory(dir, base, keep); err != nil {
60
t.Fatal(err)
61
}
62
63
entries, err := ListHistory(dir, base)
64
if err != nil {
65
t.Fatal(err)
66
}
67
if len(entries) != keep {
68
t.Errorf("want %d entries after prune, got %d", keep, len(entries))
69
}
70
}
71
72
func TestConfigSaveRoundTrip(t *testing.T) {
73
dir := t.TempDir()
74
path := filepath.Join(dir, "scuttlebot.yaml")
75
76
var cfg Config
77
cfg.Defaults()
78
cfg.Topology.Channels = []StaticChannelConfig{
79
{Name: "#general", Topic: "Fleet coordination", Autojoin: []string{"bridge"}},
80
}
81
cfg.Topology.Types = []ChannelTypeConfig{
82
{Name: "task", Prefix: "task.", Ephemeral: true, TTL: Duration{72 * time.Hour}},
83
}
84
85
if err := cfg.Save(path); err != nil {
86
t.Fatalf("Save: %v", err)
87
}
88
89
var loaded Config
90
loaded.Defaults()
91
if err := loaded.LoadFile(path); err != nil {
92
t.Fatalf("LoadFile: %v", err)
93
}
94
95
if len(loaded.Topology.Channels) != 1 || loaded.Topology.Channels[0].Name != "#general" {
96
t.Errorf("topology channels not round-tripped: %+v", loaded.Topology.Channels)
97
}
98
if len(loaded.Topology.Types) != 1 || loaded.Topology.Types[0].Name != "task" {
99
t.Errorf("topology types not round-tripped: %+v", loaded.Topology.Types)
100
}
101
if loaded.Topology.Types[0].TTL.Duration != 72*time.Hour {
102
t.Errorf("TTL = %v, want 72h", loaded.Topology.Types[0].TTL.Duration)
103
}
104
}
105

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button