<commit_before>package io.cucumber.htmlformatter;

import io.cucumber.messages.Messages;
import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

class MessagesToHtmlWriterTest {

    @Test
    void it_writes_html_to_output() throws IOException {
        List<Messages.Envelope> messages = Arrays.asList(
                Messages.Envelope.newBuilder()
                        .setTestRunStarted(Messages.TestRunStarted.newBuilder()
                                .setTimestamp(Messages.Timestamp.newBuilder()
                                        .setSeconds(10)
                                        .build())
                                .build())
                        .build(),
                Messages.Envelope.newBuilder()
                        .setTestRunFinished(Messages.TestRunFinished.newBuilder()
                                .setTimestamp(Messages.Timestamp.newBuilder()
                                        .setSeconds(15)
                                        .build())
                                .build())
                        .build()
        );

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(bytes, UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        try (MessagesToHtmlWriter messagesToHtmlWriter = new MessagesToHtmlWriter(bw)) {
            for (Messages.Envelope message : messages) {
                messagesToHtmlWriter.write(message);
            }
        }

        assertThat(new String(bytes.toByteArray(), UTF_8), containsString("" +
                "window.CUCUMBER_MESSAGES = [" +
                "{\"testRunStarted\":{\"timestamp\":{\"seconds\":\"10\"}}}," +
                "{\"testRunFinished\":{\"timestamp\":{\"seconds\":\"15\"}}}" +
                "];"));
    }
}<commit_msg>Additional coverage<commit_after>package io.cucumber.htmlformatter;

import io.cucumber.messages.Messages;
import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

class MessagesToHtmlWriterTest {

    @Test
    void it_writes_one_message_to_html() throws IOException {
        String html = renderAsHtml(Arrays.asList(
                Messages.Envelope.newBuilder()
                        .setTestRunStarted(Messages.TestRunStarted.newBuilder()
                                .setTimestamp(Messages.Timestamp.newBuilder()
                                        .setSeconds(10)
                                        .build())
                                .build())
                        .build()
        ));
        assertThat(html, containsString("" +
                "window.CUCUMBER_MESSAGES = [" +
                "{\"testRunStarted\":{\"timestamp\":{\"seconds\":\"10\"}}}" +
                "];"));
    }

    @Test
    void it_writes_two_messages_separated_by_a_comma() throws IOException {
        String html = renderAsHtml(Arrays.asList(
                Messages.Envelope.newBuilder()
                        .setTestRunStarted(Messages.TestRunStarted.newBuilder()
                                .setTimestamp(Messages.Timestamp.newBuilder()
                                        .setSeconds(10)
                                        .build())
                                .build())
                        .build(),
                Messages.Envelope.newBuilder()
                        .setTestRunFinished(Messages.TestRunFinished.newBuilder()
                                .setTimestamp(Messages.Timestamp.newBuilder()
                                        .setSeconds(15)
                                        .build())
                                .build())
                        .build()
        ));
        assertThat(html, containsString("" +
                "window.CUCUMBER_MESSAGES = [" +
                "{\"testRunStarted\":{\"timestamp\":{\"seconds\":\"10\"}}}," +
                "{\"testRunFinished\":{\"timestamp\":{\"seconds\":\"15\"}}}" +
                "];"));
    }

    private String renderAsHtml(List<Messages.Envelope> messages) throws IOException {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(bytes, UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        try (MessagesToHtmlWriter messagesToHtmlWriter = new MessagesToHtmlWriter(bw)) {
            for (Messages.Envelope message : messages) {
                messagesToHtmlWriter.write(message);
            }
        }

        return new String(bytes.toByteArray(), UTF_8);
    }
}
{'fraction_non_alphanumeric': 0.10894340351554954, 'fraction_numerical': 0.005022213637241646, 'mean_word_length': 1.6745867768595042, 'pattern_counts': {'":': 15, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5400432', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1243, 'n_words': 190}