| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package simCardQueue
- import (
- "context"
- "encoding/json"
- "fwh-toolbox-server/domain"
- "io"
- "net/http"
- "strings"
- "testing"
- "github.com/hibiken/asynq"
- )
- type roundTripFunc func(*http.Request) (*http.Response, error)
- func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return fn(req) }
- func TestAccumulateSimCardStatistics(t *testing.T) {
- stats := domain.SimCardDailyStatistics{Date: "2026-09-24"}
- zero := 0.0
- previous := 200.0 * 1024
- // Thresholds are strict; cards without a previous snapshot count only toward totals.
- accumulateSimCardStatistics(&stats, 200*1024, &zero)
- accumulateSimCardStatistics(&stats, 1024*1024, &previous)
- accumulateSimCardStatistics(&stats, 2*1024*1024, nil)
- accumulateSimCardStatistics(&stats, 3*1024*1024, &zero)
- if stats.CardCount != 4 || stats.TotalOver200MB != 3 || stats.TotalOver1GB != 2 || stats.TotalOver2GB != 1 {
- t.Fatalf("unexpected total statistics: %+v", stats)
- }
- if stats.MaxTotalKB != 3*1024*1024 || stats.IncrementCardCount != 3 {
- t.Fatalf("unexpected maxima or comparison count: %+v", stats)
- }
- if stats.IncrementOver20MB != 3 || stats.IncrementOver50MB != 3 || stats.IncrementOver200MB != 2 {
- t.Fatalf("unexpected increment statistics: %+v", stats)
- }
- if stats.MaxIncrementKB == nil || *stats.MaxIncrementKB != 3*1024*1024 {
- t.Fatalf("unexpected maximum increment: %+v", stats.MaxIncrementKB)
- }
- if !strings.Contains(formatSimCardStatistics(stats), "最大增量:3072.00 MB") {
- t.Fatal("report does not show maximum increment")
- }
- }
- func TestPostSimCardStatistics(t *testing.T) {
- client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
- if r.Method != http.MethodPost || r.Header.Get("Content-Type") != "application/json" {
- t.Errorf("unexpected request: %s %s", r.Method, r.Header.Get("Content-Type"))
- }
- if r.URL.Query().Get("timestamp") == "" || r.URL.Query().Get("sign") == "" || r.URL.Query().Get("access_token") != "test" {
- t.Errorf("signature or access token missing: %s", r.URL.RawQuery)
- }
- var message struct {
- MsgType string `json:"msgtype"`
- Markdown struct {
- Text string `json:"text"`
- } `json:"markdown"`
- }
- if err := json.NewDecoder(r.Body).Decode(&message); err != nil {
- t.Error(err)
- }
- if message.MsgType != "markdown" || !strings.Contains(message.Markdown.Text, "2026-09-24") {
- t.Errorf("unexpected message: %+v", message)
- }
- return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(`{"errcode":0,"errmsg":"ok"}`))}, nil
- })}
- stats := domain.SimCardDailyStatistics{Date: "2026-09-24"}
- if err := postSimCardStatistics(context.Background(), client, "https://oapi.dingtalk.com/robot/send?access_token=test", "secret", stats); err != nil {
- t.Fatal(err)
- }
- }
- func TestPostSimCardStatisticsRejectsBotError(t *testing.T) {
- client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
- return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(`{"errcode":310000,"errmsg":"rejected"}`))}, nil
- })}
- if err := postSimCardStatistics(context.Background(), client, "https://oapi.dingtalk.com/robot/send", "", domain.SimCardDailyStatistics{}); err == nil {
- t.Fatal("DingTalk error response must be retried")
- }
- }
- func TestUnconfiguredWebhookSkipsStatistics(t *testing.T) {
- processor := &SimCardProcessor{}
- if err := processor.enqueueSimCardReportIfComplete("2026-09-24"); err != nil {
- t.Fatal(err)
- }
- if err := processor.handleSimCardDailyReport(context.Background(), asynq.NewTask(taskTypeSimCardDailyReport, []byte("2026-09-24"))); err != nil {
- t.Fatal(err)
- }
- }
- func TestAccumulateSimCardStatisticsMissingPrevious(t *testing.T) {
- stats := domain.SimCardDailyStatistics{}
- accumulateSimCardStatistics(&stats, 300*1024, nil)
- if stats.IncrementCardCount != 0 || stats.MaxIncrementKB != nil {
- t.Fatalf("missing previous day must not create an increment: %+v", stats)
- }
- }
- func TestAccumulateSimCardStatisticsCounterReset(t *testing.T) {
- stats := domain.SimCardDailyStatistics{}
- previous := 2.0 * 1024 * 1024
- accumulateSimCardStatistics(&stats, 60*1024, &previous)
- if stats.MaxIncrementKB == nil || *stats.MaxIncrementKB != 60*1024 || stats.IncrementOver50MB != 1 {
- t.Fatalf("reset counter should use current accumulated usage: %+v", stats)
- }
- }
|