package test import ( "fmt" "os" "strings" "testing" "time" "llm-api-benchmark-tool/pkg/report" "llm-api-benchmark-tool/pkg/stats" "github.com/stretchr/testify/assert" ) // createSampleStats creates a sample FinalStats struct for testing. func createSampleStats() stats.FinalStats { return stats.FinalStats{ TotalRequests: 1000, SuccessfulRequests: 950, FailedRequests: 50, TotalDuration: 30 * time.Second, AvgLatency: 150 * time.Millisecond, MinLatency: 50 * time.Millisecond, MaxLatency: 500 * time.Millisecond, P90Latency: 250 * time.Millisecond, P95Latency: 300 * time.Millisecond, P99Latency: 400 * time.Millisecond, AvgTimeToFirstToken: 80 * time.Millisecond, MinTimeToFirstToken: 30 * time.Millisecond, MaxTimeToFirstToken: 200 * time.Millisecond, P90TimeToFirstToken: 120 * time.Millisecond, P95TimeToFirstToken: 150 * time.Millisecond, P99TimeToFirstToken: 180 * time.Millisecond, AvgQPS: 33.33, AvgTokensPerSecond: 500.5, // Add more fields if necessary for report testing } } // TestGenerateHTMLReport tests the basic HTML report generation. func TestGenerateHTMLReport(t *testing.T) { sampleStats := createSampleStats() outputDir := t.TempDir() // Create a temporary directory for the report outputFilePath := outputDir + "/report.html" // Define expected chart IDs (must match IDs set in generator.go) latencyChartID := "latencyHistogram" ttftChartID := "ttftHistogram" // Generate the report err := report.GenerateHTMLReport(sampleStats, outputFilePath) assert.NoError(t, err, "GenerateHTMLReport should not return an error") // Check if the file was created _, err = os.Stat(outputFilePath) assert.NoError(t, err, "HTML report file should be created") // Read the generated file content contentBytes, err := os.ReadFile(outputFilePath) assert.NoError(t, err, "Should be able to read the generated report file") content := string(contentBytes) // Basic checks on the HTML content assert.True(t, strings.HasPrefix(content, ""), "Output should start with DOCTYPE html") assert.Contains(t, content, "LLM API Benchmark Report", "HTML should contain the correct title") assert.Contains(t, content, "

LLM API Benchmark Report

", "HTML should contain the main heading") assert.Contains(t, content, "

Summary

", "HTML should contain Summary section") assert.Contains(t, content, "

Charts

", "HTML should contain Charts section") // Check if some key stats are present (as simple text for now) assert.Contains(t, content, "Total Requests:1000", "HTML should contain Total Requests stat") assert.Contains(t, content, "Avg Latency:150ms", "HTML should contain Avg Latency stat") assert.Contains(t, content, "Avg TTFT:80ms", "HTML should contain Avg TTFT stat") assert.Contains(t, content, "Avg QPS:33.33", "HTML should contain Avg QPS stat") // Check if the ECharts initialization script snippet is present for each chart // latencyInitScript := fmt.Sprintf("echarts.init(document.getElementById('%s'))", latencyChartID) // assert.Contains(t, content, latencyInitScript, "HTML should contain JS init for latency chart") // Check if the chart divs are present (the ones generated by go-echarts render) assert.Contains(t, content, fmt.Sprintf(`