package main

import (
	"local/cloudly/capture"
	"os"
	"strings"
	"testing"
)

func Test_EncDec(t *testing.T) {
	os.Setenv("KEY", "123")
	defer os.Setenv("KEY", "")
	cases := []struct {
		in string
	}{
		{in: "hello world"},
		{in: "h"},
		{in: ""},
		{in: "hello world my name is charl" + strings.Repeat("i", 100) + "e!"},
	}
	for _, c := range cases {
		encoded := capture.Stdout(func() {
			capture.Stdin(c.in, func() {
				if err := Enc(); err != nil {
					t.Fatalf("cannot encode: %v", err)
				}
			})
		})
		if encoded == "" || encoded == c.in {
			t.Errorf("failed to encode (%v): got (%v)", len(c.in), len(encoded))
		}
		t.Logf("%v => %v => ?", len(c.in), len(encoded))
		decoded := capture.Stdout(func() {
			capture.Stdin(encoded, func() {
				if err := Dec(); err != nil {
					t.Fatalf("cannot decode: %v", err)
				}
			})
		})
		if decoded != c.in {
			t.Errorf("failed to encode-decode (%v): got (%v)", len(c.in), len(encoded))
			t.Logf("%q => (%v) => %q", c.in, len(encoded), decoded)
		}
	}
}
