main_test.go 原为单行 import "testing",补 import 的替换锚点没命中, 导致 os/filepath 未导入、history-server 编译失败(承接上一提交的坏状态)。 本次改为块形式导入,go vet 与 go test 均通过。
48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package main
|
||
|
||
import (
|
||
"os"
|
||
"path/filepath"
|
||
"testing"
|
||
)
|
||
|
||
func TestFriendlyHistoryTitle(t *testing.T) {
|
||
cases := []struct {
|
||
name, title, rawURL, want string
|
||
}{
|
||
{name: "real title", title: "Docker Documentation", rawURL: "https://www.docker.com/docs/", want: "Docker Documentation"},
|
||
{name: "missing title", rawURL: "https://www.docker.com/", want: "docker.com"},
|
||
{name: "url title", title: "https://www.docker.com/", rawURL: "https://www.docker.com/", want: "docker.com"},
|
||
{name: "http url title", title: "http://example.org/a", rawURL: "http://example.org/a", want: "example.org"},
|
||
{name: "invalid fallback", rawURL: "://", want: "历史站点"},
|
||
}
|
||
for _, tc := range cases {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
if got := friendlyHistoryTitle(tc.title, tc.rawURL); got != tc.want {
|
||
t.Fatalf("friendlyHistoryTitle(%q, %q) = %q; want %q", tc.title, tc.rawURL, got, tc.want)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// 导航点击计数独立落盘,且不影响浏览器历史。
|
||
func TestNavVisitCounting(t *testing.T) {
|
||
dir := t.TempDir()
|
||
oldd := dataDir
|
||
dataDir = dir
|
||
defer func() { dataDir = oldd }()
|
||
if n := bumpNavVisit("a.example.com"); n != 1 {
|
||
t.Fatalf("首次应为 1,得到 %d", n)
|
||
}
|
||
if n := bumpNavVisit("a.example.com"); n != 2 {
|
||
t.Fatalf("第二次应为 2,得到 %d", n)
|
||
}
|
||
m := loadNavVisits()
|
||
if m["a.example.com"] != 2 || len(m) != 1 {
|
||
t.Fatalf("落盘结果不对: %v", m)
|
||
}
|
||
if _, err := os.Stat(filepath.Join(dir, "nav-visits.json")); err != nil {
|
||
t.Fatalf("计数文件未生成: %v", err)
|
||
}
|
||
}
|