package com.zb.mom.ww.mxgateway.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

final class MxGatewaySecretsTests {
    @Test
    void redactExactReplacesEveryOccurrenceOfASecret() {
        String message = "verify s3cr3t, retry s3cr3t, done s3cr3t";

        String result = MxGatewaySecrets.redactExact(message, "s3cr3t");

        assertFalse(result.contains("s3cr3t"), "no occurrence of the secret may survive");
        assertEquals("verify <redacted>, retry <redacted>, done <redacted>", result);
    }

    @Test
    void redactExactFullyRedactsOverlappingSecretsWhenOneIsASubstringOfTheOther() {
        String message = "password=hunter2 token=hunter2extra";

        String result = MxGatewaySecrets.redactExact(message, "hunter2extra", "hunter2");

        assertFalse(result.contains("hunter2"), "both the secret and its superstring must be fully redacted");
        assertEquals("password=<redacted> token=<redacted>", result);
    }

    @Test
    void redactExactWithNoSecretsReturnsMessageUnchanged() {
        String message = "nothing to scrub here";

        assertEquals(message, MxGatewaySecrets.redactExact(message));
    }

    @Test
    void redactExactToleratesNullMessage() {
        assertNull(MxGatewaySecrets.redactExact(null, "secret"));
    }

    @Test
    void redactExactIgnoresBlankSecretSoRealSpacesAreNotOverRedacted() {
        String message = "keep these spaces intact";

        String result = MxGatewaySecrets.redactExact(message, " ", "");

        assertEquals(message, result);
    }
}
