wintf 0.0.2

Windows Tategaki Framework - Rust UI library with Japanese vertical text support
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
use super::*;
use crate::ecs::drag::{
    DragAccumulatorResource, DragTransition, WindowDragContext, WindowDragContextResource,
    install_drag_accumulator,
};
use bevy_ecs::world::World;

// NOTE: `DRAG_STATE` is a thread_local。テストワーカースレッドは再利用され得るため、
// 各テストは Idle へ強制リセットしてから開始する(前のテストが残した状態への依存を排除)。
// SetCapture/ReleaseCapture は HWND::default()(null)に対し UI スレッド外では実質 no-op の
// ため、状態機械そのものはデバイス非依存に検証できる(capture_guard_panic_safety_test と同じ前提)。

/// thread_local を Idle へ強制的に戻す(CaptureGuard が残っていれば borrow 解放後にドロップ)。
fn force_idle() {
    let _guard = update_drag_state(|state| {
        let old = std::mem::replace(state, DragState::Idle);
        // 旧状態に CaptureGuard があれば取り出して borrow 解放後にドロップさせる
        match old {
            DragState::Preparing { capture_guard, .. }
            | DragState::JustStarted { capture_guard, .. }
            | DragState::Dragging { capture_guard, .. } => Some(capture_guard),
            _ => None,
        }
    });
}

fn null_hwnd() -> HWND {
    HWND::default()
}

fn entity(idx: u32) -> Entity {
    // bevy の World 無しでテスト用 Entity を生成する(index→Entity の決定論的生成)
    Entity::from_raw_u32(idx).expect("valid entity index")
}

/// snapshot のバリアント判別子のみを比較するためのヘルパ。
fn variant_name(s: &DragStateSnapshot) -> &'static str {
    match s {
        DragStateSnapshot::Idle => "Idle",
        DragStateSnapshot::Preparing { .. } => "Preparing",
        DragStateSnapshot::JustStarted { .. } => "JustStarted",
        DragStateSnapshot::Dragging { .. } => "Dragging",
        DragStateSnapshot::JustEnded { .. } => "JustEnded",
    }
}

// --- start_preparing -----------------------------------------------------

/// Idle から start_preparing で Preparing へ遷移し、entity/start_pos を保持する。
#[test]
fn test_start_preparing_from_idle_enters_preparing() {
    force_idle();
    let e = entity(1);
    let pos = PhysicalPoint::new(10, 20);
    start_preparing(e, pos, null_hwnd());

    let snap = snapshot_drag_state();
    match snap {
        DragStateSnapshot::Preparing {
            entity, start_pos, ..
        } => {
            assert_eq!(entity, e);
            assert_eq!(start_pos.x, 10);
            assert_eq!(start_pos.y, 20);
        }
        other => panic!("expected Preparing, got {}", variant_name(&other)),
    }
    force_idle();
}

/// 既に Preparing 中の start_preparing は無視される(複数ボタン同時ドラッグ禁止)。
/// 既存の entity/start_pos が維持されることを確認する。
#[test]
fn test_start_preparing_ignored_when_already_active() {
    force_idle();
    let first = entity(1);
    let second = entity(2);
    start_preparing(first, PhysicalPoint::new(1, 1), null_hwnd());
    // 2 回目(別エンティティ・別座標)は無視されるはず
    start_preparing(second, PhysicalPoint::new(99, 99), null_hwnd());

    let snap = snapshot_drag_state();
    match snap {
        DragStateSnapshot::Preparing {
            entity, start_pos, ..
        } => {
            assert_eq!(entity, first, "最初の press が維持されるべき");
            assert_eq!((start_pos.x, start_pos.y), (1, 1));
        }
        other => panic!("expected Preparing, got {}", variant_name(&other)),
    }
    force_idle();
}

/// JustEnded からの start_preparing は許可される(前ドラッグ終了後の新規ドラッグ)。
#[test]
fn test_start_preparing_allowed_from_just_ended() {
    force_idle();
    let e1 = entity(1);
    start_preparing(e1, PhysicalPoint::new(5, 5), null_hwnd());
    end_dragging(PhysicalPoint::new(5, 5), false); // → JustEnded
    assert!(matches!(
        snapshot_drag_state(),
        DragStateSnapshot::JustEnded { .. }
    ));

    let e2 = entity(2);
    start_preparing(e2, PhysicalPoint::new(7, 8), null_hwnd());
    match snapshot_drag_state() {
        DragStateSnapshot::Preparing { entity, .. } => assert_eq!(entity, e2),
        other => panic!("expected Preparing, got {}", variant_name(&other)),
    }
    force_idle();
}

// --- start_dragging ------------------------------------------------------

/// Preparing → JustStarted 遷移。entity/start_pos は維持、current_pos が反映される。
#[test]
fn test_start_dragging_preparing_to_just_started() {
    force_idle();
    let e = entity(3);
    start_preparing(e, PhysicalPoint::new(10, 10), null_hwnd());
    start_dragging(PhysicalPoint::new(18, 14));

    match snapshot_drag_state() {
        DragStateSnapshot::JustStarted {
            entity,
            start_pos,
            current_pos,
            ..
        } => {
            assert_eq!(entity, e);
            assert_eq!((start_pos.x, start_pos.y), (10, 10));
            assert_eq!((current_pos.x, current_pos.y), (18, 14));
        }
        other => panic!("expected JustStarted, got {}", variant_name(&other)),
    }
    force_idle();
}

/// start_dragging は Preparing 以外では no-op(Idle のまま)。
#[test]
fn test_start_dragging_noop_when_not_preparing() {
    force_idle();
    start_dragging(PhysicalPoint::new(1, 1));
    assert!(
        matches!(snapshot_drag_state(), DragStateSnapshot::Idle),
        "Idle からの start_dragging は何もしないべき"
    );
    force_idle();
}

// --- update_dragging -----------------------------------------------------

/// JustStarted → Dragging 遷移。drag_context=None のとき HWND/位置/move_window/constraint は
/// デフォルト値(null HWND, (0,0), false, None)になる。
#[test]
fn test_update_dragging_just_started_to_dragging_without_context() {
    force_idle();
    let e = entity(4);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    start_dragging(PhysicalPoint::new(6, 6));
    update_dragging(PhysicalPoint::new(6, 6), None);

    match snapshot_drag_state() {
        DragStateSnapshot::Dragging {
            entity,
            current_pos,
            prev_pos,
            move_window,
            constraint,
            initial_window_pos,
            ..
        } => {
            assert_eq!(entity, e);
            assert_eq!((current_pos.x, current_pos.y), (6, 6));
            // 初回 Dragging では prev_pos == current_pos
            assert_eq!((prev_pos.x, prev_pos.y), (6, 6));
            assert!(!move_window, "context 無しでは move_window=false");
            assert!(constraint.is_none());
            assert_eq!((initial_window_pos.x, initial_window_pos.y), (0, 0));
        }
        other => panic!("expected Dragging, got {}", variant_name(&other)),
    }
    force_idle();
}

/// JustStarted → Dragging 遷移で WindowDragContextResource の hwnd/initial_window_pos/
/// move_window/constraint が DragState::Dragging に取り込まれる。
#[test]
fn test_update_dragging_reads_window_drag_context() {
    force_idle();
    let e = entity(5);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    start_dragging(PhysicalPoint::new(3, 3));

    let ctx_res = WindowDragContextResource::new();
    let constraint = DragConstraint {
        min_x: Some(-10),
        max_x: Some(500),
        min_y: None,
        max_y: None,
    };
    ctx_res.set(WindowDragContext {
        hwnd: Some(null_hwnd()),
        initial_window_pos: Some(Point { x: 100, y: 200 }),
        move_window: true,
        constraint: Some(constraint),
    });

    update_dragging(PhysicalPoint::new(3, 3), Some(&ctx_res));

    match snapshot_drag_state() {
        DragStateSnapshot::Dragging {
            move_window,
            initial_window_pos,
            constraint,
            ..
        } => {
            assert!(move_window, "context の move_window=true が反映されるべき");
            assert_eq!((initial_window_pos.x, initial_window_pos.y), (100, 200));
            let c = constraint.expect("constraint が反映されるべき");
            assert_eq!(c.min_x, Some(-10));
            assert_eq!(c.max_x, Some(500));
        }
        other => panic!("expected Dragging, got {}", variant_name(&other)),
    }
    force_idle();
}

/// Dragging → Dragging 更新で current_pos が新値、prev_pos が直前の current_pos になる。
#[test]
fn test_update_dragging_dragging_updates_prev_pos() {
    force_idle();
    let e = entity(6);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    start_dragging(PhysicalPoint::new(10, 10));
    update_dragging(PhysicalPoint::new(10, 10), None); // → Dragging (current=prev=10,10)
    update_dragging(PhysicalPoint::new(25, 30), None); // → current=25,30 / prev=10,10

    match snapshot_drag_state() {
        DragStateSnapshot::Dragging {
            current_pos,
            prev_pos,
            ..
        } => {
            assert_eq!((current_pos.x, current_pos.y), (25, 30));
            assert_eq!(
                (prev_pos.x, prev_pos.y),
                (10, 10),
                "prev_pos は直前の current_pos を保持するべき"
            );
        }
        other => panic!("expected Dragging, got {}", variant_name(&other)),
    }
    force_idle();
}

/// update_dragging は Idle/JustEnded など対象外状態では no-op。
#[test]
fn test_update_dragging_noop_when_idle() {
    force_idle();
    update_dragging(PhysicalPoint::new(5, 5), None);
    assert!(matches!(snapshot_drag_state(), DragStateSnapshot::Idle));
    force_idle();
}

// --- end_dragging --------------------------------------------------------

/// Preparing からの end_dragging は JustEnded(cancelled=false) へ。entity を保持。
#[test]
fn test_end_dragging_from_preparing() {
    force_idle();
    let e = entity(7);
    start_preparing(e, PhysicalPoint::new(40, 50), null_hwnd());
    assert_eq!(
        end_dragging(PhysicalPoint::new(41, 52), false),
        DragClose::Closed {
            entity: e,
            notified: false
        },
        "準備中からは種を積まない"
    );

    match snapshot_drag_state() {
        DragStateSnapshot::JustEnded {
            entity,
            position,
            cancelled,
        } => {
            assert_eq!(entity, e);
            assert_eq!((position.x, position.y), (41, 52));
            assert!(!cancelled);
        }
        other => panic!("expected JustEnded, got {}", variant_name(&other)),
    }
    force_idle();
}

/// Dragging からの end_dragging(cancelled=true 指定)は JustEnded(cancelled=true) へ。
#[test]
fn test_end_dragging_from_dragging_preserves_cancelled_flag() {
    force_idle();
    let e = entity(8);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    start_dragging(PhysicalPoint::new(9, 9));
    update_dragging(PhysicalPoint::new(9, 9), None);
    end_dragging(PhysicalPoint::new(60, 70), true);

    match snapshot_drag_state() {
        DragStateSnapshot::JustEnded {
            entity,
            position,
            cancelled,
        } => {
            assert_eq!(entity, e);
            assert_eq!((position.x, position.y), (60, 70));
            assert!(
                cancelled,
                "end_dragging に渡した cancelled=true が反映されるべき"
            );
        }
        other => panic!("expected JustEnded, got {}", variant_name(&other)),
    }
    force_idle();
}

/// Idle からの end_dragging は no-op(JustEnded を作らない)。
#[test]
fn test_end_dragging_noop_when_idle() {
    force_idle();
    assert_eq!(
        end_dragging(PhysicalPoint::new(1, 1), false),
        DragClose::NotActive
    );
    assert!(matches!(snapshot_drag_state(), DragStateSnapshot::Idle));
    force_idle();
}

// --- cancel_dragging -----------------------------------------------------

/// cancel_dragging は常に JustEnded(cancelled=true, position=start_pos) へ。
#[test]
fn test_cancel_dragging_uses_start_pos_and_sets_cancelled() {
    force_idle();
    let e = entity(9);
    start_preparing(e, PhysicalPoint::new(33, 44), null_hwnd());
    start_dragging(PhysicalPoint::new(80, 90)); // start_pos は 33,44 のまま
    cancel_dragging();

    match snapshot_drag_state() {
        DragStateSnapshot::JustEnded {
            entity,
            position,
            cancelled,
        } => {
            assert_eq!(entity, e);
            assert!(cancelled);
            assert_eq!(
                (position.x, position.y),
                (33, 44),
                "cancel は終了位置に start_pos を使うべき"
            );
        }
        other => panic!("expected JustEnded, got {}", variant_name(&other)),
    }
    force_idle();
}

/// Idle からの cancel_dragging は no-op。
#[test]
fn test_cancel_dragging_noop_when_idle() {
    force_idle();
    assert_eq!(cancel_dragging(), DragClose::NotActive);
    assert!(matches!(snapshot_drag_state(), DragStateSnapshot::Idle));
    force_idle();
}

// --- check_threshold -----------------------------------------------------

/// Preparing 中、ユークリッド距離の二乗が閾値の二乗以上なら true。
#[test]
fn test_check_threshold_true_at_or_beyond_distance() {
    force_idle();
    let e = entity(12);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    // (3,4) は距離 5 = 閾値ちょうど → true(>=)
    assert!(check_threshold(PhysicalPoint::new(3, 4), 5));
    // (10,0) は距離 10 > 5 → true
    assert!(check_threshold(PhysicalPoint::new(10, 0), 5));
    force_idle();
}

/// Preparing 中、距離が閾値未満なら false。
#[test]
fn test_check_threshold_false_below_distance() {
    force_idle();
    let e = entity(13);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    // (3,3) は距離 sqrt(18)≈4.24 < 5 → false
    assert!(!check_threshold(PhysicalPoint::new(3, 3), 5));
    force_idle();
}

/// Preparing 以外の状態では check_threshold は常に false(warn ログのみ)。
#[test]
fn test_check_threshold_false_when_not_preparing() {
    force_idle();
    // Idle 状態
    assert!(!check_threshold(PhysicalPoint::new(100, 100), 1));
    force_idle();
}

/// 負方向の座標差(start_pos > current_pos)でも dx*dx が正に評価され、
/// 距離判定が対称に働く(i32 乗算は符号で結果が変わらないことの特性化)。
#[test]
fn test_check_threshold_symmetric_for_negative_delta() {
    force_idle();
    let e = entity(20);
    // start_pos を正値にし、current_pos を左上へ動かして dx/dy を負にする
    start_preparing(e, PhysicalPoint::new(100, 100), null_hwnd());
    // (97,96) → dx=-3, dy=-4, 距離 5 = 閾値ちょうど → true(>= かつ符号非依存)
    assert!(check_threshold(PhysicalPoint::new(97, 96), 5));
    // (98,98) → dx=-2, dy=-2, 距離 sqrt(8)≈2.83 < 5 → false
    assert!(!check_threshold(PhysicalPoint::new(98, 98), 5));
    force_idle();
}

/// i16 実用座標の極値差(本番入力範囲の上限相当)でも i32 算術が桁あふれせず
/// 正確に評価される安全鎖の特性化。dx=32767, dy=0 → distance_sq=1_073_676_289
/// (< i32::MAX=2_147_483_647)で、閾値 5 を上回り true。
/// (ドキュメントの桁あふれ境界 |dx|>46340 には達しない実用上限を固定)。
#[test]
fn test_check_threshold_i16_extent_delta_no_overflow() {
    force_idle();
    let e = entity(21);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    // i16::MAX 相当の水平デルタ。dx*dx は i32 範囲内で桁あふれせず true。
    assert!(check_threshold(PhysicalPoint::new(32767, 0), 5));
    force_idle();
}

// --- snapshot ------------------------------------------------------------

/// DragState::snapshot が各バリアントを対応する DragStateSnapshot バリアントに写像する。
#[test]
fn test_snapshot_maps_each_variant() {
    force_idle();
    // Idle
    assert_eq!(variant_name(&snapshot_drag_state()), "Idle");

    let e = entity(14);
    // Preparing
    start_preparing(e, PhysicalPoint::new(1, 2), null_hwnd());
    assert_eq!(variant_name(&snapshot_drag_state()), "Preparing");

    // JustStarted
    start_dragging(PhysicalPoint::new(2, 3));
    assert_eq!(variant_name(&snapshot_drag_state()), "JustStarted");

    // Dragging
    update_dragging(PhysicalPoint::new(2, 3), None);
    assert_eq!(variant_name(&snapshot_drag_state()), "Dragging");

    // JustEnded
    end_dragging(PhysicalPoint::new(5, 5), false);
    assert_eq!(variant_name(&snapshot_drag_state()), "JustEnded");

    force_idle();
}

/// read_drag_state クロージャに現在の DragState 参照が渡される。
#[test]
fn test_read_drag_state_observes_current_state() {
    force_idle();
    let observed_idle = read_drag_state(|s| matches!(s, DragState::Idle));
    assert!(observed_idle);

    let e = entity(15);
    start_preparing(e, PhysicalPoint::new(0, 0), null_hwnd());
    let observed_preparing = read_drag_state(|s| matches!(s, DragState::Preparing { .. }));
    assert!(observed_preparing);
    force_idle();
}

// --- 休止の契約(解放・中断のあとは次の左押下まで休む) -------------------

/// 状態そのものと写しの両方の `is_button_held` が期待どおりで、写しの variant も一致する。
fn assert_held(expected_variant: &str, expected_held: bool) {
    assert_eq!(variant_name(&snapshot_drag_state()), expected_variant);
    assert_eq!(
        read_drag_state(|s| s.is_button_held()),
        expected_held,
        "DragState::is_button_held({expected_variant})"
    );
    assert_eq!(
        snapshot_drag_state().is_button_held(),
        expected_held,
        "DragStateSnapshot::is_button_held({expected_variant})"
    );
}

/// 述語の真偽表: 製品と同じ遷移関数で 5 状態を順に組み、
/// Preparing・JustStarted・Dragging で真、Idle・JustEnded で偽。
#[test]
fn test_is_button_held_truth_table_over_product_transitions() {
    force_idle();
    assert_held("Idle", false);
    start_preparing(entity(20), PhysicalPoint::new(0, 0), null_hwnd());
    assert_held("Preparing", true);
    start_dragging(PhysicalPoint::new(8, 0));
    assert_held("JustStarted", true);
    update_dragging(PhysicalPoint::new(9, 0), None);
    assert_held("Dragging", true);
    end_dragging(PhysicalPoint::new(9, 0), false);
    assert_held("JustEnded", false);
    force_idle();
}

/// 解放の契約: 解放の直後は JustEnded・述語は偽で、他の関数を呼ばずに
/// 別の entity の左押下がそのまま受け付けられる。
#[test]
fn test_release_rests_until_next_press() {
    force_idle();
    start_preparing(entity(21), PhysicalPoint::new(1, 1), null_hwnd());
    end_dragging(PhysicalPoint::new(1, 1), false);
    assert_held("JustEnded", false);

    start_preparing(entity(22), PhysicalPoint::new(2, 2), null_hwnd());
    match snapshot_drag_state() {
        DragStateSnapshot::Preparing { entity: e, .. } => assert_eq!(e, entity(22)),
        other => panic!("expected Preparing, got {}", variant_name(&other)),
    }
    force_idle();
}

/// 中断の契約: 移動を始めてからの中断でも、解放と同じく次の左押下まで休む。
#[test]
fn test_cancel_rests_until_next_press() {
    force_idle();
    start_preparing(entity(23), PhysicalPoint::new(1, 1), null_hwnd());
    start_dragging(PhysicalPoint::new(9, 1));
    cancel_dragging();
    assert_held("JustEnded", false);

    start_preparing(entity(24), PhysicalPoint::new(2, 2), null_hwnd());
    match snapshot_drag_state() {
        DragStateSnapshot::Preparing { entity: e, .. } => assert_eq!(e, entity(24)),
        other => panic!("expected Preparing, got {}", variant_name(&other)),
    }
    force_idle();
}

// --- 休ませた結果と終了の種(要件 2.1・2.2・3.1〜3.3・4.1〜4.3) -------------

/// このスレッドの控えへ新しい累積器を置き、World の資源と同じ実体の複製を返す(種を数えるため)。
fn install_handle() -> DragAccumulatorResource {
    let mut world = World::new();
    install_drag_accumulator(&mut world);
    world.resource::<DragAccumulatorResource>().clone()
}

/// 開始の種を手で積む(開始の種は `mouse_move.rs` が積み、`start_dragging` は積まない)。
fn push_started(acc: &DragAccumulatorResource, e: Entity) {
    acc.set_transition(DragTransition::Started {
        entity: e,
        start_pos: PhysicalPoint::new(0, 0),
        timestamp: Instant::now(),
    });
}

/// flush して終了の種だけを (対象, 位置, 取り消しか) で並べる。
fn ended_seeds(acc: &DragAccumulatorResource) -> Vec<(Entity, (i32, i32), bool)> {
    acc.flush()
        .expect("flush")
        .transitions
        .into_iter()
        .filter_map(|t| match t {
            DragTransition::Ended {
                entity,
                end_pos,
                cancelled,
            } => Some((entity, (end_pos.x, end_pos.y), cancelled)),
            DragTransition::Started { .. } => None,
        })
        .collect()
}

/// 押し → 閾値越え(`dragging` なら配る段の写し `update_dragging` で Dragging)まで進める。
fn drive_to(e: Entity, dragging: bool) {
    start_preparing(e, PhysicalPoint::new(10, 10), null_hwnd());
    start_dragging(PhysicalPoint::new(20, 10));
    if dragging {
        update_dragging(PhysicalPoint::new(30, 15), None);
    }
}

/// 休ませる 4 つの関数(離しは捕捉を取った窓=空の窓・位置なし)。
const CLOSERS: [(&str, fn() -> DragClose); 4] = [
    ("end_dragging", || {
        end_dragging(PhysicalPoint::new(1, 1), false)
    }),
    ("cancel_dragging", cancel_dragging),
    ("capture_lost", cancel_dragging_on_capture_lost),
    ("release", || end_dragging_on_release(HWND::default(), None)),
];

/// 開始済み(JustStarted・Dragging)からの `end_dragging` は、休ませて終了の種をちょうど 1 件積む。
#[test]
fn test_end_dragging_from_started_states_pushes_exactly_one_end_seed() {
    for dragging in [false, true] {
        force_idle();
        let acc = install_handle();
        let e = entity(30);
        push_started(&acc, e);
        drive_to(e, dragging);

        let result = end_dragging(PhysicalPoint::new(60, 70), false);

        assert_eq!(
            result,
            DragClose::Closed {
                entity: e,
                notified: true
            },
            "dragging={dragging}"
        );
        assert_eq!(ended_seeds(&acc), vec![(e, (60, 70), false)]);
        assert_held("JustEnded", false);
    }
    force_idle();
}

/// 開始済みからの `cancel_dragging` は、押した位置・取り消しの印つきの種を 1 件積む。
#[test]
fn test_cancel_dragging_from_started_states_pushes_cancelled_seed_at_press() {
    for dragging in [false, true] {
        force_idle();
        let acc = install_handle();
        let e = entity(31);
        push_started(&acc, e);
        drive_to(e, dragging);

        assert_eq!(
            cancel_dragging(),
            DragClose::Closed {
                entity: e,
                notified: true
            },
            "dragging={dragging}"
        );
        assert_eq!(ended_seeds(&acc), vec![(e, (10, 10), true)]);
    }
    force_idle();
}

/// 準備中(閾値前)からは休ませるが種は積まない(4 つの関数とも)。
/// 累積器が終了を受け付ける形(開始を積んである)にしておき、積めば見える状態で確かめる。
#[test]
fn test_closing_from_preparing_pushes_no_seed() {
    for (name, close) in CLOSERS {
        force_idle();
        let acc = install_handle();
        let e = entity(32);
        push_started(&acc, e);
        start_preparing(e, PhysicalPoint::new(10, 10), null_hwnd());

        assert_eq!(
            close(),
            DragClose::Closed {
                entity: e,
                notified: false
            },
            "{name}"
        );
        assert_held("JustEnded", false);
        assert!(ended_seeds(&acc).is_empty(), "{name}: 準備中から種を積んだ");
    }
    force_idle();
}

/// 押していない(Idle・JustEnded)ときは、4 つの関数とも何もせず NotActive。種も積まない。
#[test]
fn test_closing_when_not_held_returns_not_active() {
    for (name, close) in CLOSERS {
        force_idle();
        let acc = install_handle();
        assert_eq!(close(), DragClose::NotActive, "{name}: Idle");
        assert_held("Idle", false);

        let e = entity(33);
        push_started(&acc, e);
        drive_to(e, true);
        end_dragging(PhysicalPoint::new(5, 5), false);
        let _ = ended_seeds(&acc);
        assert_eq!(close(), DragClose::NotActive, "{name}: JustEnded");
        assert!(ended_seeds(&acc).is_empty(), "{name}: 休んだ後に種を積んだ");
    }
    force_idle();
}

/// 捕捉を取った窓と違う窓の離しは何もしない(OtherWindow・状態はそのまま)。同じ窓なら休ませる。
#[test]
fn test_release_on_other_window_returns_other_window_and_keeps_state() {
    let other = HWND(0x1 as *mut _);
    for dragging in [false, true] {
        force_idle();
        let acc = install_handle();
        let e = entity(34);
        push_started(&acc, e);
        drive_to(e, dragging);
        let before = variant_name(&snapshot_drag_state());

        assert_eq!(
            end_dragging_on_release(other, Some(PhysicalPoint::new(1, 1))),
            DragClose::OtherWindow
        );
        assert_eq!(variant_name(&snapshot_drag_state()), before);
        assert!(ended_seeds(&acc).is_empty());

        assert_eq!(
            end_dragging_on_release(null_hwnd(), Some(PhysicalPoint::new(40, 50))),
            DragClose::Closed {
                entity: e,
                notified: true
            }
        );
        assert_eq!(ended_seeds(&acc), vec![(e, (40, 50), false)]);
    }
    // 準備中でも同じ判断(窓が違えば何もしない)
    force_idle();
    start_preparing(entity(35), PhysicalPoint::new(1, 1), null_hwnd());
    assert_eq!(end_dragging_on_release(other, None), DragClose::OtherWindow);
    assert_held("Preparing", true);
    force_idle();
}

/// 離しに位置が無いときは、状態が持つ最後の画面の座標で終える
/// (JustStarted・Dragging は current_pos、Preparing は start_pos)。取り消しの印は付けない。
#[test]
fn test_release_without_position_uses_last_screen_position() {
    for (dragging, last) in [(false, (20, 10)), (true, (30, 15))] {
        force_idle();
        let acc = install_handle();
        let e = entity(36);
        push_started(&acc, e);
        drive_to(e, dragging);

        end_dragging_on_release(null_hwnd(), None);

        match snapshot_drag_state() {
            DragStateSnapshot::JustEnded {
                position,
                cancelled,
                ..
            } => {
                assert_eq!((position.x, position.y), last);
                assert!(!cancelled);
            }
            other => panic!("expected JustEnded, got {}", variant_name(&other)),
        }
        assert_eq!(ended_seeds(&acc), vec![(e, last, false)]);
    }

    force_idle();
    start_preparing(entity(37), PhysicalPoint::new(7, 8), null_hwnd());
    end_dragging_on_release(null_hwnd(), None);
    match snapshot_drag_state() {
        DragStateSnapshot::JustEnded { position, .. } => {
            assert_eq!((position.x, position.y), (7, 8))
        }
        other => panic!("expected JustEnded, got {}", variant_name(&other)),
    }
    force_idle();
}

/// 捕捉の喪失は、捕捉の守りに解放済みの印を付けてから取り消す
/// (守りが落ちても `ReleaseCapture` を呼ばない)。ふつうの取り消しは呼ぶ。
#[test]
fn test_capture_lost_marks_guard_released_before_cancelling() {
    let released_via_drop = |events: &[log_capture_kit::CapturedEvent]| {
        events
            .iter()
            .filter(|e| e.message().contains("Capture released via Drop"))
            .count()
    };

    force_idle();
    start_preparing(entity(38), PhysicalPoint::new(3, 4), null_hwnd());
    let (result, events) = log_capture_kit::capture(cancel_dragging_on_capture_lost);
    assert!(matches!(result, DragClose::Closed { .. }), "{result:?}");
    assert_eq!(released_via_drop(&events), 0, "{events:?}");
    match snapshot_drag_state() {
        DragStateSnapshot::JustEnded {
            position,
            cancelled,
            ..
        } => {
            assert!(cancelled);
            assert_eq!((position.x, position.y), (3, 4));
        }
        other => panic!("expected JustEnded, got {}", variant_name(&other)),
    }

    // 対照: ふつうの取り消しは守りが落ちるときに ReleaseCapture を呼ぶ
    force_idle();
    start_preparing(entity(39), PhysicalPoint::new(3, 4), null_hwnd());
    let (_, events) = log_capture_kit::capture(cancel_dragging);
    assert_eq!(released_via_drop(&events), 1, "{events:?}");
    force_idle();
}