// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// reader.go
package file

import "testing"

func TestOpenReader(t *testing.T) {
	r, err := OpenReader("t_reader_test.go")
	if err != nil {
		t.Fatalf("OpenReader failed: %v", err)
	}
	defer r.Close()

	if !r.Valid() {
		t.Fatal("Reader should be valid after opening")
	}

	if r.TypeName() != "fileReader" {
		t.Fatalf("Expected TypeName 'fileReader', got '%s'", r.TypeName())
	}

	if r.String() != "reader" {
		t.Fatalf("Expected String 'reader', got '%s'", r.String())
	}

	// GetName may return either "t_reader_test.go" or "./t_reader_test.go" depending on the environment
	name := r.GetName()
	if (name != "t_reader_test.go") && (name != "./t_reader_test.go") {
		t.Fatalf("Expected GetName 't_reader_test.go' or './t_reader_test.go', got '%s'", name)
	}

	// Test reading a byte
	b, err := r.ReadByte()
	if err != nil {
		t.Fatalf("ReadByte failed: %v", err)
	}
	if b == 0 {
		t.Fatal("ReadByte should not return zero byte")
	}

	err = r.Reset()
	if err != nil {
		t.Fatalf("Reset failed: %v", err)
	}

	if s, err := r.ReadString('\n'); err != nil {
		t.Fatalf("ReadString failed: %v", err)
	} else {
		t.Logf("ReadString: %s", s)
	}

	// Test reading all content
	content, err := r.ReadAll()
	if err != nil {
		t.Fatalf("ReadAll failed: %v", err)
	}
	if len(content) == 0 {
		t.Fatal("ReadAll should return non-empty content")
	}
}
