provider_empty_test.go

 1package config
 2
 3import (
 4	"encoding/json"
 5	"os"
 6	"testing"
 7
 8	"github.com/charmbracelet/catwalk/pkg/catwalk"
 9	"github.com/stretchr/testify/require"
10)
11
12type emptyProviderClient struct{}
13
14func (m *emptyProviderClient) GetProviders() ([]catwalk.Provider, error) {
15	return []catwalk.Provider{}, nil
16}
17
18func TestProvider_loadProvidersEmptyResult(t *testing.T) {
19	client := &emptyProviderClient{}
20	tmpPath := t.TempDir() + "/providers.json"
21
22	cfg := &Config{}
23	providers, err := loadProviders(false, client, tmpPath, cfg)
24	require.Contains(t, err.Error(), "Crush was unable to fetch an updated list of providers")
25	require.Empty(t, providers)
26	require.Len(t, providers, 0)
27
28	// Check that no cache file was created for empty results
29	require.NoFileExists(t, tmpPath, "Cache file should not exist for empty results")
30}
31
32func TestProvider_loadProvidersEmptyCache(t *testing.T) {
33	client := &mockProviderClient{shouldFail: false}
34	tmpPath := t.TempDir() + "/providers.json"
35
36	// Create an empty cache file
37	emptyProviders := []catwalk.Provider{}
38	data, err := json.Marshal(emptyProviders)
39	require.NoError(t, err)
40	require.NoError(t, os.WriteFile(tmpPath, data, 0o644))
41
42	// Should refresh and get real providers instead of using empty cache
43	cfg := &Config{}
44	providers, err := loadProviders(false, client, tmpPath, cfg)
45	require.NoError(t, err)
46	require.NotNil(t, providers)
47	require.Len(t, providers, 1)
48	require.Equal(t, "Mock", providers[0].Name)
49}