feat: 卡实名时间以上游 archiveTime 为准并记录实名时间历史,新增二次实名批次
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
This commit is contained in:
149
internal/domain/cardobservation/realname_time_test.go
Normal file
149
internal/domain/cardobservation/realname_time_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package cardobservation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
var cst = time.FixedZone("CST", 8*3600)
|
||||
|
||||
func at(value string) *time.Time {
|
||||
parsed, err := time.ParseInLocation("2006-01-02 15:04:05", value, cst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &parsed
|
||||
}
|
||||
|
||||
func verifiedObservation(source string, observedAt, upstream *time.Time) RealnameObservation {
|
||||
return RealnameObservation{CardID: 1, Verified: true, UpstreamRealnameAt: upstream,
|
||||
Metadata: ObservationMetadata{ObservationID: "o", Source: source, Scene: "s", ObservedAt: *observedAt}}
|
||||
}
|
||||
|
||||
func TestDecideRealnameTime(t *testing.T) {
|
||||
polling := constants.CardObservationSourcePolling
|
||||
cases := []struct {
|
||||
name string
|
||||
snapshot RealnameTimeSnapshot
|
||||
observation RealnameObservation
|
||||
gate RealnameResetGate
|
||||
wantAt *time.Time
|
||||
wantSource string
|
||||
wantHistory bool
|
||||
wantChange string
|
||||
wantChanged bool
|
||||
wantUpstream *time.Time
|
||||
}{
|
||||
{
|
||||
name: "每次轮询读到相同上游时间不写",
|
||||
snapshot: RealnameTimeSnapshot{Exists: true, RealnameAt: at("2026-07-16 19:43:39"), RealnameAtSource: constants.RealnameAtSourceUpstream,
|
||||
UpstreamRealnameAt: at("2026-07-16 19:43:39"), FirstObservedAt: at("2026-07-16 20:03:25")},
|
||||
observation: verifiedObservation(polling, at("2026-11-08 17:10:51"), at("2026-07-16 19:43:39")),
|
||||
wantAt: at("2026-07-16 19:43:39"), wantSource: constants.RealnameAtSourceUpstream,
|
||||
wantUpstream: at("2026-07-16 19:43:39"),
|
||||
},
|
||||
{
|
||||
name: "上游更正迁移时间",
|
||||
snapshot: RealnameTimeSnapshot{Exists: true, RealnameAt: at("2026-07-16 20:03:25"), RealnameAtSource: constants.RealnameAtSourceMigrated,
|
||||
FirstObservedAt: at("2026-07-16 20:03:25")},
|
||||
observation: verifiedObservation(polling, at("2026-11-08 17:10:51"), at("2026-07-16 19:43:39")),
|
||||
wantAt: at("2026-07-16 19:43:39"), wantSource: constants.RealnameAtSourceUpstream,
|
||||
wantHistory: true, wantChange: constants.RealnameChangeUpstreamFix, wantChanged: true, wantUpstream: at("2026-07-16 19:43:39"),
|
||||
},
|
||||
{
|
||||
name: "不返回时间的运营商首次实名",
|
||||
snapshot: RealnameTimeSnapshot{},
|
||||
observation: verifiedObservation(polling, at("2026-11-08 10:00:00"), nil),
|
||||
wantAt: at("2026-11-08 10:00:00"), wantSource: constants.RealnameAtSourceFirstObserved,
|
||||
wantHistory: true, wantChange: constants.RealnameChangeFirst, wantChanged: true,
|
||||
},
|
||||
{
|
||||
name: "不返回时间的运营商再次读到不写",
|
||||
snapshot: RealnameTimeSnapshot{Exists: true, RealnameAt: at("2026-11-08 10:00:00"), RealnameAtSource: constants.RealnameAtSourceFirstObserved,
|
||||
FirstObservedAt: at("2026-11-08 10:00:00")},
|
||||
observation: verifiedObservation(polling, at("2026-11-08 12:00:00"), nil),
|
||||
wantAt: at("2026-11-08 10:00:00"), wantSource: constants.RealnameAtSourceFirstObserved,
|
||||
},
|
||||
{
|
||||
name: "迁移沿用的时间在无上游时间时保持",
|
||||
snapshot: RealnameTimeSnapshot{Exists: true, RealnameAt: at("2026-09-01 10:00:00"), RealnameAtSource: constants.RealnameAtSourceMigrated,
|
||||
FirstObservedAt: at("2026-09-01 10:00:00")},
|
||||
observation: verifiedObservation(polling, at("2026-11-08 12:00:00"), nil),
|
||||
wantAt: at("2026-09-01 10:00:00"), wantSource: constants.RealnameAtSourceMigrated,
|
||||
},
|
||||
{
|
||||
name: "已实名但无记录的老卡首次读到上游时间",
|
||||
snapshot: RealnameTimeSnapshot{},
|
||||
observation: verifiedObservation(polling, at("2026-11-09 00:10:00"), at("2026-08-01 09:00:00")),
|
||||
wantAt: at("2026-08-01 09:00:00"), wantSource: constants.RealnameAtSourceUpstream,
|
||||
wantHistory: true, wantChange: constants.RealnameChangeFirst, wantChanged: true, wantUpstream: at("2026-08-01 09:00:00"),
|
||||
},
|
||||
{
|
||||
name: "二次实名完成",
|
||||
snapshot: RealnameTimeSnapshot{Exists: true, RealnameAt: nil, RealnameAtSource: "", UpstreamRealnameAt: at("2026-07-16 19:43:39"),
|
||||
FirstObservedAt: at("2026-07-16 20:03:25"), ResetStatus: constants.RealnameResetStatusPending, ResetBaselineAt: at("2026-07-16 19:43:39")},
|
||||
observation: verifiedObservation(polling, at("2026-11-09 10:05:00"), at("2026-11-09 10:00:00")),
|
||||
gate: RealnameResetGateCompleted,
|
||||
wantAt: at("2026-11-09 10:00:00"), wantSource: constants.RealnameAtSourceUpstream,
|
||||
wantHistory: true, wantChange: constants.RealnameChangeResetCompleted, wantChanged: true, wantUpstream: at("2026-11-09 10:00:00"),
|
||||
},
|
||||
{
|
||||
name: "时间相同只是来源不同静默更正",
|
||||
snapshot: RealnameTimeSnapshot{Exists: true, RealnameAt: at("2026-07-16 19:43:39"), RealnameAtSource: constants.RealnameAtSourceMigrated,
|
||||
FirstObservedAt: at("2026-07-16 19:43:39")},
|
||||
observation: verifiedObservation(polling, at("2026-11-08 17:10:51"), at("2026-07-16 19:43:39")),
|
||||
wantAt: at("2026-07-16 19:43:39"), wantSource: constants.RealnameAtSourceUpstream, wantChanged: true,
|
||||
wantUpstream: at("2026-07-16 19:43:39"),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := DecideRealnameTime(tc.snapshot, tc.observation, tc.gate)
|
||||
if !sameTime(got.RealnameAt, tc.wantAt) || got.Source != tc.wantSource {
|
||||
t.Fatalf("realname_at=%v source=%s, want %v %s", got.RealnameAt, got.Source, tc.wantAt, tc.wantSource)
|
||||
}
|
||||
if got.WriteHistory != tc.wantHistory || got.ChangeType != tc.wantChange {
|
||||
t.Fatalf("history=%v change=%s, want %v %s", got.WriteHistory, got.ChangeType, tc.wantHistory, tc.wantChange)
|
||||
}
|
||||
if got.Changed != tc.wantChanged {
|
||||
t.Fatalf("changed=%v, want %v", got.Changed, tc.wantChanged)
|
||||
}
|
||||
if tc.wantUpstream != nil && !sameTime(got.UpstreamRealnameAt, tc.wantUpstream) {
|
||||
t.Fatalf("upstream=%v, want %v", got.UpstreamRealnameAt, tc.wantUpstream)
|
||||
}
|
||||
if tc.gate == RealnameResetGateCompleted && !got.ClearReset {
|
||||
t.Fatal("reset completion must clear reset state")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateRealnameResetGate(t *testing.T) {
|
||||
pending := RealnameTimeSnapshot{Exists: true, ResetStatus: constants.RealnameResetStatusPending, ResetBaselineAt: at("2026-07-16 19:43:39")}
|
||||
polling := constants.CardObservationSourcePolling
|
||||
now := at("2026-11-09 10:05:00")
|
||||
cases := []struct {
|
||||
name string
|
||||
snapshot RealnameTimeSnapshot
|
||||
observation RealnameObservation
|
||||
want RealnameResetGate
|
||||
}{
|
||||
{"不在等待", RealnameTimeSnapshot{Exists: true}, verifiedObservation(polling, now, nil), RealnameResetGateNone},
|
||||
{"读到旧实名", pending, verifiedObservation(polling, now, at("2026-07-16 19:43:39")), RealnameResetGateBlocked},
|
||||
{"读到更早实名", pending, verifiedObservation(polling, now, at("2026-07-01 00:00:00")), RealnameResetGateBlocked},
|
||||
{"没有上游时间", pending, verifiedObservation(polling, now, nil), RealnameResetGateBlocked},
|
||||
{"回调没有时间", pending, verifiedObservation(constants.CardObservationSourceCarrierCallback, now, nil), RealnameResetGateBlocked},
|
||||
{"新实名", pending, verifiedObservation(polling, now, at("2026-11-09 10:00:00")), RealnameResetGateCompleted},
|
||||
{"人工改为已实名", pending, verifiedObservation(constants.CardObservationSourceManualOverride, now, nil), RealnameResetGateCompleted},
|
||||
{"未实名观测", pending, RealnameObservation{CardID: 1, Metadata: ObservationMetadata{Source: polling, ObservedAt: *now}}, RealnameResetGateNone},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := EvaluateRealnameResetGate(tc.snapshot, tc.observation); got != tc.want {
|
||||
t.Fatalf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user