client_test.go

 1package lsp
 2
 3import (
 4	"context"
 5	"testing"
 6	"time"
 7
 8	"github.com/charmbracelet/crush/internal/config"
 9	"github.com/charmbracelet/crush/internal/env"
10	"github.com/stretchr/testify/require"
11)
12
13func TestClient(t *testing.T) {
14	ctx := context.Background()
15
16	// Create a simple config for testing
17	cfg := config.LSPConfig{
18		Command:   "$THE_CMD", // Use echo as a dummy command that won't fail
19		Args:      []string{"hello"},
20		FileTypes: []string{"go"},
21		Env:       map[string]string{},
22	}
23
24	// Test creating a powernap client - this will likely fail with echo
25	// but we can still test the basic structure
26	client, err := New(ctx, "test", cfg, config.NewEnvironmentVariableResolver(env.NewFromMap(map[string]string{
27		"THE_CMD": "echo",
28	})), ".", false)
29	if err != nil {
30		// Expected to fail with echo command, skip the rest
31		t.Skipf("Powernap client creation failed as expected with dummy command: %v", err)
32		return
33	}
34
35	// If we get here, test basic interface methods
36	if client.GetName() != "test" {
37		t.Errorf("Expected name 'test', got '%s'", client.GetName())
38	}
39
40	if !client.HandlesFile("test.go") {
41		t.Error("Expected client to handle .go files")
42	}
43
44	if client.HandlesFile("test.py") {
45		t.Error("Expected client to not handle .py files")
46	}
47
48	// Test server state
49	client.SetServerState(StateReady)
50	if client.GetServerState() != StateReady {
51		t.Error("Expected server state to be StateReady")
52	}
53
54	// Clean up - expect this to fail with echo command
55	if err := client.Close(t.Context()); err != nil {
56		// Expected to fail with echo command
57		t.Logf("Close failed as expected with dummy command: %v", err)
58	}
59}
60
61func TestNilClient(t *testing.T) {
62	t.Parallel()
63
64	var c *Client
65
66	require.False(t, c.HandlesFile("/some/file.go"))
67	require.Equal(t, DiagnosticCounts{}, c.GetDiagnosticCounts())
68	require.Nil(t, c.GetDiagnostics())
69	require.Nil(t, c.OpenFileOnDemand(context.Background(), "/some/file.go"))
70	require.Nil(t, c.NotifyChange(context.Background(), "/some/file.go"))
71	c.WaitForDiagnostics(context.Background(), time.Second)
72}