1package component
2
3import "testing"
4
5// reconcileEntryState decides how row-selected handlers resync the entry
6// buffer with the realInput/ghostSuffix shadow. The entry buffer is the source
7// of truth: search-changed is debounced (~150ms), so realInput lags behind
8// what the user actually typed. The old force-restore behavior rewrote the
9// buffer to the stale realInput, deleting characters typed during the
10// debounce window (fast typing dropped letters).
11func TestReconcileEntryState(t *testing.T) {
12 tests := []struct {
13 name string
14 buffer string
15 realInput string
16 ghostSuffix string
17 wantRealInput string
18 wantRewrite bool
19 }{
20 {
21 name: "ghost visible and untouched: strip it from the buffer",
22 buffer: "google.com",
23 realInput: "goo",
24 ghostSuffix: "gle.com",
25 wantRealInput: "goo",
26 wantRewrite: true,
27 },
28 {
29 name: "buffer in sync, no ghost: nothing to do",
30 buffer: "goo",
31 realInput: "goo",
32 ghostSuffix: "",
33 wantRealInput: "goo",
34 wantRewrite: false,
35 },
36 {
37 name: "user typed ahead of debounced realInput: buffer wins, never rewrite",
38 buffer: "bne",
39 realInput: "bn",
40 ghostSuffix: "",
41 wantRealInput: "bne",
42 wantRewrite: false,
43 },
44 {
45 name: "user typed over the selected ghost suffix: buffer wins",
46 buffer: "goog",
47 realInput: "goo",
48 ghostSuffix: "gle.com",
49 wantRealInput: "goog",
50 wantRewrite: false,
51 },
52 {
53 name: "stale ghost flag but buffer already shows real input only",
54 buffer: "goo",
55 realInput: "goo",
56 ghostSuffix: "gle.com",
57 wantRealInput: "goo",
58 wantRewrite: false,
59 },
60 {
61 name: "user cleared the entry: buffer wins",
62 buffer: "",
63 realInput: "goo",
64 ghostSuffix: "",
65 wantRealInput: "",
66 wantRewrite: false,
67 },
68 {
69 name: "empty ghost with empty input stays empty",
70 buffer: "",
71 realInput: "",
72 ghostSuffix: "",
73 wantRealInput: "",
74 wantRewrite: false,
75 },
76 }
77
78 for _, tt := range tests {
79 t.Run(tt.name, func(t *testing.T) {
80 gotRealInput, gotRewrite := reconcileEntryState(tt.buffer, tt.realInput, tt.ghostSuffix)
81 if gotRealInput != tt.wantRealInput {
82 t.Errorf("realInput = %q, want %q", gotRealInput, tt.wantRealInput)
83 }
84 if gotRewrite != tt.wantRewrite {
85 t.Errorf("rewrite = %v, want %v", gotRewrite, tt.wantRewrite)
86 }
87 })
88 }
89}