1- const { jest } = require ( '@jest/globals ' )
1+ const sinon = require ( 'sinon ' )
22const fs = require ( 'fs' )
33const cp = require ( 'child_process' )
4+ const assert = require ( 'assert' )
45
5- // Mock dependencies
6- jest . mock ( 'fs' )
7- jest . mock ( 'child_process' )
8- jest . mock ( 'gh-helpers' , ( ) => ( ) => ( {
6+ // Mock gh-helpers
7+ const mockGithub = {
98 mock : true ,
10- createPullRequest : jest . fn ( ) . mockResolvedValue ( { number : 123 , url : 'test-pr' } )
11- } ) )
9+ createPullRequest : sinon . stub ( ) . resolves ( { number : 123 , url : 'test-pr' } )
10+ }
1211
13- describe ( 'Node Minecraft Protocol Updator' , ( ) => {
12+ // Mock modules
13+ const Module = require ( 'module' )
14+ const originalRequire = Module . prototype . require
15+
16+ Module . prototype . require = function ( id ) {
17+ if ( id === 'gh-helpers' ) {
18+ return ( ) => mockGithub
19+ }
20+ return originalRequire . apply ( this , arguments )
21+ }
22+
23+ describe ( 'Node Minecraft Protocol Updator' , function ( ) {
1424 let originalEnv
15- let mockFs
16- let mockCp
25+ let fsStub
26+ let cpStub
1727
18- beforeEach ( ( ) => {
28+ beforeEach ( function ( ) {
1929 originalEnv = process . env
2030 process . env = { ...originalEnv }
2131
22- mockFs = {
23- readFileSync : jest . fn ( ) ,
24- writeFileSync : jest . fn ( )
32+ // Stub fs and child_process
33+ fsStub = {
34+ readFileSync : sinon . stub ( fs , 'readFileSync' ) ,
35+ writeFileSync : sinon . stub ( fs , 'writeFileSync' )
2536 }
2637
27- mockCp = {
28- execSync : jest . fn ( )
38+ cpStub = {
39+ execSync : sinon . stub ( cp , 'execSync' )
2940 }
3041
31- fs . readFileSync = mockFs . readFileSync
32- fs . writeFileSync = mockFs . writeFileSync
33- cp . execSync = mockCp . execSync
34-
35- jest . clearAllMocks ( )
42+ sinon . reset ( )
3643 } )
3744
38- afterEach ( ( ) => {
45+ afterEach ( function ( ) {
3946 process . env = originalEnv
40- jest . restoreAllMocks ( )
47+ sinon . restore ( )
4148 } )
4249
43- describe ( 'Version Update' , ( ) => {
44- test ( 'should add new version to supportedVersions array' , ( ) => {
45- process . env . NEW_MC_VERSION = '1.21.9'
46- process . env . MCDATA_BRANCH = 'test-branch'
47-
50+ describe ( 'Version Update' , function ( ) {
51+ it ( 'should add new version to supportedVersions array' , function ( ) {
4852 const currentVersionFile = `'use strict'
4953
5054module.exports = {
5155 defaultVersion: '1.21.8',
5256 supportedVersions: ['1.7', '1.8.8', '1.21.8']
5357}`
5458
55- const expectedVersionFile = `'use strict'
56-
57- module.exports = {
58- defaultVersion: '1.21.8',
59- supportedVersions: ['1.7', '1.8.8', '1.21.8', '1.21.9']
60- }`
61-
62- mockFs . readFileSync . mockReturnValue ( currentVersionFile )
63-
64- delete require . cache [ require . resolve ( '../updator.js' ) ]
59+ fsStub . readFileSync . returns ( currentVersionFile )
6560
66- // Mock the actual script behavior
61+ // Test the logic for adding new version
6762 const newContents = currentVersionFile . replace ( ", '1.21.8'" , ", '1.21.8', '1.21.9'" )
68- expect ( newContents ) . toContain ( "'1.21.9'" )
63+ assert ( newContents . includes ( "'1.21.9'" ) , 'Should contain new version' )
6964 } )
7065
71- test ( 'should not duplicate existing versions' , ( ) => {
72- process . env . NEW_MC_VERSION = '1.21.8'
73-
66+ it ( 'should not duplicate existing versions' , function ( ) {
7467 const versionFileWithExisting = `module.exports = {
7568 supportedVersions: ['1.21.6', '1.21.8']
7669}`
7770
78- mockFs . readFileSync . mockReturnValue ( versionFileWithExisting )
71+ fsStub . readFileSync . returns ( versionFileWithExisting )
7972
8073 // Should not add duplicate
8174 const result = versionFileWithExisting . includes ( '1.21.8' )
8275 ? versionFileWithExisting
8376 : versionFileWithExisting . replace ( "]" , ", '1.21.8']" )
8477
85- expect ( result ) . toBe ( versionFileWithExisting ) // No change
78+ assert . strictEqual ( result , versionFileWithExisting , 'Should not change if version exists' )
8679 } )
8780
88- test ( 'should update README.md with new version' , ( ) => {
81+ it ( 'should update README.md with new version' , function ( ) {
8982 const readmeContent = `# Minecraft Protocol
9083
9184Supports Minecraft 1.8 to 1.21.8 (https://wiki.vg/Protocol_version_numbers)
9285
93- Versions 1.7.10, 1.8.8, 1.9.4, 1.10.2, 1.11.2, 1.12.2, 1.13.2, 1.14.4, 1.15.2, 1.16.5, 1.17.1, 1.18.2, 1.19, 1.19.2, 1.19.3, 1.19.4, 1.20, 1.20.1, 1.20.2, 1.20.4, 1.20.6, 1.21.1, 1.21.3, 1.21.4, 1.21.5, 1. 21.6, 1.21.8) <!--version-->`
86+ Versions 1.7.10, 1.8.8, 1.21.6, 1.21.8) <!--version-->`
9487
9588 const expectedReadme = readmeContent
9689 . replace ( 'Minecraft 1.8 to 1.21.8 (' , 'Minecraft 1.8 to 1.21.9 (' )
9790 . replace ( ') <!--version-->' , ', 1.21.9) <!--version-->' )
9891
99- expect ( expectedReadme ) . toContain ( '1.21.9' )
100- expect ( expectedReadme ) . toContain ( 'Minecraft 1.8 to 1.21.9' )
92+ assert ( expectedReadme . includes ( '1.21.9' ) , 'README should contain new version ')
93+ assert ( expectedReadme . includes ( 'Minecraft 1.8 to 1.21.9' ) , 'README should update version range ')
10194 } )
10295 } )
10396
104- describe ( 'Git Operations' , ( ) => {
105- test ( 'should create correct branch name' , ( ) => {
97+ describe ( 'Git Operations' , function ( ) {
98+ it ( 'should create correct branch name' , function ( ) {
10699 const version = '1.21.9'
107100 const expectedBranch = 'pc' + version . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
108- expect ( expectedBranch ) . toBe ( 'pc1_21_9' )
101+ assert . strictEqual ( expectedBranch , 'pc1_21_9' )
109102 } )
110103
111- test ( 'should execute git commands in correct order' , ( ) => {
112- process . env . NEW_MC_VERSION = '1.21.9'
113-
104+ it ( 'should execute git commands in correct order' , function ( ) {
114105 const expectedCommands = [
115106 'git checkout -b pc1_21_9' ,
116107 'git config user.name "github-actions[bot]"' ,
@@ -122,22 +113,22 @@ Versions 1.7.10, 1.8.8, 1.9.4, 1.10.2, 1.11.2, 1.12.2, 1.13.2, 1.14.4, 1.15.2, 1
122113
123114 // Verify command sequence would be correct
124115 expectedCommands . forEach ( ( cmd , index ) => {
125- expect ( cmd ) . toContain ( 'git' )
116+ assert ( cmd . includes ( 'git' ) , `Command ${ index } should be a git command` )
126117 } )
127118 } )
128119 } )
129120
130- describe ( 'Environment Variable Validation' , ( ) => {
131- test ( 'should fail without required NEW_MC_VERSION' , ( ) => {
121+ describe ( 'Environment Variable Validation' , function ( ) {
122+ it ( 'should fail without required NEW_MC_VERSION' , function ( ) {
132123 delete process . env . NEW_MC_VERSION
133124
134- expect ( ( ) => {
125+ assert . throws ( ( ) => {
135126 const newVersion = process . env . NEW_MC_VERSION ?. replace ( / [ ^ a - z A - Z 0 - 9 _ . ] / g, '_' )
136127 if ( ! newVersion ) throw new Error ( 'NEW_MC_VERSION required' )
137- } ) . toThrow ( ' NEW_MC_VERSION required' )
128+ } , / N E W _ M C _ V E R S I O N r e q u i r e d / )
138129 } )
139130
140- test ( 'should sanitize version strings correctly' , ( ) => {
131+ it ( 'should sanitize version strings correctly' , function ( ) {
141132 const testCases = [
142133 { input : '1.21.9' , expected : '1.21.9' } ,
143134 { input : '1.21.9-test' , expected : '1.21.9_test' } ,
@@ -147,13 +138,13 @@ Versions 1.7.10, 1.8.8, 1.9.4, 1.10.2, 1.11.2, 1.12.2, 1.13.2, 1.14.4, 1.15.2, 1
147138
148139 testCases . forEach ( ( { input, expected } ) => {
149140 const sanitized = input . replace ( / [ ^ a - z A - Z 0 - 9 _ . ] / g, '_' )
150- expect ( sanitized ) . toBe ( expected )
141+ assert . strictEqual ( sanitized , expected )
151142 } )
152143 } )
153144 } )
154145
155- describe ( 'PR Creation' , ( ) => {
156- test ( 'should create PR with correct title and body' , ( ) => {
146+ describe ( 'PR Creation' , function ( ) {
147+ it ( 'should create PR with correct title and body' , function ( ) {
157148 const version = '1.21.9'
158149 const expectedTitle = `🎈 ${ version } `
159150 const expectedBody = `This automated PR sets up the relevant boilerplate for Minecraft version ${ version } .
@@ -163,41 +154,35 @@ Ref:
163154* You can help contribute to this PR by opening a PR against this <code branch>pc1_21_9</code> branch instead of <code>master</code>.
164155 `
165156
166- expect ( expectedTitle ) . toBe ( '🎈 1.21.9' )
167- expect ( expectedBody ) . toContain ( 'Minecraft version 1.21.9' )
168- expect ( expectedBody ) . toContain ( 'pc1_21_9' )
157+ assert . strictEqual ( expectedTitle , '🎈 1.21.9' )
158+ assert ( expectedBody . includes ( 'Minecraft version 1.21.9' ) , 'Body should contain version ')
159+ assert ( expectedBody . includes ( 'pc1_21_9' ) , 'Body should contain branch name ')
169160 } )
170161 } )
171162
172- describe ( 'Error Handling' , ( ) => {
173- test ( 'should handle git command failures' , ( ) => {
174- mockCp . execSync . mockImplementation ( ( cmd ) => {
175- if ( cmd . includes ( 'git push' ) ) {
176- throw new Error ( 'Push failed' )
177- }
178- } )
163+ describe ( 'Error Handling' , function ( ) {
164+ it ( 'should handle git command failures' , function ( ) {
165+ cpStub . execSync . throws ( new Error ( 'Push failed' ) )
179166
180- expect ( ( ) => {
167+ assert . throws ( ( ) => {
181168 try {
182- mockCp . execSync ( 'git push origin test --force' )
169+ cpStub . execSync ( 'git push origin test --force' )
183170 } catch ( e ) {
184171 throw new Error ( `Git operation failed: ${ e . message } ` )
185172 }
186- } ) . toThrow ( ' Git operation failed: Push failed' )
173+ } , / G i t o p e r a t i o n f a i l e d : P u s h f a i l e d / )
187174 } )
188175
189- test ( 'should handle file system errors' , ( ) => {
190- mockFs . readFileSync . mockImplementation ( ( ) => {
191- throw new Error ( 'File not found' )
192- } )
176+ it ( 'should handle file system errors' , function ( ) {
177+ fsStub . readFileSync . throws ( new Error ( 'File not found' ) )
193178
194- expect ( ( ) => {
179+ assert . throws ( ( ) => {
195180 try {
196- mockFs . readFileSync ( 'non-existent-file' )
181+ fsStub . readFileSync ( 'non-existent-file' )
197182 } catch ( e ) {
198183 throw new Error ( `File operation failed: ${ e . message } ` )
199184 }
200- } ) . toThrow ( ' File operation failed: File not found' )
185+ } , / F i l e o p e r a t i o n f a i l e d : F i l e n o t f o u n d / )
201186 } )
202187 } )
203188} )
0 commit comments