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

// writer.go
package file

import "testing"

func TestCreateWriter(t *testing.T) {
	w, err := CreateWriter("/tmp/test_writer.txt")
	if err != nil {
		t.Fatalf("CreateWriter failed: %v", err)
	}
	defer w.Close()

	if !w.Valid() {
		t.Fatal("Writer should be valid after creation")
	}

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

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

	name := w.GetName()
	if name != "/tmp/test_writer.txt" {
		t.Fatalf("Expected GetName '/tmp/test_writer.txt', got '%s'", name)
	}

	if n, err := w.Write("Hello, World!\n"); err != nil {
		t.Fatalf("Write failed: %v", err)
	} else if n != len("Hello, World!\n") {
		t.Fatalf("Expected to write %d bytes, wrote %d", len("Hello, World!\n"), n)
	}

	if n, err := w.Writef("This is a %s.\n", "test"); err != nil {
		t.Fatalf("Writef failed: %v", err)
	} else if n != len("This is a test.\n") {
		t.Fatalf("Expected to write %d bytes, wrote %d", len("This is a test.\n"), n)
	}
}

func TestAppendWriter(t *testing.T) {
	w, err := AppendWriter("/tmp/test_writer.txt")
	if err != nil {
		t.Fatalf("AppendWriter failed: %v", err)
	}
	defer w.Close()

	if !w.Valid() {
		t.Fatal("Writer should be valid after opening for append")
	}

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

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

	name := w.GetName()
	if name != "/tmp/test_writer.txt" {
		t.Fatalf("Expected GetName '/tmp/test_writer.txt', got '%s'", name)
	}
}
