scan_ussd_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package device
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. )
  7. func TestScanOperatorsParsesNetworks(t *testing.T) {
  8. client := &transcriptClient{steps: []clientStep{
  9. {command: "AT+COPS=?", response: okResponse(
  10. `+COPS: (2,"China Mobile","CMCC","46000",7),(1,"China Unicom","CU","46001",0),(3,"China Telecom","CT","46011",7)`,
  11. )},
  12. }}
  13. manager, id := newStartedTestManager(t, client)
  14. result, err := manager.ScanOperators(context.Background(), id)
  15. if err != nil {
  16. t.Fatalf("ScanOperators: %v", err)
  17. }
  18. if result.Status != "complete" {
  19. t.Fatalf("scan status = %q, want complete", result.Status)
  20. }
  21. if len(result.Operators) != 3 {
  22. t.Fatalf("operators = %d, want 3 (%+v)", len(result.Operators), result.Operators)
  23. }
  24. first := result.Operators[0]
  25. if first.Status != "current" || first.Numeric != "46000" || first.Name != "China Mobile" || first.Act != "LTE" {
  26. t.Fatalf("first operator = %+v", first)
  27. }
  28. if result.Operators[2].Status != "forbidden" || result.Operators[2].Act != "LTE" {
  29. t.Fatalf("third operator = %+v", result.Operators[2])
  30. }
  31. client.assertDone(t)
  32. }
  33. func TestParseOperatorScanHandlesEmptyAndMalformed(t *testing.T) {
  34. if got := parseOperatorScan(okResponse()); len(got) != 0 {
  35. t.Fatalf("empty response operators = %v", got)
  36. }
  37. if got := parseOperatorScan(okResponse(`+COPS: `)); len(got) != 0 {
  38. t.Fatalf("empty list operators = %v", got)
  39. }
  40. // A tuple with too few fields is skipped, valid siblings still parse.
  41. got := parseOperatorScan(okResponse(`+COPS: (1,"Only"),(1,"Good","G","31026",7)`))
  42. if len(got) != 1 || got[0].Numeric != "31026" {
  43. t.Fatalf("mixed malformed operators = %+v", got)
  44. }
  45. }
  46. func TestUSSDSessionLifecycle(t *testing.T) {
  47. client := &transcriptClient{
  48. steps: []clientStep{
  49. {command: `AT+CUSD=1,"*100#",15`, response: okResponse()},
  50. {command: `AT+CUSD=1,"1",15`, response: okResponse()},
  51. {command: "AT+CUSD=2", response: okResponse()},
  52. },
  53. urcs: []string{
  54. `+CUSD: 1,"Main menu",15`,
  55. `+CUSD: 1,"Sub menu",15`,
  56. },
  57. }
  58. manager, id := newStartedTestManager(t, client)
  59. start, err := manager.USSD(context.Background(), id, "*100#")
  60. if err != nil {
  61. t.Fatalf("start USSD: %v", err)
  62. }
  63. if start.Status != "awaiting_input" || !start.Continueable || start.SessionID == "" {
  64. t.Fatalf("start result = %+v, want an awaiting_input session", start)
  65. }
  66. if start.Text != "Main menu" {
  67. t.Fatalf("start text = %q", start.Text)
  68. }
  69. cont, err := manager.ContinueUSSD(context.Background(), start.SessionID, "1")
  70. if err != nil {
  71. t.Fatalf("continue USSD: %v", err)
  72. }
  73. if cont.Status != "awaiting_input" || cont.SessionID != start.SessionID || cont.Text != "Sub menu" {
  74. t.Fatalf("continue result = %+v", cont)
  75. }
  76. if err := manager.CancelUSSD(context.Background(), start.SessionID); err != nil {
  77. t.Fatalf("cancel USSD: %v", err)
  78. }
  79. // After cancel the session is gone.
  80. if _, err := manager.ContinueUSSD(context.Background(), start.SessionID, "1"); !errors.Is(err, ErrUSSDSessionNotFound) {
  81. t.Fatalf("continue after cancel err = %v, want ErrUSSDSessionNotFound", err)
  82. }
  83. client.assertDone(t)
  84. }
  85. func TestUSSDFinalAnswerNeedsNoSession(t *testing.T) {
  86. client := &transcriptClient{
  87. steps: []clientStep{
  88. {command: `AT+CUSD=1,"*#06#",15`, response: okResponse()},
  89. },
  90. urcs: []string{`+CUSD: 0,"Your balance is 5.00",15`},
  91. }
  92. manager, id := newStartedTestManager(t, client)
  93. result, err := manager.USSD(context.Background(), id, "*#06#")
  94. if err != nil {
  95. t.Fatalf("USSD: %v", err)
  96. }
  97. if result.Status != "final" || result.Continueable || result.SessionID != "" {
  98. t.Fatalf("final result = %+v, want no session", result)
  99. }
  100. client.assertDone(t)
  101. }