vdsl-sync 0.6.0

File synchronization engine — N-location, pluggable store & backend
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
//! TopologyStore — Topology中心モデルのapplication-layer facade。
//!
//! TopologyFile(inode)+ LocationFile(directory entry)+ RouteGraph による
//! 分散ファイルストレージの統合API。
//!
//! # API カテゴリ
//!
//! - **Sync** — トポロジー全体の同期 ([`sync`], [`sync_route`])
//! - **File CRUD** — ファイル操作 ([`put`], [`get`], [`list`], [`delete`])
//! - **Status** — 監視 ([`status`])
//!
//! # 3フェーズパイプライン
//!
//! ```text
//! Phase 1: Ingest   — scan → TopologyDelta → TopologyFile/LocationFile更新
//! Phase 2: Distribute — TopologyFile × LocationFile → DistributeAction[]
//! Phase 3: Route     — DistributeAction + RouteGraph → PlannedTransfer → Transfer実行
//! ```

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::application::error::SyncError;
use crate::domain::file_type::FileType;
use crate::domain::fingerprint::FileFingerprint;
use crate::domain::graph::RouteGraph;
use crate::domain::location::LocationId;
use crate::domain::location_file::{self, LocationFile};
use crate::domain::plan::{plan_distribution, PlannedTransfer};
use tracing::{debug, info, trace};

use crate::domain::distribute::distribute_actions;
use crate::domain::topology_delta::TopologyDelta;
use crate::domain::topology_file::TopologyFile;
use crate::domain::transfer::{Transfer, TransferKind};
use crate::infra::location_file_store::LocationFileStore;
use crate::infra::topology_file_store::TopologyFileStore;
use crate::infra::transfer_store::TransferStore;

// =============================================================================
// Result types
// =============================================================================

/// sync()の結果。
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct TopologySyncResult {
    /// スキャンで検出されたファイル数。
    pub scanned: usize,
    /// Ingestで生成されたTopologyDelta数。
    pub ingested: usize,
    /// Distributeで生成されたDistributeAction数。
    pub distributed: usize,
    /// 作成されたTransfer数。
    pub transfers_created: usize,
    /// 検出されたコンフリクト。
    ///
    /// 複数Locationで同一ファイルが異なる内容に更新された場合に報告される。
    /// コンフリクトがあるファイルのUpdate転送は抑止される。
    pub conflicts: Vec<crate::domain::distribute::ConflictEntry>,
}

/// put()の結果。
#[derive(Debug, serde::Serialize)]
pub struct TopologyPutResult {
    /// 登録/更新されたTopologyFile ID。
    pub topology_file_id: String,
    /// 新規登録 = true、更新 = false。
    pub is_new: bool,
    /// 作成されたTransfer数。
    pub transfers_created: usize,
}

// =============================================================================
// TopologyStore
// =============================================================================

/// Topology中心モデルの分散ファイルストレージ。
///
/// 3つの永続化トレイトに依存し、ドメインロジックをオーケストレーションする。
pub struct TopologyStore {
    topology_files: Arc<dyn TopologyFileStore>,
    location_files: Arc<dyn LocationFileStore>,
    transfers: Arc<dyn TransferStore>,
    graph: RouteGraph,
    /// 全Locationの一覧(target_locations用)。
    locations: Vec<LocationId>,
}

impl TopologyStore {
    pub fn new(
        topology_files: Arc<dyn TopologyFileStore>,
        location_files: Arc<dyn LocationFileStore>,
        transfers: Arc<dyn TransferStore>,
        graph: RouteGraph,
        locations: Vec<LocationId>,
    ) -> Self {
        Self {
            topology_files,
            location_files,
            transfers,
            graph,
            locations,
        }
    }

    // =========================================================================
    // Sync — 全体同期
    // =========================================================================

    /// 全体同期: Ingest済みのTopologyDelta群を受け取り、
    /// Apply → Distribute → Route → Transfer作成 を実行する。
    ///
    /// Ingest(スキャン→TopologyDelta生成)は呼び出し元が実行する。
    /// この関数はその後の3ステップを担当する。
    ///
    /// # フロー
    ///
    /// 1. Apply: TopologyDelta → TopologyFile/LocationFile更新
    /// 2. Distribute: 全TopologyFile × 全LocationFile → DistributeAction[]
    /// 3. Route: DistributeAction + RouteGraph → PlannedTransfer → Transfer作成
    pub async fn sync(&self, deltas: &[TopologyDelta]) -> Result<TopologySyncResult, SyncError> {
        info!(delta_count = deltas.len(), "topology_store::sync: start");

        // Phase 1: Apply — TopologyDelta → TopologyFile/LocationFile更新
        let ingest_origins = self.apply_ingest(deltas).await?;
        info!(
            origins = ingest_origins.len(),
            "topology_store::sync: phase1 apply done"
        );

        // Phase 2: Distribute
        let active_tfs = self.topology_files.list_active(None, None).await?;
        let active_tf_refs: Vec<&TopologyFile> = active_tfs.iter().collect();
        let file_ids: Vec<&str> = active_tfs.iter().map(|tf| tf.id()).collect();
        debug!(
            active_files = active_tfs.len(),
            "topology_store::sync: loaded active topology_files"
        );

        let lf_map = self.location_files.list_by_files(&file_ids).await?;
        let lf_ref_map = to_ref_map(&lf_map);
        debug!(
            location_file_groups = lf_map.len(),
            "topology_store::sync: loaded location_files"
        );

        let dist_result = distribute_actions(
            &active_tf_refs,
            &lf_ref_map,
            &self.locations,
            &ingest_origins,
        );
        info!(
            actions = dist_result.actions.len(),
            conflicts = dist_result.conflicts.len(),
            "topology_store::sync: phase2 distribute done"
        );

        // 削除済みファイル → 各LocationFileのdestへDelete Transfer直接発行
        let deleted_tfs = self.topology_files.list_deleted().await?;
        trace!(
            deleted_tfs = deleted_tfs.len(),
            "topology_store::sync: checking deleted topology_files for delete transfers"
        );
        let mut delete_transfers_created = 0;
        let pending_dests = self.collect_pending_dests().await?;
        for dtf in &deleted_tfs {
            let lfs = self.location_files.list_by_file(dtf.id()).await?;
            let empty = HashSet::new();
            let pending = pending_dests.get(dtf.id()).unwrap_or(&empty);
            for lf in &lfs {
                let dest = lf.location_id().clone();
                if pending.contains(&dest) {
                    trace!(
                        file_id = %dtf.id(),
                        dest = %dest,
                        "topology_store::sync: delete transfer skipped (pending)"
                    );
                    continue;
                }
                let src = self
                    .locations
                    .iter()
                    .find(|l| *l != &dest)
                    .cloned()
                    .unwrap_or_else(|| dest.clone());
                if src == dest {
                    trace!(
                        file_id = %dtf.id(),
                        dest = %dest,
                        "topology_store::sync: delete transfer skipped (single location)"
                    );
                    continue;
                }
                trace!(
                    file_id = %dtf.id(),
                    src = %src,
                    dest = %dest,
                    "topology_store::sync: creating delete transfer"
                );
                let transfer = Transfer::new_delete(dtf.id().to_string(), src, dest)?;
                self.transfers.insert_transfer(&transfer).await?;
                delete_transfers_created += 1;
            }
        }
        if delete_transfers_created > 0 {
            debug!(
                count = delete_transfers_created,
                "topology_store::sync: delete transfers created"
            );
        }

        let distributed = dist_result.actions.len();

        // Phase 3: Route → Transfer作成(Send/Updateのみ)
        // 既存データ保持locationをMulti-source Dijkstraに渡す
        // Stale/Missing/Syncing のLocationFileはsource eligible ではないため除外
        let existing_presences: HashMap<String, HashSet<LocationId>> = lf_map
            .iter()
            .map(|(file_id, lfs)| {
                let locs: HashSet<LocationId> = lfs
                    .iter()
                    .filter(|lf| lf.state().is_source_eligible())
                    .map(|lf| lf.location_id().clone())
                    .collect();
                (file_id.clone(), locs)
            })
            .collect();
        let planned = plan_distribution(
            &dist_result.actions,
            &self.graph,
            &pending_dests,
            &existing_presences,
        );
        debug!(
            planned_count = planned.len(),
            "topology_store::sync: phase3 route planned"
        );

        let transfers_created = self.create_transfers(&planned).await? + delete_transfers_created;
        info!(
            transfers_created = transfers_created,
            "topology_store::sync: phase3 route done"
        );

        Ok(TopologySyncResult {
            scanned: 0, // 呼び出し元がセット
            ingested: deltas.len(),
            distributed,
            transfers_created,
            conflicts: dist_result.conflicts,
        })
    }

    // =========================================================================
    // Sync — 単一ルート
    // =========================================================================

    /// 単一ルート同期: src→dest の経路のみ処理する。
    ///
    /// dest側のLocationFileを確認し、不足・古いものだけDistribute + Transfer作成。
    pub async fn sync_route(
        &self,
        src: &LocationId,
        dest: &LocationId,
    ) -> Result<TopologySyncResult, SyncError> {
        let active_tfs = self.topology_files.list_active(None, None).await?;
        let active_tf_refs: Vec<&TopologyFile> = active_tfs.iter().collect();
        let file_ids: Vec<&str> = active_tfs.iter().map(|tf| tf.id()).collect();
        let lf_map = self.location_files.list_by_files(&file_ids).await?;
        let lf_ref_map = to_ref_map(&lf_map);

        // source=src, target=[dest] でDistribute
        let mut ingest_origins = HashMap::new();
        for tf in &active_tfs {
            // src側のLocationFileがActiveなファイルのみ対象
            if let Some(lfs) = lf_map.get(tf.id()) {
                if lfs
                    .iter()
                    .any(|lf| lf.location_id() == src && lf.state().is_source_eligible())
                {
                    ingest_origins
                        .entry(tf.id().to_string())
                        .or_insert_with(HashSet::new)
                        .insert(src.clone());
                }
            }
        }

        let dist_result = distribute_actions(
            &active_tf_refs,
            &lf_ref_map,
            std::slice::from_ref(dest),
            &ingest_origins,
        );

        let distributed = dist_result.actions.len();

        // Route: この場合src→destの直接Transferのみ(optimal_tree不要)
        let pending_dests = self.collect_pending_dests().await?;
        let transfers: Vec<PlannedTransfer> = dist_result
            .actions
            .iter()
            .filter_map(|action| {
                let file_id = action.topology_file_id();
                let empty = HashSet::new();
                let pending = pending_dests.get(file_id).unwrap_or(&empty);
                if pending.contains(dest) {
                    return None;
                }
                Some(PlannedTransfer {
                    file_id: file_id.to_string(),
                    src: src.clone(),
                    dest: dest.clone(),
                    kind: if action.is_delete() {
                        TransferKind::Delete
                    } else {
                        TransferKind::Sync
                    },
                    depends_on_index: None,
                })
            })
            .collect();

        let transfers_created = self.create_transfers(&transfers).await?;

        Ok(TopologySyncResult {
            scanned: 0,
            ingested: 0,
            distributed,
            transfers_created,
            conflicts: dist_result.conflicts,
        })
    }

    // =========================================================================
    // File CRUD
    // =========================================================================

    /// ファイル登録。
    ///
    /// TopologyFile + LocationFile(origin) を作成し、
    /// 全到達可能Locationへの転送を計画する。
    pub async fn put(
        &self,
        relative_path: &str,
        file_type: FileType,
        fingerprint: FileFingerprint,
        origin: &LocationId,
        embedded_id: Option<String>,
    ) -> Result<TopologyPutResult, SyncError> {
        // 既存チェック(path or canonical_hash)
        let existing = self.topology_files.get_by_path(relative_path).await?;

        let (tf, is_new) = if let Some(mut tf) = existing {
            // 既存 → canonical_hash昇格 + LocationFile更新
            tf.promote_canonical_digest(&fingerprint);
            self.topology_files.upsert(&tf).await?;
            (tf, false)
        } else {
            // 新規作成
            let mut tf = TopologyFile::new(relative_path.to_string(), file_type)
                .map_err(SyncError::Domain)?;
            tf.promote_canonical_digest(&fingerprint);
            self.topology_files.upsert(&tf).await?;
            (tf, true)
        };

        // LocationFile作成/更新
        let existing_lf = self.location_files.get(tf.id(), origin).await?;
        match existing_lf {
            Some(mut lf) => {
                lf.update_fingerprint(fingerprint.clone(), embedded_id);
                self.location_files.upsert(&lf).await?;
            }
            None => {
                let lf = tf
                    .materialize(
                        origin.clone(),
                        relative_path.to_string(),
                        fingerprint.clone(),
                        embedded_id,
                    )
                    .map_err(SyncError::Domain)?;
                self.location_files.upsert(&lf).await?;
            }
        }

        // Transfer計画: origin → 全到達可能Location
        let mut ingest_origins = HashMap::new();
        ingest_origins.insert(tf.id().to_string(), HashSet::from([origin.clone()]));

        let lfs = self.location_files.list_by_file(tf.id()).await?;
        let mut lf_map: HashMap<String, Vec<&LocationFile>> = HashMap::new();
        lf_map.insert(tf.id().to_string(), lfs.iter().collect());

        let dist_result = distribute_actions(&[&tf], &lf_map, &self.locations, &ingest_origins);

        let pending_dests = self.collect_pending_dests().await?;
        let sync_actions: Vec<_> = dist_result
            .actions
            .iter()
            .filter(|a| !a.is_delete())
            .cloned()
            .collect();
        let existing_presences: HashMap<String, HashSet<LocationId>> = {
            let locs: HashSet<LocationId> = lfs.iter().map(|lf| lf.location_id().clone()).collect();
            let mut m = HashMap::new();
            m.insert(tf.id().to_string(), locs);
            m
        };
        let planned = plan_distribution(
            &sync_actions,
            &self.graph,
            &pending_dests,
            &existing_presences,
        );

        let transfers_created = self.create_transfers(&planned).await?;

        Ok(TopologyPutResult {
            topology_file_id: tf.id().to_string(),
            is_new,
            transfers_created,
        })
    }

    /// ファイル取得。
    pub async fn get(&self, relative_path: &str) -> Result<Option<TopologyFileView>, SyncError> {
        let tf = match self.topology_files.get_by_path(relative_path).await? {
            Some(tf) => tf,
            None => return Ok(None),
        };
        let lfs = self.location_files.list_by_file(tf.id()).await?;
        Ok(Some(TopologyFileView {
            topology_file: tf,
            location_files: lfs,
        }))
    }

    /// ファイル一覧。
    pub async fn list(
        &self,
        file_type: Option<FileType>,
        limit: Option<usize>,
    ) -> Result<Vec<TopologyFileView>, SyncError> {
        let tfs = self.topology_files.list_active(file_type, limit).await?;
        let file_ids: Vec<&str> = tfs.iter().map(|tf| tf.id()).collect();
        let lf_map = self.location_files.list_by_files(&file_ids).await?;

        let views = tfs
            .into_iter()
            .map(|tf| {
                let lfs = lf_map.get(tf.id()).cloned().unwrap_or_default();
                TopologyFileView {
                    topology_file: tf,
                    location_files: lfs,
                }
            })
            .collect();
        Ok(views)
    }

    /// ファイル削除。
    ///
    /// TopologyFile mark_deleted + LocationFile保持先への Delete Transfer 直接発行。
    /// Intentベース: distribute/plan を経由せず各destへ直結N件。
    pub async fn delete(&self, relative_path: &str) -> Result<usize, SyncError> {
        let mut tf = self
            .topology_files
            .get_by_path(relative_path)
            .await?
            .ok_or_else(|| SyncError::NotRegistered(relative_path.to_string()))?;

        tf.mark_deleted();
        self.topology_files.upsert(&tf).await?;

        // LocationFileを持つ各LocationへDelete Transfer直接発行
        let lfs = self.location_files.list_by_file(tf.id()).await?;
        let pending_dests = self.collect_pending_dests().await?;
        let empty = HashSet::new();
        let pending = pending_dests.get(tf.id()).unwrap_or(&empty);

        let mut created = 0;
        for lf in &lfs {
            let dest = lf.location_id().clone();
            if pending.contains(&dest) {
                continue;
            }
            // src: dest以外の任意Location(Delete実行に物理srcは不要)
            let src = self
                .locations
                .iter()
                .find(|l| *l != &dest)
                .cloned()
                .unwrap_or_else(|| dest.clone());
            if src == dest {
                // 単一Location環境 — ローカル削除はTransfer不要
                continue;
            }
            let transfer = Transfer::new_delete(tf.id().to_string(), src, dest)?;
            self.transfers.insert_transfer(&transfer).await?;
            created += 1;
        }

        Ok(created)
    }

    // =========================================================================
    // Status
    // =========================================================================

    /// ファイル数。
    pub async fn file_count(&self) -> Result<usize, SyncError> {
        Ok(self.topology_files.count_active().await?)
    }

    /// Location一覧。
    pub fn locations(&self) -> &[LocationId] {
        &self.locations
    }

    // =========================================================================
    // Internal: Apply Ingest
    // =========================================================================

    /// TopologyDelta群をApply: TopologyFile/LocationFile更新。
    ///
    /// delta適用順序を正規化する:
    ///   Renamed(0) → ContentChanged(1) → Discovered(2) → Vanished(3)
    ///
    /// Renameが先に処理されることで、Rename先pathとDiscoveredのpath衝突を防ぐ。
    ///
    /// 返り値: file_id → ingest origin LocationId集合(Distribute用)。
    async fn apply_ingest(
        &self,
        deltas: &[TopologyDelta],
    ) -> Result<HashMap<String, HashSet<LocationId>>, SyncError> {
        let mut ingest_origins: HashMap<String, HashSet<LocationId>> = HashMap::new();

        // delta適用順を正規化: Renamed → ContentChanged → Discovered → Vanished
        let mut sorted_deltas: Vec<&TopologyDelta> = deltas.iter().collect();
        sorted_deltas.sort_by_key(|d| match d {
            TopologyDelta::Renamed(_) => 0,
            TopologyDelta::ContentChanged(_) => 1,
            TopologyDelta::Discovered(_) => 2,
            TopologyDelta::Vanished(_) => 3,
        });

        let total = sorted_deltas.len();
        let log_interval = (total / 10).max(1);

        for (i, delta) in sorted_deltas.iter().enumerate() {
            if i % log_interval == 0 {
                info!(progress = i, total = total, "apply_ingest: processing");
            }
            match delta {
                TopologyDelta::Discovered(d) => {
                    // 既存TopologyFileがあれば再利用(複数Locationが同一ファイルを
                    // Discoveredとして報告するケース)。なければ新規作成。
                    let existing = self.topology_files.get_by_path(&d.relative_path).await?;
                    let is_new = existing.is_none();
                    let mut tf = if let Some(existing) = existing {
                        trace!(
                            path = %d.relative_path,
                            tf_id = %existing.id(),
                            origin = %d.origin,
                            "apply_ingest: Discovered — reusing existing TopologyFile"
                        );
                        existing
                    } else {
                        trace!(
                            path = %d.relative_path,
                            origin = %d.origin,
                            size = d.fingerprint.size,
                            content_digest = ?d.fingerprint.content_digest,
                            "apply_ingest: Discovered — creating new TopologyFile"
                        );
                        TopologyFile::new(d.relative_path.clone(), d.file_type)
                            .map_err(SyncError::Domain)?
                    };
                    tf.promote_canonical_digest(&d.fingerprint);
                    self.topology_files.upsert(&tf).await?;

                    let lf = tf
                        .materialize(
                            d.origin.clone(),
                            d.relative_path.clone(),
                            d.fingerprint.clone(),
                            d.embedded_id.clone(),
                        )
                        .map_err(SyncError::Domain)?;
                    self.location_files.upsert(&lf).await?;

                    if is_new {
                        debug!(
                            path = %d.relative_path,
                            tf_id = %tf.id(),
                            origin = %d.origin,
                            "apply_ingest: NEW file registered"
                        );
                    }

                    ingest_origins
                        .entry(tf.id().to_string())
                        .or_default()
                        .insert(d.origin.clone());
                }
                TopologyDelta::ContentChanged(c) => {
                    trace!(
                        path = %c.relative_path,
                        tf_id = %c.topology_file_id,
                        origin = %c.origin,
                        old_size = c.old_fingerprint.size,
                        new_size = c.new_fingerprint.size,
                        "apply_ingest: ContentChanged"
                    );
                    // TopologyFile: canonical_hash昇格
                    if let Some(mut tf) = self.topology_files.get_by_id(&c.topology_file_id).await?
                    {
                        tf.promote_canonical_digest(&c.new_fingerprint);
                        self.topology_files.upsert(&tf).await?;
                    }

                    // LocationFile: fingerprint更新 or 新規作成
                    let existing_lf = self
                        .location_files
                        .get(&c.topology_file_id, &c.origin)
                        .await?;
                    match existing_lf {
                        Some(mut lf) => {
                            lf.update_fingerprint(c.new_fingerprint.clone(), c.embedded_id.clone());
                            self.location_files.upsert(&lf).await?;
                        }
                        None => {
                            if let Some(tf) =
                                self.topology_files.get_by_id(&c.topology_file_id).await?
                            {
                                let lf = tf
                                    .materialize(
                                        c.origin.clone(),
                                        c.relative_path.clone(),
                                        c.new_fingerprint.clone(),
                                        c.embedded_id.clone(),
                                    )
                                    .map_err(SyncError::Domain)?;
                                self.location_files.upsert(&lf).await?;
                            }
                        }
                    }

                    // 他LocationのLocationFileをStaleに(cross-location比較で実際に異なるもののみ)
                    let all_lfs = self
                        .location_files
                        .list_by_file(&c.topology_file_id)
                        .await?;
                    for stale_lf in
                        location_file::stale_candidates(&all_lfs, &c.origin, &c.new_fingerprint)
                    {
                        let mut lf = stale_lf.clone();
                        lf.mark_stale();
                        self.location_files.upsert(&lf).await?;
                    }

                    ingest_origins
                        .entry(c.topology_file_id.clone())
                        .or_default()
                        .insert(c.origin.clone());
                }
                TopologyDelta::Renamed(r) => {
                    trace!(
                        tf_id = %r.topology_file_id,
                        old_path = %r.old_path,
                        new_path = %r.new_path,
                        origin = %r.origin,
                        "apply_ingest: Renamed"
                    );
                    if let Some(mut tf) = self.topology_files.get_by_id(&r.topology_file_id).await?
                    {
                        tf.update_path(r.new_path.clone());
                        tf.promote_canonical_digest(&r.fingerprint);
                        self.topology_files.upsert(&tf).await?;
                    }

                    // LocationFile: fingerprint更新
                    let existing_lf = self
                        .location_files
                        .get(&r.topology_file_id, &r.origin)
                        .await?;
                    match existing_lf {
                        Some(mut lf) => {
                            lf.update_fingerprint(r.fingerprint.clone(), r.embedded_id.clone());
                            self.location_files.upsert(&lf).await?;
                        }
                        None => {
                            if let Some(tf) =
                                self.topology_files.get_by_id(&r.topology_file_id).await?
                            {
                                let lf = tf
                                    .materialize(
                                        r.origin.clone(),
                                        r.new_path.clone(),
                                        r.fingerprint.clone(),
                                        r.embedded_id.clone(),
                                    )
                                    .map_err(SyncError::Domain)?;
                                self.location_files.upsert(&lf).await?;
                            }
                        }
                    }

                    ingest_origins
                        .entry(r.topology_file_id.clone())
                        .or_default()
                        .insert(r.origin.clone());
                }
                TopologyDelta::Vanished(v) => {
                    trace!(
                        path = %v.relative_path,
                        tf_id = %v.topology_file_id,
                        origin = %v.origin,
                        "apply_ingest: Vanished"
                    );
                    // LocationFile: mark_missing
                    let existing_lf = self
                        .location_files
                        .get(&v.topology_file_id, &v.origin)
                        .await?;
                    if let Some(mut lf) = existing_lf {
                        lf.mark_missing();
                        self.location_files.upsert(&lf).await?;
                    }
                    // Vanished ではingest_originsに追加しない(消失はsourceにならない)
                    // NOTE: scan-based delete propagation (Vanished on local → mark_deleted)
                    // は ByHash誤判定によるpath conflict retire → 大量誤削除の問題があるため
                    // 撤回。削除は明示 delete() API のみで行う。
                }
            }
        }

        info!(
            processed = total,
            origins = ingest_origins.len(),
            "apply_ingest: done"
        );
        Ok(ingest_origins)
    }

    // =========================================================================
    // Internal: Transfer作成
    // =========================================================================

    /// PlannedTransfer群からTransferを作成しDBに書き込む。
    async fn create_transfers(&self, planned: &[PlannedTransfer]) -> Result<usize, SyncError> {
        let mut created = 0;
        // depends_on_index → 実Transfer IDのマッピング
        let mut transfer_ids: Vec<String> = Vec::with_capacity(planned.len());

        for pt in planned.iter() {
            trace!(
                file_id = %pt.file_id,
                src = %pt.src,
                dest = %pt.dest,
                kind = ?pt.kind,
                depends_on = ?pt.depends_on_index,
                "create_transfers: creating transfer"
            );
            let transfer = if let Some(dep_idx) = pt.depends_on_index {
                let dep_id = &transfer_ids[dep_idx];
                Transfer::with_dependency(
                    pt.file_id.clone(),
                    pt.src.clone(),
                    pt.dest.clone(),
                    pt.kind,
                    dep_id.clone(),
                )?
            } else {
                Transfer::with_kind(pt.file_id.clone(), pt.src.clone(), pt.dest.clone(), pt.kind)?
            };
            self.transfers.insert_transfer(&transfer).await?;
            transfer_ids.push(transfer.id().to_string());
            created += 1;
        }

        trace!(created = created, "create_transfers: done");
        Ok(created)
    }

    /// 未完了Transferのdest集合をfile_id別に収集する。
    async fn collect_pending_dests(
        &self,
    ) -> Result<HashMap<String, HashSet<LocationId>>, SyncError> {
        let pending = self.transfers.all_pending_transfers().await?;
        let mut map: HashMap<String, HashSet<LocationId>> = HashMap::new();
        for t in &pending {
            map.entry(t.file_id().to_string())
                .or_default()
                .insert(t.dest().clone());
        }
        Ok(map)
    }
}

// =============================================================================
// View types
// =============================================================================

/// TopologyFileとその全LocationFileを束ねたビュー。
#[derive(Debug, Clone, serde::Serialize)]
pub struct TopologyFileView {
    pub topology_file: TopologyFile,
    pub location_files: Vec<LocationFile>,
}

// =============================================================================
// Internal helpers
// =============================================================================

/// HashMap<String, Vec<LocationFile>> → HashMap<String, Vec<&LocationFile>>
fn to_ref_map(map: &HashMap<String, Vec<LocationFile>>) -> HashMap<String, Vec<&LocationFile>> {
    map.iter()
        .map(|(k, v)| (k.clone(), v.iter().collect()))
        .collect()
}

// =============================================================================
// Tests — 3フェーズパイプライン設計検証
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use chrono::{DateTime, Utc};
    use tokio::sync::Mutex;

    use crate::domain::location_file::LocationFileState;
    use crate::domain::topology_delta::{ContentChangedFile, DiscoveredFile, VanishedFile};
    use crate::domain::transfer::TransferState;
    use crate::infra::error::InfraError;
    use crate::infra::transfer_store::TransferStatRow;

    // =========================================================================
    // Mock stores — パイプライン検証に必要な最小実装
    // =========================================================================

    struct MockTopologyFileStore {
        files: Mutex<Vec<TopologyFile>>,
    }

    impl MockTopologyFileStore {
        fn new() -> Self {
            Self {
                files: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl TopologyFileStore for MockTopologyFileStore {
        async fn upsert(&self, file: &TopologyFile) -> Result<(), InfraError> {
            let mut files = self.files.lock().await;
            if let Some(pos) = files.iter().position(|f| f.id() == file.id()) {
                files[pos] = file.clone();
            } else {
                files.push(file.clone());
            }
            Ok(())
        }

        async fn get_by_id(&self, id: &str) -> Result<Option<TopologyFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .find(|f| f.id() == id)
                .cloned())
        }

        async fn get_by_path(&self, path: &str) -> Result<Option<TopologyFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .find(|f| f.relative_path() == path && f.deleted_at().is_none())
                .cloned())
        }

        async fn find_by_canonical_hash(
            &self,
            hash: &str,
        ) -> Result<Option<TopologyFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .find(|f| f.canonical_hash() == Some(hash) && f.deleted_at().is_none())
                .cloned())
        }

        async fn list_active(
            &self,
            file_type: Option<FileType>,
            limit: Option<usize>,
        ) -> Result<Vec<TopologyFile>, InfraError> {
            let files = self.files.lock().await;
            let mut result: Vec<_> = files
                .iter()
                .filter(|f| f.deleted_at().is_none())
                .filter(|f| file_type.is_none_or(|ft| f.file_type() == ft))
                .cloned()
                .collect();
            if let Some(n) = limit {
                result.truncate(n);
            }
            Ok(result)
        }

        async fn list_deleted(&self) -> Result<Vec<TopologyFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .filter(|f| f.deleted_at().is_some())
                .cloned()
                .collect())
        }

        async fn hard_delete(&self, id: &str) -> Result<bool, InfraError> {
            let mut files = self.files.lock().await;
            let len_before = files.len();
            files.retain(|f| !(f.id() == id && f.deleted_at().is_some()));
            Ok(files.len() < len_before)
        }

        async fn count_active(&self) -> Result<usize, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .filter(|f| f.deleted_at().is_none())
                .count())
        }

        async fn list_active_paths(&self) -> Result<Vec<String>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .filter(|f| f.deleted_at().is_none())
                .map(|f| f.relative_path().to_string())
                .collect())
        }
    }

    struct MockLocationFileStore {
        files: Mutex<Vec<LocationFile>>,
    }

    impl MockLocationFileStore {
        fn new() -> Self {
            Self {
                files: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl LocationFileStore for MockLocationFileStore {
        async fn upsert(&self, file: &LocationFile) -> Result<(), InfraError> {
            let mut files = self.files.lock().await;
            if let Some(pos) = files.iter().position(|f| {
                f.file_id() == file.file_id() && f.location_id() == file.location_id()
            }) {
                files[pos] = file.clone();
            } else {
                files.push(file.clone());
            }
            Ok(())
        }

        async fn get(
            &self,
            file_id: &str,
            location_id: &LocationId,
        ) -> Result<Option<LocationFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .find(|f| f.file_id() == file_id && f.location_id() == location_id)
                .cloned())
        }

        async fn list_by_file(&self, file_id: &str) -> Result<Vec<LocationFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .filter(|f| f.file_id() == file_id)
                .cloned()
                .collect())
        }

        async fn list_by_location(
            &self,
            location_id: &LocationId,
        ) -> Result<Vec<LocationFile>, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .filter(|f| f.location_id() == location_id)
                .cloned()
                .collect())
        }

        async fn list_by_files(
            &self,
            file_ids: &[&str],
        ) -> Result<HashMap<String, Vec<LocationFile>>, InfraError> {
            let files = self.files.lock().await;
            let mut map: HashMap<String, Vec<LocationFile>> = HashMap::new();
            for f in files.iter() {
                if file_ids.contains(&f.file_id()) {
                    map.entry(f.file_id().to_string())
                        .or_default()
                        .push(f.clone());
                }
            }
            Ok(map)
        }

        async fn delete(
            &self,
            file_id: &str,
            location_id: &LocationId,
        ) -> Result<bool, InfraError> {
            let mut files = self.files.lock().await;
            let before = files.len();
            files.retain(|f| !(f.file_id() == file_id && f.location_id() == location_id));
            Ok(files.len() < before)
        }

        async fn count_by_location(&self, location_id: &LocationId) -> Result<usize, InfraError> {
            Ok(self
                .files
                .lock()
                .await
                .iter()
                .filter(|f| f.location_id() == location_id)
                .count())
        }
    }

    struct MockTransferStore {
        transfers: Mutex<Vec<Transfer>>,
    }

    impl MockTransferStore {
        fn new() -> Self {
            Self {
                transfers: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl TransferStore for MockTransferStore {
        async fn insert_transfer(&self, transfer: &Transfer) -> Result<(), InfraError> {
            self.transfers.lock().await.push(transfer.clone());
            Ok(())
        }

        async fn update_transfer(&self, transfer: &Transfer) -> Result<(), InfraError> {
            let mut transfers = self.transfers.lock().await;
            if let Some(pos) = transfers.iter().position(|t| t.id() == transfer.id()) {
                transfers[pos] = transfer.clone();
            }
            Ok(())
        }

        async fn queued_transfers(&self, dest: &LocationId) -> Result<Vec<Transfer>, InfraError> {
            Ok(self
                .transfers
                .lock()
                .await
                .iter()
                .filter(|t| t.dest() == dest && t.state() == TransferState::Queued)
                .cloned()
                .collect())
        }

        async fn latest_transfers_by_file(
            &self,
            file_id: &str,
        ) -> Result<Vec<Transfer>, InfraError> {
            Ok(self
                .transfers
                .lock()
                .await
                .iter()
                .filter(|t| t.file_id() == file_id)
                .cloned()
                .collect())
        }

        async fn failed_transfers(&self) -> Result<Vec<Transfer>, InfraError> {
            Ok(Vec::new())
        }

        async fn prune_completed(&self, _before: DateTime<Utc>) -> Result<usize, InfraError> {
            Ok(0)
        }

        async fn count_queued(&self) -> Result<usize, InfraError> {
            Ok(self
                .transfers
                .lock()
                .await
                .iter()
                .filter(|t| t.state() == TransferState::Queued)
                .count())
        }

        async fn cancel_orphaned_inflight(&self) -> Result<usize, InfraError> {
            Ok(0)
        }

        async fn unblock_dependents(
            &self,
            _completed_transfer_id: &str,
        ) -> Result<usize, InfraError> {
            Ok(0)
        }

        async fn all_pending_transfers(&self) -> Result<Vec<Transfer>, InfraError> {
            Ok(self
                .transfers
                .lock()
                .await
                .iter()
                .filter(|t| {
                    t.state() == TransferState::Queued || t.state() == TransferState::Blocked
                })
                .cloned()
                .collect())
        }

        async fn transfer_stats(&self) -> Result<Vec<TransferStatRow>, InfraError> {
            Ok(Vec::new())
        }

        async fn present_counts_by_location(
            &self,
        ) -> Result<HashMap<LocationId, usize>, InfraError> {
            Ok(HashMap::new())
        }
    }

    // =========================================================================
    // Helpers
    // =========================================================================

    fn loc(name: &str) -> LocationId {
        LocationId::new(name).expect("valid location name")
    }

    fn fp(hash: &str, size: u64) -> FileFingerprint {
        use crate::domain::digest::{ByteDigest, ContentDigest};
        FileFingerprint {
            byte_digest: Some(ByteDigest::Djb2(hash.to_string())),
            content_digest: Some(ContentDigest(hash.to_string())),
            meta_digest: None,
            size,
            modified_at: None,
        }
    }

    /// local ⇄ pod ⇄ cloud の3拠点双方向グラフ。
    fn three_loc_setup() -> (RouteGraph, Vec<LocationId>) {
        let local = loc("local");
        let pod = loc("pod");
        let cloud = loc("cloud");
        let mut g = RouteGraph::new();
        g.add(local.clone(), pod.clone());
        g.add(pod.clone(), cloud.clone());
        g.add(pod.clone(), local.clone());
        g.add(cloud.clone(), pod.clone());
        (g, vec![local, pod, cloud])
    }

    fn make_store(
        tf: Arc<MockTopologyFileStore>,
        lf: Arc<MockLocationFileStore>,
        tr: Arc<MockTransferStore>,
    ) -> TopologyStore {
        let (graph, locations) = three_loc_setup();
        TopologyStore::new(tf, lf, tr, graph, locations)
    }

    fn discovered(path: &str, hash: &str, origin: &str) -> TopologyDelta {
        TopologyDelta::Discovered(DiscoveredFile {
            relative_path: path.to_string(),
            file_type: FileType::Image,
            fingerprint: fp(hash, 1024),
            origin: loc(origin),
            embedded_id: None,
        })
    }

    // =========================================================================
    // Pipeline Test 1: Discovered → Ingest → Distribute → Route
    //
    // 検証: 新規ファイルがlocalで検出された場合、
    //       TopologyFile/LocationFile作成後、pod/cloudへTransferが計画される。
    // =========================================================================

    #[tokio::test]
    async fn pipeline_discovered_creates_topology_and_routes_transfers() {
        let tf_s = Arc::new(MockTopologyFileStore::new());
        let lf_s = Arc::new(MockLocationFileStore::new());
        let tr_s = Arc::new(MockTransferStore::new());
        let store = make_store(tf_s.clone(), lf_s.clone(), tr_s.clone());

        let result = store
            .sync(&[discovered("output/gen-001.png", "abc123", "local")])
            .await
            .unwrap();

        // Phase 1 検証: TopologyFile 1件 + LocationFile(origin=local) 1件
        let tfs = tf_s.files.lock().await;
        assert_eq!(tfs.len(), 1);
        assert_eq!(tfs[0].relative_path(), "output/gen-001.png");
        assert!(
            tfs[0].canonical_hash().is_some(),
            "canonical_hash should be promoted from fingerprint"
        );

        let lfs = lf_s.files.lock().await;
        assert_eq!(lfs.len(), 1);
        assert_eq!(lfs[0].location_id(), &loc("local"));
        assert_eq!(lfs[0].state(), LocationFileState::Active);

        // Phase 2 検証: origin=local以外の2 locations (pod, cloud) へdistribute
        assert_eq!(result.ingested, 1);
        assert!(result.distributed >= 2, "pod + cloud へのDistributeAction");

        // Phase 3 検証: Transfer作成(local→pod→cloudの経路)
        let transfers = tr_s.transfers.lock().await;
        assert!(
            !transfers.is_empty(),
            "Transfers should be created for reachable locations"
        );
        let tf_id = tfs[0].id();
        for t in transfers.iter() {
            assert_eq!(t.file_id(), tf_id, "All transfers for same file");
            assert_ne!(t.src(), t.dest(), "No self-transfer");
        }
    }

    // =========================================================================
    // Pipeline Test 2: ContentChanged → Stale化 + 更新Transfer
    //
    // 検証: podに既存LocationFile(Active)がある状態で、localでコンテンツ変更。
    //       pod側がStaleになり、更新Transferが計画される。
    // =========================================================================

    #[tokio::test]
    async fn pipeline_content_changed_stales_others_and_creates_transfers() {
        let tf_s = Arc::new(MockTopologyFileStore::new());
        let lf_s = Arc::new(MockLocationFileStore::new());
        let tr_s = Arc::new(MockTransferStore::new());
        let store = make_store(tf_s.clone(), lf_s.clone(), tr_s.clone());

        // 初回: localで検出
        store
            .sync(&[discovered("output/img.png", "v1_hash", "local")])
            .await
            .unwrap();

        let tf_id = tf_s.files.lock().await[0].id().to_string();

        // podにLocationFile追加(Transfer完了をシミュレート)
        let pod_lf = LocationFile::new(
            tf_id.clone(),
            loc("pod"),
            "output/img.png".to_string(),
            fp("v1_hash", 1024),
            None,
        )
        .unwrap();
        lf_s.upsert(&pod_lf).await.unwrap();

        // 初回Transferをクリア
        tr_s.transfers.lock().await.clear();

        // localでコンテンツ変更
        let delta = TopologyDelta::ContentChanged(ContentChangedFile {
            topology_file_id: tf_id.clone(),
            relative_path: "output/img.png".to_string(),
            file_type: FileType::Image,
            old_fingerprint: fp("v1_hash", 1024),
            new_fingerprint: fp("v2_hash", 2048),
            origin: loc("local"),
            embedded_id: None,
        });

        let result = store.sync(&[delta]).await.unwrap();

        // pod側がStale
        let pod_lf = lf_s.get(&tf_id, &loc("pod")).await.unwrap().unwrap();
        assert_eq!(pod_lf.state(), LocationFileState::Stale);

        // local側はActive(更新元)
        let local_lf = lf_s.get(&tf_id, &loc("local")).await.unwrap().unwrap();
        assert_eq!(local_lf.state(), LocationFileState::Active);

        // 更新Transferが作成されている
        assert!(result.distributed > 0);
        assert!(result.transfers_created > 0);
    }

    // =========================================================================
    // Pipeline Test 3: Vanished → LocationFile Missing化
    //
    // 検証: localからファイルが消失した場合、LocationFileがMissingに遷移。
    //       TopologyFile自体は生存(他Locationに存在し得る)。
    // =========================================================================

    #[tokio::test]
    async fn pipeline_vanished_marks_location_file_missing() {
        let tf_s = Arc::new(MockTopologyFileStore::new());
        let lf_s = Arc::new(MockLocationFileStore::new());
        let tr_s = Arc::new(MockTransferStore::new());
        let store = make_store(tf_s.clone(), lf_s.clone(), tr_s.clone());

        store
            .sync(&[discovered("output/gone.png", "gone_hash", "local")])
            .await
            .unwrap();

        let tf_id = tf_s.files.lock().await[0].id().to_string();

        let delta = TopologyDelta::Vanished(VanishedFile {
            topology_file_id: tf_id.clone(),
            relative_path: "output/gone.png".to_string(),
            origin: loc("local"),
        });

        store.sync(&[delta]).await.unwrap();

        // LocationFileがMissing
        let lf = lf_s.get(&tf_id, &loc("local")).await.unwrap().unwrap();
        assert_eq!(lf.state(), LocationFileState::Missing);

        // TopologyFileは削除されていない(他Locationに存在し得る)
        let tf = tf_s.files.lock().await;
        assert!(tf[0].deleted_at().is_none());
    }

    // =========================================================================
    // Pipeline Test 4: 複数delta一括 → バッチパイプライン
    //
    // 検証: 3ファイル同時Discovered(origin混在)→ 各ファイルに対するTransfer。
    // =========================================================================

    #[tokio::test]
    async fn pipeline_batch_discovered_multi_origin() {
        let tf_s = Arc::new(MockTopologyFileStore::new());
        let lf_s = Arc::new(MockLocationFileStore::new());
        let tr_s = Arc::new(MockTransferStore::new());
        let store = make_store(tf_s.clone(), lf_s.clone(), tr_s.clone());

        let deltas = vec![
            discovered("a.png", "ha", "local"),
            discovered("b.png", "hb", "local"),
            discovered("c.png", "hc", "pod"), // pod origin
        ];

        let result = store.sync(&deltas).await.unwrap();

        assert_eq!(result.ingested, 3);
        assert_eq!(tf_s.files.lock().await.len(), 3);
        assert_eq!(lf_s.files.lock().await.len(), 3);

        // 各ファイルにorigin以外のlocationへのDistributeAction
        assert!(result.distributed >= 3);
        assert!(result.transfers_created >= 3);
    }

    // =========================================================================
    // Pipeline Test 5: put → sync パイプライン整合性
    //
    // 検証: put()で登録後、sync()の空delta呼び出しが既存データと矛盾しない。
    // =========================================================================

    #[tokio::test]
    async fn pipeline_put_then_empty_sync_is_consistent() {
        let tf_s = Arc::new(MockTopologyFileStore::new());
        let lf_s = Arc::new(MockLocationFileStore::new());
        let tr_s = Arc::new(MockTransferStore::new());
        let store = make_store(tf_s.clone(), lf_s.clone(), tr_s.clone());

        // put で1ファイル登録
        let put_result = store
            .put("x.png", FileType::Image, fp("xh", 100), &loc("local"), None)
            .await
            .unwrap();

        assert!(put_result.is_new);
        let initial_transfers = tr_s.transfers.lock().await.len();
        assert!(initial_transfers > 0);

        // 空delta sync — 既にpendingなTransferがあるので重複Transferは作られないはず
        let sync_result = store.sync(&[]).await.unwrap();

        assert_eq!(sync_result.ingested, 0);
        // distribute自体は走るが、pending重複で新規Transferは0(または少数)
        // ここではパイプラインがpanicせず完走することが重要
    }

    // =========================================================================
    // Pipeline Test 6: delete → Transfer計画
    //
    // 検証: ファイル削除後、LocationFileを持つLocationへのDelete Transferが計画される。
    // =========================================================================

    #[tokio::test]
    async fn pipeline_delete_creates_delete_transfers() {
        let tf_s = Arc::new(MockTopologyFileStore::new());
        let lf_s = Arc::new(MockLocationFileStore::new());
        let tr_s = Arc::new(MockTransferStore::new());
        let store = make_store(tf_s.clone(), lf_s.clone(), tr_s.clone());

        // ファイル登録
        store
            .put(
                "del.png",
                FileType::Image,
                fp("dh", 100),
                &loc("local"),
                None,
            )
            .await
            .unwrap();

        // podにもLocationFile追加
        let tf_id = tf_s.files.lock().await[0].id().to_string();
        let pod_lf = LocationFile::new(
            tf_id.clone(),
            loc("pod"),
            "del.png".to_string(),
            fp("dh", 100),
            None,
        )
        .unwrap();
        lf_s.upsert(&pod_lf).await.unwrap();

        // put時のTransferをクリア
        tr_s.transfers.lock().await.clear();

        // 削除
        let delete_count = store.delete("del.png").await.unwrap();

        // TopologyFileがdeleted
        let tf = tf_s.files.lock().await;
        assert!(tf[0].deleted_at().is_some());

        // LocationFile保持先(local, pod)へDelete Transfer直接発行
        // local: src=pod, dest=local / pod: src=local, dest=pod
        assert_eq!(delete_count, 2, "Delete Transfer for local + pod");
        let transfers = tr_s.transfers.lock().await;
        assert_eq!(transfers.len(), 2);
        for t in transfers.iter() {
            assert!(t.is_delete(), "All should be Delete kind");
            assert_ne!(t.src(), t.dest(), "No self-transfer");
        }
    }
}