| 
 | 1 | +package types_test  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"crypto/sha256"  | 
 | 5 | +	"fmt"  | 
 | 6 | +	"math/rand/v2"  | 
 | 7 | +	"strings"  | 
 | 8 | +	"testing"  | 
 | 9 | + | 
 | 10 | +	"github.com/stretchr/testify/require"  | 
 | 11 | + | 
 | 12 | +	"github.com/coder/preview/types"  | 
 | 13 | +)  | 
 | 14 | + | 
 | 15 | +// TestParameterEquality might be a bit pointless. It just ensures the  | 
 | 16 | +// Hash function returns a consistent value for the same input.  | 
 | 17 | +// TODO: Remove this, just wanted to create some random parameters  | 
 | 18 | +func TestParameterEquality(t *testing.T) {  | 
 | 19 | +	t.Parallel()  | 
 | 20 | + | 
 | 21 | +	for i := 0; i < 100; i++ {  | 
 | 22 | +		t.Run(fmt.Sprintf("EqualityCheck_%d", i), func(t *testing.T) {  | 
 | 23 | +			t.Parallel()  | 
 | 24 | +			seed := sha256.Sum256([]byte(t.Name()))  | 
 | 25 | +			src := rand.NewChaCha8(seed)  | 
 | 26 | + | 
 | 27 | +			param := randomParameter(src)  | 
 | 28 | +			a, err := param.Hash()  | 
 | 29 | +			require.NoError(t, err)  | 
 | 30 | + | 
 | 31 | +			b, err := param.Hash()  | 
 | 32 | +			require.NoError(t, err)  | 
 | 33 | + | 
 | 34 | +			require.Equal(t, a, b)  | 
 | 35 | +		})  | 
 | 36 | +	}  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +func randomParameter(src *rand.ChaCha8) *types.RichParameter {  | 
 | 40 | +	ty := randomElement(src, "string", "number", "bool", "list(string)")  | 
 | 41 | +	opts := make([]*types.RichParameterOption, randomInt(src, 0, 5))  | 
 | 42 | +	for i := range opts {  | 
 | 43 | +		opts[i] = randomParameterOption(src, ty)  | 
 | 44 | +	}  | 
 | 45 | + | 
 | 46 | +	return &types.RichParameter{  | 
 | 47 | +		Name:         randomString(src, 20),  | 
 | 48 | +		Description:  randomString(src, 20),  | 
 | 49 | +		Type:         ty,  | 
 | 50 | +		Mutable:      randomElement(src, true, false),  | 
 | 51 | +		DefaultValue: randomValue(src, ty),  | 
 | 52 | +		Icon:         randomString(src, 10),  | 
 | 53 | +		Options:      opts,  | 
 | 54 | +		Validation:   randomValidation(src, ty),  | 
 | 55 | +		Required:     randomElement(src, true, false),  | 
 | 56 | +		DisplayName:  randomString(src, 10),  | 
 | 57 | +		Order:        int32(randomInt(src, 0, 10)),  | 
 | 58 | +		Ephemeral:    randomElement(src, true, false),  | 
 | 59 | +	}  | 
 | 60 | +}  | 
 | 61 | + | 
 | 62 | +func randomValidation(src *rand.ChaCha8, ty string) *types.ParameterValidation {  | 
 | 63 | +	var minVal, maxVal *int32  | 
 | 64 | +	mono := ""  | 
 | 65 | +	if ty == "number" {  | 
 | 66 | +		mv := int32(randomInt(src, 0, 10))  | 
 | 67 | +		mxv := int32(randomInt(src, uint64(mv), uint64(10+mv)))  | 
 | 68 | +		minVal = &mv  | 
 | 69 | +		maxVal = &mxv  | 
 | 70 | +		mono = randomElement(src, "", "increasing", "decreasing")  | 
 | 71 | +	}  | 
 | 72 | + | 
 | 73 | +	return &types.ParameterValidation{  | 
 | 74 | +		Regex:     randomString(src, 10),  | 
 | 75 | +		Error:     randomString(src, 10),  | 
 | 76 | +		Min:       minVal,  | 
 | 77 | +		Max:       maxVal,  | 
 | 78 | +		Monotonic: mono,  | 
 | 79 | +	}  | 
 | 80 | +}  | 
 | 81 | + | 
 | 82 | +func randomParameterOption(src *rand.ChaCha8, ty string) *types.RichParameterOption {  | 
 | 83 | +	return &types.RichParameterOption{  | 
 | 84 | +		Name:        randomString(src, 10),  | 
 | 85 | +		Description: randomString(src, 20),  | 
 | 86 | +		Value:       randomValue(src, ty),  | 
 | 87 | +		Icon:        randomString(src, 10),  | 
 | 88 | +	}  | 
 | 89 | +}  | 
 | 90 | + | 
 | 91 | +func randomValue(src *rand.ChaCha8, ty string) string {  | 
 | 92 | +	switch ty {  | 
 | 93 | +	case "string":  | 
 | 94 | +		return randomString(src, 20)  | 
 | 95 | +	case "number":  | 
 | 96 | +		return fmt.Sprintf("%d", randomInt(src, 0, 100))  | 
 | 97 | +	case "bool":  | 
 | 98 | +		return fmt.Sprintf("%t", randomElement(src, true, false))  | 
 | 99 | +	case "list(string)":  | 
 | 100 | +		elems := make([]string, randomInt(src, 0, 5))  | 
 | 101 | +		for i := range elems {  | 
 | 102 | +			elems[i] = fmt.Sprintf("%q", randomString(src, 7))  | 
 | 103 | +		}  | 
 | 104 | +		return fmt.Sprintf("[%s]", strings.Join(elems, ", "))  | 
 | 105 | +	}  | 
 | 106 | +	panic(fmt.Sprintf("unsupported type: %s", ty))  | 
 | 107 | +}  | 
 | 108 | + | 
 | 109 | +func randomInt(src *rand.ChaCha8, min, max uint64) int64 {  | 
 | 110 | +	v := src.Uint64() % max  | 
 | 111 | +	return int64(v) + int64(min)  | 
 | 112 | +}  | 
 | 113 | + | 
 | 114 | +func randomElement[T any](src *rand.ChaCha8, elements ...T) T {  | 
 | 115 | +	if len(elements) > 255 {  | 
 | 116 | +		panic(fmt.Sprintf("randomElement only supports 255 elements, got %d", len(elements)))  | 
 | 117 | +	}  | 
 | 118 | +	b := make([]byte, 1)  | 
 | 119 | +	_, err := src.Read(b)  | 
 | 120 | +	if err != nil {  | 
 | 121 | +		panic(fmt.Errorf("rand.Read: %w", err))  | 
 | 122 | +	}  | 
 | 123 | +	return elements[int(b[0])%len(elements)]  | 
 | 124 | +}  | 
 | 125 | + | 
 | 126 | +func randomString(src *rand.ChaCha8, length int) string {  | 
 | 127 | +	out := make([]byte, length)  | 
 | 128 | +	_, err := src.Read(out)  | 
 | 129 | +	if err != nil {  | 
 | 130 | +		panic(fmt.Errorf("rand.Read: %w", err))  | 
 | 131 | +	}  | 
 | 132 | + | 
 | 133 | +	for i := range out {  | 
 | 134 | +		out[i] = out[i] % (26 * 2)  | 
 | 135 | +		if out[i] >= 26 {  | 
 | 136 | +			out[i] += 'A' - 26 // subtract off the 0-26 lowercase  | 
 | 137 | +		} else {  | 
 | 138 | +			out[i] += 'a'  | 
 | 139 | +		}  | 
 | 140 | +	}  | 
 | 141 | +	return string(out)  | 
 | 142 | +}  | 
0 commit comments