package main import ( "net/http" "net/http/httptest" "os" "strings" "testing" "time" "cmautobuy/admin/model" "cmautobuy/admin/repository" "cmautobuy/admin/service" ) func TestPaginationCSS_桌面右对齐且窄窗口不遮挡内容(t *testing.T) { content, err := os.ReadFile("static/css/app.css") if err != nil { t.Fatal(err) } css := string(content) statusbarRightAligned := `.statusbar { position: fixed; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: flex-end;` for _, want := range []string{ statusbarRightAligned, "flex: 0 1 auto", "text-align: right", "margin-left: 0", "flex: 0 0 auto", "@media (max-width: 760px)", "position: static", ".statusbar > span:first-child { flex-basis: 100%; }", } { if !strings.Contains(css, want) { t.Fatalf("分页布局 CSS 缺少 %q", want) } } } // 六个主页面都走一次真实路由和模板渲染。 // 这样模板字段写错或新增列漏接时,测试阶段就会失败,不必等人工点页面。 func TestMainPagesReturnOK(t *testing.T) { db := newMySQLTestDB(t) router, err := newRouter(db) if err != nil { t.Fatalf("组装路由失败: %v", err) } // 全新库的业务页必须先去初始化,不能匿名打开。 unauthenticated := httptest.NewRequest(http.MethodGet, "/shopee", nil) unauthenticatedResponse := httptest.NewRecorder() router.ServeHTTP(unauthenticatedResponse, unauthenticated) if unauthenticatedResponse.Code != http.StatusSeeOther || unauthenticatedResponse.Header().Get("Location") != "/setup" { t.Fatalf("全新库访问业务页应跳转 /setup,实际 %d %s", unauthenticatedResponse.Code, unauthenticatedResponse.Header().Get("Location")) } if err := service.SetupInitialAdmin(db, "admin", "test-password", "test-password", time.Now()); err != nil { t.Fatalf("准备测试管理员失败: %v", err) } token, adminUser, _, err := service.Login(db, "admin", "test-password", time.Now()) if err != nil { t.Fatalf("准备测试登录 Session 失败: %v", err) } addAuth := func(request *http.Request) { request.AddCookie(&http.Cookie{Name: "cmautobuy_session", Value: token}) } for _, path := range []string{"/shopee", "/pdd", "/syb", "/tasks", "/clients", "/users"} { t.Run(path, func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, path, nil) addAuth(request) response := httptest.NewRecorder() router.ServeHTTP(response, request) if response.Code != http.StatusOK { t.Fatalf("GET %s = %d,期望 200,响应:%s", path, response.Code, response.Body.String()) } body := response.Body.String() if !strings.Contains(body, `