package context import "testing" // 主题作用域:/user 后台必须命中(否则设置页、题目管理页拿不到自己的主题), // 其余路径不得命中(提问箱页面由 question.Pager 按箱主注入,公开页保持站点默认)。 func TestIsThemeScope(t *testing.T) { cases := map[string]bool{ "/user": true, "/user/": true, "/user/profile": true, "/user/questions": true, "/user/votes/1/results": true, "/username": false, // 前缀相近但不是用户后台 "/_/admin6": false, "/_/admin6/share-card": false, "/": false, "/explore": false, "/vote/abc": false, "/api/v1/user/admin6": false, "/api/v1/user": false, "/deactivated": false, } for path, want := range cases { if got := isThemeScope(path); got != want { t.Errorf("isThemeScope(%q) = %v, want %v", path, got, want) } } } // 页面作用域:主题按页面分别摆装饰靠这个值区分。 // 重点是把「箱主页 / 单题页 / 题目管理页」分清楚 —— 三者路径前缀很像。 func TestPageScope(t *testing.T) { cases := map[string]string{ "/": "home", "/explore": "explore", "/explore/users": "explore", "/login": "auth", "/register": "auth", "/user": "profile", "/user/profile": "profile", "/user/questions": "admin", // 题目列表:信息密,装饰要退 "/user/votes/1/results": "admin", "/_/admin6": "box", "/_/admin6/": "box", "/_/admin6/tag/杂谈": "box", "/_/admin6/answer": "box", // 非数字段 → 不是单题页 "/_/admin6/123": "question", "/_/admin6/123/": "question", "/vote/abc123": "vote", "/username": "", "/_": "", "/deactivated": "", "/api/v1/user/admin6": "", "/api/v1/user/admin6/questions": "", } for path, want := range cases { if got := pageScope(path); got != want { t.Errorf("pageScope(%q) = %q, want %q", path, got, want) } } }