Mirror of github.com/gissleh/irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
5.1 KiB

  1. package irc_test
  2. import (
  3. "context"
  4. "testing"
  5. "git.aiterp.net/gisle/irc"
  6. "git.aiterp.net/gisle/irc/internal/irctest"
  7. )
  8. func TestClient(t *testing.T) {
  9. client := irc.New(context.Background(), irc.Config{
  10. Nick: "Test",
  11. User: "Tester",
  12. RealName: "...",
  13. Alternatives: []string{"Test2", "Test3", "Test4"},
  14. })
  15. t.Logf("Client.ID = %#+v", client.ID())
  16. if client.ID() == "" {
  17. t.Fail()
  18. }
  19. interaction := irctest.Interaction{
  20. Strict: false,
  21. Lines: []irctest.InteractionLine{
  22. {Kind: 'C', Data: "CAP LS 302"},
  23. {Kind: 'C', Data: "NICK Test"},
  24. {Kind: 'C', Data: "USER Tester 8 * :..."},
  25. {Kind: 'S', Data: ":testserver.example.com CAP * LS :multi-prefix userhost-in-names"},
  26. {Kind: 'C', Data: "CAP REQ :multi-prefix userhost-in-names"},
  27. {Kind: 'S', Data: ":testserver.example.com CAP * ACK :multi-prefix userhost-in-names"},
  28. {Kind: 'C', Data: "CAP END"},
  29. {Kind: 'S', Data: ":testserver.example.com 443 * Test :Nick is not available"},
  30. {Kind: 'C', Data: "NICK Test2"},
  31. {Kind: 'S', Data: ":testserver.example.com 443 * Test2 :Nick is not available"},
  32. {Kind: 'C', Data: "NICK Test3"},
  33. {Kind: 'S', Data: ":testserver.example.com 443 * Test3 :Nick is not available"},
  34. {Kind: 'C', Data: "NICK Test4"},
  35. {Kind: 'S', Data: ":testserver.example.com 443 * Test4 :Nick is not available"},
  36. {Kind: 'C', Data: "NICK Test*"},
  37. {Kind: 'S', Data: ":testserver.example.com 001 Test768 :Welcome to the TestServer Internet Relay Chat Network test"},
  38. {Kind: 'C', Data: "WHO Test768*"},
  39. {Kind: 'S', Data: ":testserver.example.com 002 Test768 :Your host is testserver.example.com[testserver.example.com/6667], running version charybdis-4-rc3"},
  40. {Kind: 'S', Data: ":testserver.example.com 003 Test768 :This server was created Fri Nov 25 2016 at 17:28:20 CET"},
  41. {Kind: 'S', Data: ":testserver.example.com 004 Test768 testserver.example.com charybdis-4-rc3 DQRSZagiloswxz CFILNPQbcefgijklmnopqrstvz bkloveqjfI"},
  42. {Kind: 'S', Data: ":testserver.example.com 005 Test768 FNC SAFELIST ELIST=CTU MONITOR=100 WHOX ETRACE KNOCK CHANTYPES=#& EXCEPTS INVEX CHANMODES=eIbq,k,flj,CFLNPQcgimnprstz CHANLIMIT=#&:15 :are supported by this server"},
  43. {Kind: 'S', Data: ":testserver.example.com 005 Test768 PREFIX=(ov)@+ MAXLIST=bqeI:100 MODES=4 NETWORK=TestServer STATUSMSG=@+ CALLERID=g CASEMAPPING=rfc1459 NICKLEN=30 MAXNICKLEN=31 CHANNELLEN=50 TOPICLEN=390 DEAF=D :are supported by this server"},
  44. {Kind: 'S', Data: ":testserver.example.com 005 Test768 TARGMAX=NAMES:1,LIST:1,KICK:1,WHOIS:1,PRIVMSG:4,NOTICE:4,ACCEPT:,MONITOR: EXTBAN=$,&acjmorsuxz| CLIENTVER=3.0 :are supported by this server"},
  45. {Kind: 'S', Data: ":testserver.example.com 251 Test768 :There are 0 users and 2 invisible on 1 servers"},
  46. {Kind: 'S', Data: ":testserver.example.com 254 Test768 1 :channels formed"},
  47. {Kind: 'S', Data: ":testserver.example.com 255 Test768 :I have 2 clients and 0 servers"},
  48. {Kind: 'S', Data: ":testserver.example.com 265 Test768 2 2 :Current local users 2, max 2"},
  49. {Kind: 'S', Data: ":testserver.example.com 266 Test768 2 2 :Current global users 2, max 2"},
  50. {Kind: 'S', Data: ":testserver.example.com 250 Test768 :Highest connection count: 2 (2 clients) (8 connections received)"},
  51. {Kind: 'S', Data: ":testserver.example.com 352 Test768 * ~Tester testclient.example.com testserver.example.com Test768 H :0 ..."},
  52. {Kind: 'S', Data: ":testserver.example.com 375 Test768 :- testserver.example.com Message of the Day - "},
  53. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- This server is only for testing irce, not chatting. If you happen"},
  54. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- to connect to it by accident, please disconnect immediately."},
  55. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- "},
  56. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- - #Test :: Test Channel"},
  57. {Kind: 'S', Data: ":testserver.example.com 372 Test768 :- - #Test2 :: Other Test Channel"},
  58. {Kind: 'S', Data: ":testserver.example.com 376 Test768 :End of /MOTD command."},
  59. {Kind: 'S', Data: ":test MODE Test768 :+i"},
  60. {Kind: 'C', Data: "JOIN #Test"},
  61. },
  62. }
  63. addr, err := interaction.Listen()
  64. if err != nil {
  65. t.Fatal("Listen:", err)
  66. }
  67. irc.Handle(func(event *irc.Event, client *irc.Client) {
  68. if event.Name() == "packet.376" {
  69. client.SendQueued("JOIN #Test")
  70. }
  71. })
  72. err = client.Connect(addr, false)
  73. if err != nil {
  74. t.Fatal("Connect:", err)
  75. return
  76. }
  77. interaction.Wait()
  78. fail := interaction.Failure
  79. if fail != nil {
  80. t.Error("Index:", fail.Index)
  81. t.Error("NetErr:", fail.NetErr)
  82. t.Error("Result:", fail.Result)
  83. if fail.Index >= 0 {
  84. t.Error("Line.Kind:", interaction.Lines[fail.Index].Kind)
  85. t.Error("Line.Data:", interaction.Lines[fail.Index].Data)
  86. }
  87. }
  88. if client.Nick() != "Test768" {
  89. t.Errorf("Nick: %#+v != %#+v (Expectation)", client.Nick(), "Test768")
  90. }
  91. if client.User() != "~Tester" {
  92. t.Errorf("User: %#+v != %#+v (Expectation)", client.User(), "~Tester")
  93. }
  94. if client.Host() != "testclient.example.com" {
  95. t.Errorf("Host: %#+v != %#+v (Expectation)", client.Host(), "testclient.example.com")
  96. }
  97. for i, logLine := range interaction.Log {
  98. t.Logf("Log[%d] = %#+v", i, logLine)
  99. }
  100. }