1
- import { promises as fs } from "fs"
2
1
import * as http from "http"
3
2
import * as path from "path"
4
- import { tmpdir } from "../../../src/node/constants "
3
+ import { clean , tmpdir } from "../../utils/helpers "
5
4
import { SettingsProvider , UpdateSettings } from "../../../src/node/settings"
6
5
import { LatestResponse , UpdateProvider } from "../../../src/node/update"
7
6
@@ -29,22 +28,29 @@ describe("update", () => {
29
28
response . end ( "not found" )
30
29
} )
31
30
32
- const jsonPath = path . join ( tmpdir , "tests/updates/update.json" )
33
- const settings = new SettingsProvider < UpdateSettings > ( jsonPath )
31
+ let _settings : SettingsProvider < UpdateSettings > | undefined
32
+ const settings = ( ) : SettingsProvider < UpdateSettings > => {
33
+ if ( ! _settings ) {
34
+ throw new Error ( "Settings provider has not been created" )
35
+ }
36
+ return _settings
37
+ }
34
38
35
39
let _provider : UpdateProvider | undefined
36
40
const provider = ( ) : UpdateProvider => {
37
41
if ( ! _provider ) {
38
- const address = server . address ( )
39
- if ( ! address || typeof address === "string" || ! address . port ) {
40
- throw new Error ( "unexpected address" )
41
- }
42
- _provider = new UpdateProvider ( `http://${ address . address } :${ address . port } /latest` , settings )
42
+ throw new Error ( "Update provider has not been created" )
43
43
}
44
44
return _provider
45
45
}
46
46
47
47
beforeAll ( async ( ) => {
48
+ const testName = "update"
49
+ await clean ( testName )
50
+ const testDir = await tmpdir ( testName )
51
+ const jsonPath = path . join ( testDir , "update.json" )
52
+ _settings = new SettingsProvider < UpdateSettings > ( jsonPath )
53
+
48
54
await new Promise ( ( resolve , reject ) => {
49
55
server . on ( "error" , reject )
50
56
server . on ( "listening" , resolve )
@@ -53,8 +59,13 @@ describe("update", () => {
53
59
host : "localhost" ,
54
60
} )
55
61
} )
56
- await fs . rmdir ( path . join ( tmpdir , "tests/updates" ) , { recursive : true } )
57
- await fs . mkdir ( path . join ( tmpdir , "tests/updates" ) , { recursive : true } )
62
+
63
+ const address = server . address ( )
64
+ if ( ! address || typeof address === "string" || ! address . port ) {
65
+ throw new Error ( "unexpected address" )
66
+ }
67
+
68
+ _provider = new UpdateProvider ( `http://${ address . address } :${ address . port } /latest` , _settings )
58
69
} )
59
70
60
71
afterAll ( ( ) => {
@@ -72,7 +83,7 @@ describe("update", () => {
72
83
const now = Date . now ( )
73
84
const update = await p . getUpdate ( )
74
85
75
- await expect ( settings . read ( ) ) . resolves . toEqual ( { update } )
86
+ await expect ( settings ( ) . read ( ) ) . resolves . toEqual ( { update } )
76
87
expect ( isNaN ( update . checked ) ) . toEqual ( false )
77
88
expect ( update . checked < Date . now ( ) && update . checked >= now ) . toEqual ( true )
78
89
expect ( update . version ) . toStrictEqual ( "2.1.0" )
@@ -86,7 +97,7 @@ describe("update", () => {
86
97
const now = Date . now ( )
87
98
const update = await p . getUpdate ( )
88
99
89
- await expect ( settings . read ( ) ) . resolves . toEqual ( { update } )
100
+ await expect ( settings ( ) . read ( ) ) . resolves . toEqual ( { update } )
90
101
expect ( isNaN ( update . checked ) ) . toStrictEqual ( false )
91
102
expect ( update . checked < now ) . toBe ( true )
92
103
expect ( update . version ) . toStrictEqual ( "2.1.0" )
@@ -100,7 +111,7 @@ describe("update", () => {
100
111
const now = Date . now ( )
101
112
const update = await p . getUpdate ( true )
102
113
103
- await expect ( settings . read ( ) ) . resolves . toEqual ( { update } )
114
+ await expect ( settings ( ) . read ( ) ) . resolves . toEqual ( { update } )
104
115
expect ( isNaN ( update . checked ) ) . toStrictEqual ( false )
105
116
expect ( update . checked < Date . now ( ) && update . checked >= now ) . toStrictEqual ( true )
106
117
expect ( update . version ) . toStrictEqual ( "4.1.1" )
@@ -113,12 +124,12 @@ describe("update", () => {
113
124
expect ( spy ) . toEqual ( [ ] )
114
125
115
126
let checked = Date . now ( ) - 1000 * 60 * 60 * 23
116
- await settings . write ( { update : { checked, version } } )
127
+ await settings ( ) . write ( { update : { checked, version } } )
117
128
await p . getUpdate ( )
118
129
expect ( spy ) . toEqual ( [ ] )
119
130
120
131
checked = Date . now ( ) - 1000 * 60 * 60 * 25
121
- await settings . write ( { update : { checked, version } } )
132
+ await settings ( ) . write ( { update : { checked, version } } )
122
133
123
134
const update = await p . getUpdate ( )
124
135
expect ( update . checked ) . not . toStrictEqual ( checked )
@@ -143,14 +154,14 @@ describe("update", () => {
143
154
} )
144
155
145
156
it ( "should not reject if unable to fetch" , async ( ) => {
146
- let provider = new UpdateProvider ( "invalid" , settings )
157
+ let provider = new UpdateProvider ( "invalid" , settings ( ) )
147
158
let now = Date . now ( )
148
159
let update = await provider . getUpdate ( true )
149
160
expect ( isNaN ( update . checked ) ) . toStrictEqual ( false )
150
161
expect ( update . checked < Date . now ( ) && update . checked >= now ) . toEqual ( true )
151
162
expect ( update . version ) . toStrictEqual ( "unknown" )
152
163
153
- provider = new UpdateProvider ( "http://probably.invalid.dev.localhost/latest" , settings )
164
+ provider = new UpdateProvider ( "http://probably.invalid.dev.localhost/latest" , settings ( ) )
154
165
now = Date . now ( )
155
166
update = await provider . getUpdate ( true )
156
167
expect ( isNaN ( update . checked ) ) . toStrictEqual ( false )
0 commit comments