9 "github.com/charmbracelet/x/ansi"
10 "github.com/charmbracelet/x/exp/golden"
11 "github.com/opencode-ai/opencode/internal/exp/diffview"
14//go:embed testdata/TestDefault.before
15var TestDefaultBefore string
17//go:embed testdata/TestDefault.after
18var TestDefaultAfter string
20//go:embed testdata/TestMultipleHunks.before
21var TestMultipleHunksBefore string
23//go:embed testdata/TestMultipleHunks.after
24var TestMultipleHunksAfter string
26//go:embed testdata/TestNarrow.before
27var TestNarrowBefore string
29//go:embed testdata/TestNarrow.after
30var TestNarrowAfter string
33 TestFunc func(dv *diffview.DiffView) *diffview.DiffView
34 TestFuncs map[string]TestFunc
38 UnifiedFunc = func(dv *diffview.DiffView) *diffview.DiffView {
41 SplitFunc = func(dv *diffview.DiffView) *diffview.DiffView {
45 DefaultFunc = func(dv *diffview.DiffView) *diffview.DiffView {
47 Before("main.go", TestDefaultBefore).
48 After("main.go", TestDefaultAfter)
50 NoLineNumbersFunc = func(dv *diffview.DiffView) *diffview.DiffView {
52 Before("main.go", TestDefaultBefore).
53 After("main.go", TestDefaultAfter).
56 MultipleHunksFunc = func(dv *diffview.DiffView) *diffview.DiffView {
58 Before("main.go", TestMultipleHunksBefore).
59 After("main.go", TestMultipleHunksAfter)
61 CustomContextLinesFunc = func(dv *diffview.DiffView) *diffview.DiffView {
63 Before("main.go", TestMultipleHunksBefore).
64 After("main.go", TestMultipleHunksAfter).
67 NarrowFunc = func(dv *diffview.DiffView) *diffview.DiffView {
69 Before("text.txt", TestNarrowBefore).
70 After("text.txt", TestNarrowAfter)
72 SmallWidthFunc = func(dv *diffview.DiffView) *diffview.DiffView {
74 Before("main.go", TestMultipleHunksBefore).
75 After("main.go", TestMultipleHunksAfter).
78 LargeWidthFunc = func(dv *diffview.DiffView) *diffview.DiffView {
80 Before("main.go", TestMultipleHunksBefore).
81 After("main.go", TestMultipleHunksAfter).
85 LightModeFunc = func(dv *diffview.DiffView) *diffview.DiffView {
86 return dv.Style(diffview.DefaultLightStyle)
88 DarkModeFunc = func(dv *diffview.DiffView) *diffview.DiffView {
89 return dv.Style(diffview.DefaultDarkStyle)
92 LayoutFuncs = TestFuncs{
93 "Unified": UnifiedFunc,
96 BehaviorFuncs = TestFuncs{
97 "Default": DefaultFunc,
98 "NoLineNumbers": NoLineNumbersFunc,
99 "MultipleHunks": MultipleHunksFunc,
100 "CustomContextLines": CustomContextLinesFunc,
101 "Narrow": NarrowFunc,
102 "SmallWidth": SmallWidthFunc,
103 "LargeWidth": LargeWidthFunc,
105 ThemeFuncs = TestFuncs{
106 "LightMode": LightModeFunc,
107 "DarkMode": DarkModeFunc,
111func TestDiffView(t *testing.T) {
112 for layoutName, layoutFunc := range LayoutFuncs {
113 t.Run(layoutName, func(t *testing.T) {
114 for behaviorName, behaviorFunc := range BehaviorFuncs {
115 t.Run(behaviorName, func(t *testing.T) {
116 for themeName, themeFunc := range ThemeFuncs {
117 t.Run(themeName, func(t *testing.T) {
120 dv = behaviorFunc(dv)
123 output := dv.String()
124 golden.RequireEqual(t, []byte(output))
126 switch behaviorName {
128 assertLineWidth(t, 40, output)
130 assertLineWidth(t, 120, output)
140func TestDiffViewWidth(t *testing.T) {
141 for layoutName, layoutFunc := range LayoutFuncs {
142 t.Run(layoutName, func(t *testing.T) {
143 for themeName, themeFunc := range ThemeFuncs {
144 t.Run(themeName, func(t *testing.T) {
145 for width := 1; width <= 110; width++ {
146 if layoutName == "Unified" && width > 60 {
150 t.Run(fmt.Sprintf("WidthOf%03d", width), func(t *testing.T) {
151 dv := diffview.New().
152 Before("main.go", TestMultipleHunksBefore).
153 After("main.go", TestMultipleHunksAfter).
158 output := dv.String()
159 golden.RequireEqual(t, []byte(output))
161 assertLineWidth(t, width, output)
170func TestDiffViewHeight(t *testing.T) {
171 for layoutName, layoutFunc := range LayoutFuncs {
172 t.Run(layoutName, func(t *testing.T) {
173 for themeName, themeFunc := range ThemeFuncs {
174 t.Run(themeName, func(t *testing.T) {
175 for height := 1; height <= 20; height++ {
176 t.Run(fmt.Sprintf("HeightOf%03d", height), func(t *testing.T) {
177 dv := diffview.New().
178 Before("main.go", TestMultipleHunksBefore).
179 After("main.go", TestMultipleHunksAfter).
184 output := dv.String()
185 golden.RequireEqual(t, []byte(output))
187 assertHeight(t, height, output)
196func assertLineWidth(t *testing.T, expected int, output string) {
198 for line := range strings.SplitSeq(output, "\n") {
199 lineWidth = max(lineWidth, ansi.StringWidth(line))
201 if lineWidth != expected {
202 t.Errorf("expected output width to be == %d, got %d", expected, lineWidth)
206func assertHeight(t *testing.T, expected int, output string) {
207 output = strings.TrimSuffix(output, "\n")
208 lines := strings.Count(output, "\n") + 1
209 if lines != expected {
210 t.Errorf("expected output height to be == %d, got %d", expected, lines)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list