Skip to content

Commit 8a9be0d

Browse files
Merge pull request #132 from NessieCanCode/update-loadratesconfig-for-async-file-read
Use async fs for rate config loading
2 parents 7859d02 + e1033c7 commit 8a9be0d

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/cost-calculator.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
'use strict';
22

3-
const fs = require('fs');
3+
const fs = require('fs').promises;
44
const path = require('path');
55

66
/**
77
* Load rate configuration from rates.json.
88
*
9-
* @returns {Object} rate configuration
9+
* @returns {Promise<Object>} rate configuration
1010
*/
11-
function loadRatesConfig() {
11+
async function loadRatesConfig() {
1212
const cfgPath = path.join(__dirname, 'rates.json');
1313
try {
14-
const data = fs.readFileSync(cfgPath, 'utf8');
14+
const data = await fs.readFile(cfgPath, 'utf8');
1515
return JSON.parse(data);
1616
} catch (e) {
1717
if (e.code === 'ENOENT') {
1818
return {};
1919
}
2020
if (e instanceof SyntaxError) {
21-
console.error(`Invalid JSON in rate configuration: ${e.message}`);
21+
console.error('Invalid JSON in rate configuration', {
22+
path: cfgPath,
23+
error: e.message,
24+
});
2225
} else {
23-
console.error(`Error loading rate configuration: ${e.message}`);
26+
console.error('Error loading rate configuration', {
27+
path: cfgPath,
28+
error: e.message,
29+
code: e.code,
30+
});
2431
}
2532
throw e;
2633
}

test/unit/calculator.test.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const fs = require('fs');
33
const path = require('path');
44
const { 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

116116
if (require.main === module) {
117-
run();
117+
run().catch(err => {
118+
console.error(err);
119+
process.exit(1);
120+
});
118121
}
119122

120123
module.exports = run;

0 commit comments

Comments
 (0)