on main help sign in
fix: update UI tests to use test workspace
2182fc4Ayman Bagabas
internal/ui/model/ui_test.go+9−21
@@ -1,15 +1,13 @@
1package model
1package model
2
2
3import (
3import (
4− "reflect"
5 "testing"
4 "testing"
6− "unsafe"
7
5
8 "charm.land/catwalk/pkg/catwalk"
6 "charm.land/catwalk/pkg/catwalk"
9− "github.com/charmbracelet/crush/internal/app"
10 "github.com/charmbracelet/crush/internal/config"
7 "github.com/charmbracelet/crush/internal/config"
11 "github.com/charmbracelet/crush/internal/csync"
8 "github.com/charmbracelet/crush/internal/csync"
12 "github.com/charmbracelet/crush/internal/ui/common"
9 "github.com/charmbracelet/crush/internal/ui/common"
10+ "github.com/charmbracelet/crush/internal/workspace"
13 "github.com/stretchr/testify/require"
11 "github.com/stretchr/testify/require"
14)
12)
15
13
@@ -79,29 +77,19 @@ func TestCurrentModelSupportsImages(t *testing.T) {
79func newTestUIWithConfig(t *testing.T, cfg *config.Config) *UI {
77func newTestUIWithConfig(t *testing.T, cfg *config.Config) *UI {
80 t.Helper()
78 t.Helper()
81
79
82− store := &config.ConfigStore{}
83− setUnexportedField(t, store, "config", cfg)
84−
85− appInstance := &app.App{}
86− setUnexportedField(t, appInstance, "config", store)
87−
88 return &UI{
80 return &UI{
89 com: &common.Common{
81 com: &common.Common{
90− App: appInstance,
82+ Workspace: &testWorkspace{cfg: cfg},
91 },
83 },
92 }
84 }
93}
85}
94
86
95−func setUnexportedField(t *testing.T, target any, name string, value any) {
87+// testWorkspace is a minimal [workspace.Workspace] stub for unit tests.
96− t.Helper()
88+type testWorkspace struct {
97−
89+ workspace.Workspace
98− v := reflect.ValueOf(target)
90+ cfg *config.Config
99− require.Equal(t, reflect.Pointer, v.Kind())
91+}
100− require.False(t, v.IsNil())
101−
102− field := v.Elem().FieldByName(name)
103− require.Truef(t, field.IsValid(), "field %q not found", name)
104
92
105− fieldValue := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
93+func (w *testWorkspace) Config() *config.Config {
106− fieldValue.Set(reflect.ValueOf(value))
94+ return w.cfg
107}
95}