|
1 | 1 | require('../../testUtils'); |
2 | 2 | const assert = require('assert'); |
| 3 | +const sinon = require('sinon'); |
3 | 4 | const { OpenDBIObject } = require('#src/utility/lmdb/OpenDBIObject'); |
4 | 5 | const envMngr = require('#src/utility/environment/environmentManager'); |
5 | 6 | const { CONFIG_PARAMS } = require('#src/utility/hdbTerms'); |
6 | 7 |
|
7 | 8 | // Covers the global storage.randomAccessFields path in the OpenDBIObject constructor. The directive |
8 | 9 | // tests (databases.test.js / randomAccessFieldsDirective.test.js) stamp dbiInit.randomAccessStructure |
9 | 10 | // 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). |
12 | 19 | describe('OpenDBIObject storage.randomAccessFields global config', () => { |
13 | | - let previous; |
14 | | - beforeEach(() => { |
15 | | - previous = envMngr.get(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS); |
16 | | - }); |
| 20 | + let getStub; |
17 | 21 | afterEach(() => { |
18 | | - envMngr.setProperty(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS, previous); |
| 22 | + if (getStub) { |
| 23 | + getStub.restore(); |
| 24 | + getStub = undefined; |
| 25 | + } |
19 | 26 | }); |
20 | 27 |
|
| 28 | + function stubRandomAccessFields(value) { |
| 29 | + getStub = sinon.stub(envMngr, 'get'); |
| 30 | + getStub.callThrough(); |
| 31 | + getStub.withArgs(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS).returns(value); |
| 32 | + } |
| 33 | + |
21 | 34 | it('enables randomAccessStructure on a primary DBI when the global config is true', () => { |
22 | | - envMngr.setProperty(CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS, true); |
| 35 | + stubRandomAccessFields(true); |
23 | 36 | assert.strictEqual(new OpenDBIObject(false, true).randomAccessStructure, true); |
24 | 37 | }); |
25 | 38 |
|
26 | 39 | 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'); |
28 | 48 | assert.strictEqual(new OpenDBIObject(false, true).randomAccessStructure, false); |
29 | 49 | }); |
30 | 50 |
|
31 | 51 | 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); |
35 | 55 | assert.strictEqual(new OpenDBIObject(false, false).randomAccessStructure, false); |
36 | 56 | }); |
37 | 57 | }); |
0 commit comments