-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSTibber.Tests.ps1
168 lines (137 loc) · 6.09 KB
/
PSTibber.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
param()
BeforeAll {
$TibberAccessToken = $env:TIBBER_ACCESS_TOKEN = "5K4MVS-OjfWhK_4yrjOlFe1F6kJXPVf7eQYggo8ebAE" # demo token
$TibberURI = 'https://api.tibber.com/v1-beta/gql'
$TibberUserAgent = $env:TIBBER_USER_AGENT = "Pester/$((Get-Module -Name Pester)[0].Version.ToString())"
$TibberHomeId = '96a14971-525a-4420-aae9-e5aedaa129ff'
$TibberAddress = 'Winterfell Castle 1'
$TibberEAN = '735999102107573183'
$TibberEmail = 'arya@winterfell.com'
$TibberWebSocket = @{
connection = $null
subscription = $null
}
}
Describe "Invoke-TibberQuery" {
It "Can invoke GraphQL query" {
$query = "{ viewer{ homes{ id } } }"
(Invoke-TibberQuery -Query $query).viewer.homes[0].id | Should -Be $TibberHomeId
}
It "Fails when invalid query" {
{ Invoke-TibberQuery -Query "{}" } | Should -Throw
}
It "Fails when invalid query data" {
Invoke-TibberQuery -Query "{ viewer{ home(id:`"__$TibberHomeId`"){ id }}}" -ErrorAction SilentlyContinue | Should -Be $null
}
It "Fails when invalid URI" {
{ Invoke-TibberQuery -URI $($TibberURI -replace '.com', '.net') -Query "{}" } | Should -Throw
}
}
Describe "Get-TibberUser" {
It "Can get user info" {
(Get-TibberUser).login | Should -Be $TibberEmail
}
It "Can get user info (force)" {
(Get-TibberUser -Force).login | Should -Be $TibberEmail
}
It "Can get user info (w/ access token)" {
(Get-TibberUser -PersonalAccessToken $TibberAccessToken).login | Should -Be $TibberEmail
}
}
Describe "Get-TibberHome" {
It "Can get home info" {
(Get-TibberHome).id | Should -Be $TibberHomeId
}
It "Can get home info by Id (w/ access token)" {
(Get-TibberHome -PersonalAccessToken $TibberAccessToken -HomeId $TibberHomeId).id | Should -Be $TibberHomeId
}
It "Can get home address" {
(Get-TibberHome -IncludeAddress).address.address1 | Should -Be $TibberAddress
}
It "Can get home owner" {
(Get-TibberHome -IncludeOwner).owner.contactInfo.email | Should -Be $TibberEmail
}
It "Can get metering data" {
(Get-TibberHome -IncludeMetering).meteringPointData.consumptionEan | Should -Be $TibberEAN
}
It "Can get subscription info" {
(Get-TibberHome -IncludeSubscription).currentSubscription.subscriber.contactInfo.email | Should -Be $TibberEmail
}
It "Can get home features" {
(Get-TibberHome -IncludeFeatures).features.realTimeConsumptionEnabled | Should -BeTrue
}
}
Describe "Get-TibberConsumption" {
It "Can get power consumption" {
Get-TibberConsumption | Should -Not -Be $null
}
It "Can get power consumption (based on date)" {
Get-TibberConsumption -From ([DateTime]::Now).AddHours(-10) -To ([DateTime]::Now).AddHours(-9) | Should -Not -Be $null
}
It "Can get power consumption (w/ access token)" {
Get-TibberConsumption -PersonalAccessToken $TibberAccessToken -HomeId $TibberHomeId -Last 100 -FilterEmptyNodes | Should -Not -Be $null
}
}
Describe "Get-TibberProduction" {
It "Can get power production" {
Get-TibberProduction | Should -Not -Be $null
}
It "Can get power production (based on date)" {
Get-TibberProduction -From ([DateTime]::Now).AddHours(-10) -To ([DateTime]::Now).AddHours(-9) | Should -Not -Be $null
}
It "Can get power production (w/ access token)" {
Get-TibberProduction -PersonalAccessToken $TibberAccessToken -HomeId $TibberHomeId -Last 100 -FilterEmptyNodes | Should -Not -Be $null
}
}
Describe "Get-TibberPriceInfo" {
It "Can get power production" {
Get-TibberPriceInfo -IncludeToday -ExcludePast | Should -Not -Be $null
}
It "Can get power production (w/ access token)" {
Get-TibberPriceInfo -IncludeTomorrow -ExcludeCurrent -PersonalAccessToken $TibberAccessToken -HomeId $TibberHomeId -Last 100 | Should -Not -Be $null
}
}
Describe "Send-PushNotification" {
It "Fails w/ demo token" {
Send-PushNotification -Title 'Hello' -Message 'World!' -ScreenToOpen CONSUMPTION -ErrorAction SilentlyContinue | Should -Be $null
}
It "Fails w/ demo token (w/ access token)" {
Send-PushNotification -PersonalAccessToken $TibberAccessToken -Title 'Hello' -Message 'World!' -ScreenToOpen CONSUMPTION -ErrorAction SilentlyContinue | Should -Be $null
}
}
Describe "Connect-TibberWebSocket" -Tag "graphql-ws" {
It "Can connect WebSocket" {
$TibberWebSocket.connection = Connect-TibberWebSocket -HomeId $TibberHomeId
$TibberWebSocket.connection.WebSocket | Should -Not -Be $null
}
}
Describe "Register-TibberLiveMeasurementSubscription" -Tag "graphql-ws" {
It "Can register live measurement subscription" {
$TibberWebSocket.subscription = Register-TibberLiveMeasurementSubscription -Connection $TibberWebSocket.connection
$TibberWebSocket.subscription.Id | Should -Not -Be $null
}
}
Describe "Read-TibberWebSocket" -Tag "graphql-ws" {
It "Can read from WebSocket" {
Read-TibberWebSocket -Connection $TibberWebSocket.connection -Callback { param($Json, $Argument)
$Json | Should -Not -Be $null
} -CallbackArgumentList @( 'Argument' ) -PackageCount 1
}
}
Describe "Read-TibberWebSocket" -Tag "graphql-ws" {
It "Fails with read timeout" {
{ Read-TibberWebSocket -Connection $TibberWebSocket.connection -Callback { } -TimeoutInSeconds 0 } | Should -Throw
}
}
Describe "Unregister-TibberLiveMeasurementSubscription" -Tag "graphql-ws" {
It "Can unregister live measurement subscription" {
{ Unregister-TibberLiveMeasurementSubscription -Connection $TibberWebSocket.connection -Subscription $TibberWebSocket.subscription } | Should -Not -Throw
}
}
Describe "Disconnect-TibberWebSocket" -Tag "graphql-ws" {
It "Can disconnect WebSocket" {
{ Disconnect-TibberWebSocket -Connection $TibberWebSocket.connection } | Should -Not -Throw
}
}