package cloud.molberg.mgutility; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public final class DashcamPolicyTest { private static int failures; private static void check(boolean ok, String name) { if (ok) System.out.println("ok - " + name); else { System.out.println("FAIL - " + name); failures++; } } private static final long MIN = 60_000L; private static final long MB = 1024L * 1024L; private static DashcamPolicy.Clip clip(String name, long start, long sizeMb) { return new DashcamPolicy.Clip(name, start, start + MIN, sizeMb * MB); } private static List names(List clips) { List out = new ArrayList(); for (DashcamPolicy.Clip c : clips) out.add(c.name); return out; } public static void main(String[] args) { long now = 1_800_000_000_000L; long day = DashcamPolicy.DAY_MS; // Names round-trip. String name = DashcamPolicy.clipName(now); long parsed = DashcamPolicy.clipStart(name); check(Math.abs(parsed - now) < 1000L, "clip name round-trips to the second"); check(DashcamPolicy.clipStart("notes.txt") == -1L, "foreign file ignored"); check(DashcamPolicy.clipStart("20261399_000000.mp4") == -1L, "invalid date ignored"); // Age-based retention deletes only unprotected clips older than the limit. List clips = Arrays.asList( clip("old", now - 40 * day, 50), clip("kept-old", now - 35 * day, 50), clip("recent", now - 2 * day, 50)); List tags = Collections.singletonList( new DashcamPolicy.Range(now - 35 * day + 10_000L, now - 35 * day + 20_000L)); List del = names(DashcamPolicy.selectForDeletion(clips, tags, now, 0, 30, 0, null)); check(del.equals(Collections.singletonList("old")), "30-day retention removes only the old unprotected clip"); del = names(DashcamPolicy.selectForDeletion(clips, tags, now, 0, 0, 0, null)); check(del.isEmpty(), "retention 0 disables age deletion"); // Storage cap deletes oldest unprotected first, skips protected and active. List cap = Arrays.asList( clip("a", now - 10 * MIN, 100), clip("b", now - 9 * MIN, 100), clip("c", now - 8 * MIN, 100), clip("d", now - 7 * MIN, 100), clip("live", now - MIN, 100)); List keepB = Collections.singletonList( new DashcamPolicy.Range(now - 9 * MIN + 1000L, now - 9 * MIN + 2000L)); del = names(DashcamPolicy.selectForDeletion(cap, keepB, now, 300 * MB, 30, 0, "live")); check(del.equals(Arrays.asList("a", "c")), "cap trims oldest unprotected clips: " + del); del = names(DashcamPolicy.selectForDeletion(cap, keepB, now, 300 * MB, 30, 100 * MB, "live")); check(del.equals(Arrays.asList("a", "c", "d")), "headroom for next segment is respected: " + del); del = names(DashcamPolicy.selectForDeletion(cap, keepB, now, 50 * MB, 30, 0, "live")); check(!del.contains("b") && !del.contains("live"), "never deletes protected or active clip even when over cap"); check(DashcamPolicy.protectedBytes(cap, keepB) == 100 * MB, "protected bytes counted"); // Tag spanning a boundary protects both neighbours. List span = Collections.singletonList( new DashcamPolicy.Range(now - 8 * MIN - 5000L, now - 8 * MIN + 5000L)); check(DashcamPolicy.clipsInRange(cap, span.get(0)).size() == 2, "range across boundary covers two clips"); // Sessions split on gaps. List drive = Arrays.asList( clip("s2b", now + MIN, 1), clip("s1a", now - 60 * MIN, 1), clip("s1b", now - 59 * MIN, 1), clip("s2a", now, 1)); List> sessions = DashcamPolicy.sessions(drive, 2 * MIN); check(sessions.size() == 2 && sessions.get(0).size() == 2 && sessions.get(1).get(0).name.equals("s2a"), "clips grouped into drives"); check(DashcamPolicy.segmentBytes(9_000_000, 60) == 70_875_000L, "segment size estimate"); check(DashcamPolicy.minutesForBudget(10L * 1024 * MB, 9_000_000) == 151L, "minutes for 10 GB at 9 Mbit/s"); if (failures > 0) { System.out.println(failures + " failure(s)"); System.exit(1); } System.out.println("DashcamPolicyTest: all tests passed"); } }