Skip to content

Commit a41b47e

Browse files
kriszypclaude
andcommitted
test: stub envMngr.get instead of mutating config (suite-stable)
The first version of this test set storage.randomAccessFields via envMngr.setProperty, which passed in isolation but failed in the full unit suite (CI) — config state isn't reliably isolated across the suite, so the constructor read a stale false. Stub envMngr.get per-test (restored in afterEach) so the assertion is deterministic regardless of suite ordering; callThrough keeps other config reads real and asserts the correct key is used. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 41cc8df commit a41b47e

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
11
require('../../testUtils');
22
const assert = require('assert');
3+
const sinon = require('sinon');
34
const { OpenDBIObject } = require('#src/utility/lmdb/OpenDBIObject');
45
const envMngr = require('#src/utility/environment/environmentManager');
56
const { CONFIG_PARAMS } = require('#src/utility/hdbTerms');
67

78
// Covers the global storage.randomAccessFields path in the OpenDBIObject constructor. The directive
89
// tests (databases.test.js / randomAccessFieldsDirective.test.js) stamp dbiInit.randomAccessStructure
910
// in databases.ts before the store opens, bypassing this constructor branch — so a wrong config key,
10-
// a non-boolean value, or a hdbTerms/YAML name mismatch would go uncaught. These open a DBI WITHOUT a
11-
// directive and assert the constructor reads the global config correctly.
11+
// a non-boolean value, or a hdbTerms/YAML name mismatch would go uncaught.
12+
//
13+
// The constructor reads the value via envMngr.get(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS), so we
14+
// stub that getter per-test (restored in afterEach) rather than mutating shared config state — the
15+
// latter is order-dependent across the full unit suite and isn't reliably isolated. callThrough()
16+
// keeps every other config read (e.g. STORAGE_CACHING) on the real value; only the random-access
17+
// key is forced, which also asserts the constructor reads the *correct* key (a wrong key would fall
18+
// through to the real value and fail these assertions).
1219
describe('OpenDBIObject storage.randomAccessFields global config', () => {
13-
let previous;
14-
beforeEach(() => {
15-
previous = envMngr.get(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS);
16-
});
20+
let getStub;
1721
afterEach(() => {
18-
envMngr.setProperty(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS, previous);
22+
if (getStub) {
23+
getStub.restore();
24+
getStub = undefined;
25+
}
1926
});
2027

28+
function stubRandomAccessFields(value) {
29+
getStub = sinon.stub(envMngr, 'get');
30+
getStub.callThrough();
31+
getStub.withArgs(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS).returns(value);
32+
}
33+
2134
it('enables randomAccessStructure on a primary DBI when the global config is true', () => {
22-
envMngr.setProperty(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS, true);
35+
stubRandomAccessFields(true);
2336
assert.strictEqual(new OpenDBIObject(false, true).randomAccessStructure, true);
2437
});
2538

2639
it('leaves randomAccessStructure off on a primary DBI when the global config is false (default)', () => {
27-
envMngr.setProperty(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS, false);
40+
stubRandomAccessFields(false);
41+
assert.strictEqual(new OpenDBIObject(false, true).randomAccessStructure, false);
42+
});
43+
44+
it('treats a non-boolean truthy config value as off (strict === true)', () => {
45+
// envMngr should hand back a real boolean; guard the strict comparison so a stray truthy
46+
// (e.g. the string "true") can't silently flip encoding on.
47+
stubRandomAccessFields('true');
2848
assert.strictEqual(new OpenDBIObject(false, true).randomAccessStructure, false);
2949
});
3050

3151
it('keeps randomAccessStructure off on non-primary DBIs even when the global config is true', () => {
32-
// Non-primary stores (e.g. the __dbis__ metadata DBI) must stay in records mode for v4-downgrade
33-
// decodability, regardless of the global setting.
34-
envMngr.setProperty(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS, true);
52+
// Non-primary stores (e.g. the __dbis__ metadata DBI) must stay in records mode for
53+
// v4-downgrade decodability, regardless of the global setting.
54+
stubRandomAccessFields(true);
3555
assert.strictEqual(new OpenDBIObject(false, false).randomAccessStructure, false);
3656
});
3757
});

0 commit comments

Comments
 (0)