package trafficusage import ( "git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model" "testing" "time" ) func TestPreviousUsageMonth(t *testing.T) { for input, want := range map[string]string{"2026-03-31T10:00:00+08:00": "202602", "2024-03-31T10:00:00+08:00": "202402", "2026-01-31T00:00:00+08:00": "202512", "2026-08-31T16:00:00Z": "202608"} { date, err := time.Parse(time.RFC3339, input) if err != nil { t.Fatal(err) } if got := previousUsageMonth(date).Format("200601"); got != want { t.Fatalf("%s: got %s want %s", input, got, want) } } } func TestReconcileUsage(t *testing.T) { keys := []*model.SimKey{{ICCID: "ok"}, {ICCID: "missing"}, {ICCID: "duplicate"}, {ICCID: "failed"}} output := []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{ {SimKey: keys[0]}, {SimKey: keys[2]}, {SimKey: keys[2]}, {SimKey: keys[3], Code: 1}, {}, {SimKey: &model.SimKey{ICCID: "unknown"}}, } success, failed := reconcileUsage(keys, output, func(item model.SimDataUsageDailyOutputItem) float64 { return item.DataAmount }) if len(success) != 1 || success[0].SimKey != keys[0] || len(failed) != 3 { t.Fatalf("success=%+v failed=%+v", success, failed) } } func TestReconcileUsageWithPeriod(t *testing.T) { key := &model.SimKey{ICCID: "period"} output := []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{ {SimKey: key, ResData: model.SimDataUsageDailyOutputItem{QueryDate: "2026-07-14", DataAmount: 1}}, } success, failed := reconcileUsageWithPeriod([]*model.SimKey{key}, output, func(item model.SimDataUsageDailyOutputItem) float64 { return item.DataAmount }, func(item model.SimDataUsageDailyOutputItem) string { return item.QueryDate }, normalizeUsageDay, "20260713") if len(success) != 0 || len(failed) != 1 { t.Fatalf("expected period mismatch to fail, success=%+v failed=%+v", success, failed) } }