2026-08-15 08:59:40 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-15 15:56:16 +08:00
|
|
|
"bytes"
|
2026-08-15 08:59:40 +08:00
|
|
|
"html/template"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestInnerCodeTemplate_独立导航与安全表单(t *testing.T) {
|
|
|
|
|
if _, err := template.ParseFS(templateFS, "templates/*/*.html"); err != nil {
|
|
|
|
|
t.Fatalf("模板必须可解析: %v", err)
|
|
|
|
|
}
|
|
|
|
|
header, err := os.ReadFile("templates/partials/header.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
headerText := string(header)
|
|
|
|
|
clientIndex := strings.Index(headerText, `href="/clients"`)
|
|
|
|
|
innerCodeIndex := strings.Index(headerText, `href="/inner-codes"`)
|
|
|
|
|
if clientIndex < 0 || innerCodeIndex < 0 || innerCodeIndex < clientIndex {
|
|
|
|
|
t.Fatal("档口入库码导航必须紧跟在客户端列表之后")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
raw, err := os.ReadFile("templates/inner_code/list.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
page := string(raw)
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`action="/inner-codes/import"`, `action="/inner-codes/match"`,
|
2026-08-15 10:49:28 +08:00
|
|
|
`action="/inner-codes/apply"`, `action="/inner-codes/recheck"`, `action="/inner-codes/delete"`,
|
2026-08-15 08:59:40 +08:00
|
|
|
`name="csrf_token"`, `data-check-all`, `data-inner-code-apply-open`,
|
2026-08-15 09:41:45 +08:00
|
|
|
`data-need-checked="inner-code-match"`, `data-inner-code-id="{{.ID}}"`,
|
2026-08-15 10:49:28 +08:00
|
|
|
`data-need-checked="inner-code-delete"`, `data-confirm-delete-action="inner-code-delete"`,
|
|
|
|
|
`inner-code-delete {{if .CanMatch}}inner-code-match{{end}}{{if .CanApply}} inner-code-apply`,
|
|
|
|
|
`删除不会撤销顺运宝已写入的快递单号`, `匹配已选`, `aria-label="档口入库码记录列表"`,
|
2026-08-15 15:56:16 +08:00
|
|
|
`data-auto-dismiss-toast data-transient-feedback`, `role="alertdialog"`,
|
|
|
|
|
`id="inner-code-feedback-error-modal" data-auto-open-modal`,
|
2026-08-15 08:59:40 +08:00
|
|
|
} {
|
|
|
|
|
if !strings.Contains(page, want) {
|
|
|
|
|
t.Errorf("正式页面缺少 %s", want)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-15 10:49:28 +08:00
|
|
|
if strings.Contains(page, `{{if not .CanMatch}}disabled{{end}}`) {
|
|
|
|
|
t.Fatal("所有状态都应允许勾选删除,不能再按 CanMatch 禁用复选框")
|
|
|
|
|
}
|
2026-08-15 15:56:16 +08:00
|
|
|
if strings.Contains(page, `inner-code-notice`) {
|
|
|
|
|
t.Fatal("档口入库码反馈不应继续占用列表上方布局")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInnerCodeTemplate_错误弹窗自动转义且成功提示不占布局(t *testing.T) {
|
|
|
|
|
tmpl, err := template.ParseFS(templateFS, "templates/*/*.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
|
data := map[string]any{
|
|
|
|
|
"Title": "档口入库码", "Active": "inner-codes", "CSRFToken": "test-csrf",
|
|
|
|
|
"FeedbackError": true, "FeedbackMessage": `<script>alert("secret")</script>`,
|
|
|
|
|
}
|
|
|
|
|
if err := tmpl.ExecuteTemplate(&output, "inner_code/list", data); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
body := output.String()
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`role="alertdialog"`, `data-auto-open-modal`,
|
|
|
|
|
`<script>alert("secret")</script>`,
|
|
|
|
|
`系统不会因本弹窗自动重复匹配或回写`,
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(body, want) {
|
|
|
|
|
t.Errorf("错误反馈弹窗缺少 %q", want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(body, `<script>alert("secret")</script>`) {
|
|
|
|
|
t.Fatal("错误反馈未经 html/template 自动转义")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rawJS, err := os.ReadFile("static/js/app.js")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
js := string(rawJS)
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`function setupTransientFeedback()`, `data-auto-dismiss-toast`,
|
|
|
|
|
`window.setTimeout(function ()`, `}, 4000);`,
|
|
|
|
|
`current.searchParams.delete(name)`, `window.history.replaceState`,
|
|
|
|
|
`setupTransientFeedback();`,
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(js, want) {
|
|
|
|
|
t.Errorf("临时反馈脚本缺少 %q", want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cssRaw, err := os.ReadFile("static/css/app.css")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
css := string(cssRaw)
|
|
|
|
|
if !strings.Contains(css, `.inner-code-toast {`) || !strings.Contains(css, `position: fixed;`) {
|
|
|
|
|
t.Fatal("成功提示必须固定定位,不能占用表格布局")
|
|
|
|
|
}
|
2026-08-15 08:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInnerCodeTemplate_模块状态白名单已登记(t *testing.T) {
|
|
|
|
|
raw, err := os.ReadFile("static/js/app.js")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-08-15 15:32:01 +08:00
|
|
|
if !strings.Contains(string(raw), `"/inner-codes": ["date", "status", "q", "page", "page_size"]`) {
|
2026-08-15 08:59:40 +08:00
|
|
|
t.Fatal("档口入库码页面切换模块后应保持稳定筛选和页码")
|
|
|
|
|
}
|
2026-08-15 09:41:45 +08:00
|
|
|
for _, want := range []string{
|
|
|
|
|
`appendSelectedIDs(matchForm, "inner-code-match")`,
|
|
|
|
|
`appendSelectedIDs(applyForm, "inner-code-apply")`,
|
2026-08-15 10:49:28 +08:00
|
|
|
`appendSelectedIDs(deleteForm, "inner-code-delete")`,
|
|
|
|
|
`checkedCount(action)`,
|
2026-08-15 09:41:45 +08:00
|
|
|
`field.name = "ids"`,
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(string(raw), want) {
|
|
|
|
|
t.Errorf("档口入库码选中 ID 提交缺少 %s", want)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-15 08:59:40 +08:00
|
|
|
}
|
2026-08-15 11:04:10 +08:00
|
|
|
|
|
|
|
|
func TestInnerCodeTemplate_筛选顺序与紧凑宽度(t *testing.T) {
|
|
|
|
|
raw, err := os.ReadFile("templates/inner_code/list.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
page := string(raw)
|
|
|
|
|
parts := []string{
|
|
|
|
|
`id="inner-code-status"`,
|
|
|
|
|
`id="inner-code-date" type="date" name="date"`,
|
|
|
|
|
`id="inner-code-keyword"`,
|
2026-08-15 15:15:38 +08:00
|
|
|
`class="button-link" href="/inner-codes?date={{.BusinessDate}}&page_size={{.CurrentPageSize}}">清除</a>`,
|
2026-08-15 11:04:10 +08:00
|
|
|
`<button type="submit">搜索</button>`,
|
|
|
|
|
}
|
|
|
|
|
lastIndex := -1
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
index := strings.Index(page, part)
|
|
|
|
|
if index < 0 {
|
|
|
|
|
t.Fatalf("筛选工具栏缺少 %s", part)
|
|
|
|
|
}
|
|
|
|
|
if index <= lastIndex {
|
|
|
|
|
t.Fatalf("筛选工具栏顺序不正确,%s 没有位于上一控件之后", part)
|
|
|
|
|
}
|
|
|
|
|
lastIndex = index
|
|
|
|
|
}
|
|
|
|
|
filterStart := strings.Index(page, `<form id="inner-code-filter"`)
|
|
|
|
|
if filterStart < 0 {
|
|
|
|
|
t.Fatal("没有找到档口入库码筛选表单")
|
|
|
|
|
}
|
|
|
|
|
filterEnd := strings.Index(page[filterStart:], `</form>`)
|
|
|
|
|
if filterEnd < 0 {
|
|
|
|
|
t.Fatal("没有找到完整的档口入库码筛选表单")
|
|
|
|
|
}
|
|
|
|
|
filter := page[filterStart : filterStart+filterEnd]
|
|
|
|
|
if strings.Contains(filter, `type="hidden" name="date"`) {
|
|
|
|
|
t.Fatal("筛选表单应直接提交可见业务日期,不能继续保留重复隐藏字段")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
css, err := os.ReadFile("static/css/app.css")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
styles := string(css)
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`.inner-code-toolbar input[type="text"].inner-code-search`,
|
|
|
|
|
`flex: 0 0 150px`, `width: 150px`, `min-width: 150px`,
|
|
|
|
|
`.inner-code-toolbar .inner-code-filter-form { flex-wrap: wrap; }`,
|
|
|
|
|
`.inner-code-toolbar .inner-code-filter-form { flex-basis: 100%; }`,
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(styles, want) {
|
|
|
|
|
t.Errorf("筛选工具栏样式缺少 %s", want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|