Repository navigation
Expand file tree
/
Copy pathtui_delete_test.go
More file actions
575 lines (493 loc) · 18 KB
/
Copy pathtui_delete_test.go
File metadata and controls
575 lines (493 loc) · 18 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package main
import (
"strings"
"testing"
"github.com/foliagecp/easyjson"
)
func typeVertexLinks() []displayLink {
return []displayLink{
{info: makeLinkInfo("hub/types", "srv", "hub/srv", "__type"), isOut: false},
}
}
func objectVertexLinks() []displayLink {
return []displayLink{
{info: makeLinkInfo("hub/objects", "srv-1", "hub/srv-1", "__object"), isOut: false},
{info: makeLinkInfo("hub/srv-1", "type", "hub/srv", "__type"), isOut: true},
}
}
// ── Which entity `d` targets ──────────────────────────────────────────────────
func TestDelete_DOnLinkRowTargetsTheLink(t *testing.T) {
m := makeModel("root", threeLinks(), nil)
m = update(m, key("x")) // a raw link needs the low-level API
m.focus = panelOut
m.rCursor = 1 // first link row, below the group header
m = update(m, key("d"))
if m.form == nil {
t.Fatal("d should open a confirmation")
}
if m.form.kind != formDeleteLink {
t.Fatalf("kind = %v, want formDeleteLink", m.form.kind)
}
if m.form.ctx.linkName != "l1" {
t.Errorf("link name = %q, want l1", m.form.ctx.linkName)
}
}
// TestDelete_ASideColumnNeverReachesTheVertex. Standing in the outgoing panel
// on a group header, `d` used to fall back to deleting the VERTEX — which
// undoes the whole point of putting the centre column in the focus cycle.
func TestDelete_ASideColumnNeverReachesTheVertex(t *testing.T) {
withOps(t, graphOps{}) // any call panics
m := makeModel("hub/x", threeLinks(), nil)
m.focus = panelOut
m.rCursor = 0 // the group header, not a link
m = update(m, key("d"))
if m.form != nil {
t.Fatalf("a side column must not act on the vertex, got %+v", m.form)
}
if !strings.Contains(stripANSI(m.queryResult), "centre column") {
t.Errorf("the refusal should point at the centre column, got %q", stripANSI(m.queryResult))
}
}
func TestDelete_FromTheCentreColumnTargetsTheVertex(t *testing.T) {
m := makeModel("hub/srv-1", objectVertexLinks(), nil) // focus starts on the centre
m = update(m, key("d"))
if m.form == nil || m.form.kind != formDeleteVertex {
t.Fatalf("the centre column is where the vertex lives, got %+v", m.form)
}
}
// ── Tiers ─────────────────────────────────────────────────────────────────────
func TestDelete_BuiltInIsRefusedWithoutAForm(t *testing.T) {
m := makeModel("hub/types", nil, nil)
m = update(m, key("d"))
if m.form != nil {
t.Error("a built-in vertex must not even open a confirmation")
}
if !strings.Contains(m.queryResult, "built-in") {
t.Errorf("toast = %q, want it to explain the refusal", m.queryResult)
}
}
func TestDelete_OrdinaryTierConfirmsWithY(t *testing.T) {
called := ""
withOps(t, graphOps{
objectDelete: func(id string) opResult { called = id; return opResult{status: opApplied} },
})
m := makeModel("hub/srv-1", objectVertexLinks(), nil)
m = update(m, key("d"))
if m.form == nil || m.form.confirmWord != "" {
t.Fatal("an object delete should be a single-key confirmation")
}
m, cmd := updateCmd(m, key("y"))
msg := runCmd(cmd)
if called != "hub/srv-1" {
t.Fatalf("objectDelete(%q), want hub/srv-1", called)
}
if _, ok := msg.(mutationResultMsg); !ok {
t.Fatalf("expected a mutationResultMsg, got %T", msg)
}
}
func TestDelete_OrdinaryTierCancelsWithN(t *testing.T) {
withOps(t, graphOps{}) // any call panics
m := makeModel("hub/srv-1", objectVertexLinks(), nil)
m = update(m, key("d"))
m = update(m, key("n"))
if m.form != nil {
t.Error("n should close the confirmation")
}
}
func TestDelete_TypeTierRequiresTheNameTyped(t *testing.T) {
called := false
withOps(t, graphOps{
typeDelete: func(string) opResult { called = true; return opResult{status: opApplied} },
})
m := makeModel("hub/srv", typeVertexLinks(), nil)
m = update(m, key("d"))
if m.form == nil {
t.Fatal("d should open a confirmation")
}
if m.form.confirmWord != "srv" {
t.Fatalf("confirmWord = %q, want the bare type name", m.form.confirmWord)
}
if !strings.Contains(m.form.title, "every object") {
t.Errorf("title = %q, want it to state the cascade", m.form.title)
}
// Enter with nothing typed must not delete.
m, cmd := updateCmd(m, keyEnter())
runCmd(cmd)
if called {
t.Fatal("a cascading delete ran without the name being typed")
}
if m.form == nil {
t.Fatal("the form should stay open after a failed confirmation")
}
// Wrong name must not delete either.
for _, r := range "srvX" {
m = update(m, key(string(r)))
}
m, cmd = updateCmd(m, keyEnter())
runCmd(cmd)
if called {
t.Fatal("a cascading delete ran with the wrong name typed")
}
// The right name goes through.
m = makeModel("hub/srv", typeVertexLinks(), nil)
m = update(m, key("d"))
for _, r := range "srv" {
m = update(m, key(string(r)))
}
_, cmd = updateCmd(m, keyEnter())
runCmd(cmd)
if !called {
t.Error("typing the exact name should confirm the delete")
}
}
// ── Consequences of a successful delete ───────────────────────────────────────
func TestDelete_NavigatesAwayFromTheDeletedVertex(t *testing.T) {
// gWalkTo persists the current id on every load. Staying on a deleted
// vertex would leave a dead id on disk and break the NEXT launch.
withOps(t, graphOps{
vertexDelete: func(string) opResult { return opResult{status: opApplied} },
})
m := makeModel("hub/x", nil, nil)
m.history = []string{"hub/parent"}
m = update(m, key("x"))
m = update(m, key("d"))
m, cmd := updateCmd(m, key("y"))
if m.pendingNavAfterDelete != "hub/parent" {
t.Fatalf("pendingNavAfterDelete = %q, want the history entry", m.pendingNavAfterDelete)
}
msg := runCmd(cmd)
next, _ := m.Update(msg)
m = next.(tuiModel)
if !m.loading {
t.Error("a load should have been kicked off toward a live vertex")
}
if m.pendingNavAfterDelete != "" {
t.Error("the pending navigation should be consumed")
}
}
func TestDelete_FallsBackToRootWithNoHistory(t *testing.T) {
withOps(t, graphOps{
vertexDelete: func(string) opResult { return opResult{status: opApplied} },
})
m := makeModel("hub/x", nil, nil)
m.history = nil
m = update(m, key("x"))
m = update(m, key("d"))
m, _ = updateCmd(m, key("y"))
if m.pendingNavAfterDelete != hubID("root") {
t.Errorf("pendingNavAfterDelete = %q, want %q", m.pendingNavAfterDelete, hubID("root"))
}
}
func TestDelete_FailureStaysPut(t *testing.T) {
withOps(t, graphOps{
vertexDelete: func(string) opResult { return opResult{status: opFailed, details: "in use"} },
})
m := makeModel("hub/x", nil, nil)
m.history = []string{"hub/parent"}
m = update(m, key("x"))
m = update(m, key("d"))
m, cmd := updateCmd(m, key("y"))
next, _ := m.Update(runCmd(cmd))
m = next.(tuiModel)
if m.loading {
t.Error("a failed delete must not navigate away")
}
if m.errMsg != "in use" {
t.Errorf("errMsg = %q, want the server's reason", m.errMsg)
}
}
func TestDelete_ClearsAnAnchorOnTheDeletedVertex(t *testing.T) {
withOps(t, graphOps{
vertexDelete: func(string) opResult { return opResult{status: opApplied} },
})
m := makeModel("hub/x", nil, nil)
m.linking = &pendingLink{fromID: "hub/x"}
m = update(m, key("x"))
m = update(m, key("d"))
m, cmd := updateCmd(m, key("y"))
next, _ := m.Update(runCmd(cmd))
m = next.(tuiModel)
if m.linking != nil {
t.Error("an anchor on the deleted vertex should be cleared")
}
}
func TestDelete_LinkUsesTheOwningVertex(t *testing.T) {
// For an incoming link the owner is the OTHER vertex; linkId.from already
// holds it, and deleting against the displayed vertex would be wrong.
var gotFrom, gotName string
withOps(t, graphOps{
linkDelete: func(from, name string) opResult {
gotFrom, gotName = from, name
return opResult{status: opApplied}
},
})
m := makeModel("root", mixedLinks(), nil)
m = update(m, key("x")) // a raw link is a low-level entity
m.focus = panelIn
m.lCursor = 1 // the incoming link from hub/parent
m = update(m, key("d"))
if m.form == nil {
t.Fatal("d should open a confirmation")
}
_, cmd := updateCmd(m, key("y"))
runCmd(cmd)
if gotFrom != "parent" {
t.Errorf("linkDelete from = %q, want the owning vertex (parent)", gotFrom)
}
if gotName != "p_link" {
t.Errorf("linkDelete name = %q, want p_link", gotName)
}
}
func TestDelete_RefusedWhileLoading(t *testing.T) {
m := makeModel("hub/x", nil, nil)
m.loading = true
m = update(m, key("d"))
if m.form != nil {
t.Error("a delete must not be staged against a vertex that is still loading")
}
}
// TestDelete_ConfirmationNamesTheWordToType is the fix for typing the wrong
// name and being refused with no explanation.
//
// The prompt used to say "type the name" and never say WHICH — and for a
// types-link the word is the OWNER TYPE's, because the deletion cascades into
// every object of it, so the obvious reading (the link's own name) fails every
// time. It was also one line in the status bar behind a long explanation, so
// the prompt was the first thing truncated away.
func TestDelete_ConfirmationNamesTheWordToType(t *testing.T) {
m := makeModel("hub/srv", typeVertexLinks(), nil)
m.width, m.height = 120, 32
m = update(m, key("d"))
if m.form == nil || m.form.confirmWord != "srv" {
t.Fatalf("fixture: want a typed-word confirmation, got %+v", m.form)
}
out := stripANSI(m.renderCenterPanel())
if !strings.Contains(out, "Type srv to confirm") {
t.Errorf("the prompt must name the exact word:\n%s", out)
}
if !strings.Contains(out, "every object") {
t.Errorf("the blast radius must be readable, not truncated:\n%s", out)
}
if !strings.Contains(stripANSI(m.renderStatus()), "Enter:confirm") {
t.Error("the status bar should carry the confirmation keys")
}
}
func TestDelete_SimpleConfirmationAsksPlainly(t *testing.T) {
m := makeModel("hub/srv-1", objectVertexLinks(), nil)
m.width, m.height = 120, 32
m = update(m, key("d"))
out := stripANSI(m.renderCenterPanel())
if !strings.Contains(out, "Delete it?") {
t.Errorf("a single-key confirmation should say so:\n%s", out)
}
if !strings.Contains(stripANSI(m.renderStatus()), "y:delete") {
t.Error("the status bar should carry y/n")
}
}
// TestDelete_TypingTheWrongWordShowsItIsWrong. Feedback while typing beats a
// refusal at the end: the word is on screen next to what you have typed.
func TestDelete_TypingTheWrongWordShowsItIsWrong(t *testing.T) {
m := makeModel("hub/srv", typeVertexLinks(), nil)
m.width, m.height = 120, 32
m = update(m, key("d"))
for _, r := range "wrong" {
m = update(m, key(string(r)))
}
out := stripANSI(m.renderCenterPanel())
if !strings.Contains(out, "wrong") {
t.Errorf("what was typed should be visible:\n%s", out)
}
if !strings.Contains(out, "Type srv to confirm") {
t.Errorf("and so should the word it has to match:\n%s", out)
}
}
func TestDelete_EscapeClosesWithoutCalling(t *testing.T) {
withOps(t, graphOps{}) // any call panics
m := makeModel("hub/srv-1", objectVertexLinks(), nil)
m = update(m, key("d"))
m = update(m, keyEsc())
if m.form != nil {
t.Error("esc should close the confirmation")
}
}
// Guard the low-level override: with `x` on, a type must be deleted through the
// raw graph API, not the CMDB one.
func TestDelete_LowLevelModeUsesTheRawAPI(t *testing.T) {
called := ""
withOps(t, graphOps{
typeDelete: func(string) opResult { called = "type"; return opResult{status: opApplied} },
vertexDelete: func(string) opResult { called = "vertex"; return opResult{status: opApplied} },
})
m := makeModel("hub/srv", typeVertexLinks(), nil)
m.llMode = true
m = update(m, key("d"))
if m.form.confirmWord != "" {
t.Error("a raw vertex delete should not demand a typed name")
}
_, cmd := updateCmd(m, key("y"))
runCmd(cmd)
if called != "vertex" {
t.Errorf("called the %q API, want vertex", called)
}
}
var _ = easyjson.NewJSONObject
// ── Where a delete leaves you, and what it invalidates ────────────────────────
// TestDelete_ObjectGoesUpToItsType. Staying on the deleted object shows its
// body as though nothing happened — and on a runtime with a trash can that is
// literally true, because the object is parked rather than erased. The type is
// where the deletion is visible: the object is no longer in its list.
func TestDelete_ObjectGoesUpToItsType(t *testing.T) {
withOps(t, graphOps{
objectDelete: func(string) opResult { return opResult{status: opApplied} },
})
m := makeModel("hub/srv-1", objectVertexLinks(), nil)
m.history = []string{"hub/somewhere-unrelated"}
m = update(m, key("d"))
m, _ = updateCmd(m, key("y"))
if m.pendingNavAfterDelete != "hub/srv" {
t.Errorf("after deleting an object you land on %q, want its type", m.pendingNavAfterDelete)
}
if len(m.history) != 1 {
t.Error("going to the entity's home is not a step back — the history stands")
}
}
func TestDelete_TypeGoesUpToTheTypesRoot(t *testing.T) {
withOps(t, graphOps{
typeDelete: func(string) opResult { return opResult{status: opApplied} },
})
m := makeModel("hub/srv", typeVertexLinks(), nil)
m = update(m, key("d"))
for _, r := range "srv" {
m = update(m, key(string(r)))
}
m, _ = updateCmd(m, keyEnter())
if m.pendingNavAfterDelete != hubID("types") {
t.Errorf("after deleting a type you land on %q, want the types root", m.pendingNavAfterDelete)
}
}
func TestDelete_PlainVertexStillFallsBackToTheHistory(t *testing.T) {
// A plain vertex has no home, so there is nothing better than where the
// user came from.
withOps(t, graphOps{
vertexDelete: func(string) opResult { return opResult{status: opApplied} },
})
m := makeModel("hub/x", nil, nil)
m.history = []string{"hub/parent"}
m = update(m, key("x"))
m = update(m, key("d"))
m, _ = updateCmd(m, key("y"))
if m.pendingNavAfterDelete != "hub/parent" {
t.Errorf("pendingNavAfterDelete = %q, want the history entry", m.pendingNavAfterDelete)
}
}
// TestNavigation_AlwaysFetches is the replacement for a whole family of
// cache-invalidation tests, and the answer to "I delete an object, walk to the
// trash can, and it is not there until I restart the CLI".
//
// It was there. The browser was replaying a link list captured before the
// object was parked. Chasing that with invalidation lists is unwinnable — the
// CLI cannot know every vertex a server-side operation touches (the trash can
// is one; restoring FROM it is the next), and it cannot know about the other
// people writing to the same graph at all.
//
// So nothing is cached and every navigation is a real read.
func TestNavigation_AlwaysFetches(t *testing.T) {
m := makeModel("hub/srv-1", objectVertexLinks(), nil)
for _, dest := range []string{hubID("trash_can"), "hub/srv", hubID("root")} {
next, cmd := m.navigateTo(dest)
if cmd == nil {
t.Fatalf("navigating to %s should load it", dest)
}
if _, isFetch := cmd().(vertexInfoMsg); !isFetch {
t.Errorf("%s was not fetched — a browser that shows a snapshot is "+
"showing something that may no longer be true", dest)
}
if !next.loading {
t.Errorf("%s: a real fetch should show as loading", dest)
}
}
}
// TestReload_IsTheOneThingNavigationCannotDo. Standing still while somebody
// else changes the graph is the one case a fetch-on-navigate model does not
// cover, which is exactly what `r` is for.
func TestReload_IsTheOneThingNavigationCannotDo(t *testing.T) {
m := makeModel("hub/x", threeLinks(), nil)
gen := m.loadGen
m, cmd := updateCmd(m, key("r"))
if cmd == nil || m.loadGen != gen+1 || !m.loading {
t.Fatal("r should reload where you are standing")
}
if _, isFetch := runCmd(cmd).(vertexInfoMsg); !isFetch {
t.Error("r should issue a real read")
}
}
// TestDelete_SaysItIsWorking is the fix for "I type the word, it goes green, I
// press Enter, and nothing happens".
//
// Something did happen — the request went out. But formState.submitting gated
// input and was rendered NOWHERE, so a submitted form looked exactly like an
// unsubmitted one, for as long as the server took to answer. On a slow call, or
// one that runs to the NATS timeout, that reads as a dead key.
func TestDelete_SaysItIsWorking(t *testing.T) {
withOps(t, graphOps{
typesLinkDelete: func(string, string) opResult { return opResult{status: opApplied} },
})
schema := displayLink{info: makeLinkInfo("hub/srv", "rack", "hub/rack", ltInstanceOf), isOut: true}
m := makeModel("hub/srv", []displayLink{
{info: makeLinkInfo(hubID("types"), "srv", "hub/srv", ltInstanceOf), isOut: false},
schema,
}, nil)
m.width, m.height = 130, 24
m.focus = panelOut
m.rCursor = 1
m = update(m, key("d"))
before := stripANSI(m.renderCenterPanel())
for _, r := range "srv" {
m = update(m, key(string(r)))
}
m, cmd := updateCmd(m, keyEnter())
if cmd == nil {
t.Fatal("Enter on a matching word must submit")
}
if m.form == nil || !m.form.submitting {
t.Fatal("the form should be in flight")
}
after := stripANSI(m.renderCenterPanel())
if after == before {
t.Error("the screen must change when the request goes out — an unchanged " +
"screen is indistinguishable from a key that did nothing")
}
if !strings.Contains(after, "deleting…") {
t.Errorf("say what is happening:\n%s", after)
}
if !strings.Contains(stripANSI(m.renderStatus()), "Esc") {
t.Error("say how to stop waiting")
}
}
func TestDelete_AFailureSaysWhy(t *testing.T) {
withOps(t, graphOps{
typesLinkDelete: func(string, string) opResult {
return opResult{status: opFailed, details: "type is in use"}
},
})
schema := displayLink{info: makeLinkInfo("hub/srv", "rack", "hub/rack", ltInstanceOf), isOut: true}
m := makeModel("hub/srv", []displayLink{
{info: makeLinkInfo(hubID("types"), "srv", "hub/srv", ltInstanceOf), isOut: false},
schema,
}, nil)
m.width, m.height = 130, 24
m.focus = panelOut
m.rCursor = 1
m = update(m, key("d"))
for _, r := range "srv" {
m = update(m, key(string(r)))
}
_, cmd := updateCmd(m, keyEnter())
m = update(m, runCmd(cmd))
if m.form != nil {
t.Error("a finished request should close the form either way")
}
if !strings.Contains(stripANSI(m.renderStatus()), "type is in use") {
t.Errorf("the server's reason must be on screen:\n%s", stripANSI(m.renderStatus()))
}
}