package git

import (
	"context"
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestStatus_CleanAndDirty(t *testing.T) {
	t.Parallel()
	gitAvailable(t)
	bare := makeBareRepo(t)
	r := newTestRunner(t)
	dst := filepath.Join(t.TempDir(), "wc")
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	require.NoError(t, r.Clone(ctx, dst, bare, "main", nil))

	st, err := r.Status(ctx, dst)
	require.NoError(t, err)
	require.Empty(t, st, "fresh clone should be clean")

	require.NoError(t, os.WriteFile(filepath.Join(dst, "x.txt"), []byte("x"), 0o644))
	st, err = r.Status(ctx, dst)
	require.NoError(t, err)
	require.Len(t, st, 1)
	require.Equal(t, "x.txt", st[0].Path)
	require.Equal(t, byte('?'), st[0].WorktreeY)
}

func TestParsePorcelainV1_Renamed(t *testing.T) {
	t.Parallel()
	// "R  to_path\0from_path\0" → 1 record，path="to_path"
	buf := []byte("R  to_path\x00from_path\x00")
	got := parsePorcelainV1(buf)
	require.Len(t, got, 1)
	require.Equal(t, "to_path", got[0].Path)
	require.Equal(t, byte('R'), got[0].IndexX)
}
