11import { logger } from "@coder/logger"
22import { mockLogger } from "../../utils/helpers"
3+ import * as semver from "semver"
34
45describe ( "constants" , ( ) => {
56 let constants : typeof import ( "../../../src/node/constants" )
@@ -13,9 +14,15 @@ describe("constants", () => {
1314 commit : "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b" ,
1415 }
1516
17+ const mockCodePackageJson = {
18+ name : "mock-code-oss-dev" ,
19+ version : "1.2.3" ,
20+ }
21+
1622 beforeAll ( ( ) => {
1723 mockLogger ( )
1824 jest . mock ( "../../../package.json" , ( ) => mockPackageJson , { virtual : true } )
25+ jest . mock ( "../../../vendor/modules/code-oss-dev/package.json" , ( ) => mockCodePackageJson , { virtual : true } )
1926 constants = require ( "../../../src/node/constants" )
2027 } )
2128
@@ -24,12 +31,46 @@ describe("constants", () => {
2431 jest . resetModules ( )
2532 } )
2633
34+ it ( "should provide the package name" , ( ) => {
35+ expect ( constants . pkgName ) . toBe ( mockPackageJson . name )
36+ } )
37+
2738 it ( "should provide the commit" , ( ) => {
2839 expect ( constants . commit ) . toBe ( mockPackageJson . commit )
2940 } )
3041
3142 it ( "should return the package.json version" , ( ) => {
3243 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+ )
3374 } )
3475
3576 describe ( "getPackageJson" , ( ) => {
@@ -47,6 +88,9 @@ describe("constants", () => {
4788 // so to get the root package.json we need to use ../../
4889 const packageJson = constants . getPackageJson ( "../../package.json" )
4990 expect ( packageJson ) . toStrictEqual ( mockPackageJson )
91+
92+ const codePackageJson = constants . getPackageJson ( "../../vendor/modules/code-oss-dev/package.json" )
93+ expect ( codePackageJson ) . toStrictEqual ( mockCodePackageJson )
5094 } )
5195 } )
5296 } )
@@ -55,9 +99,13 @@ describe("constants", () => {
5599 const mockPackageJson = {
56100 name : "mock-code-server" ,
57101 }
102+ const mockCodePackageJson = {
103+ name : "mock-code-oss-dev" ,
104+ }
58105
59106 beforeAll ( ( ) => {
60107 jest . mock ( "../../../package.json" , ( ) => mockPackageJson , { virtual : true } )
108+ jest . mock ( "../../../vendor/modules/code-oss-dev/package.json" , ( ) => mockCodePackageJson , { virtual : true } )
61109 constants = require ( "../../../src/node/constants" )
62110 } )
63111
@@ -69,8 +117,24 @@ describe("constants", () => {
69117 it ( "version should return 'development'" , ( ) => {
70118 expect ( constants . version ) . toBe ( "development" )
71119 } )
120+
72121 it ( "commit should return 'development'" , ( ) => {
73122 expect ( constants . commit ) . toBe ( "development" )
74123 } )
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+ } )
75139 } )
76140} )
0 commit comments