feat(v1.11.5-L): lite 精简版——移除大屏/客户自查/多用户/小键盘,套餐仅保留按日
需求与确认(用户逐项确认): - 老数据「按次 / 通行证 / 深渊」统一迁移成按日,代码里彻底删除三种模式 - 打手归属一并移除(只剩一个人,颜色与指派没有区分意义) - 盲水印做成完全不可见:只存在于 DOM / 源码,肉眼与截图都看不到 - 拼写采用更正后的 Powered by Baka春秋 - 版本号与品牌全站统一带 -L - screen_public / portal_public 保留解析但不起作用,保证旧 ini 能直接启动 - README 同步更新 品牌与版本(全站统一): - const version = "1.11.5-L":<title>、启动横幅、日志前缀 [代打台-L]、 设置页版本号、页脚版本号全部带 -L - 左上角导航品牌 代打台 → 代打台-L(登录页、初始化页同步) - 页脚新增可见署名 Powered by Baka春秋 盲水印: - _layout.html 在 <body> 起始处注入 .wm-blind 容器(含注释与 data-wm 双标记), 覆盖包括登录/初始化在内的所有页面 - app.css 用零尺寸 + 透明 + opacity:0 + clip-path:inset(50%) 四重隐藏, 同时留同名注释;不占位、不上色、不进无障碍树、不影响打印 功能移除: - 大屏:删除 handlers_screen.go、pages/screen.html、_screen_layout.html、 static/screen.css、static/screen.js、store.Stats/ScreenStats; 导航「大屏 ↗」、页脚「大屏」、设置页「大屏刷新/大屏」行、启动横幅大屏行一并删除 - 客户自查:删除 handlers_portal.go、pages/portal.html 与 .portal-*/.pcal-* 样式, 设置页「客户自助」行删除 - 多管理员:删除 handleUserCreate/Toggle/Color/UserEditPage/UserEditSave、 pages/user_form.html、store 侧 ActiveUsers/SetUserColor/SetUserActive/ UpdateUserByAdmin/NormalizeColor/AssigneePalette;设置页「管理员」整块移除, 只保留首次启动创建的那一个账户与「修改我的密码」 - 打手归属:删除 customers.assignee_id 相关读写与 UI(客户详情指派下拉、 客户表单「归属打手」、日历格左侧打手色竖条、任务卡打手标签、套餐详情客户列) - 登录页与初始化页的说明文案同步改为单账户口径(「只有一个管理员账户」 「创建后不能再添加其他管理员」),字数与 main 对齐,卡片不换行、高度不变 - 触摸小键盘:删除 3×4 键盘、⌨/触控开关、localStorage 记忆、.keypad* 样式 - 套餐计费方式:只保留按日。删除按次/通行证/深渊的 Package 与 Subscription 字段、 domain 里的 UnitCount/StateUsedUp/CycleIndex、package_form 计费方式单选与 通行证/深渊/有效期字段、日历整段色块(has-block/cell-block/season-band)、 追加次数分支、SeasonComplete/AddCount、CountActiveSubs 的次数条件 数据库迁移(幂等,Migrate 内自动执行): - normalizeLegacyData:套餐一律 unit='day' 并清 season/abyss/cycle_days/ default_validity_days/season_*;订阅清 season/abyss/cycle_days/season_done - 通行证、深渊当年就是 unit='day' 且起止日完整,仅清标记,服务区间原样保留 - 按次按「1 次 = 1 天」折算:结束日 = 开始日 + 总次数 − 1;仍缺结束日退回开始日 - 只动数据,不删表不删列:老库的 season/abyss/cycle_days/total_count/used_count 保留但不再使用,升级可逆、不会因为 DDL 失败卡住 - 新库 schema 干净:不再创建上述列,users.color / customers.assignee_id 也不创建 - 老库 ensureColumn 保留 calendar_separate / tag / archived 三项(真增量列) 兼容: - config 仍解析 screen_public / portal_public(结构体字段注明已废弃), 默认 ini 模板里改为「已废弃,写了不报错、不生效」 测试: - go vet ./... 通过;go test ./... 全部通过 (新增 TestNormalizeLegacyTypeData 验证三模式折算成按日; TestSingleUserAccount / TestSessionLifecycle 覆盖单用户与会话; 删除按次、深渊、打手颜色、多用户等已不存在的能力的用例) - 新增 scripts/smoke-lite.ps1:起隔离实例,19 个页面级请求全部 200, 已移除的 /screen、/customer、/api/screen/day 均 404, 水印/品牌/v1.11.5-L/无残留词 12 项断言通过, 真实数据下日历出现待打任务、「剩 30 天」、今天到期徽标 4 项断言通过, 本次 0 失败 README:改为 lite 版说明,删除大屏/客户自查/通行证/深渊/按次/小键盘/多用户章节, 新增「盲水印」与「与 main 分支的差异」两节(含老数据折算规则与列保留说明)。
This commit is contained in:
+196
-287
@@ -2,10 +2,8 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -26,9 +24,9 @@ func newTestDB(t *testing.T) *DB {
|
||||
return db
|
||||
}
|
||||
|
||||
func mustPackage(t *testing.T, db *DB, name, unit string, amount int) *Package {
|
||||
func mustPackage(t *testing.T, db *DB, name string, amount int) *Package {
|
||||
t.Helper()
|
||||
id, err := db.CreatePackage(&Package{Name: name, Unit: unit, DefaultAmount: amount, Color: "#2F5D50"})
|
||||
id, err := db.CreatePackage(&Package{Name: name, DefaultAmount: amount, Color: "#2F5D50"})
|
||||
if err != nil {
|
||||
t.Fatalf("创建套餐失败: %v", err)
|
||||
}
|
||||
@@ -50,7 +48,7 @@ func mustCustomer(t *testing.T, db *DB, tail, name string) int64 {
|
||||
|
||||
func TestCreateSubAndDuplicate(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
today := domain.Today()
|
||||
|
||||
@@ -63,8 +61,8 @@ func TestCreateSubAndDuplicate(t *testing.T) {
|
||||
if sub.EndDate != domain.Date(domain.AddDays(today, 29)) {
|
||||
t.Fatalf("结束日=%s", sub.EndDate)
|
||||
}
|
||||
if sub.Unit != domain.UnitDay || sub.Status != domain.StatusActive {
|
||||
t.Fatalf("unit=%s status=%s", sub.Unit, sub.Status)
|
||||
if sub.Status != domain.StatusActive {
|
||||
t.Fatalf("status=%s", sub.Status)
|
||||
}
|
||||
|
||||
if _, err := db.CreateSub(CreateSubInput{
|
||||
@@ -85,7 +83,7 @@ func TestCreateSubAndDuplicate(t *testing.T) {
|
||||
|
||||
func TestExtendDaysFromTodayWhenExpired(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
today := domain.Today()
|
||||
|
||||
@@ -129,7 +127,7 @@ func TestExtendDaysFromTodayWhenExpired(t *testing.T) {
|
||||
|
||||
func TestExtendDaysFromOldEndWhenActive(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
today := domain.Today()
|
||||
|
||||
@@ -151,7 +149,7 @@ func TestExtendDaysFromOldEndWhenActive(t *testing.T) {
|
||||
|
||||
func TestExtendDaysRejectsBackwards(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
today := domain.Today()
|
||||
|
||||
@@ -163,84 +161,9 @@ func TestExtendDaysRejectsBackwards(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddCountAndCheckinConsumption(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "深渊十次", domain.UnitCount, 10)
|
||||
cid := mustCustomer(t, db, "5678", "大鹏")
|
||||
today := domain.Today()
|
||||
|
||||
sub, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), TotalCount: 10,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sub.TotalCount != 10 || sub.UsedCount != 0 {
|
||||
t.Fatalf("total=%d used=%d", sub.TotalCount, sub.UsedCount)
|
||||
}
|
||||
|
||||
// 打卡完成 → 扣 1 次
|
||||
if err := db.SaveCheckin(sub.ID, domain.Date(today), "done", "", 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ := db.Sub(sub.ID)
|
||||
if got.UsedCount != 1 {
|
||||
t.Fatalf("used=%d want 1", got.UsedCount)
|
||||
}
|
||||
|
||||
// 同一天改成「跳过」→ 返还
|
||||
if err := db.SaveCheckin(sub.ID, domain.Date(today), "skipped", "", 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ = db.Sub(sub.ID)
|
||||
if got.UsedCount != 0 {
|
||||
t.Fatalf("used=%d want 0", got.UsedCount)
|
||||
}
|
||||
|
||||
// 再改回完成 → 再扣
|
||||
if err := db.SaveCheckin(sub.ID, domain.Date(today), "done", "", 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ = db.Sub(sub.ID)
|
||||
if got.UsedCount != 1 {
|
||||
t.Fatalf("used=%d want 1", got.UsedCount)
|
||||
}
|
||||
|
||||
// 撤销打卡 → 返还
|
||||
ci, err := db.CheckinFor(sub.ID, domain.Date(today))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.DeleteCheckin(ci.ID, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ = db.Sub(sub.ID)
|
||||
if got.UsedCount != 0 {
|
||||
t.Fatalf("used=%d want 0", got.UsedCount)
|
||||
}
|
||||
|
||||
// 次数用尽后不能再打卡
|
||||
_, _ = db.sql.Exec(`UPDATE subscriptions SET used_count=total_count WHERE id=?`, sub.ID)
|
||||
if err := db.SaveCheckin(sub.ID, domain.Date(domain.AddDays(today, -1)), "done", "", 0); err == nil {
|
||||
t.Fatal("次数用尽时打卡应被拒绝")
|
||||
}
|
||||
|
||||
// 追加次数
|
||||
if err := db.AddCount(sub.ID, 5, "", "加购", 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ = db.Sub(sub.ID)
|
||||
if got.TotalCount != 15 {
|
||||
t.Fatalf("total=%d want 15", got.TotalCount)
|
||||
}
|
||||
if err := db.SaveCheckin(sub.ID, domain.Date(domain.AddDays(today, -1)), "done", "", 0); err != nil {
|
||||
t.Fatalf("追加后应可打卡: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPauseResumeShiftsEndDate(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
today := domain.Today()
|
||||
|
||||
@@ -276,35 +199,9 @@ func TestPauseResumeShiftsEndDate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarSubsExcludesOpenEndedCount(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
dayPkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
cntPkg := mustPackage(t, db, "深渊十次", domain.UnitCount, 10)
|
||||
today := domain.Today()
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
|
||||
daySub, _ := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: dayPkg.ID, StartDate: domain.Date(today), Days: 30,
|
||||
})
|
||||
cid2 := mustCustomer(t, db, "5678", "大鹏")
|
||||
if _, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid2, PackageID: cntPkg.ID, StartDate: domain.Date(today), TotalCount: 10,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
subs, err := db.CalendarSubs(domain.Date(today), domain.Date(today))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(subs) != 1 || subs[0].ID != daySub.ID {
|
||||
t.Fatalf("无有效期的按次订阅不应出现在日历: %+v", subs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackup(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
mustPackage(t, db, "包月日常", 30)
|
||||
path, err := db.BackupNow(14)
|
||||
if err != nil {
|
||||
t.Fatalf("备份失败: %v", err)
|
||||
@@ -337,7 +234,7 @@ func TestSettingsAndBadges(t *testing.T) {
|
||||
t.Fatalf("默认值=%s", got)
|
||||
}
|
||||
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
today := domain.Today()
|
||||
_, _ = db.CreateSub(CreateSubInput{
|
||||
@@ -384,33 +281,9 @@ func TestUniqueCustomer(t *testing.T) {
|
||||
|
||||
var _ = time.Now
|
||||
|
||||
func TestScreenTopLimitedToTen(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
today := domain.Today()
|
||||
for i := 0; i < 12; i++ {
|
||||
cid, err := db.CreateCustomer(&Customer{Tail: "T" + strconv.Itoa(i), Name: "客户" + strconv.Itoa(i)})
|
||||
if err != nil {
|
||||
t.Fatalf("建客户失败: %v", err)
|
||||
}
|
||||
if _, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(today), Days: 30 - i,
|
||||
}); err != nil {
|
||||
t.Fatalf("建订阅失败: %v", err)
|
||||
}
|
||||
}
|
||||
st, err := db.ScreenStats(today, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("统计失败: %v", err)
|
||||
}
|
||||
if len(st.Top) != 10 {
|
||||
t.Fatalf("TOP 榜应为 10 条,实际 %d 条", len(st.Top))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupKeepPrune(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
mustPackage(t, db, "包月日常", 30)
|
||||
dir := db.BackupDir()
|
||||
names := []string{
|
||||
"daili-20260101-000001.db",
|
||||
@@ -478,7 +351,7 @@ func TestSingleFileDatabase(t *testing.T) {
|
||||
t.Fatalf("journal_mode=%s err=%v", mode, err)
|
||||
}
|
||||
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
if _, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.Today()), Days: 30,
|
||||
@@ -513,7 +386,7 @@ func TestCopyDatabaseIsLossless(t *testing.T) {
|
||||
if err := db.Migrate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "1234", "老张")
|
||||
sub, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.Today()), Days: 30,
|
||||
@@ -651,7 +524,7 @@ func TestOpenLegacyWALDatabaseKeepsData(t *testing.T) {
|
||||
|
||||
func TestPackageCalendarSeparate(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "每日任务", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "每日任务", 30)
|
||||
if pkg.CalendarSeparate {
|
||||
t.Fatal("新建套餐默认不应勾选独立日历")
|
||||
}
|
||||
@@ -678,7 +551,7 @@ func TestPackageCalendarSeparate(t *testing.T) {
|
||||
|
||||
func TestTaskUrgentFlags(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
cid := mustCustomer(t, db, "4321", "老王")
|
||||
sub1, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: pkg.ID, StartDate: domain.Date(domain.Today()), Days: 30,
|
||||
@@ -686,10 +559,9 @@ func TestTaskUrgentFlags(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pkg2 := mustPackage(t, db, "按次", domain.UnitCount, 10)
|
||||
pkg2 := mustPackage(t, db, "包周", 7)
|
||||
sub2, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid, PackageID: pkg2.ID, StartDate: domain.Date(domain.Today()),
|
||||
TotalCount: 10, EndDate: domain.Date(domain.AddDays(domain.Today(), 30)),
|
||||
CustomerID: cid, PackageID: pkg2.ID, StartDate: domain.Date(domain.Today()), Days: 7,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -825,26 +697,17 @@ func TestMigrateOldDatabaseAddsColumn(t *testing.T) {
|
||||
if got, _ := db.Package(1); !got.CalendarSeparate {
|
||||
t.Fatal("迁移后新列不可写")
|
||||
}
|
||||
// 老库的 users / customers 也补上了新列
|
||||
var userColor string
|
||||
if err := db.sql.QueryRow(`SELECT IFNULL(color,'') FROM users WHERE id=1`).Scan(&userColor); err != nil {
|
||||
t.Fatalf("users.color 未补上: %v", err)
|
||||
}
|
||||
if err := db.SetUserColor(1, "#123456"); err != nil {
|
||||
t.Fatalf("老库 users.color 不可写: %v", err)
|
||||
}
|
||||
c, err := db.Customer(1)
|
||||
if err != nil {
|
||||
// 老客户仍可读
|
||||
if _, err := db.Customer(1); err != nil {
|
||||
t.Fatalf("老客户读取失败: %v", err)
|
||||
}
|
||||
if c.AssigneeID != 0 {
|
||||
t.Fatalf("老客户不应有打手: %+v", c)
|
||||
// 老套餐已被规范化成按日
|
||||
if pkg.UnitLabel() != "按日" {
|
||||
t.Fatalf("老套餐未规范化为按日: %s", pkg.UnitLabel())
|
||||
}
|
||||
if err := db.SetCustomerAssignee(1, 1); err != nil {
|
||||
t.Fatalf("老库 customers.assignee_id 不可写: %v", err)
|
||||
}
|
||||
if c2, _ := db.Customer(1); c2.AssigneeID != 1 {
|
||||
t.Fatalf("指派未生效: %+v", c2)
|
||||
// 迁移后仍可新建套餐(老库 packages.unit 是 NOT NULL 且没有默认值)
|
||||
if _, err := db.CreatePackage(&Package{Name: "新套餐", DefaultAmount: 7, Color: "#123456"}); err != nil {
|
||||
t.Fatalf("迁移后无法新建套餐: %v", err)
|
||||
}
|
||||
|
||||
// 新表 task_flags 已建立
|
||||
@@ -858,171 +721,89 @@ func TestMigrateOldDatabaseAddsColumn(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserColorAndAssignee(t *testing.T) {
|
||||
// TestSingleUserAccount 覆盖 lite 版只保留一个账户后的账户与会话行为。
|
||||
func TestSingleUserAccount(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
|
||||
// 新建管理员自动分配调色板颜色
|
||||
u1, err := db.CreateUser("dashou1", "阿强", "daili123")
|
||||
u, err := db.CreateUser("admin", "老板", "daili123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u2, err := db.CreateUser("dashou2", "小美", "daili123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
if u.ID != 1 {
|
||||
t.Fatalf("首个账户 id 应为 1,实际 %d", u.ID)
|
||||
}
|
||||
if u1.Color == "" || u2.Color == "" || u1.Color == u2.Color {
|
||||
t.Fatalf("自动颜色不合理: %q %q", u1.Color, u2.Color)
|
||||
if n, _ := db.CountUsers(); n != 1 {
|
||||
t.Fatalf("账户数=%d want 1", n)
|
||||
}
|
||||
// 用户名唯一:不会再出现第二个账户
|
||||
if _, err := db.CreateUser("admin", "另一个", "daili123"); err == nil {
|
||||
t.Fatal("重复用户名应被拒绝")
|
||||
}
|
||||
|
||||
// 改色(小写转大写)
|
||||
if err := db.SetUserColor(u2.ID, "#c0392b"); err != nil {
|
||||
t.Fatal(err)
|
||||
// 登录
|
||||
if _, err := db.Authenticate("admin", "daili123"); err != nil {
|
||||
t.Fatalf("正确密码应可登录: %v", err)
|
||||
}
|
||||
got, err := db.UserByID(u2.ID)
|
||||
if err != nil || got.Color != "#C0392B" {
|
||||
t.Fatalf("颜色未保存或未规范化: %+v err=%v", got, err)
|
||||
}
|
||||
// 非法颜色
|
||||
if err := db.SetUserColor(u2.ID, "red"); err == nil {
|
||||
t.Fatal("非法颜色应报错")
|
||||
}
|
||||
if err := db.SetUserColor(u2.ID, "#12345"); err == nil {
|
||||
t.Fatal("长度不对应报错")
|
||||
}
|
||||
// 不存在的账户
|
||||
if err := db.SetUserColor(9999, "#123456"); err == nil {
|
||||
t.Fatal("不存在的账户应报错")
|
||||
if _, err := db.Authenticate("admin", "wrong"); err == nil {
|
||||
t.Fatal("错误密码不应通过")
|
||||
}
|
||||
|
||||
// 客户指派
|
||||
cid := mustCustomer(t, db, "3001", "客户甲")
|
||||
if err := db.SetCustomerAssignee(cid, u1.ID); err != nil {
|
||||
// 改密码后旧密码失效
|
||||
if err := db.UpdatePassword(u.ID, "newpass123"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := db.Customer(cid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
if _, err := db.Authenticate("admin", "daili123"); err == nil {
|
||||
t.Fatal("旧密码不应还能登录")
|
||||
}
|
||||
if c.AssigneeID != u1.ID || c.AssigneeName != "阿强" || c.AssigneeColor != "#2F5D50" {
|
||||
t.Fatalf("客户指派信息不对: %+v", c)
|
||||
}
|
||||
// 取消指派
|
||||
if err := db.SetCustomerAssignee(cid, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c2, _ := db.Customer(cid); c2.AssigneeID != 0 || c2.AssigneeName != "" {
|
||||
t.Fatalf("取消指派失败: %+v", c2)
|
||||
}
|
||||
// 指派给不存在的打手
|
||||
if err := db.SetCustomerAssignee(cid, 9999); err == nil {
|
||||
t.Fatal("不存在的打手应报错")
|
||||
if _, err := db.Authenticate("admin", "newpass123"); err != nil {
|
||||
t.Fatalf("新密码应可登录: %v", err)
|
||||
}
|
||||
|
||||
// 客户创建时直接带指派
|
||||
cid2, err := db.CreateCustomer(&Customer{Tail: "3002", Name: "客户乙", AssigneeID: u2.ID})
|
||||
if err != nil {
|
||||
// 改显示名称
|
||||
if err := db.UpdateDisplayName(u.ID, " 强哥 "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c3, _ := db.Customer(cid2)
|
||||
if c3.AssigneeName != "小美" || c3.AssigneeColor != "#C0392B" {
|
||||
t.Fatalf("新建客户的指派未生效: %+v", c3)
|
||||
}
|
||||
|
||||
// 订阅联表能带出打手
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
sub, err := db.CreateSub(CreateSubInput{
|
||||
CustomerID: cid2, PackageID: pkg.ID, StartDate: domain.Date(domain.Today()), Days: 30,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gotSub, err := db.Sub(sub.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotSub.AssigneeName != "小美" || gotSub.AssigneeColor != "#C0392B" {
|
||||
t.Fatalf("订阅未带出打手: %+v", gotSub)
|
||||
}
|
||||
|
||||
// 只有启用中的账户可作为打手候选
|
||||
if err := db.SetUserActive(u1.ID, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users, err := db.ActiveUsers()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, u := range users {
|
||||
if u.ID == u1.ID {
|
||||
t.Fatal("停用账户不应出现在打手候选里")
|
||||
}
|
||||
got, err := db.UserByID(u.ID)
|
||||
if err != nil || got.DisplayName != "强哥" {
|
||||
t.Fatalf("显示名称未更新: %+v err=%v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateUserByAdmin(t *testing.T) {
|
||||
// TestSessionLifecycle 覆盖会话创建、过期清理与登出。
|
||||
func TestSessionLifecycle(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
|
||||
admin, err := db.CreateUser("admin", "老板", "daili123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u, err := db.CreateUser("dashou", "阿强", "dashou123")
|
||||
u, err := db.CreateUser("admin", "老板", "daili123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 建一个会话,稍后用密码重置把它踢掉
|
||||
token := "tok-" + domain.Date(domain.Today())
|
||||
token := "tok-test"
|
||||
if err := db.CreateSession(token, u.ID, time.Hour, "127.0.0.1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.SessionUser(token); err != nil {
|
||||
t.Fatalf("会话应有效: %v", err)
|
||||
}
|
||||
|
||||
// 只改昵称:会话保留
|
||||
if err := db.UpdateUserByAdmin(u.ID, "dashou", "强哥", ""); err != nil {
|
||||
// 过期会话不可用,且会被删除
|
||||
if err := db.CreateSession("tok-old", u.ID, -time.Hour, "127.0.0.1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ := db.UserByID(u.ID)
|
||||
if got.DisplayName != "强哥" || got.Username != "dashou" {
|
||||
t.Fatalf("昵称未更新: %+v", got)
|
||||
if _, err := db.SessionUser("tok-old"); err == nil {
|
||||
t.Fatal("过期会话不应有效")
|
||||
}
|
||||
if _, err := db.SessionUser(token); err != nil {
|
||||
t.Fatalf("未改密码不应踢下线: %v", err)
|
||||
}
|
||||
|
||||
// 改用户名 + 重置密码:会话失效,新密码可登录
|
||||
if err := db.UpdateUserByAdmin(u.ID, "laoban", "强哥", "newpass123"); err != nil {
|
||||
// 登出
|
||||
if err := db.DeleteSession(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.SessionUser(token); err == nil {
|
||||
t.Fatal("改密后旧会话应失效")
|
||||
}
|
||||
if _, err := db.Authenticate("laoban", "newpass123"); err != nil {
|
||||
t.Fatalf("新密码应可登录: %v", err)
|
||||
}
|
||||
if _, err := db.Authenticate("laoban", "dashou123"); err == nil {
|
||||
t.Fatal("旧密码不应还能登录")
|
||||
}
|
||||
|
||||
// 用户名冲突
|
||||
if err := db.UpdateUserByAdmin(u.ID, "admin", "", ""); !errors.Is(err, ErrDuplicate) {
|
||||
t.Fatalf("重名应返回 ErrDuplicate, got %v", err)
|
||||
}
|
||||
// 空用户名
|
||||
if err := db.UpdateUserByAdmin(u.ID, " ", "", ""); err == nil {
|
||||
t.Fatal("空用户名应报错")
|
||||
}
|
||||
// 超管自己也在表里,但接口层才限制“只能改别人”
|
||||
if admin.ID != 1 {
|
||||
t.Fatalf("首个账户 id 应为 1,实际 %d", admin.ID)
|
||||
t.Fatal("登出后会话应失效")
|
||||
}
|
||||
db.PurgeSessions()
|
||||
}
|
||||
|
||||
func TestCustomerArchiveFilters(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
pkg := mustPackage(t, db, "包月日常", domain.UnitDay, 30)
|
||||
pkg := mustPackage(t, db, "包月日常", 30)
|
||||
|
||||
// 一个正常客户 + 一个按次客户
|
||||
cid := mustCustomer(t, db, "7001", "小张")
|
||||
@@ -1107,10 +888,138 @@ func TestCustomerArchiveFilters(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestNormalizeLegacyTypeData 验证 main 分支遗留的「按次 / 通行证 / 深渊」
|
||||
// 数据在 lite 版迁移后统一折算成按日。
|
||||
func TestNormalizeLegacyTypeData(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "legacy.db")
|
||||
old, err := sql.Open("sqlite", strings.ReplaceAll(path, "\\", "/"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stmts := []string{
|
||||
`CREATE TABLE packages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,
|
||||
unit TEXT NOT NULL CHECK (unit IN ('day','count')),
|
||||
default_amount INTEGER NOT NULL DEFAULT 30, price_cents INTEGER,
|
||||
color TEXT NOT NULL DEFAULT '#3B6EA5', note TEXT NOT NULL DEFAULT '',
|
||||
archived INTEGER NOT NULL DEFAULT 0, sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
calendar_separate INTEGER NOT NULL DEFAULT 0, tag TEXT NOT NULL DEFAULT '',
|
||||
season INTEGER NOT NULL DEFAULT 0, default_validity_days INTEGER NOT NULL DEFAULT 0,
|
||||
season_start TEXT NOT NULL DEFAULT '', season_end TEXT NOT NULL DEFAULT '',
|
||||
abyss INTEGER NOT NULL DEFAULT 0, cycle_days INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL, updated_at TEXT NOT NULL)`,
|
||||
`CREATE TABLE customers (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, tail TEXT NOT NULL, name TEXT NOT NULL,
|
||||
game_name TEXT NOT NULL DEFAULT '', game_server TEXT NOT NULL DEFAULT '',
|
||||
contact TEXT NOT NULL DEFAULT '', tags TEXT NOT NULL DEFAULT '',
|
||||
source TEXT NOT NULL DEFAULT '', note TEXT NOT NULL DEFAULT '',
|
||||
archived INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL, updated_at TEXT NOT NULL, UNIQUE (tail, name))`,
|
||||
`CREATE TABLE subscriptions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, customer_id INTEGER NOT NULL, package_id INTEGER NOT NULL,
|
||||
unit TEXT NOT NULL CHECK (unit IN ('day','count')),
|
||||
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active','paused','closed')),
|
||||
start_date TEXT NOT NULL, end_date TEXT,
|
||||
total_count INTEGER NOT NULL DEFAULT 0, used_count INTEGER NOT NULL DEFAULT 0,
|
||||
season INTEGER NOT NULL DEFAULT 0, season_done TEXT NOT NULL DEFAULT '',
|
||||
abyss INTEGER NOT NULL DEFAULT 0, cycle_days INTEGER NOT NULL DEFAULT 0,
|
||||
note TEXT NOT NULL DEFAULT '', created_by INTEGER,
|
||||
created_at TEXT NOT NULL, updated_at TEXT NOT NULL, closed_at TEXT,
|
||||
UNIQUE (customer_id, package_id))`,
|
||||
// 三个套餐:按次 / 通行证 / 深渊
|
||||
`INSERT INTO packages(name,unit,default_amount,color,season,season_start,season_end,abyss,cycle_days,created_at,updated_at)
|
||||
VALUES('十次副本','count',10,'#111111',0,'','',0,0,'2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
`INSERT INTO packages(name,unit,default_amount,color,season,season_start,season_end,abyss,cycle_days,created_at,updated_at)
|
||||
VALUES('通行证','day',1,'#222222',1,'2026-01-01','2026-01-31',0,0,'2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
`INSERT INTO packages(name,unit,default_amount,color,season,season_start,season_end,abyss,cycle_days,created_at,updated_at)
|
||||
VALUES('深渊','day',4,'#333333',0,'','',1,7,'2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
`INSERT INTO customers(tail,name,created_at,updated_at) VALUES('1001','甲','2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
`INSERT INTO customers(tail,name,created_at,updated_at) VALUES('1002','乙','2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
`INSERT INTO customers(tail,name,created_at,updated_at) VALUES('1003','丙','2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
// 按次:10 次、已用 4 次、没有结束日 → 应折算成 2026-01-01 起共 10 天
|
||||
`INSERT INTO subscriptions(customer_id,package_id,unit,status,start_date,end_date,total_count,used_count,created_at,updated_at)
|
||||
VALUES(1,1,'count','active','2026-01-01',NULL,10,4,'2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
// 通行证:区间完整,清标记即可
|
||||
`INSERT INTO subscriptions(customer_id,package_id,unit,status,start_date,end_date,season,created_at,updated_at)
|
||||
VALUES(2,2,'day','active','2026-01-01','2026-01-31',1,'2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
// 深渊:周期 7 天 × 4 周期
|
||||
`INSERT INTO subscriptions(customer_id,package_id,unit,status,start_date,end_date,abyss,cycle_days,total_count,created_at,updated_at)
|
||||
VALUES(3,3,'day','active','2026-02-01','2026-02-28',1,7,4,'2026-01-01 00:00:00','2026-01-01 00:00:00')`,
|
||||
}
|
||||
for _, s := range stmts {
|
||||
if _, err := old.Exec(s); err != nil {
|
||||
t.Fatalf("准备老库失败: %v\nSQL: %s", err, s)
|
||||
}
|
||||
}
|
||||
if err := old.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("打开老库失败: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := db.Migrate(); err != nil {
|
||||
t.Fatalf("迁移失败: %v", err)
|
||||
}
|
||||
|
||||
// 套餐:全部按日,season / abyss / cycle_days 归零
|
||||
pkgs, err := db.ListPackages(true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(pkgs) != 3 {
|
||||
t.Fatalf("套餐数量=%d want 3", len(pkgs))
|
||||
}
|
||||
for _, p := range pkgs {
|
||||
if p.UnitLabel() != "按日" {
|
||||
t.Fatalf("套餐 %q 未折算成按日: %s", p.Name, p.UnitLabel())
|
||||
}
|
||||
}
|
||||
var legacy int
|
||||
if err := db.sql.QueryRow(`SELECT COUNT(*) FROM packages WHERE unit<>'day' OR season<>0 OR abyss<>0 OR cycle_days<>0`).Scan(&legacy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if legacy != 0 {
|
||||
t.Fatalf("仍有 %d 个套餐残留旧类型字段", legacy)
|
||||
}
|
||||
|
||||
// 按次订阅:折算成 10 天,剩余 10 天
|
||||
sub1, err := db.Sub(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sub1.StartDate != "2026-01-01" || sub1.EndDate != "2026-01-10" {
|
||||
t.Fatalf("按次订阅折算区间不对: %s → %s", sub1.StartDate, sub1.EndDate)
|
||||
}
|
||||
// 通行证订阅:区间保留
|
||||
sub2, err := db.Sub(2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sub2.StartDate != "2026-01-01" || sub2.EndDate != "2026-01-31" {
|
||||
t.Fatalf("通行证订阅区间被改动: %s → %s", sub2.StartDate, sub2.EndDate)
|
||||
}
|
||||
// 深渊订阅:区间保留
|
||||
sub3, err := db.Sub(3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sub3.StartDate != "2026-02-01" || sub3.EndDate != "2026-02-28" {
|
||||
t.Fatalf("深渊订阅区间被改动: %s → %s", sub3.StartDate, sub3.EndDate)
|
||||
}
|
||||
// 迁移可重复执行
|
||||
if err := db.Migrate(); err != nil {
|
||||
t.Fatalf("重复迁移失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageTag(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
|
||||
p1 := mustPackage(t, db, "包月活动托", domain.UnitDay, 30)
|
||||
p1 := mustPackage(t, db, "包月活动托", 30)
|
||||
if p1.Tag != "" {
|
||||
t.Fatalf("默认标签应为空: %q", p1.Tag)
|
||||
}
|
||||
@@ -1123,7 +1032,7 @@ func TestPackageTag(t *testing.T) {
|
||||
t.Fatalf("标签未保存: %+v err=%v", got, err)
|
||||
}
|
||||
|
||||
p2 := &Package{Name: "深渊委托", Unit: domain.UnitCount, DefaultAmount: 10, Color: "#8C4A3C", Tag: " 包月 "}
|
||||
p2 := &Package{Name: "深渊委托", DefaultAmount: 10, Color: "#8C4A3C", Tag: " 包月 "}
|
||||
if _, err := db.CreatePackage(p2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user