oh-my-posh/src/segments/nightscout_test.go

165 lines
5.8 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
2021-11-23 01:34:35 -08:00
import (
"errors"
"oh-my-posh/mock"
"oh-my-posh/properties"
2022-01-26 06:54:36 -08:00
"oh-my-posh/template"
2021-11-23 01:34:35 -08:00
"testing"
"github.com/stretchr/testify/assert"
)
const (
2021-12-11 08:31:58 -08:00
FAKEAPIURL = "FAKE"
2021-11-23 01:34:35 -08:00
)
func TestNSSegment(t *testing.T) {
cases := []struct {
Case string
JSONResponse string
ExpectedString string
ExpectedEnabled bool
CacheTimeout int
CacheFoundFail bool
2021-11-23 01:34:35 -08:00
Template string
Error error
}{
{
Case: "Flat 150",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":150,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"Flat","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 150→",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "DoubleDown 50",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":50,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"DoubleDown","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 50↓↓",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "DoubleUp 250",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":250,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"DoubleUp","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 250↑↑",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "SingleUp 130",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":130,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"SingleUp","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 130↑",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "FortyFiveUp 174",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":174,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"FortyFiveUp","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 174↗",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "FortyFiveDown 61",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":61,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"FortyFiveDown","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 61↘",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "DoubleDown 50",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":50,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"DoubleDown","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 50↓↓",
2021-11-23 01:34:35 -08:00
ExpectedEnabled: true,
},
{
Case: "Error in retrieving data",
JSONResponse: "nonsense",
Error: errors.New("Something went wrong"),
ExpectedEnabled: false,
},
{
Case: "Empty array",
JSONResponse: "[]",
ExpectedEnabled: false,
},
{
Case: "DoubleDown 50 from cache",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":50,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"DoubleDown","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 50↓↓",
ExpectedEnabled: true,
CacheTimeout: 10,
},
{
Case: "DoubleDown 50 from cache not found",
JSONResponse: `
[{"_id":"619d6fa819696e8ded5b2206","sgv":50,"date":1637707537000,"dateString":"2021-11-23T22:45:37.000Z","trend":4,"direction":"DoubleDown","device":"share2","type":"sgv","utcOffset":0,"sysTime":"2021-11-23T22:45:37.000Z","mills":1637707537000}]`, //nolint:lll
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 50↓↓",
ExpectedEnabled: true,
CacheTimeout: 10,
CacheFoundFail: true,
},
{
Case: "Error parsing response",
JSONResponse: `
4tffgt4e4567`,
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.TrendIcon}}",
ExpectedString: "\ue2a1 50↓↓",
ExpectedEnabled: false,
CacheTimeout: 10,
},
{
Case: "Faulty template",
JSONResponse: `
[{"sgv":50,"direction":"DoubleDown"}]`,
2022-02-01 03:10:46 -08:00
Template: "\ue2a1 {{.Sgv}}{{.Burp}}",
2022-01-26 06:54:36 -08:00
ExpectedString: template.IncorrectTemplate,
ExpectedEnabled: true,
CacheTimeout: 10,
},
2021-11-23 01:34:35 -08:00
}
for _, tc := range cases {
env := &mock.MockedEnvironment{}
props := properties.Map{
2022-07-15 04:24:56 -07:00
properties.CacheTimeout: tc.CacheTimeout,
URL: "FAKE",
2021-11-23 01:34:35 -08:00
}
cache := &mock.MockedCache{}
cache.On("Get", FAKEAPIURL).Return(tc.JSONResponse, !tc.CacheFoundFail)
cache.On("Set", FAKEAPIURL, tc.JSONResponse, tc.CacheTimeout).Return()
2022-01-07 10:41:58 -08:00
env.On("HTTPRequest", FAKEAPIURL).Return([]byte(tc.JSONResponse), tc.Error)
env.On("Cache").Return(cache)
2021-11-23 01:34:35 -08:00
2022-01-26 05:10:18 -08:00
ns := &Nightscout{
2021-11-23 01:34:35 -08:00
props: props,
env: env,
}
enabled := ns.Enabled()
2021-11-23 01:34:35 -08:00
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if !enabled {
continue
}
if tc.Template == "" {
tc.Template = ns.Template()
}
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, ns), tc.Case)
2021-11-23 01:34:35 -08:00
}
}