main pico / pkg / shared / bucket_test.go
Antonio Mika  ·  2026-10-10
 1package shared
 2
 3import (
 4	"io/fs"
 5	"testing"
 6
 7	"github.com/picosh/pico/pkg/send/utils"
 8)
 9
10func TestGetProjectName(t *testing.T) {
11	tests := []struct {
12		name     string
13		filepath string
14		isDir    bool
15		want     string
16	}{
17		// Standard cases: /project/file -> project
18		{
19			name:     "standard project with file",
20			filepath: "/myproject/index.html",
21			isDir:    false,
22			want:     "myproject",
23		},
24		{
25			name:     "nested path",
26			filepath: "/myproject/subdir/file.txt",
27			isDir:    false,
28			want:     "myproject",
29		},
30		// A file at the root is outside every project
31		{
32			name:     "root-level file",
33			filepath: "/bin",
34			isDir:    false,
35			want:     "",
36		},
37		// Directory cases
38		{
39			name:     "directory with no slash",
40			filepath: "myproject",
41			isDir:    true,
42			want:     "myproject",
43		},
44		// Edge cases (caught by uploader validation, not valid inputs)
45		{
46			name:     "empty path",
47			filepath: "",
48			isDir:    false,
49			want:     ".",
50		},
51		{
52			name:     "root path",
53			filepath: "/",
54			isDir:    false,
55			want:     "",
56		},
57	}
58
59	for _, tt := range tests {
60		t.Run(tt.name, func(t *testing.T) {
61			entry := &utils.FileEntry{
62				Filepath: tt.filepath,
63				Mode:     fs.FileMode(0644),
64			}
65			if tt.isDir {
66				entry.Mode = fs.ModeDir
67			}
68
69			got := GetProjectName(entry)
70			if got != tt.want {
71				t.Errorf("GetProjectName(%q, isDir=%v) = %q, want %q", tt.filepath, tt.isDir, got, tt.want)
72			}
73		})
74	}
75}