/**
 * Copyright © 2012-2025 by Data Virtuality GmbH
 */
package org.teiid.util;

import org.junit.jupiter.api.Test;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Simple test to reproduce the specific issue with SELECT to_chars(to_bytes('é', 'UTF-8'), 'UTF-8-BOM')
 *
 * @author Serhii Bashniak
 * @date 2025.10.06
 */
public class TestUtf8BomSimple {

    @Test
    public void testFrenchCharacterConversion() {
        String original = "é";

        // Simulate to_bytes('é', 'UTF-8')
        byte[] utf8Bytes = original.getBytes(StandardCharsets.UTF_8);

        // Simulate to_chars(..., 'UTF-8-BOM')
        Charset utf8Bom = CharsetUtils.getCharset("UTF-8-BOM");
        String result = new String(utf8Bytes, utf8Bom);

        assertEquals("é", result);
        assertFalse(result.contains("�"), "Should not contain replacement character");
    }

    @Test
    public void testDebugFrenchCharacterBytes() throws Exception {
        String original = "é";

        // Check UTF-8 bytes
        byte[] utf8Bytes = original.getBytes(StandardCharsets.UTF_8);
        System.out.println("UTF-8 bytes for 'é': " + java.util.Arrays.toString(utf8Bytes));

        // Try decoding with UTF-8-BOM
        Charset utf8Bom = CharsetUtils.getCharset("UTF-8-BOM");
        String result = new String(utf8Bytes, utf8Bom);
        System.out.println("Result: '" + result + "'");
        System.out.println("Result length: " + result.length());

        // Check if it contains replacement character
        if (result.contains("�")) {
            System.out.println("Contains replacement character!");
        }
    }
}