1package config
2
3import (
4 "testing"
5
6 "github.com/bnema/dumber/internal/application/port"
7)
8
9func TestConfigDiffFormatter_FormatChangesAsDiff(t *testing.T) {
10 f := NewDiffFormatter()
11
12 t.Run("no changes", func(t *testing.T) {
13 got := f.FormatChangesAsDiff(nil)
14 want := noChangesMsg
15 if got != want {
16 t.Errorf("got %q, want %q", got, want)
17 }
18 })
19
20 t.Run("empty slice", func(t *testing.T) {
21 got := f.FormatChangesAsDiff([]port.KeyChange{})
22 want := noChangesMsg
23 if got != want {
24 t.Errorf("got %q, want %q", got, want)
25 }
26 })
27
28 t.Run("added change", func(t *testing.T) {
29 changes := []port.KeyChange{
30 {Type: port.KeyChangeAdded, NewKey: "update.enable", NewValue: "true"},
31 }
32 got := f.FormatChangesAsDiff(changes)
33 want := "Config migration changes:\n\n + update.enable = true\n"
34 if got != want {
35 t.Errorf("got %q, want %q", got, want)
36 }
37 })
38
39 t.Run("removed change", func(t *testing.T) {
40 changes := []port.KeyChange{
41 {Type: port.KeyChangeRemoved, OldKey: "old.key", OldValue: "42"},
42 }
43 got := f.FormatChangesAsDiff(changes)
44 want := "Config migration changes:\n\n - old.key = 42 (deprecated)\n"
45 if got != want {
46 t.Errorf("got %q, want %q", got, want)
47 }
48 })
49
50 t.Run("renamed change", func(t *testing.T) {
51 changes := []port.KeyChange{
52 {Type: port.KeyChangeRenamed, OldKey: "old.name", NewKey: "new.name", OldValue: "hello"},
53 }
54 got := f.FormatChangesAsDiff(changes)
55 want := "Config migration changes:\n\n ~ old.name -> new.name\n (value: hello)\n"
56 if got != want {
57 t.Errorf("got %q, want %q", got, want)
58 }
59 })
60
61 t.Run("all change types combined", func(t *testing.T) {
62 changes := []port.KeyChange{
63 {Type: port.KeyChangeAdded, NewKey: "feature.flag", NewValue: "false"},
64 {Type: port.KeyChangeRemoved, OldKey: "legacy.opt", OldValue: "1"},
65 {Type: port.KeyChangeRenamed, OldKey: "foo.bar", NewKey: "foo.baz", OldValue: "qux"},
66 }
67 got := f.FormatChangesAsDiff(changes)
68 want := "Config migration changes:\n\n" +
69 " + feature.flag = false\n" +
70 " - legacy.opt = 1 (deprecated)\n" +
71 " ~ foo.bar -> foo.baz\n" +
72 " (value: qux)\n"
73 if got != want {
74 t.Errorf("got %q, want %q", got, want)
75 }
76 })
77}