hasTable('business_mirror_table_states')) { $schema->create('business_mirror_table_states', function (Blueprint $table): void { $table->string('dataset_key')->primary(); foreach (['source_key','source_object','target_table','status','run_mode','generation','last_error_code'] as $key) $table->string($key)->nullable(); $table->text('cursor')->nullable(); foreach (['window_started_at','window_ends_at','initial_completed_at','last_full_succeeded_at','last_attempted_at','last_succeeded_at'] as $key) $table->dateTime($key)->nullable(); $table->integer('last_upserted_rows')->default(0); $table->integer('mirror_row_count')->default(0); $table->timestamps(); }); } if (! $schema->hasTable(ReturnedParcelSynchronizer::TABLE)) { (require base_path('database/migrations/2026_12_03_200000_create_courier_returned_parcel_mirror.php'))->up(); } } private function synchronizer(array $items): ReturnedParcelFakeSynchronizer { return new ReturnedParcelFakeSynchronizer($items); } private function item(int $id, string $received = '2026-11-02 12:00:00'): array { return ['Id'=>$id,'as_id'=>100,'asi_id'=>$id+1000,'receive_date'=>$received,'sku_id'=>'SKU-'.$id, 'i_id'=>'STYLE','name'=>'测试商品','properties_value'=>'白色','qty'=>2,'r_qty'=>1, 'price'=>'12.3400','amount'=>'24.6800','remark'=>'明细备注']; } public function test_sync_preserves_item_grain_boundaries_and_is_idempotent(): void { $items = [$this->item(1, '2026-09-30 23:59:59'), $this->item(2, '2026-10-01 00:00:00'), $this->item(3, '2026-12-02 23:59:59'), $this->item(4, '2026-12-03 00:00:00')]; $sync = $this->synchronizer($items); $this->assertSame(2, $sync->sync('2026-10-01','2026-12-02', null, 1)['rows']); $sync->sync('2026-10-01','2026-12-02', null, 1); $rows=DB::connection('business_mirror')->table(ReturnedParcelSynchronizer::TABLE)->orderBy('source_id')->get(); $this->assertSame([2,3], $rows->pluck('source_id')->all()); $this->assertSame([100,100], $rows->pluck('after_sales_id')->all()); $this->assertEquals(1, $rows[0]->returned_qty); $this->assertEquals(12.34, $rows[0]->price); $this->assertSame('TEST-RETURN', $rows[0]->waybill); } public function test_failed_sync_keeps_live_rows_and_resumes_from_committed_batch(): void { $sync=$this->synchronizer([$this->item(10)]); $sync->sync('2026-10-01','2026-12-02'); $sync=$this->synchronizer([$this->item(20),$this->item(21),$this->item(22)]); $sync->failAfter=1; try {$sync->sync('2026-10-01','2026-12-02', null, 1); $this->fail('Expected upstream failure');} catch (RuntimeException $e) {$this->assertSame('upstream_failed', $e->getMessage());} $db=DB::connection('business_mirror'); $this->assertSame([10], $db->table(ReturnedParcelSynchronizer::TABLE)->pluck('source_id')->all()); $this->assertSame('failed', $db->table('business_mirror_table_states')->value('status')); $sync->failAfter=null; $sync->readCursors=[]; $sync->sync('2026-10-01','2026-12-02', null, 1); $this->assertSame(20, $sync->readCursors[0]); $this->assertSame([20,21,22], $db->table(ReturnedParcelSynchronizer::TABLE)->orderBy('source_id')->pluck('source_id')->all()); } public function test_count_mismatch_keeps_previous_snapshot_and_resets_checkpoint(): void { $sync=$this->synchronizer([$this->item(1)]); $sync->sync('2026-10-01','2026-12-02'); $sync=$this->synchronizer([$this->item(2)]); $sync->countOffset=1; try {$sync->sync('2026-10-01','2026-12-02'); $this->fail('Expected count mismatch');} catch (RuntimeException $e) {$this->assertSame('source_count_changed_retry_required', $e->getMessage());} $db=DB::connection('business_mirror'); $this->assertSame([1], $db->table(ReturnedParcelSynchronizer::TABLE)->pluck('source_id')->all()); $this->assertNull($db->table('business_mirror_table_states')->value('cursor')); } public function test_page_reads_mirror_filters_received_date_and_paginates(): void { $items=[]; for($id=1;$id<=55;$id++) $items[]=$this->item($id); $items[]=$this->item(90,'2026-12-03 12:00:00'); $this->synchronizer($items)->sync('2026-10-01','2026-12-03'); $controller=app(ReturnedParcelController::class); $request=Request::create('/?from=2026-11-01&to=2026-11-30'); $data=$controller->page($request); $this->assertSame(55,$data['rows']->total()); $this->assertCount(50,$data['rows']->items()); $this->assertEquals(55,$data['summary']->quantity); $this->assertSame(1,(int)$data['summary']->orders_count); $filtered=$controller->page(Request::create('/?from=2026-11-01&to=2026-11-30&sku=SKU-2')); $this->assertSame(1,$filtered['rows']->total()); $this->assertSame(2,$filtered['rows']->first()->source_id); $section=collect(BusinessNavigation::all())->firstWhere('slug','courier-payables'); $this->assertSame('退回件明细',collect($section['pages'])->firstWhere('slug','returned-parcels')['title']); $this->user->attachPermission(Permission::where('name','read-admin-panel')->firstOrFail()); $this->loginAs()->get(route('business.pages.show',['section'=>'courier-payables','page'=>'returned-parcels','from'=>'2026-11-01','to'=>'2026-11-30'])) ->assertOk()->assertSeeText('进仓时间')->assertSeeText('测试商品')->assertSeeText('55 条商品明细')->assertSeeText('10:00、16:00'); } public function test_schedule_uses_managed_batch_at_both_requested_times(): void { foreach (['activate-release.sh','activate-incremental-release.sh'] as $file) { $text=file_get_contents(base_path('deploy/'.$file)); $this->assertStringContainsString('0 10,16 * * * root /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-returned-parcels', $text); } $this->assertStringContainsString('/usr/bin/timeout 1800', file_get_contents(base_path('deploy/run-finance-batch.sh'))); } public function test_returned_page_inherits_existing_shipping_visibility_only(): void { $this->user->roles()->sync([Role::where('name', 'user')->firstOrFail()->id]); $subject = 'user_' . $this->user->id; setting()->set('system.sidebar_permissions', ['configured' => [$subject], 'subjects' => [$subject => ['courier-payables/shipping-data']]]); setting()->save(); $this->assertContains('courier-payables/returned-parcels', SidebarPermissions::effectiveAllowedKeys($this->user)); $this->assertNotContains('courier-payables/courier-information', SidebarPermissions::effectiveAllowedKeys($this->user)); } public function test_export_contains_every_filtered_row_and_preserves_long_identifiers(): void { $items=[]; for($id=1;$id<=55;$id++)$items[]=$this->item($id); $this->synchronizer($items)->sync('2026-10-01','2026-12-02'); $this->user->attachPermission(Permission::where('name','read-admin-panel')->firstOrFail()); $response=$this->loginAs()->get(route('business.courier-payables.returned-parcels.export',[ 'from'=>'2026-11-01','to'=>'2026-11-30','columns'=>'online_order_no,sku', ]))->assertOk(); $csv=$response->streamedContent(); $this->assertSame(56, count(array_filter(explode("\n",$csv)))); $this->assertStringContainsString("'00001234567890123456", $csv); $this->assertStringContainsString("'SKU-55", $csv); $this->assertStringNotContainsString('明细金额', $csv); } public function test_command_rejects_invalid_dates_before_querying_source(): void { $this->artisan('business-mirror:sync-returned-parcels', ['--from'=>'2026-02-30','--to'=>'2026-12-02'])->assertFailed(); $this->artisan('business-mirror:sync-returned-parcels', ['--from'=>'2026-11-01','--to'=>'2026-10-01'])->assertFailed(); } } class ReturnedParcelFakeSynchronizer extends ReturnedParcelSynchronizer { public ?int $failAfter=null; public int $countOffset=0; public array $readCursors=[]; public function __construct(public array $source) {} protected function sourceCount(string $from,string $until): int { return count(array_filter($this->source,fn($row)=>$row['receive_date'] >= $from && $row['receive_date'] < $until))+$this->countOffset; } protected function items(string $from,string $until,int $last,int $limit): array { $this->readCursors[]=$last; if($this->failAfter!==null && count($this->readCursors)>$this->failAfter)throw new RuntimeException('upstream_failed'); return array_slice(array_values(array_filter($this->source,fn($row)=>$row['Id']>$last && $row['receive_date'] >= $from && $row['receive_date'] < $until)),0,$limit); } protected function headers(array $ids): array { $h=['Id'=>10,'as_id'=>100,'outer_as_id'=>'ONLINE-AS','o_id'=>99,'so_id'=>'00001234567890123456','l_id'=>'TEST-RETURN', 'logistics_company'=>'测试快递','shop_name'=>'测试店铺','warehouse'=>'测试仓库','status'=>'Confirmed','type'=>'退货退款', 'good_status'=>'已收货','question_type'=>'测试原因','refund'=>'24.6800','remark'=>'售后备注','updateTime'=>'2026-12-03 10:00:00']; return [100=>$h]; } }