OtpDigitsTest.kt

1.6 KB · raw

1package com.peachpasswords.autofill23import org.junit.Assert.assertEquals4import org.junit.Test56class OtpDigitsTest {78    @Test fun distributes_a_code_one_character_per_box() {9        assertEquals(10            listOf("1", "2", "3", "4", "5", "6"),11            AutofillResponseBuilder.otpDigitsFor(6, "123456"),12        )13    }1415    @Test fun stops_at_the_box_count_when_the_code_is_longer() {16        assertEquals(listOf("1", "2", "3", "4"), AutofillResponseBuilder.otpDigitsFor(4, "123456"))17    }1819    @Test fun fills_fewer_boxes_than_code_length_when_there_are_more_boxes() {20        assertEquals(21            listOf("1", "2", "3", "4", "5", "6"),22            AutofillResponseBuilder.otpDigitsFor(8, "123456"),23        )24    }2526    @Test fun strips_whitespace_before_distributing() {27        assertEquals(listOf("1", "2", "3"), AutofillResponseBuilder.otpDigitsFor(6, "1 2 3"))28    }2930    @Test fun six_boxes_map_to_a_full_six_digit_code() {31        // Common 2FA split layout: 6 single-character boxes for a 6-digit TOTP.32        // Mirrors the inline chip path where [parsed.otpDigitBoxIds] is six33        // AutofillIds and the entry's TOTP seed generates a fresh code.34        val boxes = AutofillResponseBuilder.otpDigitsFor(6, "481208")35        assertEquals(listOf("4", "8", "1", "2", "0", "8"), boxes)36    }3738    @Test fun empty_code_produces_empty_boxes() {39        // Edge case: the entry has no TOTP seed and generateCode returned null;40        // otpDigitsFor must not crash when there is no code to distribute.41        assertEquals(emptyList<String>(), AutofillResponseBuilder.otpDigitsFor(6, ""))42    }43}