From 924f2d1d8abb7a9e104b7cc0bae165d0817ac4f7 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Sat, 31 Oct 2026 11:03:11 +0100 Subject: [PATCH] test: market sends, offers, routes, lost villages and staying out of the rally lists Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/Feature/MarketTest.php | 360 +++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 tests/Feature/MarketTest.php diff --git a/tests/Feature/MarketTest.php b/tests/Feature/MarketTest.php new file mode 100644 index 0000000..100ea79 --- /dev/null +++ b/tests/Feature/MarketTest.php @@ -0,0 +1,360 @@ + $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'))); + +/** A player village at x|y with a market at $level (none at 0), storage 10 and this stock. */ +function trader(int $x, int $y, int $level = 5, array $stock = []): Village +{ + $village = startVillage(); + $village->update(['x' => $x, 'y' => $y, 'resources_at' => now(), ...$stock]); + $village->buildings()->where('type', 'storage')->update(['level' => 10]); + if ($level > 0) { + $village->buildings()->create(['type' => 'market', 'level' => $level]); + } + + return $village->fresh(); +} + +/** The market.show payload as the owner would get it now. */ +function market(Village $village): array +{ + return MarketPresenter::present(app(ShowMarket::class)(owner($village), $village->id), now()->toImmutable()); +} + +function ship(Village $from, Village $to, array $cargo): array +{ + return MarketPresenter::present(app(MarketDeals::class)->send(owner($from), $from->id, $to->x, $to->y, $cargo), now()->toImmutable()); +} + +/** Move the clock to hh:mm:ss today and let the worker run what is due. */ +function tradeAt(string $time): void +{ + test()->travelTo(CarbonImmutable::parse("2026-10-29 $time")); + test()->artisan('game:events --once')->assertSuccessful(); +} + +it('sends resources: they arrive, what doesn\'t fit is lost, and the merchants come home', function () { + $from = trader(100, 100, 5, ['wood' => 3000, 'clay' => 2000, 'iron' => 1000]); + // 5 fields away, with the start storage (1000). + $to = startVillage(); + $to->update(['x' => 103, 'y' => 104, 'wood' => 500, 'resources_at' => now()]); + + $state = ship($from, $to, ['wood' => 2500, 'clay' => 0, 'iron' => 0, 'food' => 100]); + expect($state['market'])->toMatchArray(['level' => 5, 'merchants' => 5, 'busy' => 3, 'free' => 2, 'carry' => 1000, 'resources' => ['wood', 'clay', 'iron'], 'max_ratio' => 3.0]) + ->and($state['trades'])->toHaveCount(1) + ->and($state['trades'][0])->toMatchArray([ + 'kind' => 'out', 'cargo' => ['wood' => 2500, 'clay' => 0, 'iron' => 0], 'merchants' => 3, + 'sent_at' => '2026-10-29T12:00:00.000Z', 'arrives_at' => '2026-10-29T12:30:00.000Z', 'route_id' => null, 'offer' => false, + ]) + ->and($state['trades'][0]['to'])->toMatchArray(['id' => $to->id, 'x' => 103, 'y' => 104]) + ->and(shown($from)['market'])->toMatchArray(['level' => 5, 'merchants' => 5, 'free' => 2, 'carry' => 1000]) + ->and(shown($from)['resources']['wood'])->toEqualWithDelta(500, 0.01) + ->and(shown($to)['market'])->toBeNull(); + // The receiver sees them coming, without a market of its own. + expect(market($to)['market'])->toBeNull() + ->and(market($to)['trades'][0])->toMatchArray(['kind' => 'in', 'cargo' => ['wood' => 2500, 'clay' => 0, 'iron' => 0]]); + + // The sender's own read catches up the arrival at the target. + $this->travel(1800)->seconds(); + $state = market($from); + expect($state['trades'][0])->toMatchArray(['kind' => 'back', 'cargo' => null, 'merchants' => 3, 'arrives_at' => '2026-10-29T13:00:00.000Z']) + ->and($state['market']['free'])->toBe(2) + ->and($to->fresh()->wood)->toEqualWithDelta(1000, 0.01) + ->and(market($to)['trades'])->toBe([]); + $report = Report::where('player_id', $to->player_id)->sole(); + expect($report->type)->toBe('trade') + ->and($report->payload)->toMatchArray(['cargo' => ['wood' => 2500, 'clay' => 0, 'iron' => 0], 'offer' => false]) + ->and($report->payload['from']['id'])->toBe($from->id) + ->and(Report::where('player_id', $from->player_id)->where('type', 'trade')->count())->toBe(1); + + $this->travel(1800)->seconds(); + expect(market($from))->toMatchArray(['trades' => []]) + ->and(market($from)['market']['free'])->toBe(5); +}); + +it('writes no report for deliveries between one player\'s villages', function () { + $from = trader(100, 100); + $other = app(VillageFactory::class)->create($from->world, $from->player, 'Second', 101, 100, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]); + + ship($from, $other, ['wood' => 100]); + $this->travel(1)->hours(); + $this->artisan('game:events --once')->assertSuccessful(); + + expect(Report::count())->toBe(0) + ->and($other->fresh()->wood)->toBeGreaterThan(599.0); +}); + +it('previews a trade and refuses what the market can\'t send', function () { + $from = trader(100, 100, 1, ['wood' => 3000]); + $to = trader(103, 104, 0); + $barbarian = app(VillageFactory::class)->create($from->world, null, 'Barbarian village', 99, 100, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]); + $plan = fn (int $x, int $y) => app(PlanTrade::class)(owner($from), $from->id, $x, $y); + $send = fn (Village $v, int $x, int $y, array $cargo) => app(MarketDeals::class)->send(owner($v), $v->id, $x, $y, $cargo); + + expect($plan(103, 104))->toMatchArray(['distance' => 5.0, 'seconds' => 1800, 'arrives_at' => '2026-10-29T12:30:00.000Z']) + ->and($plan(103, 104)['target']['id'])->toBe($to->id) + ->and(rule(fn () => $plan(150, 150)))->toBe('not_found') + ->and(rule(fn () => $plan(100, 100)))->toBe('same_village') + ->and(rule(fn () => $plan(99, 100)))->toBe('barbarian') + ->and(rule(fn () => $send($to, 100, 100, ['wood' => 10])))->toBe('no_market') + ->and(rule(fn () => $send($from, 100, 100, ['wood' => 10])))->toBe('same_village') + ->and(rule(fn () => $send($from, 99, 100, ['wood' => 10])))->toBe('barbarian') + ->and(rule(fn () => $send($from, 150, 150, ['wood' => 10])))->toBe('not_found') + ->and(rule(fn () => $send($from, 103, 104, ['wood' => 0, 'food' => 50])))->toBe('nothing') + ->and(rule(fn () => $send($from, 103, 104, ['clay' => 900])))->toBe('resources') + // One merchant at level 1 carries 1000. + ->and(rule(fn () => $send($from, 103, 104, ['wood' => 1001])))->toBe('merchants'); + + World::whereKey($from->world_id)->update(['ended_at' => now()]); + expect(rule(fn () => $send($from, 103, 104, ['wood' => 10])))->toBe('world_ended') + ->and(market($from)['market']['free'])->toBe(1); +}); + +it('reserves an offer\'s goods and merchants, and gives them back on cancel', function () { + $village = trader(100, 100, 5, ['wood' => 3000, 'clay' => 3000]); + $offer = fn (array $give, array $want, int $count, ?Village $v = null) => app(MarketDeals::class)->offer(owner($v ?? $village), ($v ?? $village)->id, $give[0], $give[1], $want[0], $want[1], $count); + + $state = MarketPresenter::present($offer(['wood', 1000], ['clay', 1500], 2), now()->toImmutable()); + expect($state['offers'])->toHaveCount(1) + ->and($state['offers'][0])->toMatchArray(['give' => ['resource' => 'wood', 'amount' => 1000], 'want' => ['resource' => 'clay', 'amount' => 1500], 'count' => 2, 'merchants' => 1]) + ->and($state['market'])->toMatchArray(['busy' => 2, 'free' => 3]) + ->and($village->fresh()->wood)->toEqualWithDelta(1000, 0.01); + + expect(rule(fn () => $offer(['wood', 100], ['clay', 301], 1)))->toBe('ratio') + ->and(rule(fn () => $offer(['wood', 301], ['clay', 100], 1)))->toBe('ratio') + ->and(rule(fn () => $offer(['wood', 100], ['wood', 100], 1)))->toBe('same_resource') + ->and(rule(fn () => $offer(['wood', 0], ['clay', 100], 1)))->toBe('amount') + ->and(rule(fn () => $offer(['clay', 100], ['wood', 100], 0)))->toBe('count') + ->and(rule(fn () => $offer(['wood', 600], ['clay', 600], 2)))->toBe('resources') + ->and(rule(fn () => $offer(['clay', 1100], ['wood', 1000], 3)))->toBe('resources') + // Three merchants are free. + ->and(rule(fn () => $offer(['clay', 500], ['wood', 500], 4)))->toBe('merchants') + ->and(rule(fn () => $offer(['wood', 10], ['clay', 10], 1, trader(110, 110, 0))))->toBe('no_market'); + + $id = TradeOffer::sole()->id; + $state = MarketPresenter::present(app(MarketDeals::class)->cancelOffer(owner($village), $village->id, $id), now()->toImmutable()); + expect($state['offers'])->toBe([]) + ->and($state['market']['free'])->toBe(5) + ->and($village->fresh()->wood)->toEqualWithDelta(3000, 0.01) + ->and(rule(fn () => app(MarketDeals::class)->cancelOffer(owner($village), $village->id, $id)))->toBe('not_found'); +}); + +it('accepts an offer: both shipments leave at once, and the count can only be taken once', function () { + $seller = trader(100, 100, 5, ['wood' => 3000, 'clay' => 0]); + $buyer = trader(103, 104, 5, ['wood' => 0, 'clay' => 5000]); + $late = trader(97, 96, 5, ['clay' => 5000]); + app(MarketDeals::class)->offer(owner($seller), $seller->id, 'wood', 1000, 'clay', 2000, 3); + $offer = TradeOffer::sole(); + + $list = app(ListOffers::class)(owner($buyer), $buyer->id); + expect($list['offers'])->toHaveCount(1) + ->and($list['offers'][0])->toMatchArray(['id' => $offer->id, 'count' => 3, 'distance' => 5.0, 'seconds' => 1800, 'own' => false]) + ->and($list['offers'][0]['village']['id'])->toBe($seller->id) + ->and(app(ListOffers::class)(owner($seller), $seller->id)['offers'][0]['own'])->toBeTrue() + ->and(app(ListOffers::class)(owner($buyer), $buyer->id, 'clay')['offers'])->toBe([]) + ->and(app(ListOffers::class)(owner($buyer), $buyer->id, 'wood', 'clay')['offers'])->toHaveCount(1); + + $accept = fn (Village $v, int $count) => app(MarketDeals::class)->accept(owner($v), $v->id, $offer->id, $count); + expect(rule(fn () => $accept($seller, 1)))->toBe('own_offer') + ->and(rule(fn () => $accept($buyer, 4)))->toBe('count') + ->and(rule(fn () => $accept($buyer, 0)))->toBe('count'); + + $state = MarketPresenter::present($accept($buyer, 2), now()->toImmutable()); + // 4000 clay on four merchants out, 2000 wood coming in on the seller's two. + expect(collect($state['trades'])->firstWhere('kind', 'out'))->toMatchArray(['cargo' => ['wood' => 0, 'clay' => 4000, 'iron' => 0], 'merchants' => 4, 'offer' => true]) + ->and(collect($state['trades'])->firstWhere('kind', 'in'))->toMatchArray(['cargo' => ['wood' => 2000, 'clay' => 0, 'iron' => 0], 'merchants' => 2, 'offer' => true, 'arrives_at' => '2026-10-29T12:30:00.000Z']) + ->and($state['market']['free'])->toBe(1) + ->and($offer->fresh()->count)->toBe(1) + ->and(market($seller)['market'])->toMatchArray(['busy' => 3, 'free' => 2]); + + // Two buyers for the last one: the first takes it, the second finds it gone. + $accept($late, 1); + expect(TradeOffer::count())->toBe(0) + ->and(rule(fn () => $accept($buyer, 1)))->toBe('not_found'); + + $this->travel(30)->minutes(); + $this->artisan('game:events --once')->assertSuccessful(); + expect($buyer->fresh()->wood)->toEqualWithDelta(2000 + 2.5, 0.1) + // 4000 from the buyer and 2000 from the late buyer. + ->and($seller->fresh()->clay)->toEqualWithDelta(6000 + 2.5, 0.1) + ->and(Report::where('player_id', $buyer->player_id)->where('type', 'trade')->get()->every(fn (Report $r) => $r->payload['offer'] === true))->toBeTrue() + ->and(Report::where('type', 'trade')->count())->toBe(8); +}); + +it('runs a trade route every interval with what the stock and the merchants allow', function () { + $from = trader(100, 100, 2, ['wood' => 5000]); + // 3 fields: 18 minutes each way. + $to = trader(103, 100, 0); + $create = fn (array $changes = []) => app(MarketRoutes::class)->create(owner($from), $from->id, $to->x, $to->y, ['wood' => 3000, 'clay' => 0, 'iron' => 0, ...$changes], $changes['every'] ?? 3600); + + $state = MarketPresenter::present($create(), now()->toImmutable()); + expect($state['routes'])->toHaveCount(1) + ->and($state['routes'][0])->toMatchArray([ + 'resources' => ['wood' => 3000, 'clay' => 0, 'iron' => 0], 'every' => 3600, 'next_at' => '2026-10-29T13:00:00.000Z', + 'paused' => false, 'paused_reason' => null, 'last_sent_at' => null, 'last_cargo' => null, + ]) + ->and($state['routes'][0]['to']['id'])->toBe($to->id); + $route = TradeRoute::sole(); + + // Two merchants carry 2000 of the 3000. + tradeAt('13:00:00'); + $state = market($from); + expect($state['routes'][0])->toMatchArray(['last_sent_at' => '2026-10-29T13:00:00.000Z', 'last_cargo' => ['wood' => 2000, 'clay' => 0, 'iron' => 0], 'next_at' => '2026-10-29T14:00:00.000Z']) + ->and($state['trades'][0])->toMatchArray(['kind' => 'out', 'route_id' => $route->id, 'merchants' => 2]); + tradeAt('13:18:00'); + tradeAt('13:36:00'); + expect(market($from)['market']['free'])->toBe(2) + ->and($to->fresh()->wood)->toEqualWithDelta(2500, 10); + + // A shipment by hand keeps both merchants busy at 14:00: nothing goes this time. + tradeAt('13:40:00'); + ship($from, $to, ['wood' => 2000]); + tradeAt('14:00:00'); + expect(market($from)['routes'][0])->toMatchArray(['last_sent_at' => '2026-10-29T14:00:00.000Z', 'last_cargo' => ['wood' => 0, 'clay' => 0, 'iron' => 0], 'next_at' => '2026-10-29T15:00:00.000Z']); + + // Back at 14:16; at 15:00 the stock is short of 2000, so all of it goes. + tradeAt('14:00:00'); + tradeAt('14:16:00'); + tradeAt('15:00:00'); + $cargo = market($from)['routes'][0]['last_cargo']; + expect($cargo['wood'])->toBeGreaterThan(1000)->toBeLessThan(1020) + ->and($from->fresh()->wood)->toBeLessThan(1); +}); + +it('pauses a route when the target changes hands, resumes it for the new owner, and deletes it', function () { + $from = trader(100, 100, 5, ['wood' => 5000]); + $to = trader(103, 100, 0); + $taker = trader(120, 120, 0); + app(MarketRoutes::class)->create(owner($from), $from->id, $to->x, $to->y, ['wood' => 500], 3600); + $route = TradeRoute::sole(); + + $this->travel(30)->minutes(); + Nobility::of($to->fresh())->handOver($to->fresh(), $taker->player_id, now()->toImmutable()); + Village::whereKey($to->id)->update(['player_id' => $taker->player_id]); + tradeAt('13:00:00'); + expect(market($from)['routes'][0])->toMatchArray(['paused' => true, 'paused_reason' => 'owner_changed', 'last_sent_at' => null]) + ->and(Movement::count())->toBe(0); + + $this->travel(10)->minutes(); + $update = fn (array $changes) => MarketPresenter::present(app(MarketRoutes::class)->update(owner($from), $from->id, $route->id, $changes), now()->toImmutable()); + $state = $update(['paused' => false, 'wood' => 800]); + expect($state['routes'][0])->toMatchArray(['paused' => false, 'paused_reason' => null, 'next_at' => '2026-10-29T14:10:00.000Z', 'resources' => ['wood' => 800, 'clay' => 0, 'iron' => 0]]) + ->and($route->fresh()->target_player_id)->toBe($taker->player_id); + tradeAt('14:10:00'); + expect(market($from)['routes'][0]['last_cargo'])->toBe(['wood' => 800, 'clay' => 0, 'iron' => 0]); + + // Pausing by hand drops the next run; a new interval only counts from the resume. + $state = $update(['paused' => true, 'every' => 7200]); + expect($state['routes'][0])->toMatchArray(['paused' => true, 'paused_reason' => null, 'every' => 7200]) + ->and(GameEvent::where('type', 'trade.route')->whereNull('processed_at')->count())->toBe(0) + ->and(rule(fn () => $update(['every' => 599])))->toBe('every') + ->and(rule(fn () => $update(['wood' => 0])))->toBe('nothing'); + + $state = MarketPresenter::present(app(MarketRoutes::class)->delete(owner($from), $from->id, $route->id), now()->toImmutable()); + expect($state['routes'])->toBe([]) + ->and(rule(fn () => app(MarketRoutes::class)->delete(owner($from), $from->id, $route->id)))->toBe('not_found'); +}); + +it('refuses routes without a market, to itself, to barbarians, of nothing, too often or too many', function () { + config(['game.market.route.max_routes' => 1]); + $from = trader(100, 100, 5); + $to = trader(103, 100, 0); + app(VillageFactory::class)->create($from->world, null, 'Barbarian village', 99, 100, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]); + $create = fn (int $x, int $y, array $amounts = ['wood' => 100], int $every = 3600, ?Village $v = null) => app(MarketRoutes::class)->create(owner($v ?? $from), ($v ?? $from)->id, $x, $y, $amounts, $every); + + expect(rule(fn () => $create(100, 100, v: $to)))->toBe('no_market') + ->and(rule(fn () => $create(100, 100)))->toBe('same_village') + ->and(rule(fn () => $create(99, 100)))->toBe('barbarian') + ->and(rule(fn () => $create(150, 150)))->toBe('not_found') + ->and(rule(fn () => $create(103, 100, ['food' => 100])))->toBe('nothing') + ->and(rule(fn () => $create(103, 100, every: 599)))->toBe('every') + ->and(rule(fn () => $create(103, 100, every: 604801)))->toBe('every'); + $create(103, 100); + expect(rule(fn () => $create(103, 100)))->toBe('max_routes'); +}); + +it('drops a lost village\'s trades, offers and routes, while trades to it still arrive', function () { + $lost = trader(100, 100, 10, ['wood' => 5000, 'clay' => 5000]); + $friend = trader(103, 100, 5, ['iron' => 3000]); + $far = trader(110, 100, 0); + + // Merchants on their way home, merchants on their way out, an offer and a route. + ship($lost, $friend, ['wood' => 1000]); + tradeAt('12:20:00'); + ship($lost, $far, ['clay' => 1000]); + app(MarketDeals::class)->offer(owner($lost), $lost->id, 'wood', 500, 'iron', 500, 2); + app(MarketRoutes::class)->create(owner($lost), $lost->id, $friend->x, $friend->y, ['wood' => 100], 3600); + // And iron coming in from a friend. + ship($friend, $lost, ['iron' => 2000]); + $before = $lost->fresh()->iron; + expect(Movement::count())->toBe(3); + + Abandon::player($lost->player, now()->toImmutable()); + expect(Movement::sole()->origin_id)->toBe($friend->id) + ->and(TradeOffer::count())->toBe(0) + ->and(TradeRoute::count())->toBe(0) + ->and(GameEvent::where('type', 'trade.route')->whereNull('processed_at')->count())->toBe(0); + + tradeAt('12:40:00'); + expect($lost->fresh()->iron)->toEqualWithDelta($before + 2000, 2) + ->and(Report::where('player_id', $friend->player_id)->where('type', 'trade')->latest('id')->first()->payload['to']['player'])->toBeNull(); +}); + +it('keeps merchants out of the rally lists, the map and the noble slots', function () { + $from = trader(100, 100, 5); + $to = trader(103, 100, 5); + $from->buildings()->create(['type' => 'rally_point', 'level' => 1]); + $from->troops()->create(['home_village_id' => $from->id, 'unit' => 'spear', 'count' => 5]); + + ship($from, $to, ['wood' => 100]); + ship($to, $from, ['clay' => 100]); + app(SendTroops::class)(owner($from), $from->id, 'support', $to->x, $to->y, ['spear' => 5], null, null); + + $map = app(ShowMap::class)(owner($from), $from->world_id, 100, 100, 15, 15); + expect(collect(shown($from)['movements'])->pluck('type')->all())->toBe(['support']) + ->and(collect(shown($to)['movements'])->pluck('type')->all())->toBe(['support']) + ->and(collect($map['movements'])->pluck('type')->all())->toBe(['support']) + ->and(shown($from)['conquest']['used'])->toBe(0) + ->and(rule(fn () => app(CancelMovement::class)(owner($from), $from->id, Movement::where('type', 'trade')->where('origin_id', $from->id)->value('id'))))->toBe('not_found'); + + // Merchants on their way home don't show either; the spearmen are still under way. + tradeAt('12:20:00'); + expect(Movement::where('type', 'trade_return')->count())->toBe(2) + ->and(collect(shown($from)['movements'])->pluck('type')->all())->toBe(['support']) + ->and(collect(shown($to)['movements'])->pluck('type')->all())->toBe(['support']) + ->and((array) shown($from)['troops'])->toBe([]); +}); + +it('files trade reports under their own kind', function () { + $from = trader(100, 100); + $to = trader(103, 100, 0); + ship($from, $to, ['wood' => 100]); + tradeAt('12:20:00'); + + $list = app(Reports::class)->list(owner($to), $to->world_id, 1, 'trade'); + expect($list['reports'])->toHaveCount(1) + ->and($list['reports'][0])->toMatchArray(['type' => 'trade', 'read' => false]) + ->and(app(Reports::class)->list(owner($to), $to->world_id, 1, 'attack')['reports'])->toBe([]); +});