[ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', ], 'upstream_mirrors.connection' => 'business_mirror', ]); DB::purge('business_mirror'); $this->createTables(); } public function test_it_resolves_pending_stores_after_balance_equation_passes(): void { $this->seedPending($this->balancedRow()); $this->useSource([$this->balancedRow()]); $notifier = new class extends PinduoduoOpeningBalanceAlertNotifier { public int $calls = 0; public function send(array $issues): int { $this->calls++; return 1; } }; $this->app->instance(PinduoduoOpeningBalanceAlertNotifier::class, $notifier); $result = app(PinduoduoOpeningBalanceAutoResolver::class)->resolve(); $this->assertSame(1, $result['checked']); $this->assertSame(1, $result['resolved']); $this->assertSame(0, $result['pending']); $this->assertDatabaseHas(PinduoduoOpeningBalanceService::OPENING_TABLE, [ 'opening_month' => '2026-11-01', 'store_name' => '拼多多-测试店', 'opening_balance' => 0, ], 'business_mirror'); $this->assertDatabaseHas(PinduoduoPlatformBalanceImporter::TABLE, [ 'period_end' => '2026-11-30', 'store_name' => '拼多多-测试店', 'balance' => 80.15, ], 'business_mirror'); $this->assertDatabaseHas(PinduoduoOpeningBalanceService::TASK_TABLE, [ 'normalized_store_name' => '测试店', 'status' => 'resolved', 'resolution_status' => 'resolved', ], 'business_mirror'); $this->assertSame(0, $notifier->calls); } public function test_it_keeps_pending_and_alerts_only_when_the_failure_changes(): void { $row = $this->balancedRow(); $row['endingBalanceRpa'] = 70.15; $this->seedPending($row); $this->useSource([$row]); $notifier = new class extends PinduoduoOpeningBalanceAlertNotifier { public int $calls = 0; public function send(array $issues): int { $this->calls++; return 1; } }; $this->app->instance(PinduoduoOpeningBalanceAlertNotifier::class, $notifier); $resolver = app(PinduoduoOpeningBalanceAutoResolver::class); $first = $resolver->resolve(); $second = $resolver->resolve(); $this->assertSame(1, $first['pending']); $this->assertSame(1, $first['alerted']); $this->assertSame(1, $second['pending']); $this->assertSame(0, $second['alerted']); $this->assertDatabaseMissing(PinduoduoOpeningBalanceService::OPENING_TABLE, [ 'store_name' => '拼多多-测试店', ], 'business_mirror'); $this->assertDatabaseHas(PinduoduoOpeningBalanceService::TASK_TABLE, [ 'normalized_store_name' => '测试店', 'status' => 'pending', 'resolution_status' => 'failed', ], 'business_mirror'); $this->assertSame(1, $notifier->calls); } private function seedPending(array $source): void { $now = now(); DB::connection('business_mirror')->table('pdd_settlement_bill_rows')->insert([ 'store_name' => '拼多多-测试店', 'occurred_at' => '2026-11-02 10:00:00', ]); DB::connection('business_mirror')->table(PinduoduoOpeningBalanceService::TASK_TABLE)->insert([ 'store_name' => '拼多多-测试店', 'normalized_store_name' => '测试店', 'first_bill_month' => '2026-11-01', 'first_bill_at' => '2026-11-02 10:00:00', 'last_bill_at' => '2026-11-30 10:00:00', 'status' => 'pending', 'last_checked_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); } private function useSource(array $rows): void { $source = new class($rows) extends JushuitanPlatformBalanceClient { public function __construct(private readonly array $rows) { } public function fetchMonth(string $period): array { return $this->rows; } }; $this->app->instance(JushuitanPlatformBalanceClient::class, $source); } /** @return array */ private function balancedRow(): array { return [ 'platFormName' => '拼多多', 'groupName' => '拼多多货款账户', 'shopId' => 22203955, 'shopName' => '拼多多-测试店', 'openingBalance' => 0, 'currentIncome' => 100.25, 'currentExpenditure' => 20.10, 'endingBalance' => 80.15, 'endingBalanceRpa' => 80.15, 'haveOpeningBalance' => true, 'haveEndingBalanceRpa' => true, ]; } private function createTables(): void { $schema = Schema::connection('business_mirror'); $schema->create('pdd_settlement_bill_rows', function (Blueprint $table): void { $table->bigIncrements('id'); $table->string('store_name'); $table->dateTime('occurred_at'); }); $schema->create(PinduoduoOpeningBalanceService::TASK_TABLE, function (Blueprint $table): void { $table->bigIncrements('id'); $table->string('store_name'); $table->string('normalized_store_name')->unique(); $table->date('first_bill_month'); $table->dateTime('first_bill_at'); $table->dateTime('last_bill_at')->nullable(); $table->string('status', 20)->default('pending'); $table->date('resolved_opening_month')->nullable(); $table->dateTime('resolved_at')->nullable(); $table->dateTime('last_checked_at'); $table->string('resolution_status', 32)->nullable(); $table->string('resolution_message', 1000)->nullable(); $table->dateTime('resolution_checked_at')->nullable(); $table->char('alert_signature', 64)->nullable(); $table->dateTime('alerted_at')->nullable(); $table->timestamps(); }); $schema->create(PinduoduoOpeningBalanceService::OPENING_TABLE, function (Blueprint $table): void { $table->bigIncrements('id'); $table->date('opening_month'); $table->string('store_name'); $table->string('normalized_store_name'); $table->decimal('opening_balance', 20, 6); $table->string('source_type', 40); $table->string('source_filename')->nullable(); $table->char('source_sha256', 64)->nullable(); $table->dateTime('imported_at'); $table->timestamps(); $table->unique(['opening_month', 'normalized_store_name']); }); $schema->create(PinduoduoPlatformBalanceImporter::TABLE, function (Blueprint $table): void { $table->bigIncrements('id'); $table->date('period_end'); $table->string('store_name'); $table->string('normalized_store_name'); $table->string('account_name')->nullable(); $table->decimal('balance', 20, 6); $table->string('source_filename'); $table->char('source_sha256', 64); $table->dateTime('imported_at'); $table->timestamps(); $table->unique(['period_end', 'normalized_store_name']); }); } }