package main import ( "os" "path/filepath" "strings" "testing" ) // 未配置任何文案时应回落到内置默认值(不配置也照旧)。 func TestPageStrFallbacks(t *testing.T) { empty := map[string]any{} if got := pageStr(empty, "authTitle", defaultAuthTitle); got != defaultAuthTitle { t.Fatalf("空配置应回落默认,得到 %q", got) } info := map[string]any{"authTitle": " 内网入口 ", "heroTitle": ""} if got := pageStr(info, "authTitle", defaultAuthTitle); got != "内网入口" { t.Fatalf("应取配置值并去空白,得到 %q", got) } // 空串视为未配置 if got := pageStr(info, "heroTitle", defaultPortalHero); got != defaultPortalHero { t.Fatalf("空串应回落默认,得到 %q", got) } } // 认证页字段从 pageInfo 读取,缺失时回落。 func TestAuthPageInfoFallback(t *testing.T) { dir := t.TempDir() old := confFile confFile = filepath.Join(dir, "conf.yml") defer func() { confFile = old }() if err := os.WriteFile(confFile, []byte("pageInfo:\n authTitle: 我的门户\n authLogo: AB\n"), 0o644); err != nil { t.Fatal(err) } title, subtitle, logo, _, _ := authPageInfo() if title != "我的门户" || logo != "AB" { t.Fatalf("未读到配置值: %q %q", title, logo) } if subtitle != defaultAuthSubtitle { t.Fatalf("未配置的字段应回落默认,得到 %q", subtitle) } } // 数据卷里的整页覆盖优先于内置模板。 func TestPortalOverrideWins(t *testing.T) { dir := t.TempDir() old := confFile confFile = filepath.Join(dir, "conf.yml") defer func() { confFile = old }() if err := os.MkdirAll(filepath.Join(dir, "portal"), 0o755); err != nil { t.Fatal(err) } custom := "我的门户" if err := os.WriteFile(filepath.Join(dir, "portal", "index.html"), []byte(custom), 0o644); err != nil { t.Fatal(err) } if got := string(renderPortalPage()); !strings.Contains(got, "我的门户") { t.Fatalf("数据卷覆盖未生效: %q", got) } // 空白覆盖文件应被忽略(回落到内置渲染) if err := os.WriteFile(filepath.Join(dir, "portal", "index.html"), []byte(" \n"), 0o644); err != nil { t.Fatal(err) } if got := string(renderPortalPage()); strings.Contains(got, "我的门户") { t.Fatalf("空白覆盖文件应被忽略") } } // 数据卷里的卡片片段应替换标记之间的内容(改门户链接无需重建镜像)。 func TestCardsOverrideReplacesMarkedBlock(t *testing.T) { dir := t.TempDir() old := confFile defer func() { confFile = old }() confFile = filepath.Join(dir, "conf.yml") if err := os.MkdirAll(filepath.Join(dir, "portal"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "portal", "cards.html"), []byte("我的服务"), 0o644); err != nil { t.Fatal(err) } page := "
" + cardsStartMarker + "旧" + cardsEndMarker + "
" got := applyCardsOverride(page) if !strings.Contains(got, "mine.example.org") || strings.Contains(got, "old.example.com") { t.Fatalf("覆盖未生效: %s", got) } // 清空后回落内置卡片 if err := os.WriteFile(filepath.Join(dir, "portal", "cards.html"), []byte(" \n"), 0o644); err != nil { t.Fatal(err) } if got := applyCardsOverride(page); !strings.Contains(got, "old.example.com") { t.Fatalf("空白片段应回落内置卡片: %s", got) } }