package ca_test

import (
	"context"
	"testing"
	"time"
)

// TestGetCtrlNotFound covers the resolve-failure branch of GetCtrl.
func TestGetCtrlNotFound(t *testing.T) {
	srv := newTestServer(t)
	cli := newTestClient(t, srv)

	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	defer cancel()

	if _, err := cli.GetCtrl(ctx, "NO:SUCH:PV"); err == nil {
		t.Fatal("GetCtrl on missing PV: want error")
	}
}

// TestPutNotFound covers the resolve-failure branch of Put.
func TestPutNotFound(t *testing.T) {
	srv := newTestServer(t)
	cli := newTestClient(t, srv)

	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	defer cancel()

	if err := cli.Put(ctx, "NO:SUCH:PV", 1.0); err == nil {
		t.Fatal("Put on missing PV: want error")
	}
}

// TestPutUncoercibleValue covers the encodePut-failure branch of Put: the PV
// resolves but the supplied value cannot be coerced to the native type.
func TestPutUncoercibleValue(t *testing.T) {
	srv := newTestServer(t)
	cli := newTestClient(t, srv)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := cli.Put(ctx, "TEST:DOUBLE", []int{1, 2, 3}); err == nil {
		t.Fatal("Put with uncoercible value: want error")
	}
}
