|
| 1 | +// Copyright (c) 2026 Keymaster Team |
| 2 | +// Keymaster - SSH key management system |
| 3 | +// This source code is licensed under the MIT license found in the LICENSE file. |
| 4 | +package util |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/bobg/go-generics/v4/slices" |
| 11 | + "github.com/charmbracelet/lipgloss" |
| 12 | +) |
| 13 | + |
| 14 | +// Helper to joinLines lines for cleaner test cases |
| 15 | +func joinLinesSlc(lines []string) string { return strings.Join(lines, "\n") } |
| 16 | +func joinLines(lines ...string) string { return joinLinesSlc(lines) } |
| 17 | +func joinSlc(lines []string) string { return strings.Join(lines, "") } |
| 18 | + |
| 19 | +var testRows = [][]string{ |
| 20 | + {"R1C1", "R1C2", "R1C3", "R1C4", "R1C5"}, |
| 21 | + {"R2C1", "R2C2", "R2C3", "R2C4", "R2C5"}, |
| 22 | + {"R3C1", "R3C2", "R3C3", "R3C4", "R3C5"}, |
| 23 | + {"R4C1", "R4C2", "R4C3", "R4C4", "R4C5"}, |
| 24 | + {"R5C1", "R5C2", "R5C3", "R5C4", "R5C5"}, |
| 25 | +} |
| 26 | +var testContent = joinLinesSlc(slices.Map(testRows, joinSlc)) |
| 27 | + |
| 28 | +func testSubset(from, to Vec[int]) string { |
| 29 | + strs := make([]string, 0, to.Y-from.Y+1) |
| 30 | + for i := from.Y - 1; i <= to.Y-1; i++ { |
| 31 | + strs = append(strs, joinSlc(testRows[i][from.X-1:to.X])) |
| 32 | + } |
| 33 | + return joinLinesSlc(strs) |
| 34 | +} |
| 35 | +func newSize(x, y int) Size { return Size{x * 4, y} } |
| 36 | +func newPos(x, y int) Vec[int] { return Vec[int]{(x - 1) * 4, y - 1} } |
| 37 | + |
| 38 | +func TestRenderContentInViewportAlignXY(t *testing.T) { |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + vSize Size |
| 42 | + tPos Vec[int] |
| 43 | + tSize Size |
| 44 | + align Vec[lipgloss.Position] |
| 45 | + want string |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "1x1 cell", |
| 49 | + vSize: newSize(1, 1), |
| 50 | + tPos: newPos(2, 2), |
| 51 | + tSize: newSize(1, 1), |
| 52 | + align: Vec[lipgloss.Position]{lipgloss.Center, lipgloss.Center}, |
| 53 | + want: testSubset(Vec[int]{2, 2}, Vec[int]{2, 2}), |
| 54 | + // want: testRows[1][1], |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "inner 3x3 Top Left", |
| 58 | + vSize: newSize(3, 3), |
| 59 | + tPos: newPos(3, 3), |
| 60 | + tSize: newSize(1, 1), |
| 61 | + align: Vec[lipgloss.Position]{lipgloss.Top, lipgloss.Left}, |
| 62 | + want: testSubset(Vec[int]{3, 3}, Vec[int]{5, 5}), |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "inner 3x3 Center Bottom", |
| 66 | + vSize: newSize(3, 3), |
| 67 | + tPos: newPos(3, 3), |
| 68 | + tSize: newSize(1, 1), |
| 69 | + align: Vec[lipgloss.Position]{lipgloss.Center, lipgloss.Bottom}, |
| 70 | + want: testSubset(Vec[int]{2, 1}, Vec[int]{4, 3}), |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "inner 3x3 Right Center", |
| 74 | + vSize: newSize(3, 3), |
| 75 | + tPos: newPos(3, 3), |
| 76 | + tSize: newSize(1, 1), |
| 77 | + align: Vec[lipgloss.Position]{lipgloss.Right, lipgloss.Center}, |
| 78 | + want: testSubset(Vec[int]{1, 2}, Vec[int]{3, 4}), |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "inner 3x3 Center Center", |
| 82 | + vSize: newSize(3, 3), |
| 83 | + tPos: newPos(3, 3), |
| 84 | + tSize: newSize(1, 1), |
| 85 | + align: Vec[lipgloss.Position]{lipgloss.Center, lipgloss.Center}, |
| 86 | + want: testSubset(Vec[int]{2, 2}, Vec[int]{4, 4}), |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tt := range tests { |
| 91 | + t.Run(tt.name, func(t *testing.T) { |
| 92 | + got := RenderContentInViewportAlign(testContent, tt.vSize, tt.tPos, tt.tSize, tt.align) |
| 93 | + if got != tt.want { |
| 94 | + t.Errorf("got:\n%s\nwant:\n%s", got, tt.want) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments