bldr/web/bundler/web-pkg-refs_test.go
Rama master · réplica de lectura del repositorio original.
package bldr_web_bundler
import (
"reflect"
"testing"
web_pkg "github.com/s4wave/spacewave/bldr/web/pkg"
)
func TestCompactWebPkgRefConfigs(t *testing.T) {
// Describe package reference combinations and their compacted results.
tests := []struct {
name string
refs []*WebPkgRefConfig
want []*WebPkgRefConfig
}{
{
name: "empty slice",
refs: []*WebPkgRefConfig{},
want: []*WebPkgRefConfig{},
},
{
name: "single element",
refs: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b"}},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b"}},
},
},
{
name: "no duplicates",
refs: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b"}},
{Id: "pkg2", Imports: []string{"c", "d"}},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b"}},
{Id: "pkg2", Imports: []string{"c", "d"}},
},
},
{
name: "merge duplicates",
refs: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b"}},
{Id: "pkg1", Imports: []string{"c", "d"}},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b", "c", "d"}},
},
},
{
name: "merge exclude flag",
refs: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b"}},
{Id: "pkg1", Exclude: true},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Exclude: true, Imports: []string{"a", "b"}},
},
},
{
name: "skip empty ids",
refs: []*WebPkgRefConfig{
{Id: "", Imports: []string{"a", "b"}},
{Id: "pkg1", Imports: []string{"c", "d"}},
{Id: "", Imports: []string{"e", "f"}},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"c", "d"}},
},
},
{
name: "complex case with multiple duplicates",
refs: []*WebPkgRefConfig{
{Id: "pkg2", Imports: []string{"a", "b"}},
{Id: "pkg1", Imports: []string{"c", "d"}},
{Id: "pkg3", Imports: []string{"e", "f"}},
{Id: "pkg1", Imports: []string{"g", "h"}},
{Id: "pkg2", Exclude: true},
{Id: "pkg3", Imports: []string{"i", "j"}},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"c", "d", "g", "h"}},
{Id: "pkg2", Exclude: true, Imports: []string{"a", "b"}},
{Id: "pkg3", Imports: []string{"e", "f", "i", "j"}},
},
},
{
name: "deduplicate imports",
refs: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b", "c"}},
{Id: "pkg1", Imports: []string{"b", "c", "d"}},
},
want: []*WebPkgRefConfig{
{Id: "pkg1", Imports: []string{"a", "b", "c", "d"}},
},
},
}
// Check package reference compaction for each input combination.
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a copy of the input to avoid modifying the test case
input := make([]*WebPkgRefConfig, len(tt.refs))
for i, ref := range tt.refs {
input[i] = &WebPkgRefConfig{
Id: ref.Id,
Exclude: ref.Exclude,
Imports: append([]string{}, ref.Imports...),
}
}
// Compact the copied package references for comparison.
got := CompactWebPkgRefConfigs(input)
// Check if the result has the expected length
if len(got) != len(tt.want) {
t.Errorf("CompactWebPkgRefConfigs() got %d elements, want %d", len(got), len(tt.want))
return
}
// Check each element
for i, wantRef := range tt.want {
if i >= len(got) {
t.Errorf("CompactWebPkgRefConfigs() missing expected element at index %d", i)
continue
}
// Verify the compacted package ID, exclusion flag, and imports.
gotRef := got[i]
if gotRef.GetId() != wantRef.GetId() {
t.Errorf("Element %d: Id = %q, want %q", i, gotRef.GetId(), wantRef.GetId())
}
if gotRef.GetExclude() != wantRef.GetExclude() {
t.Errorf("Element %d: Exclude = %v, want %v", i, gotRef.GetExclude(), wantRef.GetExclude())
}
if !reflect.DeepEqual(gotRef.GetImports(), wantRef.GetImports()) {
t.Errorf("Element %d: Imports = %v, want %v", i, gotRef.GetImports(), wantRef.GetImports())
}
}
})
}
}
func TestMergeWebPkgRefConfigImports(t *testing.T) {
refs := MergeWebPkgRefConfigImports(
web_pkg.WebPkgRefSlice{{
WebPkgId: "@spacewave/ui",
WebPkgRoot: "/repo/web/ui",
Imports: []string{"Button.tsx"},
}, {
WebPkgId: "react",
WebPkgRoot: "/repo/node_modules/react",
Imports: []string{"index.js"},
}},
[]*WebPkgRefConfig{{
Id: "@spacewave/ui",
Imports: []string{"Dialog.tsx", "Button.tsx"},
}, {
Id: "react",
Imports: []string{"jsx-runtime.js"},
}, {
Id: "sonner",
Imports: []string{"index.js"},
}, {
Id: "ignored",
Exclude: true,
Imports: []string{"index.js"},
}},
)
if got, want := refs[0].GetImports(), []string{"Button.tsx", "Dialog.tsx"}; !reflect.DeepEqual(got, want) {
t.Fatalf("@spacewave/ui imports=%v want %v", got, want)
}
if got, want := refs[1].GetImports(), []string{"index.js", "jsx-runtime.js"}; !reflect.DeepEqual(got, want) {
t.Fatalf("react imports=%v want %v", got, want)
}
if len(refs) != 2 {
t.Fatalf("refs length=%d want 2: %v", len(refs), refs)
}
}