package com.agent.management.common;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class LogRedactorTest {

    @Test
    void redactVectors_abbreviatesLargeNumericArrays() {
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < 1024; i++) {
            if (i > 0) sb.append(",");
            sb.append(String.format("%.4f", (i - 512) * 0.001));
        }
        sb.append("]");
        String vec = sb.toString();

        String result = LogRedactor.redactVectors("embedding: " + vec);
        assertThat(result).contains("1024 个浮点数已省略");
        assertThat(result).doesNotContain("0.0123");
        assertThat(result.length()).isLessThan(vec.length() / 10);
    }

    @Test
    void redactVectors_preservesShortArrays() {
        assertThat(LogRedactor.redactVectors("coord: [1, 2, 3]")).isEqualTo("coord: [1, 2, 3]");
        // 4 个数字仍低于阈值 8，保留原样
        assertThat(LogRedactor.redactVectors("[0.1, 0.2, 0.3, 0.4]"))
                .isEqualTo("[0.1, 0.2, 0.3, 0.4]");
    }

    @Test
    void redactVectors_preservesJsonStructure() {
        String input = "{\"vectors\":[[0.1,0.2]],\"model\":\"text-embedding\"}";
        String output = LogRedactor.redactVectors(input);
        assertThat(output).contains("\"model\":\"text-embedding\"");
    }

    @Test
    void redactVectors_handlesNullAndEmpty() {
        assertThat(LogRedactor.redactVectors(null)).isNull();
        assertThat(LogRedactor.redactVectors("")).isEqualTo("");
    }

    @Test
    void redactVectors_handlesNegativeAndScientificNotation() {
        String vec = "[-1.5e-3, 2.3E+4, -0.5, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]";
        String result = LogRedactor.redactVectors(vec);
        assertThat(result).contains("10 个浮点数已省略");
    }
}
