package theme_test import ( "bytes" "fmt" "github.com/neilotoole/slogt" "github.com/sablierapp/sablier/pkg/theme" "github.com/sablierapp/sablier/pkg/version" "log/slog" "os" "testing" "testing/fstest" "time" ) var ( StartingInstanceInfo = theme.Instance{ Name: "starting-instance", Status: "instance is starting...", Error: nil, CurrentReplicas: 0, DesiredReplicas: 1, } StartedInstanceInfo = theme.Instance{ Name: "started-instance", Status: "instance is started.", Error: nil, CurrentReplicas: 1, DesiredReplicas: 1, } ErrorInstanceInfo = theme.Instance{ Name: "error-instance", Error: fmt.Errorf("instance does not exist"), CurrentReplicas: 0, DesiredReplicas: 1, } ) func TestThemes_Render(t *testing.T) { const customTheme = ` Starting {{ .DisplayName }} Your instance(s) will stop after {{ .SessionDuration }} of inactivity {{- range $i, $instance := .InstanceStates }} {{- if $instance.Error }} {{- else }} {{- end}} {{ end -}}
{{ $instance.Name }}{{ $instance.Error }}{{ $instance.Status }} ({{ $instance.CurrentReplicas }}/{{ $instance.DesiredReplicas }})
Sablier version {{ .Version }} ` version.Version = "1.0.0" themes, err := theme.NewWithCustomThemes(fstest.MapFS{ "inner/custom-theme.html": &fstest.MapFile{Data: []byte(customTheme)}, }, slogt.New(t)) if err != nil { t.Error(err) return } instances := []theme.Instance{ StartingInstanceInfo, StartedInstanceInfo, ErrorInstanceInfo, } options := theme.Options{ DisplayName: "Test", InstanceStates: instances, SessionDuration: 10 * time.Minute, RefreshFrequency: 5 * time.Second, } type args struct { name string opts theme.Options } tests := []struct { name string args args wantErr bool }{ { name: "Load ghost theme", args: args{ name: "ghost", opts: options, }, wantErr: false, }, { name: "Load hacker-terminal theme", args: args{ name: "hacker-terminal", opts: options, }, wantErr: false, }, { name: "Load matrix theme", args: args{ name: "matrix", opts: options, }, wantErr: false, }, { name: "Load shuffle theme", args: args{ name: "shuffle", opts: options, }, wantErr: false, }, { name: "Load non existent theme", args: args{ name: "non-existent", opts: options, }, wantErr: true, }, { name: "Load custom theme", args: args{ name: "custom-theme", opts: options, }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { writer := &bytes.Buffer{} if err := themes.Render(tt.args.name, tt.args.opts, writer); (err != nil) != tt.wantErr { t.Errorf("Themes.Render() error = %v, wantErr %v", err, tt.wantErr) return } }) } } func ExampleThemes_Render() { const customTheme = ` Starting {{ .DisplayName }} Your instances will stop after {{ .SessionDuration }} of inactivity {{- range $i, $instance := .InstanceStates }} {{- if $instance.Error }} {{- else }} {{- end}} {{- end }}
{{ $instance.Name }}{{ $instance.Error }}{{ $instance.Status }} ({{ $instance.CurrentReplicas }}/{{ $instance.DesiredReplicas }})
Sablier version {{ .Version }} ` version.Version = "1.0.0" themes, err := theme.NewWithCustomThemes(fstest.MapFS{ "inner/custom-theme.html": &fstest.MapFile{Data: []byte(customTheme)}, }, slog.Default()) if err != nil { panic(err) } instances := []theme.Instance{ StartingInstanceInfo, StartedInstanceInfo, ErrorInstanceInfo, } err = themes.Render("custom-theme", theme.Options{ DisplayName: "Test", InstanceStates: instances, ShowDetails: true, SessionDuration: 10 * time.Minute, RefreshFrequency: 5 * time.Second, }, os.Stdout) if err != nil { panic(err) } // Output: // // // // // // Starting Test // Your instances will stop after 10 minutes of inactivity // // // // // // // // // // // // // //
starting-instanceinstance is starting... (0/1)
started-instanceinstance is started. (1/1)
error-instanceinstance does not exist
// Sablier version 1.0.0 // // }