storageRoot = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'account-mapping-coverage-' . uniqid(); File::makeDirectory($this->storageRoot . '/app', 0777, true, true); $this->app->useStoragePath($this->storageRoot); Cache::flush(); $this->createBillMirrorSchema(); } protected function tearDown(): void { if ($this->storageRoot !== '' && File::isDirectory($this->storageRoot)) { File::deleteDirectory($this->storageRoot); } parent::tearDown(); } public function test_missing_items_follow_current_mapping_without_rescanning_source_rows(): void { $store = app(AccountMappingStore::class); $this->rule($store, '天猫', '交易收款'); $this->rule($store, '天猫', '商户费'); $this->insertBill('taobao_alipay_bill_rows', 'occurred_at', [ ['store_name' => '天猫-测试店', 'bill_item' => '交易收款', 'day' => '2026-11-01 10:00:00'], ['store_name' => '天猫-测试店', 'bill_item' => 'wx商户费', 'day' => '2026-11-01 10:01:00'], ['store_name' => '天猫-测试店', 'bill_item' => '商家货款入账', 'day' => '2026-11-01 10:02:00'], ['store_name' => '天猫-另一店', 'bill_item' => '商家货款入账', 'day' => '2026-11-02 10:02:00'], ['store_name' => '天猫-测试店', 'bill_item' => '', 'day' => '2026-11-02 10:03:00'], ['store_name' => '天猫-测试店', 'bill_item' => '上期项目', 'day' => '2026-10-31 23:59:59'], ]); $this->insertBill('taobao_alipay_bill_rows', 'occurred_at', [ ['store_name' => '支付宝-测试店', 'bill_item' => '交易收款', 'day' => '2026-11-03 10:00:00'], ['store_name' => '支付宝-测试店', 'bill_item' => '交易收款', 'day' => '2026-11-03 10:01:00'], ['store_name' => '支付宝-测试店', 'bill_item' => '交易收款', 'day' => '2026-11-03 10:02:00'], ]); DB::connection('business_mirror')->table('jd_daily_bill_periods')->insert([ 'period_start' => '2026-11-01', 'period_end' => '2026-11-30', 'active_generation' => 'active-jd', 'source_filename' => 'jd-2026-11.xlsx', 'source_sha256' => str_repeat('a', 64), 'row_count' => 2, 'income' => 0, 'expense' => 0, 'imported_at' => '2026-12-01 10:00:00', ]); $this->insertBill('jd_daily_bill_rows', 'occurred_at', [ ['store_name' => '京东-测试店', 'bill_item' => '未映射售后费', 'day' => '2026-11-04 10:00:00', 'generation' => 'active-jd'], ['store_name' => '京东-测试店', 'bill_item' => '旧版本项目', 'day' => '2026-11-04 10:00:00', 'generation' => 'old-jd'], ]); $coverage = app(AccountMappingCoverage::class); $cold = $coverage->missingForMonth('2026-11'); $this->assertSame('missing', $cold['status']); $this->assertNull($coverage->claimPendingRefresh()); $queued = $coverage->missingForMonth('2026-11', false, true); $this->assertSame('refreshing', $queued['status']); $this->assertSame(8, $queued['retry_after']); $request = $coverage->claimPendingRefresh(); $this->assertNotNull($request); $this->assertSame('2026-11', $request['month']); $this->assertFileExists($request['path']); $coverage->refreshForMonth($request['month']); $coverage->finishPendingRefresh($request['path'], true); $this->assertFileDoesNotExist($request['path']); $this->assertSame([], glob($coverage->requestDirectory() . '/*.pending') ?: []); $first = $coverage->missingForMonth('2026-11'); $this->assertSame('ready', $first['status']); $this->assertSame(9, $first['source_rows']); $this->assertSame(4, $first['missing_item_count']); $this->assertSame(7, $first['missing_row_count']); $this->assertSame([ '交易收款|支付宝|3', '商家货款入账|天猫|2', '未映射售后费|京东|1', '(未填写账单项目)|天猫|1', ], $this->missingSignature($first['items'])); $this->assertNotContains('上期项目', array_column($first['items'], 'item')); $this->assertNotContains('wx商户费', array_column($first['items'], 'item')); $snapshotFiles = glob( $this->storageRoot . '/app/account-mapping/coverage-snapshots/*.json', ) ?: []; $this->assertCount(1, $snapshotFiles); $snapshot = json_decode(File::get($snapshotFiles[0]), true, 512, JSON_THROW_ON_ERROR); $snapshot['refreshed_at'] = '2025-01-01 00:00:00'; File::put($snapshotFiles[0], json_encode($snapshot, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)); Cache::flush(); $persisted = app(AccountMappingCoverage::class)->missingForMonth('2026-11'); $this->assertSame('ready', $persisted['status']); $this->assertSame(9, $persisted['source_rows']); $this->assertSame($this->missingSignature($first['items']), $this->missingSignature($persisted['items'])); $this->rule($store, '天猫', '商家货款入账'); $second = app(AccountMappingCoverage::class)->missingForMonth('2026-11'); $this->assertSame(3, $second['missing_item_count']); $this->assertSame(5, $second['missing_row_count']); $this->assertNotContains('商家货款入账', array_column($second['items'], 'item')); } public function test_interrupted_refresh_request_is_recovered_after_stale_timeout(): void { $coverage = app(AccountMappingCoverage::class); $coverage->missingForMonth('2026-11', false, true); $claimed = $coverage->claimPendingRefresh(); $this->assertNotNull($claimed); touch($claimed['path'], now()->subMinutes(20)->getTimestamp()); $recovered = $coverage->claimPendingRefresh(); $this->assertNotNull($recovered); $this->assertSame('2026-11', $recovered['month']); $coverage->finishPendingRefresh($recovered['path'], true); } private function rule(AccountMappingStore $store, string $platform, string $item): void { $store->create([ 'platform' => $platform, 'item' => $item, 'memo' => '', 'goods' => '', 'invoice' => '-', 'category' => '收入', 'dAcct' => '其他货币资金', 'dSub' => '', 'cAcct' => '主营业务收入', 'cSub' => '', 'basis' => '收入基准', ]); } /** @param list> $items @return list */ private function missingSignature(array $items): array { $rows = array_map( static fn (array $item): string => $item['item'] . '|' . $item['platform'] . '|' . $item['source_rows'], $items, ); sort($rows); return $rows; } private function insertBill(string $table, string $dateColumn, array $rows): void { foreach ($rows as $index => $row) { $values = [ 'store_name' => $row['store_name'], 'bill_item' => $row['bill_item'], $dateColumn => $row['day'], ]; if ($table === 'jd_daily_bill_rows') { $values += [ 'record_code' => 'row-' . $index . '-' . md5($row['store_name'] . $row['day']), 'income' => 0, 'expense' => 0, 'row_data' => '{}', ]; } if (isset($row['generation'])) { $values['generation'] = $row['generation']; } DB::connection('business_mirror')->table($table)->insert($values); } } private function createBillMirrorSchema(): void { $schema = Schema::connection('business_mirror'); $sources = [ 'taobao_alipay_bill_rows' => ['date' => 'occurred_at', 'periods' => null], 'taobao_wechat_bill_rows' => ['date' => 'occurred_at', 'periods' => null], 'pdd_settlement_bill_rows' => ['date' => 'occurred_at', 'periods' => null], 'jd_daily_bill_rows' => ['date' => 'occurred_at', 'periods' => 'jd_daily_bill_periods'], 'jd_wallet_bill_rows' => ['date' => 'occurred_at', 'periods' => 'jd_wallet_bill_periods'], 'douyin_authoritative_bills' => ['date' => 'occurred_at', 'periods' => 'douyin_authoritative_periods'], 'dewu_settlement_bill_rows' => ['date' => 'bill_date', 'periods' => null], ]; foreach ($sources as $table => $definition) { if (! $schema->hasTable($table)) { $schema->create($table, function ($blueprint) use ($definition): void { $blueprint->increments('id'); $blueprint->string('store_name')->nullable(); $blueprint->string('bill_item')->nullable(); if ($definition['date'] === 'bill_date') { $blueprint->date($definition['date']); } else { $blueprint->dateTime($definition['date']); } if ($definition['periods'] !== null) { $blueprint->string('generation')->nullable(); } }); } $periods = $definition['periods']; if ($periods !== null && ! $schema->hasTable($periods)) { $schema->create($periods, function ($blueprint): void { $blueprint->increments('id'); $blueprint->date('period_start'); $blueprint->date('period_end'); $blueprint->string('active_generation')->nullable(); }); } } } }