@@ -3,14 +3,14 @@ const fs = require('fs');
33const path = require ( 'path' ) ;
44const { calculateCharges, loadRatesConfig } = require ( '../../src/cost-calculator' ) ;
55
6- function testFileConfig ( ) {
6+ async function testFileConfig ( ) {
77 const usage = [
88 { account : 'education' , date : '2024-01-15' , core_hours : 100 , gpu_hours : 10 } ,
99 { account : 'research' , date : '2024-02-01' , core_hours : 50 , gpu_hours : 5 } ,
1010 { account : 'special' , date : '2024-02-01' , core_hours : 100 , gpu_hours : 20 } ,
1111 { account : 'other' , date : '2024-02-01' , core_hours : 10 }
1212 ] ;
13- const config = loadRatesConfig ( ) ;
13+ const config = await loadRatesConfig ( ) ;
1414 const charges = calculateCharges ( usage , config ) ;
1515 assert . strictEqual ( charges [ '2024-01' ] . education . cost , ( 100 * 0.015 + 10 * 0.15 ) * 0.5 ) ;
1616 assert . strictEqual ( charges [ '2024-02' ] . research . cost , 50 * 0.01 + 5 * 0.1 ) ;
@@ -42,24 +42,24 @@ function testInvalidUsageIgnored() {
4242 assert . deepStrictEqual ( charges , { } ) ;
4343}
4444
45- function testMissingConfig ( ) {
45+ async function testMissingConfig ( ) {
4646 const cfgPath = path . join ( __dirname , '../../src/rates.json' ) ;
4747 const backup = cfgPath + '.bak' ;
4848 fs . renameSync ( cfgPath , backup ) ;
4949 try {
50- const cfg = loadRatesConfig ( ) ;
50+ const cfg = await loadRatesConfig ( ) ;
5151 assert . deepStrictEqual ( cfg , { } ) ;
5252 } finally {
5353 fs . renameSync ( backup , cfgPath ) ;
5454 }
5555}
5656
57- function testInvalidConfig ( ) {
57+ async function testInvalidConfig ( ) {
5858 const cfgPath = path . join ( __dirname , '../../src/rates.json' ) ;
5959 const original = fs . readFileSync ( cfgPath , 'utf8' ) ;
6060 fs . writeFileSync ( cfgPath , '{ invalid json' , 'utf8' ) ;
6161 try {
62- assert . throws ( ( ) => loadRatesConfig ( ) , SyntaxError ) ;
62+ await assert . rejects ( loadRatesConfig , SyntaxError ) ;
6363 } finally {
6464 fs . writeFileSync ( cfgPath , original , 'utf8' ) ;
6565 }
@@ -102,19 +102,22 @@ function testRoundingTotals() {
102102 assert . strictEqual ( june . round . cost , 0.22 ) ;
103103}
104104
105- function run ( ) {
106- testFileConfig ( ) ;
105+ async function run ( ) {
106+ await testFileConfig ( ) ;
107107 testPassedConfig ( ) ;
108108 testInvalidUsageIgnored ( ) ;
109- testMissingConfig ( ) ;
110- testInvalidConfig ( ) ;
109+ await testMissingConfig ( ) ;
110+ await testInvalidConfig ( ) ;
111111 testNegativeInputs ( ) ;
112112 testRoundingTotals ( ) ;
113113 console . log ( 'All calculator tests passed.' ) ;
114114}
115115
116116if ( require . main === module ) {
117- run ( ) ;
117+ run ( ) . catch ( err => {
118+ console . error ( err ) ;
119+ process . exit ( 1 ) ;
120+ } ) ;
118121}
119122
120123module . exports = run ;
0 commit comments