package files

import (
	"strings"
	"testing"

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

func TestSaveStoresFileUnderProjectDirectory(t *testing.T) {
	service := NewService(t.TempDir())

	stored, err := service.Save(12, "brief.md", strings.NewReader("hello"))

	require.NoError(t, err)
	require.Equal(t, "brief.md", stored.OriginalName)
	require.Contains(t, stored.RelativePath, "projects/12/")
	require.FileExists(t, stored.AbsolutePath)
}

func TestSaveNeutralizesPathTraversal(t *testing.T) {
	service := NewService(t.TempDir())

	stored, err := service.Save(12, "..\\..\\secret.txt", strings.NewReader("hello"))

	require.NoError(t, err)
	require.Equal(t, "secret.txt", stored.OriginalName)
	require.Contains(t, stored.RelativePath, "projects/12/")
}
