LotteryBetSourceTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\LhcOrder;
  4. use App\Http\Controllers\admin\JisuGame;
  5. use App\Http\Controllers\admin\GameplayRule;
  6. use App\Http\Middleware\CheckButtonPermission;
  7. use App\Jobs\SendTelegramGroupMessageJob;
  8. use App\Services\AdminRealtime\AdminRealtimeRows;
  9. use App\Services\BetService;
  10. use App\Services\LotteryOdds;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Foundation\Testing\TestCase;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Bus;
  15. use Illuminate\Support\Facades\DB;
  16. use PHPUnit\Framework\Attributes\PreserveGlobalState;
  17. use PHPUnit\Framework\Attributes\RunInSeparateProcess;
  18. class LotteryBetSourceTest extends TestCase
  19. {
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. Model::clearBootedModels();
  24. config([
  25. 'database.default' => 'lottery_source_test',
  26. 'database.connections.lottery_source_test' => ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_'],
  27. 'logging.channels.dblog' => ['driver' => 'null'],
  28. 'cache.default' => 'array',
  29. 'admin_realtime.enabled' => false,
  30. ]);
  31. Bus::fake([SendTelegramGroupMessageJob::class]);
  32. DB::statement('CREATE TABLE bot_users (id INTEGER PRIMARY KEY, member_id TEXT, first_name TEXT, "from" INTEGER, is_banned INTEGER)');
  33. DB::statement('CREATE TABLE bot_wallets (id INTEGER PRIMARY KEY, member_id TEXT, available_balance NUMERIC, updated_at TEXT)');
  34. DB::statement('CREATE TABLE bot_config (id INTEGER PRIMARY KEY, field TEXT, val TEXT)');
  35. DB::statement('CREATE TABLE bot_gameplay_rules (id INTEGER PRIMARY KEY, keywords TEXT, groups TEXT, odds NUMERIC, mininum NUMERIC, maxinum NUMERIC, created_at TEXT, updated_at TEXT)');
  36. DB::statement('CREATE TABLE bot_jisu_gameplay (id INTEGER PRIMARY KEY, type INTEGER, gameplay TEXT, number TEXT, odds TEXT, mininum INTEGER, maxinum INTEGER)');
  37. DB::statement('CREATE TABLE bot_lhc_number (id INTEGER PRIMARY KEY, game TEXT, gameplay TEXT, number TEXT, odds TEXT, mininum INTEGER, maxinum INTEGER)');
  38. DB::statement('CREATE TABLE bot_keyboard (id INTEGER PRIMARY KEY, button TEXT)');
  39. foreach (['issues', 'pc_issues'] as $table) {
  40. DB::statement("CREATE TABLE bot_{$table} (id INTEGER PRIMARY KEY, issue_no TEXT, status INTEGER, end_time TEXT, winning_numbers TEXT, combo TEXT)");
  41. DB::table($table)->insert(['issue_no' => '123', 'status' => 1, 'end_time' => date('Y-m-d H:i:s', time() + 120)]);
  42. }
  43. DB::statement('CREATE TABLE bot_lhc_order (id INTEGER PRIMARY KEY, member_id TEXT, type INTEGER, issue TEXT, ordernum TEXT,
  44. odds NUMERIC, amount NUMERIC, total_amount NUMERIC, coupon_id INTEGER, lottery_status INTEGER DEFAULT 0, created_at INTEGER, updated_at INTEGER)');
  45. DB::statement('CREATE TABLE bot_lhc_order_item (id INTEGER PRIMARY KEY, member_id TEXT, ordernum TEXT, number TEXT, created_at INTEGER, updated_at INTEGER)');
  46. DB::statement('CREATE TABLE bot_balance_logs (id INTEGER PRIMARY KEY, member_id TEXT, amount NUMERIC, before_balance NUMERIC,
  47. after_balance NUMERIC, change_type TEXT, related_id INTEGER, remark TEXT, created_at TEXT, updated_at TEXT)');
  48. DB::table('users')->insert(['member_id' => '1001', 'first_name' => 'Alice', 'from' => -1, 'is_banned' => 0]);
  49. DB::table('wallets')->insert(['member_id' => '1001', 'available_balance' => 100]);
  50. DB::table('gameplay_rules')->insert(['keywords' => '大', 'groups' => '大小单双', 'odds' => 2, 'mininum' => 1, 'maxinum' => 100]);
  51. foreach (range(1, 7) as $type) {
  52. DB::table('jisu_gameplay')->insert(['type' => $type, 'gameplay' => '第一球', 'number' => '大', 'odds' => '1.95', 'mininum' => 1, 'maxinum' => 100]);
  53. }
  54. DB::table('lhc_number')->insert(['game' => '特码', 'gameplay' => '号码', 'number' => '07', 'odds' => '48.0', 'mininum' => 1, 'maxinum' => 100]);
  55. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_120000_create_lottery_odds_templates.php')->up();
  56. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_160000_add_sort_to_lottery_odds_templates.php')->up();
  57. DB::table('config')->insert([
  58. ['field' => 'service_customer', 'val' => 'test'],
  59. ['field' => 'maintenance_switch', 'val' => '0'],
  60. ['field' => 'pc28_switch', 'val' => '0'],
  61. ['field' => 'group_language', 'val' => 'zh'],
  62. ]);
  63. }
  64. protected function tearDown(): void
  65. {
  66. DB::disconnect();
  67. Model::clearBootedModels();
  68. parent::tearDown();
  69. }
  70. private function adminOrder(string $action, array $params = []): array
  71. {
  72. $this->app->instance('request', Request::create("/admin/lhcOrder/{$action}", 'GET', $params));
  73. $response = (new LhcOrder())->{$action}()->getData(true);
  74. $this->assertSame(0, $response['code'], $response['msg']);
  75. return $response['data'];
  76. }
  77. #[RunInSeparateProcess]
  78. #[PreserveGlobalState(false)]
  79. public function test_odds_and_28_catalog_writes_require_a_visible_granted_button(): void
  80. {
  81. DB::statement('CREATE TABLE bot_menus (id INTEGER PRIMARY KEY, uri TEXT, type INTEGER, status INTEGER,
  82. parent_id INTEGER, title TEXT, permission_name TEXT, created_at TEXT, updated_at TEXT)');
  83. DB::statement('CREATE TABLE bot_roles (id INTEGER PRIMARY KEY, name TEXT)');
  84. DB::statement('CREATE TABLE bot_role_user (id INTEGER PRIMARY KEY, role_id INTEGER, user_id INTEGER)');
  85. DB::statement('CREATE TABLE bot_role_menu (id INTEGER PRIMARY KEY, role_id INTEGER, menu_id INTEGER)');
  86. DB::table('roles')->insert(['id' => 5, 'name' => 'operator']);
  87. DB::table('role_user')->insert(['role_id' => 5, 'user_id' => 7]);
  88. $check = function (string $path, array $input = [], int $userId = 7): int {
  89. $request = Request::create($path, 'POST', $input);
  90. $request->user = (object) ['id' => $userId];
  91. $route = $this->app['router']->getRoutes()->match($request);
  92. $request->setRouteResolver(fn () => $route);
  93. $this->assertContains('admin.jwt', $route->middleware());
  94. $this->assertContains('check.button.uri', $route->middleware());
  95. return (new CheckButtonPermission())->handle($request, fn () => response()->json(['code' => 0]))->getData(true)['code'];
  96. };
  97. $odds = '/admin/jisuGame/setOdds';
  98. $templates = '/admin/jisuGame/saveTemplate';
  99. $lhc = ['game' => 'lhc'];
  100. foreach ([[$odds, $lhc], [$templates, $lhc], [$odds, ['game' => 'canada28']], [$odds, []],
  101. ['/admin/gameplayRule/submit', []], ['/admin/gameplayRule/delete', []]] as [$route, $input]) {
  102. $this->assertSame(-1, $check($route, $input), $route . ' must reject missing permission');
  103. $this->assertSame(0, $check($route, $input, 1));
  104. }
  105. $button = ['uri' => 'admin/lhcNumber/setOdds', 'type' => 2];
  106. $hidden = DB::table('menus')->insertGetId($button + ['status' => 2]);
  107. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $hidden]);
  108. $this->assertSame(-1, $check($odds, $lhc));
  109. $visible = DB::table('menus')->insertGetId($button + ['status' => 1]);
  110. $this->assertSame(-1, $check($templates, $lhc), 'A visible duplicate does not grant a hidden button');
  111. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $visible]);
  112. $this->assertSame(0, $check($templates . '?game=jisu', $lhc));
  113. $this->assertSame(-1, $check($odds . '?game=lhc', ['game' => 'jisu28']));
  114. $this->assertSame(-1, $check($odds, ['game' => ['lhc']]));
  115. $this->assertSame(-1, $check($odds));
  116. $jisu = DB::table('menus')->insertGetId(['uri' => 'admin/jisuGame/setOdds', 'type' => 2, 'status' => 1]);
  117. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $jisu]);
  118. $this->assertSame(0, $check($odds), 'Authorized legacy Jisu requests may omit the game');
  119. $pc28 = DB::table('menus')->insertGetId(['uri' => 'admin/gameplayRule/submit', 'type' => 2, 'status' => 1]);
  120. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $pc28]);
  121. $this->assertSame(0, $check($templates, ['game' => 'jisu28']));
  122. $this->assertSame(0, $check('/admin/gameplayRule/submit'));
  123. $this->assertSame(-1, $check('/admin/gameplayRule/delete'), 'Deletion requires its own grant');
  124. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_11_150000_register_lottery_odds_write_permissions.php';
  125. $migration->up();
  126. $migration->up();
  127. $deleteButtons = DB::table('menus')->where('uri', 'admin/gameplayRule/delete')->get();
  128. $this->assertCount(1, $deleteButtons);
  129. $this->assertEquals(0, $deleteButtons[0]->parent_id, 'Older installations can grant the button without the page menu');
  130. $this->assertEquals(2, DB::table('menus')->where('id', $hidden)->value('status'));
  131. $this->assertSame(-1, $check('/admin/gameplayRule/delete'), 'Provisioning does not grant a role permission');
  132. DB::table('role_menu')->insert(['role_id' => 5, 'menu_id' => $deleteButtons[0]->id]);
  133. $this->assertSame(0, $check('/admin/gameplayRule/delete'));
  134. }
  135. #[RunInSeparateProcess]
  136. #[PreserveGlobalState(false)]
  137. public function test_odds_templates_copy_and_edit_only_the_selected_game(): void
  138. {
  139. $this->assertSame(13, DB::table('lottery_odds_templates')->count());
  140. foreach (LotteryOdds::gameKeys() as $key) {
  141. $template = LotteryOdds::template($key);
  142. $actual = LotteryOdds::items($template->id)->orderBy('item_id')->get();
  143. $legacy = LotteryOdds::catalog($key)->orderBy('id')->get(['id', 'odds', 'mininum', 'maxinum']);
  144. $this->assertSame($legacy->pluck('id')->all(), $actual->pluck('item_id')->all());
  145. foreach (['odds', 'mininum', 'maxinum'] as $field) {
  146. $this->assertEquals($legacy->pluck($field)->all(), $actual->pluck($field)->all());
  147. }
  148. }
  149. $scope = ['game' => 'lhc', 'type' => 3];
  150. $call = function (string $action, array $input) {
  151. $this->app->instance('request', Request::create("/admin/jisuGame/{$action}", $action === 'list' ? 'GET' : 'POST', $input));
  152. return (new JisuGame())->{$action}()->getData(true);
  153. };
  154. $created = $call('saveTemplate', $scope + ['names' => ['zh' => '测试', 'en' => 'Test']]);
  155. $this->assertSame(0, $created['code'], $created['msg']);
  156. $template = $created['data'];
  157. $this->assertFalse($template['enabled']);
  158. $selection = ['odds_template_id' => $template['id']];
  159. $prices = ['group_game' => '特码', 'gameplay' => '号码', 'odds' => '49', 'mininum' => '2', 'maxinum' => '200'];
  160. $this->assertNotSame(0, $call('setOdds', ['game' => 'lhc', 'type' => 2] + $selection + $prices)['code']);
  161. $this->assertSame(0, $call('setOdds', $scope + $selection + $prices)['code']);
  162. $enabled = $call('saveTemplate', $scope + $selection + ['enabled' => true, 'names' => ['zh' => '澳门盘']]);
  163. $this->assertSame(['zh' => '澳门盘'], $enabled['data']['names']);
  164. $this->assertTrue($enabled['data']['enabled']);
  165. $this->assertGreaterThan($template['revision'], $enabled['data']['revision']);
  166. $this->assertSame('49', $call('list', $scope + $selection)['data']['data'][0]['children'][0]['children'][0]['odds']);
  167. $this->assertSame('48.0', $call('list', $scope)['data']['data'][0]['children'][0]['children'][0]['odds']);
  168. $this->assertSame('48.0', $call('list', ['game' => 'lhc', 'type' => 2])['data']['data'][0]['children'][0]['children'][0]['odds']);
  169. }
  170. #[RunInSeparateProcess]
  171. #[PreserveGlobalState(false)]
  172. public function test_new_plays_stay_unpriced_until_the_selected_template_is_saved(): void
  173. {
  174. $draft = LotteryOdds::saveTemplate('lhc:2', ['names' => ['zh' => '香港测试']]);
  175. $jisu = LotteryOdds::template('jisu:2');
  176. $hongKong = LotteryOdds::template('lhc:2');
  177. $newJisu = DB::table('jisu_gameplay')->insertGetId([
  178. 'type' => 2, 'gameplay' => '后三', 'number' => '杂六', 'odds' => '2.8', 'mininum' => 10, 'maxinum' => 500,
  179. ]);
  180. $newMarkSix = DB::table('lhc_number')->insertGetId([
  181. 'game' => '特码', 'gameplay' => '号码', 'number' => '08', 'odds' => '47', 'mininum' => 2, 'maxinum' => 300,
  182. ]);
  183. $this->assertSame([
  184. 'id' => $newJisu, 'name' => '杂六', 'configured' => false, 'odds' => null, 'mininum' => null, 'maxinum' => null,
  185. ], LotteryOdds::list('jisu:2', null)['data'][1]['children'][0]);
  186. $markSixItems = LotteryOdds::list('lhc:2', $draft['id'])['data'][0]['children'][0]['children'];
  187. $this->assertTrue($markSixItems[0]['configured']);
  188. $this->assertFalse($markSixItems[1]['configured']);
  189. $this->assertNull($markSixItems[1]['odds']);
  190. $this->assertSame([$markSixItems[0]['id']], LotteryOdds::pricedCatalog('lhc:2', $draft['id'])->pluck('id')->all());
  191. $this->assertSame($jisu->revision, LotteryOdds::template('jisu:2')->revision);
  192. $fresh = LotteryOdds::saveTemplate('lhc:2', ['names' => ['zh' => '香港新模板']]);
  193. $this->assertFalse(LotteryOdds::items($fresh['id'])->where('item_id', $newMarkSix)->exists());
  194. LotteryOdds::setOdds('jisu:2', null, ['id' => $newJisu, 'odds' => '2.9', 'mininum' => 3, 'maxinum' => 400]);
  195. LotteryOdds::setOdds('lhc:2', $draft['id'], [
  196. 'group_game' => '特码', 'gameplay' => '号码', 'odds' => '49', 'mininum' => 5, 'maxinum' => 200,
  197. ]);
  198. $this->assertSame('2.9', LotteryOdds::items($jisu->id)->where('item_id', $newJisu)->value('odds'));
  199. $this->assertFalse(LotteryOdds::items(LotteryOdds::template('jisu:3')->id)->where('item_id', $newJisu)->exists());
  200. $this->assertSame($jisu->revision + 1, LotteryOdds::template('jisu:2')->revision);
  201. $this->assertSame(['49', '49'], LotteryOdds::items($draft['id'])->orderBy('item_id')->pluck('odds')->all());
  202. $this->assertSame($draft['revision'] + 1, LotteryOdds::template('lhc:2', $draft['id'])->revision);
  203. $this->assertFalse(LotteryOdds::items($hongKong->id)->where('item_id', $newMarkSix)->exists());
  204. $this->assertFalse(LotteryOdds::items(LotteryOdds::template('lhc:3')->id)->where('item_id', $newMarkSix)->exists());
  205. $this->assertSame('48.0', LotteryOdds::items($hongKong->id)->where('item_id', 1)->value('odds'));
  206. }
  207. #[RunInSeparateProcess]
  208. #[PreserveGlobalState(false)]
  209. public function test_equivalent_enabled_values_do_not_invalidate_quotes(): void
  210. {
  211. $default = LotteryOdds::template('lhc:2');
  212. $draft = LotteryOdds::saveTemplate('lhc:2', ['names' => ['zh' => '香港测试']]);
  213. foreach ([true, 1, '1'] as $value) {
  214. $this->assertSame($default->revision, LotteryOdds::saveTemplate('lhc:2', [
  215. 'odds_template_id' => $default->id, 'enabled' => $value,
  216. ])['revision']);
  217. }
  218. foreach ([false, 0, '0'] as $value) {
  219. $this->assertSame($draft['revision'], LotteryOdds::saveTemplate('lhc:2', [
  220. 'odds_template_id' => $draft['id'], 'enabled' => $value,
  221. ])['revision']);
  222. }
  223. $this->assertSame($draft['revision'] + 1, LotteryOdds::saveTemplate('lhc:2', [
  224. 'odds_template_id' => $draft['id'], 'enabled' => '1',
  225. ])['revision']);
  226. }
  227. #[RunInSeparateProcess]
  228. #[PreserveGlobalState(false)]
  229. public function test_28_catalog_add_rename_and_delete_update_every_template(): void
  230. {
  231. $extra = LotteryOdds::saveTemplate('jisu28', ['names' => ['zh' => '测试']]);
  232. $setRequest = fn ($input) => $this->app->instance('request', Request::create('/admin/gameplayRule', 'POST', $input));
  233. $setRequest(['keywords' => '小', 'groups' => '大小单双', 'odds' => '1.95', 'mininum' => 1, 'maxinum' => 20]);
  234. $this->assertSame(0, (new GameplayRule())->store()->getData(true)['code']);
  235. $itemId = DB::table('gameplay_rules')->where('keywords', '小')->value('id');
  236. $templates = [LotteryOdds::template('canada28')->id, LotteryOdds::template('jisu28')->id, $extra['id']];
  237. foreach ($templates as $id) {
  238. $this->assertSame('1.95', LotteryOdds::items($id)->where('item_id', $itemId)->value('odds'));
  239. }
  240. $setRequest(['id' => $itemId, 'keywords' => '小单', 'groups' => '大小单双']);
  241. $this->assertSame(0, (new GameplayRule())->store()->getData(true)['code']);
  242. $this->assertSame('小单', DB::table('gameplay_rules')->where('id', $itemId)->value('keywords'));
  243. $this->assertSame($extra['revision'] + 2, LotteryOdds::template('jisu28', $extra['id'])->revision);
  244. $setRequest(['id' => $itemId]);
  245. $this->assertSame(0, (new GameplayRule())->destroy()->getData(true)['code']);
  246. $this->assertFalse(DB::table('lottery_odds_items')->whereIn('template_id', $templates)->where('item_id', $itemId)->exists());
  247. $this->assertTrue(LotteryOdds::items(LotteryOdds::template('jisu:2')->id)->where('item_id', $itemId)->exists());
  248. }
  249. #[RunInSeparateProcess]
  250. #[PreserveGlobalState(false)]
  251. public function test_telegram_uses_the_selected_default_regardless_of_template_sort(): void
  252. {
  253. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_06_130000_add_bet_source_to_lhc_order.php')->up();
  254. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_26_120000_create_maintenance_settings.php')->up();
  255. $original = LotteryOdds::template('canada28');
  256. $jisu = LotteryOdds::template('jisu28');
  257. $save = function (array $input) {
  258. $this->app->instance('request', Request::create('/admin/jisuGame/saveTemplate', 'POST', ['game' => 'canada28'] + $input));
  259. return (new JisuGame())->saveTemplate()->getData(true);
  260. };
  261. $created = $save(['names' => ['zh' => '新模板'], 'sort' => 10]);
  262. $this->assertSame(0, $created['code'], $created['msg']);
  263. $alternate = $created['data'];
  264. $this->assertFalse($alternate['enabled']);
  265. $this->assertSame(10, $alternate['sort']);
  266. $select = ['odds_template_id' => $alternate['id']];
  267. LotteryOdds::setOdds('canada28', $alternate['id'], ['id' => 1, 'odds' => '3.25', 'mininum' => 1, 'maxinum' => 100]);
  268. $this->assertNotSame(0, $save($select + ['make_default' => true])['code']);
  269. $this->assertNotSame(0, $save(['odds_template_id' => $original->id, 'enabled' => false])['code']);
  270. $originalList = LotteryOdds::list('canada28', null);
  271. $this->assertSame([$alternate['id'], $original->id], array_column($originalList['odds_templates'], 'id'));
  272. $this->assertSame($original->id, $originalList['odds_template_id']);
  273. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大10')['text']);
  274. $this->assertSame(0, $save($select + ['enabled' => true])['code']);
  275. $revision = LotteryOdds::template('canada28', $alternate['id'])->revision;
  276. $result = $save($select + ['make_default' => true, 'sort' => 2]);
  277. $this->assertSame(0, $result['code'], $result['msg']);
  278. $this->assertTrue($result['data']['is_default']);
  279. $this->assertSame($revision, $result['data']['revision']);
  280. $this->assertSame(1, DB::table('lottery_odds_templates')->where('game_key', 'canada28')->where('is_default', 1)->count());
  281. $this->assertSame($jisu->id, LotteryOdds::template('jisu28')->id);
  282. $this->assertSame(0, $save(['odds_template_id' => $original->id, 'enabled' => false])['code']);
  283. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大10')['text']);
  284. $orders = DB::table('lhc_order')->orderBy('id')->get();
  285. $this->assertEquals([2, 3.25], $orders->pluck('odds')->all());
  286. $this->assertSame([$original->id, $alternate['id']], $orders->pluck('odds_template_id')->all());
  287. }
  288. #[RunInSeparateProcess]
  289. #[PreserveGlobalState(false)]
  290. public function test_telegram_default_price_changes_create_a_separate_order(): void
  291. {
  292. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_06_130000_add_bet_source_to_lhc_order.php')->up();
  293. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_26_120000_create_maintenance_settings.php')->up();
  294. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大10')['text']);
  295. LotteryOdds::setOdds('canada28', null, ['id' => 1, 'odds' => '2.50', 'mininum' => 1, 'maxinum' => 100]);
  296. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大15')['text']);
  297. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大20')['text']);
  298. DB::table('config')->where('field', 'pc28_switch')->update(['val' => '1']);
  299. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大5')['text']);
  300. $orders = DB::table('lhc_order')->orderBy('id')->get();
  301. $this->assertEquals([2, 2.5, 2], $orders->pluck('odds')->all());
  302. $this->assertEquals([10, 35, 5], $orders->pluck('total_amount')->all());
  303. $this->assertSame([1, 2, 1], $orders->pluck('odds_template_revision')->all());
  304. $this->assertEquals(50, DB::table('wallets')->value('available_balance'));
  305. }
  306. #[RunInSeparateProcess]
  307. #[PreserveGlobalState(false)]
  308. public function test_orders_are_tagged_and_telegram_merges_only_its_own_bets(): void
  309. {
  310. $order = ['member_id' => '1001', 'type' => 5, 'issue' => '123', 'amount' => 10, 'total_amount' => 10, 'created_at' => time()];
  311. DB::table('lhc_order')->insert($order + ['ordernum' => 'old']);
  312. DB::table('lhc_order_item')->insert(['member_id' => '1001', 'ordernum' => 'old', 'number' => '大']);
  313. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_06_130000_add_bet_source_to_lhc_order.php')->up();
  314. // Telegram bets consult the maintenance rules (all closed by default).
  315. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_26_120000_create_maintenance_settings.php')->up();
  316. DB::table('lhc_order')->insert($order + ['ordernum' => 'web', 'bet_source' => 'frontend']);
  317. DB::table('lhc_order_item')->insert(['member_id' => '1001', 'ordernum' => 'web', 'number' => '大']);
  318. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大10')['text']);
  319. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大15')['text']);
  320. DB::table('config')->where('field', 'pc28_switch')->update(['val' => '1']);
  321. $this->assertStringContainsString('下注成功', BetService::bet('1001', '大20')['text']);
  322. $list = $this->adminOrder('list');
  323. $this->assertSame(4, $list['total']);
  324. $this->assertSame([-1], array_values(array_unique(array_column($list['data'], 'user_from'))));
  325. $telegram = $this->adminOrder('list', ['bet_source' => 'telegram', 'type' => 5]);
  326. $this->assertSame(1, $telegram['total']);
  327. $this->assertEquals(25, $telegram['data'][0]['total_amount']);
  328. $this->assertSame('telegram', $this->adminOrder('info', ['id' => $telegram['data'][0]['id']])['bet_source']);
  329. $push = AdminRealtimeRows::resolve('lhc_order', (string) $telegram['data'][0]['id'], 'created');
  330. $this->assertSame('telegram', $push[0]['data']['bet_source']);
  331. $this->assertSame(1, $this->adminOrder('list', ['bet_source' => 'telegram', 'type' => 6])['total']);
  332. $frontend = $this->adminOrder('list', ['bet_source' => 'frontend']);
  333. $this->assertSame(1, $frontend['total']);
  334. $this->assertEquals(10, $frontend['data'][0]['total_amount']);
  335. $this->assertSame('frontend', $this->adminOrder('info', ['id' => $frontend['data'][0]['id']])['bet_source']);
  336. DB::table('issues')->delete();
  337. $legacy = $this->adminOrder('list', ['bet_source' => 'unknown']);
  338. $this->assertSame(1, $legacy['total']);
  339. $this->assertEquals(10, $legacy['data'][0]['total_amount']);
  340. $this->assertNull($this->adminOrder('info', ['id' => $legacy['data'][0]['id']])['bet_source']);
  341. }
  342. }