package common_test

/*
 * This file is part of Autogits.
 *
 * Copyright © 2024 SUSE LLC
 *
 * Autogits is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Foobar. If not, see <https://www.gnu.org/licenses/>.
 */

import (
	"bytes"
	"regexp"
	"testing"

	"src.opensuse.org/autogits/common"
)

type TestWriter struct {
	str string
}

func TestLogging(t *testing.T) {
	t.Run("basic logging", func(t *testing.T) {
		var strWriter, errWriter *bytes.Buffer

		strWriter = bytes.NewBuffer(make([]byte, 0, 10000))
		errWriter = bytes.NewBuffer(make([]byte, 0, 10000))

		stdLogger, errLogger := common.CreateStdoutLogger(strWriter, errWriter)
		errLogger.Printf("%d\n", 100)
		stdLogger.Printf("OKA %d Done\n", 77)
		stdLogger.Println("Another line")

		const prefixMatch = `\[\d+\] `

		errStr := errWriter.String()
		if ok, err := regexp.MatchString("^"+prefixMatch+"100\n$", errStr); !ok {
			if err != nil {
				t.Logf("err: %v", err)
			}
			t.Fatal(errStr)
		}
		str := strWriter.String()
		if ok, err := regexp.MatchString("^"+prefixMatch+"OKA 77 Done\n"+prefixMatch+"Another line\n$", str); !ok {
			if err != nil {
				t.Logf("err: %v", err)
			}
			t.Fatal(str)
		}
	})
}
