test_1.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from proxy import *
  2. from twisted.trial import unittest
  3. import sys
  4. from twisted.python import log
  5. log.startLogging(sys.stdout)
  6. from pprint import pprint
  7. from unittest.mock import Mock, patch
  8. from shutil import copyfile
  9. # import twisted.internet.base
  10. # twisted.internet.base.DelayedCall.debug = True
  11. class GameTestCase(unittest.TestCase):
  12. def setUp(self):
  13. self.game = Game()
  14. # Fake game init
  15. self.game.queue_player = defer.DeferredQueue()
  16. self.game.queue_game = defer.DeferredQueue()
  17. self.game.observer = Observer()
  18. self.game.setPlayerReceived()
  19. self.game.observer.connect("user-game", self.game.show_game)
  20. # This should silence trying to send data when we can't.
  21. # Well, it doesn't. >:(
  22. self.game.playerDataReceived = Mock()
  23. print("Set!")
  24. def setRecv(self, f):
  25. self.game.queue_game.get().addCallback(f)
  26. def sent(self, line):
  27. print("Sent: [{0}]".format(line))
  28. self.setRecv(self.sent)
  29. def show(self, line):
  30. print("Recv: [{0}]".format(line))
  31. def inject(self, data):
  32. line = data.decode("latin-1", "ignore")
  33. self.assertIn(
  34. "TWGS Proxy build v", line, "Proxy activation text is in the data."
  35. )
  36. self.fired = True
  37. def test_inject(self):
  38. """ Verify that our injection is working.
  39. This doesn't need a defer, apparently the DeferredQueue works
  40. without depending on the reactor running.
  41. """
  42. self.setRecv(self.inject)
  43. self.fired = False
  44. self.game.dataReceived(b"\r\nTWGS v2.20b www.eisonline.com\n\r\n\r\n\r")
  45. self.assertEquals(self.fired, True, "Data was not received from queue_game.")
  46. def path_part3(self, *_):
  47. self.assertIsInstance(self.game.gamedata, GameData)
  48. warps = [2565, 5468, 5128, 238]
  49. pprint(self.game.gamedata.warps)
  50. for w in warps:
  51. self.assertIn(w, self.game.gamedata.warps)
  52. # Test completed, clear the deferred.
  53. self.d.callback(0)
  54. def path_part2(self, *_):
  55. self.assertEquals(self.fired, True, "reactor.callLater is not working.")
  56. self.assertIsInstance(self.game.gamedata, GameData)
  57. self.game.dataReceived(
  58. b"The shortest path (9 hops, 27 turns) from sector 2565 to sector 2957 is:\n\r"
  59. )
  60. self.game.dataReceived(
  61. b"2565 > 5468 > 5128 > 238 > (3957) > (2531) > (1292) > (1892) > (4439) > (2957)\n\r"
  62. )
  63. reactor.callLater(0.1, self.path_part3)
  64. def path_user(self, user):
  65. self.fired = True
  66. def test_path(self):
  67. """ Verify that path detection is working.
  68. This needs a deferred(), in order for reactor.CallLater events
  69. to fire. (See: Observer.emit)
  70. """
  71. self.fired = False
  72. self.game.observer.connect("user-game", self.path_user)
  73. # emit user-game to init the game.gamedata (GameData) object.
  74. self.game.observer.emit("user-game", ("test", "A"))
  75. d = defer.Deferred()
  76. self.d = d
  77. reactor.callLater(0.1, self.path_part2)
  78. return d
  79. def trade1_part3(self, *_):
  80. print("How about now?")
  81. self.d.callback(0)
  82. def trade1_part2(self, *_):
  83. for line in self.p.trade_report:
  84. noascii = "".join([c if ord(c) < 0x7F else "" for c in line])
  85. print(cleanANSI(noascii))
  86. # print(self.p.trade_report)
  87. self.game.observer.load(self.p.save)
  88. self.p.keepalive.stop()
  89. self.p = None
  90. self.d.callback(0)
  91. # reactor.callLater(0.5, self.trade1_part3)
  92. def trade1_part1(self, *_):
  93. self.assertEquals(self.fired, True, "user-game emit ok.")
  94. ProxyMenu.awake = Mock()
  95. self.p = ProxyMenu(self.game)
  96. # self.p.keepalive.stop()
  97. # # stop the keepalive so the reactor is clean.
  98. z = coiterate(self.p.make_trade_report())
  99. z.addCallback(self.trade1_part2)
  100. def test_trade1(self):
  101. """ Attempt to run the trade script.
  102. Oh boy.
  103. """
  104. self.fired = False
  105. self.game.observer.connect("user-game", self.path_user)
  106. # emit user-game to init the game.gamedata (GameData) object.
  107. copyfile("../data_Z.json", "data_Z.json")
  108. self.game.observer.emit("user-game", ("data", "Z"))
  109. d = defer.Deferred()
  110. self.d = d
  111. reactor.callLater(1.5, self.trade1_part1)
  112. return d