client_test.go 2.0 KB

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