@@ -597,3 +597,139 @@ fn canonical_and_kind_checks() { |
597 | 597 | DiditError::Version(_) |
598 | 598 | )); |
599 | 599 | } |
| 600 | +#[test] |
| 601 | +fn interval_gap_crossing_midnight_is_judged_across_the_seam() { |
| 602 | + // #5144: an Always expectation is one continuous active stretch, |
| 603 | + // but spans arrive per local day. A gap crossing midnight must be |
| 604 | + // judged as the single consecutive gap it is, not as two edge |
| 605 | + // gaps that each fit under the max. |
| 606 | + let outside = always(expectation( |
| 607 | + 0xE0, |
| 608 | + vec![dog_a()], |
| 609 | + "outside", |
| 610 | + Window::Interval { |
| 611 | + min_seconds: 0, |
| 612 | + max_seconds: 3_600, |
| 613 | + }, |
| 614 | + )); |
| 615 | + // 2026-06-15T00:00:00Z; an Always expectation evaluates in UTC. |
| 616 | + let day = 1_781_481_600; |
| 617 | + let mut records = Vec::new(); |
| 618 | + // Hourly through Jun 15, then 01:00 and 02:00 on Jun 16 — the |
| 619 | + // 00:00 record is skipped, so the consecutive gap across the |
| 620 | + // seam is 23:00 -> 01:00 = 7200s, twice the max. |
| 621 | + for hour in 0..24i64 { |
| 622 | + records.push(record( |
| 623 | + 0x30 + hour as u8, |
| 624 | + vec![dog_a()], |
| 625 | + "outside", |
| 626 | + day + hour * 3_600, |
| 627 | + )); |
| 628 | + } |
| 629 | + records.push(record(0x48, vec![dog_a()], "outside", day + 25 * 3_600)); |
| 630 | + records.push(record(0x49, vec![dog_a()], "outside", day + 26 * 3_600)); |
| 631 | + let now = day + 26 * 3_600 + 1_800; |
| 632 | + let verdicts = evaluate(&[outside], &records, now).expect("evaluate"); |
| 633 | + assert_eq!(verdicts.len(), 1, "the two days merge into one span"); |
| 634 | + let verdict = &verdicts[0]; |
| 635 | + assert_eq!(verdict.state, VerdictState::Violated); |
| 636 | + assert_eq!(verdict.count, 26); |
| 637 | + assert!( |
| 638 | + verdict.detail.contains("7200"), |
| 639 | + "the cross-seam gap is the violation: {}", |
| 640 | + verdict.detail |
| 641 | + ); |
| 642 | + // The verdict window reflects the merged continuous span. |
| 643 | + assert_eq!(verdict.window_start, day); |
| 644 | + assert_eq!(verdict.window_end, day + 2 * 86_400); |
| 645 | +} |
| 646 | + |
| 647 | +#[test] |
| 648 | +fn interval_gap_inside_one_day_still_violates() { |
| 649 | + // The same 7200s gap inside a single day was always judged by the |
| 650 | + // pair check; merging spans must keep that verdict (#5144 control). |
| 651 | + let outside = always(expectation( |
| 652 | + 0xE1, |
| 653 | + vec![dog_a()], |
| 654 | + "outside", |
| 655 | + Window::Interval { |
| 656 | + min_seconds: 0, |
| 657 | + max_seconds: 3_600, |
| 658 | + }, |
| 659 | + )); |
| 660 | + let day = 1_781_481_600; // 2026-06-15T00:00:00Z |
| 661 | + let mut records = Vec::new(); |
| 662 | + // Hourly through Jun 14, so the previous day is satisfied and the |
| 663 | + // merged span's first gap is zero. |
| 664 | + for hour in 0..24i64 { |
| 665 | + records.push(record( |
| 666 | + 0x30 + hour as u8, |
| 667 | + vec![dog_a()], |
| 668 | + "outside", |
| 669 | + day - 86_400 + hour * 3_600, |
| 670 | + )); |
| 671 | + } |
| 672 | + // Hourly through Jun 15 with 12:00 skipped: 11:00 -> 13:00 = 7200s. |
| 673 | + for hour in 0..24i64 { |
| 674 | + if hour == 12 { |
| 675 | + continue; |
| 676 | + } |
| 677 | + records.push(record( |
| 678 | + 0x50 + hour as u8, |
| 679 | + vec![dog_a()], |
| 680 | + "outside", |
| 681 | + day + hour * 3_600, |
| 682 | + )); |
| 683 | + } |
| 684 | + let now = day + 13 * 3_600 + 1_800; |
| 685 | + let verdicts = evaluate(&[outside], &records, now).expect("evaluate"); |
| 686 | + assert_eq!(verdicts.len(), 1); |
| 687 | + assert_eq!(verdicts[0].state, VerdictState::Violated); |
| 688 | + assert_eq!(verdicts[0].count, 47); |
| 689 | + assert!(verdicts[0].detail.contains("7200"), "{}", verdicts[0].detail); |
| 690 | +} |
| 691 | + |
| 692 | +#[test] |
| 693 | +fn interval_gap_crossing_sleep_window_seam_is_judged_across_it() { |
| 694 | + // #5144: a non-wrapping sleep window's active complement is cut |
| 695 | + // at midnight — yesterday's (wake, midnight) piece and today's |
| 696 | + // (midnight, sleep) piece are one continuous stretch, and a gap |
| 697 | + // across that seam is one consecutive gap. |
| 698 | + let mut outside = expectation( |
| 699 | + 0xE2, |
| 700 | + vec![dog_a()], |
| 701 | + "outside", |
| 702 | + Window::Interval { |
| 703 | + min_seconds: 0, |
| 704 | + max_seconds: 3_600, |
| 705 | + }, |
| 706 | + ); |
| 707 | + // Asleep 01:00-07:00 UTC; active 07:00 -> 01:00 across midnight. |
| 708 | + outside.active = ActiveWindow::SleepWindow { |
| 709 | + from_minute: 60, |
| 710 | + to_minute: 420, |
| 711 | + tz: "UTC".to_owned(), |
| 712 | + }; |
| 713 | + let day = 1_781_481_600; // 2026-06-15T00:00:00Z |
| 714 | + let mut records = Vec::new(); |
| 715 | + // Hourly 07:00-23:00 on Jun 15. |
| 716 | + for hour in 7..24i64 { |
| 717 | + records.push(record( |
| 718 | + 0x30 + hour as u8, |
| 719 | + vec![dog_a()], |
| 720 | + "outside", |
| 721 | + day + hour * 3_600, |
| 722 | + )); |
| 723 | + } |
| 724 | + // The 00:00 hour is skipped; the next record is 00:45, still |
| 725 | + // inside the active stretch: 23:00 -> 00:45 = 6300s > 3600s max. |
| 726 | + records.push(record(0x48, vec![dog_a()], "outside", day + 86_400 + 2_700)); |
| 727 | + let now = day + 86_400 + 3_000; |
| 728 | + let verdicts = evaluate(&[outside], &records, now).expect("evaluate"); |
| 729 | + let seam = current(&verdicts, day + 7 * 3_600); |
| 730 | + assert_eq!(seam.len(), 1, "one verdict for the merged seam span"); |
| 731 | + assert_eq!(seam[0].state, VerdictState::Violated); |
| 732 | + assert_eq!(seam[0].count, 18); |
| 733 | + assert!(seam[0].detail.contains("6300"), "{}", seam[0].detail); |
| 734 | + assert_eq!(seam[0].window_end, day + 86_400 + 3_600); |
| 735 | +} |