Add Certificates page and Traefik API frontend client

This commit is contained in:
backup 2026-09-02 12:42:52 -05:00
commit 7fc90524b5
40 changed files with 6585 additions and 359 deletions

View file

@ -0,0 +1,110 @@
package file
import (
"strings"
"testing"
)
func TestUnifiedDiff_Insertion(t *testing.T) {
old := "a\nb\nc"
new := "a\nx\nb\nc"
diff := UnifiedDiff("app.yml", old, new)
if !strings.Contains(diff, "+x") {
t.Fatalf("expected insertion of x, got %q", diff)
}
if !strings.Contains(diff, " a") || !strings.Contains(diff, " b") {
t.Fatalf("expected context lines, got %q", diff)
}
}
func TestUnifiedDiff_Deletion(t *testing.T) {
old := "a\nb\nc\nd"
new := "a\nc\nd"
diff := UnifiedDiff("app.yml", old, new)
if !strings.Contains(diff, "-b") {
t.Fatalf("expected deletion of b, got %q", diff)
}
}
func TestUnifiedDiff_Replacement(t *testing.T) {
old := "http:\n routers:\n r1:\n rule: \"Host(`a.com`)\"\n service: s1"
new := "http:\n routers:\n r1:\n rule: \"Host(`b.com`)\"\n service: s1"
diff := UnifiedDiff("app.yml", old, new)
if !strings.Contains(diff, "-") || !strings.Contains(diff, "+") {
t.Fatalf("expected replacement diff, got %q", diff)
}
if !strings.Contains(diff, "a.com") || !strings.Contains(diff, "b.com") {
t.Fatalf("expected both old and new rule in diff, got %q", diff)
}
}
func TestUnifiedDiff_MovedBlock(t *testing.T) {
old := "line1\nline2\nline3\nline4\nline5"
new := "line1\nline4\nline5\nline2\nline3"
diff := UnifiedDiff("app.yml", old, new)
// moved block should appear as deletion and insertion (difflib shows moved 4/5)
if !strings.Contains(diff, "-line4") || !strings.Contains(diff, "-line5") {
t.Fatalf("expected moved lines as deletion (4/5), got %q", diff)
}
if !strings.Contains(diff, "+line4") || !strings.Contains(diff, "+line5") {
t.Fatalf("expected moved lines as insertion (4/5), got %q", diff)
}
// also ensure diff is not empty and contains context
if !strings.Contains(diff, "line1") {
t.Fatalf("expected context line1, got %q", diff)
}
}
func TestUnifiedDiff_EmptyFiles(t *testing.T) {
diff := UnifiedDiff("app.yml", "", "")
if diff != "" {
t.Fatalf("empty both should be empty diff, got %q", diff)
}
}
func TestUnifiedDiff_NewFile(t *testing.T) {
old := ""
new := "http:\n routers:\n r:\n rule: \"Host(`x.com`)\""
diff := UnifiedDiff("new.yml", old, new)
if !strings.Contains(diff, "+http:") {
t.Fatalf("new file should show additions, got %q", diff)
}
if !strings.Contains(diff, "--- a/new.yml") || !strings.Contains(diff, "+++ b/new.yml") {
t.Fatalf("missing header, got %q", diff)
}
}
func TestUnifiedDiff_DeletedFile(t *testing.T) {
old := "http:\n routers:\n r:\n rule: \"Host(`x.com`)\""
new := ""
diff := UnifiedDiff("old.yml", old, new)
if !strings.Contains(diff, "-http:") {
t.Fatalf("deleted file should show deletions, got %q", diff)
}
}
func TestUnifiedDiff_RollbackDiff(t *testing.T) {
v1 := "http:\n routers:\n r1:\n rule: \"Host(`a.com`)\"\n service: s1"
v2 := "http:\n routers:\n r1:\n rule: \"Host(`b.com`)\"\n service: s1"
// Simulate apply v1 -> v2, then rollback v2 -> v1 diff should be reverse
diffForward := UnifiedDiff("app.yml", v1, v2)
diffRollback := UnifiedDiff("app.yml", v2, v1)
if diffForward == "" || diffRollback == "" {
t.Fatalf("both diffs should be non-empty")
}
if !strings.Contains(diffForward, "-") || !strings.Contains(diffRollback, "-") {
t.Fatalf("expected diffs to contain changes")
}
// Rollback diff should contain inverse
if !strings.Contains(diffRollback, "a.com") || !strings.Contains(diffRollback, "b.com") {
t.Fatalf("rollback diff missing expected, got %q", diffRollback)
}
}
func TestUnifiedDiff_Identical(t *testing.T) {
content := "http:\n routers:\n r: {rule: \"Host(`a.com`)\", service: s}"
diff := UnifiedDiff("app.yml", content, content)
if diff != "" {
t.Fatalf("identical should be empty, got %q", diff)
}
}