feat: native clipboard image sync (Linux, opt-in sync_images)

This commit is contained in:
2026-08-14 09:51:25 -04:00
parent 884415c1c1
commit 147cba6d2a
15 changed files with 274 additions and 48 deletions
+38 -1
View File
@@ -9,17 +9,32 @@ import (
"strings"
"testing"
"git.dracodev.net/Projets/clip-sync/internal/clipboard"
"git.dracodev.net/Projets/clip-sync/internal/dedup"
)
// mockClipboard is an in-memory clipboard for testing.
type mockClipboard struct {
text string
text string
mime string
imgBytes []byte
}
func (m *mockClipboard) Read() (string, error) { return m.text, nil }
func (m *mockClipboard) Write(s string) error { m.text = s; return nil }
func (m *mockClipboard) ReadImage() (string, []byte, error) {
if len(m.imgBytes) == 0 {
return "", nil, clipboard.ErrNoImage
}
return m.mime, m.imgBytes, nil
}
func (m *mockClipboard) WriteImage(mime string, data []byte) error {
m.mime = mime
m.imgBytes = append([]byte(nil), data...)
return nil
}
func newTestServer(cb *mockClipboard, opts Options) *Server {
filter := dedup.NewFilter("my-machine")
return New(opts, cb, filter)
@@ -206,6 +221,28 @@ func TestHandleFileDisabled(t *testing.T) {
}
}
func TestHandleClipWritesImage(t *testing.T) {
cb := &mockClipboard{}
srv := newTestServer(cb, Options{MaxBodyBytes: 1 << 20})
// base64 "iVBORw0KGgo=" is a truncated PNG header; content is arbitrary for the test.
body := `{"mime":"image/png","data":"iVBORw0KGgo=","ts":1,"origin":"other-machine"}`
req := httptest.NewRequest(http.MethodPost, "/clip", strings.NewReader(body))
w := httptest.NewRecorder()
srv.handleClip(w, req)
if w.Code != http.StatusNoContent {
t.Errorf("status = %d, want %d", w.Code, http.StatusNoContent)
}
if cb.mime != "image/png" {
t.Errorf("mime = %q, want \"image/png\"", cb.mime)
}
if len(cb.imgBytes) == 0 {
t.Error("image bytes not written to clipboard")
}
}
func TestMain(m *testing.M) {
os.Exit(m.Run())
}