Files
HomeDash/admin-server/wp_inject_test.go

69 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"strings"
"testing"
)
// 模板里不含 NAVI-WALLPAPER 块时,applyWallpaperBlock 应正确注入;
// 已含旧块时应替换而非追加(否则壁纸规则会重复累积)。
func TestApplyWallpaperBlock_Injects(t *testing.T) {
base := "html[data-theme] body{color:red}"
got := applyWallpaperBlock(base, "/wallpapers/x.jpg", 0.35)
if !strings.Contains(got, wallpaperBegin) || !strings.Contains(got, wallpaperEnd) {
t.Fatalf("未注入标记块: %q", got)
}
if !strings.Contains(got, `url("/wallpapers/x.jpg")`) {
t.Fatalf("未注入壁纸 url: %q", got)
}
if n := strings.Count(got, wallpaperBegin); n != 1 {
t.Fatalf("标记块应只出现一次,实际 %d 次", n)
}
if !strings.Contains(got, "rgba(255,255,255,0.35)") {
t.Errorf("浅色遮罩透明度不对: %q", got)
}
// 深色遮罩应为 dim+0.15
if !strings.Contains(got, "rgba(6,12,24,0.50)") {
t.Errorf("深色遮罩应为 0.50(dim+0.15): %q", got)
}
}
func TestApplyWallpaperBlock_ReplacesOld(t *testing.T) {
old := "a{}\n\n" + wallpaperBegin + "\nOLD CONTENT\n" + wallpaperEnd + "\nb{}\n"
got := applyWallpaperBlock(old, "/wallpapers/new.jpg", 0.35)
if strings.Contains(got, "OLD CONTENT") {
t.Errorf("旧壁纸块未被替换: %q", got)
}
if n := strings.Count(got, wallpaperBegin); n != 1 {
t.Errorf("替换后标记块应只出现一次,实际 %d 次:\n%s", n, got)
}
if !strings.Contains(got, "/wallpapers/new.jpg") {
t.Errorf("新壁纸 url 未写入")
}
// 旧块前后的内容都要保留
if !strings.Contains(got, "a{}") || !strings.Contains(got, "b{}") {
t.Errorf("替换破坏了周边内容:\n%s", got)
}
}
func TestApplyWallpaperBlock_NoURL(t *testing.T) {
base := "a{}"
got := applyWallpaperBlock(base, "", 0.35)
if strings.Contains(got, wallpaperBegin) {
t.Errorf("url 为空时不应注入壁纸块: %q", got)
}
if strings.TrimSpace(got) != "a{}" {
t.Errorf("url 为空时应原样返回: %q", got)
}
}
func TestApplyWallpaperBlock_DimClamp(t *testing.T) {
got := applyWallpaperBlock("a{}", "/w.jpg", 5.0)
if strings.Contains(got, "rgba(255,255,255,5.00)") {
t.Errorf("dim 应被钳制到 0.9:\n%s", got)
}
if !strings.Contains(got, "0.90") {
t.Errorf("钳制后应写入 0.90:\n%s", got)
}
}