<commit_before>package broker

import (
	"encoding/json"
	"fmt"
	"testing"
	"time"

	"h12.me/kafka/proto"
)

func TestMeta(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32791",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	req := &proto.Request{
		APIKey:        proto.TopicMetadataRequestType,
		APIVersion:    0,
		CorrelationID: 1,
		ClientID:      "abc",
		RequestMessage: &proto.TopicMetadataRequest{
			"test",
		},
	}
	resp := &proto.Response{
		ResponseMessage: &proto.TopicMetadataResponse{},
	}
	if err := broker.Do(req, resp); err != nil {
		t.Fatal(t)
	}
	fmt.Println(toJSON(resp.ResponseMessage))
}

func TestProduce(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32793",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	tm := time.Now()
	req := &proto.Request{
		APIKey:        proto.ProduceRequestType,
		APIVersion:    0,
		CorrelationID: 1,<commit_msg>testing offset commit/fetch.<commit_after>package broker

import (
	"encoding/json"
	"fmt"
	"testing"
	"time"

	"h12.me/kafka/proto"
)

func TestMeta(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32791",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	req := &proto.Request{
		APIKey:        proto.TopicMetadataRequestType,
		APIVersion:    0,
		CorrelationID: 1,
		ClientID:      "abc",
		RequestMessage: &proto.TopicMetadataRequest{
			"test",
		},
	}
	resp := &proto.Response{
		ResponseMessage: &proto.TopicMetadataResponse{},
	}
	if err := broker.Do(req, resp); err != nil {
		t.Fatal(t)
	}
	fmt.Println(toJSON(resp.ResponseMessage))
}

func TestOffsetCommit(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32791",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	tm := time.Now()
	req := &proto.Request{
		APIKey:        proto.OffsetCommitRequestType,
		APIVersion:    1,
		CorrelationID: 1,
		ClientID:      "abc",
		RequestMessage: &proto.OffsetCommitRequestV1{
			ConsumerGroupID:           "test-1",
			ConsumerGroupGenerationID: 1,
			ConsumerID:                "consumer-1",
			OffsetCommitInTopicV1s: []proto.OffsetCommitInTopicV1{
				{
					TopicName: "test",
					OffsetCommitInPartitionV1s: []proto.OffsetCommitInPartitionV1{
						{
							Partition: 0,
							Offset:    1,
							TimeStamp: tm.Unix(),
							Metadata:  fmt.Sprint(tm.Unix()),
						},
					},
				},
			},
		},
	}
	resp := proto.OffsetCommitResponse{}
	if err := broker.Do(req, &proto.Response{ResponseMessage: &resp}); err != nil {
		t.Fatal(t)
	}
	fmt.Println(toJSON(resp))
}

func TestOffsetFetch(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32791",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	req := &proto.Request{
		APIKey:        proto.OffsetFetchRequestType,
		APIVersion:    0,
		CorrelationID: 1,
		ClientID:      "abc",
		RequestMessage: &proto.OffsetFetchRequest{
			ConsumerGroup: "test-1",
			PartitionInTopics: []proto.PartitionInTopic{
				{
					TopicName:  "test",
					Partitions: []int32{0, 1, 2},
				},
			},
		},
	}
	resp := proto.OffsetFetchResponse{}
	if err := broker.Do(req, &proto.Response{ResponseMessage: &resp}); err != nil {
		t.Fatal(t)
	}
	fmt.Println(toJSON(resp))
}

func TestConsumerMeta(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32791",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	creq := proto.ConsumerMetadataRequest("test-1")
	req := &proto.Request{
		APIKey:         proto.ConsumerMetadataRequestType,
		APIVersion:     0,
		CorrelationID:  1,
		ClientID:       "abc",
		RequestMessage: &creq,
	}
	resp := proto.ConsumerMetadataResponse{}
	if err := broker.Do(req, &proto.Response{ResponseMessage: &resp}); err != nil {
		t.Fatal(t)
	}
	fmt.Println(toJSON(resp))
}

func TestProduce(t *testing.T) {
	broker, err := New(&Config{
		Addr:         "docker:32793",
		SendQueueLen: 10,
		RecvQueueLen: 10,
	})
	if err != nil {
		t.Fatal(err)
	}
	tm := time.Now()
	req := &proto.Request{
		APIKey:        proto.ProduceRequestType,
		APIVersion:    0,
		CorrelationID: 1,
{'fraction_non_alphanumeric': 0.15401094977386337, 'fraction_numerical': 0.023327779100214236, 'mean_word_length': 3.1399014778325123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5133948', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1604, 'n_words': 302}