Ariver
2026-07-11 015f1089ea875a153dd2a18ef42c25145d4868f5
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
//go:build darwin
 
package axinput
 
import "testing"
 
func TestPrepareClipboardFallbackFailsWhenPasteboardWriteFails(t *testing.T) {
    withClipboardHooks(t,
        func() clipboardSnapshot { return clipboardSnapshot{text: "original", hasText: true} },
        func(text string) bool { return false },
        func() bool { return true },
    )
 
    driver := &darwinDriver{}
    result := driver.PrepareClipboardFallback("recognized text")
    if result.OK || result.Prepared || result.Token != "" {
        t.Fatalf("result = %+v, want failed prepare without token", result)
    }
}
 
func TestPrepareClipboardFallbackCreatesTokenAfterVerifiedWrite(t *testing.T) {
    var wrote string
    withClipboardHooks(t,
        func() clipboardSnapshot { return clipboardSnapshot{text: "original", hasText: true} },
        func(text string) bool {
            wrote = text
            return true
        },
        func() bool { return true },
    )
 
    driver := &darwinDriver{}
    result := driver.PrepareClipboardFallback("recognized text")
    if !result.OK || !result.Prepared || result.Token == "" {
        t.Fatalf("result = %+v, want prepared token", result)
    }
    if wrote != "recognized text" {
        t.Fatalf("wrote = %q", wrote)
    }
}
 
func TestCopyTextToClipboardWritesWithoutRestoreToken(t *testing.T) {
    var wrote string
    withClipboardHooks(t,
        func() clipboardSnapshot { return clipboardSnapshot{text: "original", hasText: true} },
        func(text string) bool {
            wrote = text
            return true
        },
        func() bool { return true },
    )
 
    driver := &darwinDriver{}
    result := driver.CopyTextToClipboard("recognized text")
    if !result.OK || !result.Prepared || result.Token != "" {
        t.Fatalf("result = %+v, want clipboard write without restore token", result)
    }
    if wrote != "recognized text" {
        t.Fatalf("wrote = %q", wrote)
    }
}
 
func TestRestoreClipboardFallbackReportsRestoreWriteFailure(t *testing.T) {
    writes := 0
    current := clipboardSnapshot{text: "recognized text", hasText: true}
    withClipboardHooks(t,
        func() clipboardSnapshot { return current },
        func(text string) bool {
            writes++
            return writes == 1
        },
        func() bool { return true },
    )
 
    driver := &darwinDriver{}
    prepared := driver.PrepareClipboardFallback("recognized text")
    if !prepared.OK || prepared.Token == "" {
        t.Fatalf("prepare = %+v", prepared)
    }
    restored := driver.RestoreClipboardFallback(prepared.Token)
    if restored.OK || restored.Restored {
        t.Fatalf("restore = %+v, want failure when original clipboard cannot be written", restored)
    }
}
 
func withClipboardHooks(t *testing.T, read func() clipboardSnapshot, write func(string) bool, clear func() bool) {
    t.Helper()
    origRead := readClipboardSnapshot
    origWrite := writeClipboardText
    origClear := clearClipboardText
    readClipboardSnapshot = read
    writeClipboardText = write
    clearClipboardText = clear
    t.Cleanup(func() {
        readClipboardSnapshot = origRead
        writeClipboardText = origWrite
        clearClipboardText = origClear
    })
}