1
1
import { logger } from "@coder/logger"
2
2
import { mockLogger } from "../../utils/helpers"
3
+ import * as semver from "semver"
3
4
4
5
describe ( "constants" , ( ) => {
5
6
let constants : typeof import ( "../../../src/node/constants" )
@@ -13,9 +14,15 @@ describe("constants", () => {
13
14
commit : "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b" ,
14
15
}
15
16
17
+ const mockCodePackageJson = {
18
+ name : "mock-code-oss-dev" ,
19
+ version : "1.2.3" ,
20
+ }
21
+
16
22
beforeAll ( ( ) => {
17
23
mockLogger ( )
18
24
jest . mock ( "../../../package.json" , ( ) => mockPackageJson , { virtual : true } )
25
+ jest . mock ( "../../../vendor/modules/code-oss-dev/package.json" , ( ) => mockCodePackageJson , { virtual : true } )
19
26
constants = require ( "../../../src/node/constants" )
20
27
} )
21
28
@@ -24,12 +31,46 @@ describe("constants", () => {
24
31
jest . resetModules ( )
25
32
} )
26
33
34
+ it ( "should provide the package name" , ( ) => {
35
+ expect ( constants . pkgName ) . toBe ( mockPackageJson . name )
36
+ } )
37
+
27
38
it ( "should provide the commit" , ( ) => {
28
39
expect ( constants . commit ) . toBe ( mockPackageJson . commit )
29
40
} )
30
41
31
42
it ( "should return the package.json version" , ( ) => {
32
43
expect ( constants . version ) . toBe ( mockPackageJson . version )
44
+
45
+ // Ensure the version is parseable as semver and equal
46
+ const actual = semver . parse ( constants . version )
47
+ const expected = semver . parse ( mockPackageJson . version )
48
+ expect ( actual ) . toBeTruthy ( )
49
+ expect ( actual ) . toStrictEqual ( expected )
50
+ } )
51
+
52
+ it ( "should include embedded Code version information" , ( ) => {
53
+ expect ( constants . codeVersion ) . toBe ( mockCodePackageJson . version )
54
+
55
+ // Ensure the version is parseable as semver and equal
56
+ const actual = semver . parse ( constants . codeVersion )
57
+ const expected = semver . parse ( mockCodePackageJson . version )
58
+ expect ( actual ) . toBeTruthy ( )
59
+ expect ( actual ) . toStrictEqual ( expected )
60
+ } )
61
+
62
+ it ( "should return a human-readable version string" , ( ) => {
63
+ expect ( constants . getVersionString ( ) ) . toStrictEqual ( `${ mockPackageJson . version } ${ mockPackageJson . commit } ` )
64
+ } )
65
+
66
+ it ( "should return a machine-readable version string" , ( ) => {
67
+ expect ( constants . getVersionJsonString ( ) ) . toStrictEqual (
68
+ JSON . stringify ( {
69
+ codeServer : mockPackageJson . version ,
70
+ commit : mockPackageJson . commit ,
71
+ vscode : mockCodePackageJson . version ,
72
+ } ) ,
73
+ )
33
74
} )
34
75
35
76
describe ( "getPackageJson" , ( ) => {
@@ -47,6 +88,9 @@ describe("constants", () => {
47
88
// so to get the root package.json we need to use ../../
48
89
const packageJson = constants . getPackageJson ( "../../package.json" )
49
90
expect ( packageJson ) . toStrictEqual ( mockPackageJson )
91
+
92
+ const codePackageJson = constants . getPackageJson ( "../../vendor/modules/code-oss-dev/package.json" )
93
+ expect ( codePackageJson ) . toStrictEqual ( mockCodePackageJson )
50
94
} )
51
95
} )
52
96
} )
@@ -55,9 +99,13 @@ describe("constants", () => {
55
99
const mockPackageJson = {
56
100
name : "mock-code-server" ,
57
101
}
102
+ const mockCodePackageJson = {
103
+ name : "mock-code-oss-dev" ,
104
+ }
58
105
59
106
beforeAll ( ( ) => {
60
107
jest . mock ( "../../../package.json" , ( ) => mockPackageJson , { virtual : true } )
108
+ jest . mock ( "../../../vendor/modules/code-oss-dev/package.json" , ( ) => mockCodePackageJson , { virtual : true } )
61
109
constants = require ( "../../../src/node/constants" )
62
110
} )
63
111
@@ -69,8 +117,24 @@ describe("constants", () => {
69
117
it ( "version should return 'development'" , ( ) => {
70
118
expect ( constants . version ) . toBe ( "development" )
71
119
} )
120
+
72
121
it ( "commit should return 'development'" , ( ) => {
73
122
expect ( constants . commit ) . toBe ( "development" )
74
123
} )
124
+
125
+ it ( "should return a human-readable version string" , ( ) => {
126
+ // this string is not super useful
127
+ expect ( constants . getVersionString ( ) ) . toStrictEqual ( "development development" )
128
+ } )
129
+
130
+ it ( "should return a machine-readable version string" , ( ) => {
131
+ expect ( constants . getVersionJsonString ( ) ) . toStrictEqual (
132
+ JSON . stringify ( {
133
+ codeServer : "development" ,
134
+ commit : "development" ,
135
+ vscode : "development" ,
136
+ } ) ,
137
+ )
138
+ } )
75
139
} )
76
140
} )
0 commit comments