-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtui_ops_test.go
More file actions
328 lines (286 loc) · 11.2 KB
/
Copy pathtui_ops_test.go
File metadata and controls
328 lines (286 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"errors"
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/foliagecp/easyjson"
)
// ── Additive test helpers ─────────────────────────────────────────────────────
//
// The existing update() in tui_test.go discards the tea.Cmd, which is right for
// the pure key-handling tests it was written for. Mutations need the command, so
// these live alongside rather than replacing it.
func updateCmd(m tuiModel, msg tea.Msg) (tuiModel, tea.Cmd) {
next, cmd := m.Update(msg)
return next.(tuiModel), cmd
}
func runCmd(cmd tea.Cmd) tea.Msg {
if cmd == nil {
return nil
}
return cmd()
}
// withOps swaps the live operation layer for the duration of one test. Fields
// left nil in `o` will panic if called — deliberately: that means the flow
// under test reached for an operation it should not have.
func withOps(t *testing.T, o graphOps) {
t.Helper()
old := ops
ops = o
t.Cleanup(func() { ops = old })
}
// ── opResult mapping ──────────────────────────────────────────────────────────
func TestResultFromErr_NilIsApplied(t *testing.T) {
r := resultFromErr(nil)
if r.status != opApplied {
t.Fatalf("status = %v, want opApplied", r.status)
}
if !r.ok() {
t.Error("ok() should be true")
}
}
func TestResultFromErr_ErrorIsFailed(t *testing.T) {
r := resultFromErr(errors.New("boom"))
if r.status != opFailed {
t.Fatalf("status = %v, want opFailed", r.status)
}
if r.details != "boom" {
t.Errorf("details = %q, want %q", r.details, "boom")
}
if r.ok() {
t.Error("ok() should be false")
}
}
func TestResultFromDetails_EmptyOpStackIsNoop(t *testing.T) {
// The whole point of the *WithDetails twins: the client's lenient mapping
// returns nil for both ok and idle, so an empty op_stack is the only
// honest signal that nothing was written.
data := easyjson.NewJSONObjectWithKeyValue("op_stack", easyjson.NewJSONArray())
r := resultFromDetails(data, nil)
if r.status != opNoop {
t.Fatalf("status = %v, want opNoop for an empty op_stack", r.status)
}
}
// TestResultFromDetails_MissingOpStackIsNotEvidence: absence of evidence is
// not evidence of absence. A runtime that parks a deleted object in a trash can
// replies WITHOUT an op_stack, and calling that a no-op made a successful
// delete report "already in that state" and leave the user standing on the
// vertex they had just deleted.
func TestResultFromDetails_MissingOpStackIsNotEvidence(t *testing.T) {
r := resultFromDetails(easyjson.NewJSONObject(), nil)
if r.status != opApplied {
t.Fatalf("status = %v, want opApplied when op_stack is absent — "+
"the server did not say nothing happened, it said nothing", r.status)
}
// An op_stack that came back EMPTY is the real signal: the server looked
// and wrote nothing.
empty := easyjson.NewJSONObject()
empty.SetByPath("op_stack", easyjson.NewJSONArray())
if r := resultFromDetails(empty, nil); r.status != opNoop {
t.Errorf("status = %v, want opNoop for an empty op_stack", r.status)
}
}
func TestResultFromDetails_NonEmptyOpStackIsApplied(t *testing.T) {
stack := easyjson.NewJSONArray()
stack.AddToArray(easyjson.NewJSONObjectWithKeyValue("op", easyjson.NewJSON("vertex.update")))
data := easyjson.NewJSONObjectWithKeyValue("op_stack", stack)
r := resultFromDetails(data, nil)
if r.status != opApplied {
t.Fatalf("status = %v, want opApplied", r.status)
}
}
func TestResultFromDetails_ErrorWins(t *testing.T) {
stack := easyjson.NewJSONArray()
stack.AddToArray(easyjson.NewJSON("x"))
data := easyjson.NewJSONObjectWithKeyValue("op_stack", stack)
r := resultFromDetails(data, errors.New("nope"))
if r.status != opFailed {
t.Fatalf("status = %v, want opFailed even with a populated op_stack", r.status)
}
}
// ── vertexKind ────────────────────────────────────────────────────────────────
func TestVertexKind_Plain(t *testing.T) {
m := makeModel("hub/x", threeLinks(), nil)
if k, tp := m.vertexKind(); k != vkPlain || tp != "" {
t.Fatalf("vertexKind() = (%v,%q), want (vkPlain,\"\")", k, tp)
}
}
func TestVertexKind_Type(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("hub/types", "srv", "hub/srv", "__type"), isOut: false},
}
m := makeModel("hub/srv", links, nil)
if k, _ := m.vertexKind(); k != vkType {
t.Fatalf("vertexKind() = %v, want vkType", k)
}
}
func TestVertexKind_Object(t *testing.T) {
links := []displayLink{
{info: makeLinkInfo("hub/objects", "srv-1", "hub/srv-1", "__object"), isOut: false},
{info: makeLinkInfo("hub/srv-1", "type", "hub/srv", "__type"), isOut: true},
}
m := makeModel("hub/srv-1", links, nil)
k, tp := m.vertexKind()
if k != vkObject {
t.Fatalf("vertexKind() = %v, want vkObject", k)
}
if tp != "srv" {
t.Errorf("type name = %q, want %q", tp, "srv")
}
}
func TestVertexKind_TypeWinsOverTypeOutLink(t *testing.T) {
// A types-link is stored as a __type edge, exactly like an object's
// instance-of edge. Membership in the types topology must decide, or a
// type that declares a types-link would be misread as an object.
links := []displayLink{
{info: makeLinkInfo("hub/types", "srv", "hub/srv", "__type"), isOut: false},
{info: makeLinkInfo("hub/srv", "rack", "hub/rack", "__type"), isOut: true},
}
m := makeModel("hub/srv", links, nil)
if k, _ := m.vertexKind(); k != vkType {
t.Fatalf("vertexKind() = %v, want vkType (types membership must win)", k)
}
}
func TestVertexKind_ObjectWithoutTypeLinkIsNamedBroken(t *testing.T) {
// Half-written object: in the objects topology but its instance-of link
// never landed. It used to report vkPlain, which made it indistinguishable
// from an ordinary vertex — so the high-level API kept refusing edits for
// no reason the user could see. Naming the state is the fix.
links := []displayLink{
{info: makeLinkInfo("hub/objects", "hub/srv-1", "hub/srv-1", "__object"), isOut: false},
}
m := makeModel("hub/srv-1", links, nil)
if k, _ := m.vertexKind(); k != vkBrokenObject {
t.Fatalf("vertexKind() = %v, want vkBrokenObject", k)
}
}
func TestVertexKind_StructuralRootsAreNotTypes(t *testing.T) {
for _, id := range []string{"hub/root", "hub/types", "hub/objects"} {
links := []displayLink{
{info: makeLinkInfo(id, "types", "hub/types", "__types"), isOut: true},
}
m := makeModel(id, links, nil)
if k, _ := m.vertexKind(); k != vkStructural {
t.Errorf("%s classified as %v, want vkStructural", id, k)
}
}
}
func TestVertexKind_TypesLinkOnATypeIsNotAnInstanceOf(t *testing.T) {
// A type's schema links are outgoing __type edges named after their
// target. An object's instance-of edge is an outgoing __type edge named
// "type". Only the name tells them apart.
links := []displayLink{
{info: makeLinkInfo("hub/srv", "rack", "hub/rack", "__type"), isOut: true},
{info: makeLinkInfo("hub/types", "srv", "hub/srv", "__type"), isOut: false},
}
m := makeModel("hub/srv", links, nil)
if k, tp := m.vertexKind(); k != vkType || tp != "" {
t.Fatalf("vertexKind() = (%v,%q), want (vkType,\"\")", k, tp)
}
}
// TestVertexKindBadge_NamesEveryKind pins that no kind renders as nothing.
// An absent badge used to mean three different things at once, and the create
// menu is gated on the classification — so it has to be legible.
func TestVertexKindBadge_NamesEveryKind(t *testing.T) {
cases := []struct {
name string
id string
links []displayLink
want string
}{
{"type", "hub/srv", []displayLink{
{info: makeLinkInfo("hub/types", "srv", "hub/srv", "__type"), isOut: false},
}, "[type]"},
{"object", "hub/srv-1", []displayLink{
{info: makeLinkInfo("hub/srv-1", "type", "hub/srv", "__type"), isOut: true},
}, "[object of srv]"},
{"structural", "hub/root", []displayLink{
{info: makeLinkInfo("hub/root", "types", "hub/types", "__types"), isOut: true},
}, "[built-in]"},
{"plain", "hub/x", threeLinks(), "[vertex]"},
{"broken", "hub/srv-1", []displayLink{
{info: makeLinkInfo("hub/objects", "hub/srv-1", "hub/srv-1", "__object"), isOut: false},
}, "instance-of link missing"},
}
for _, c := range cases {
got := makeModel(c.id, c.links, nil).vertexKindBadge()
if !strings.Contains(got, c.want) {
t.Errorf("%s badge = %q, want it to contain %q", c.name, got, c.want)
}
}
}
// ── viewRestore ───────────────────────────────────────────────────────────────
func TestCaptureApplyRestore_KeepsSelection(t *testing.T) {
m := makeModel("root", threeLinks(), nil)
m.rCursor = 2 // header + l1 → this is link "l2"
sel, ok := selectedLinkIn(m.grouped.outGroups, m.grouped.outFlat, m.rCursor)
if !ok {
t.Fatalf("fixture: cursor %d is not on a link", m.rCursor)
}
r := captureRestore(m)
// Simulate a reload: groups rebuilt from scratch, cursors zeroed.
m.grouped = buildGroupedView(threeLinks(), "")
m.rCursor, m.lCursor = 0, 0
m = applyRestore(m, r)
got, ok := selectedLinkIn(m.grouped.outGroups, m.grouped.outFlat, m.rCursor)
if !ok {
t.Fatalf("after restore the cursor is not on a link (idx %d)", m.rCursor)
}
if got.info.id.name != sel.info.id.name {
t.Errorf("selection = %q, want %q", got.info.id.name, sel.info.id.name)
}
}
func TestCaptureApplyRestore_KeepsCollapse(t *testing.T) {
m := makeModel("root", mixedLinks(), nil)
m.grouped.outGroups[0].collapsed = true
collapsedType := m.grouped.outGroups[0].tp
m.grouped.rebuildFlat()
r := captureRestore(m)
m.grouped = buildGroupedView(mixedLinks(), "") // rebuild drops collapse
m = applyRestore(m, r)
for _, g := range m.grouped.outGroups {
if g.tp == collapsedType && !g.collapsed {
t.Fatalf("group %q should still be collapsed after restore", collapsedType)
}
}
}
func TestApplyRestore_DeletedLinkFallsBackToItsGroupHeader(t *testing.T) {
// The normal case right after deleting the selected link: it is gone, and
// dropping the user at index 0 of an unrelated group would be disorienting.
m := makeModel("root", threeLinks(), nil)
m.rCursor = 2 // "l2"
r := captureRestore(m)
remaining := []displayLink{
{info: makeLinkInfo("root", "l1", "c1", "contains"), isOut: true},
{info: makeLinkInfo("root", "l3", "c3", "contains"), isOut: true},
}
m.grouped = buildGroupedView(remaining, "")
m.rCursor = 0
m = applyRestore(m, r)
if m.rCursor >= len(m.grouped.outFlat) {
t.Fatalf("cursor %d out of range (%d rows)", m.rCursor, len(m.grouped.outFlat))
}
item := m.grouped.outFlat[m.rCursor]
if item.kind != flatTypeGroup {
t.Errorf("cursor landed on kind %v, want the group header of the deleted link's type", item.kind)
}
}
func TestApplyRestore_NilIsNoop(t *testing.T) {
m := makeModel("root", threeLinks(), nil)
m.rCursor = 2
got := applyRestore(m, nil)
if got.rCursor != 2 {
t.Errorf("rCursor = %d, want it untouched (2)", got.rCursor)
}
}
func TestCaptureRestore_PreservesFocus(t *testing.T) {
m := makeModel("root", mixedLinks(), nil)
m.focus = panelIn
r := captureRestore(m)
m.focus = panelOut
m = applyRestore(m, r)
if m.focus != panelIn {
t.Errorf("focus = %v, want panelIn", m.focus)
}
}