@fgv/ts-web-extras 5.0.2 → 5.1.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rush/temp/81e0881271ff236956b2f52e8ca99da6574c6e1e.tar.log +223 -0
- package/.rush/temp/chunked-rush-logs/ts-web-extras.build.chunks.jsonl +35 -25
- package/.rush/temp/operation/build/all.log +35 -25
- package/.rush/temp/operation/build/log-chunks.jsonl +35 -25
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +175 -163
- package/config/jest.config.json +4 -1
- package/config/typedoc.json +6 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/packlets/crypto-utils/browserCryptoProvider.js +254 -0
- package/dist/packlets/crypto-utils/browserCryptoProvider.js.map +1 -0
- package/dist/packlets/crypto-utils/browserHashProvider.js.map +1 -0
- package/dist/packlets/{crypto → crypto-utils}/index.js +1 -0
- package/dist/packlets/crypto-utils/index.js.map +1 -0
- package/dist/packlets/file-api-types/index.js +27 -3
- package/dist/packlets/file-api-types/index.js.map +1 -1
- package/dist/packlets/file-tree/directoryHandleStore.js +124 -0
- package/dist/packlets/file-tree/directoryHandleStore.js.map +1 -0
- package/dist/packlets/file-tree/fileApiTreeAccessors.js +76 -0
- package/dist/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
- package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js +345 -0
- package/dist/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -0
- package/dist/packlets/file-tree/index.js +3 -0
- package/dist/packlets/file-tree/index.js.map +1 -1
- package/dist/packlets/file-tree/localStorageTreeAccessors.js +308 -0
- package/dist/packlets/file-tree/localStorageTreeAccessors.js.map +1 -0
- package/dist/test/mocks/idb-keyval.js +6 -0
- package/dist/test/mocks/idb-keyval.js.map +1 -0
- package/dist/test/unit/browserHashProvider.test.js +1 -1
- package/dist/test/unit/browserHashProvider.test.js.map +1 -1
- package/dist/test/unit/directoryHandleStore.test.js +190 -0
- package/dist/test/unit/directoryHandleStore.test.js.map +1 -0
- package/dist/test/unit/fileApiTypes.test.js +30 -0
- package/dist/test/unit/fileApiTypes.test.js.map +1 -1
- package/dist/test/unit/fileSystemAccessTreeAccessors.test.js +517 -0
- package/dist/test/unit/fileSystemAccessTreeAccessors.test.js.map +1 -0
- package/dist/test/unit/localStorageTreeAccessors.test.js +595 -0
- package/dist/test/unit/localStorageTreeAccessors.test.js.map +1 -0
- package/dist/test/utils/fileSystemAccessMocks.js +271 -0
- package/dist/test/utils/fileSystemAccessMocks.js.map +1 -0
- package/dist/ts-web-extras.d.ts +460 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider._constructor_.md +50 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.decrypt.md +104 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.derivekey.md +88 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.encrypt.md +72 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.frombase64.md +56 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.generatekey.md +19 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.generaterandombytes.md +56 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.md +169 -0
- package/docs/ts-web-extras.cryptoutils.browsercryptoprovider.tobase64.md +56 -0
- package/docs/{ts-web-extras.browserhashprovider.hashparts.md → ts-web-extras.cryptoutils.browserhashprovider.hashparts.md} +2 -2
- package/docs/{ts-web-extras.browserhashprovider.hashstring.md → ts-web-extras.cryptoutils.browserhashprovider.hashstring.md} +2 -2
- package/docs/{ts-web-extras.browserhashprovider.md → ts-web-extras.cryptoutils.browserhashprovider.md} +4 -4
- package/docs/ts-web-extras.cryptoutils.createbrowsercryptoprovider.md +19 -0
- package/docs/ts-web-extras.cryptoutils.md +71 -0
- package/docs/ts-web-extras.fileapitreeaccessors.createfromlocalstorage.md +74 -0
- package/docs/ts-web-extras.fileapitreeaccessors.createpersistent.md +76 -0
- package/docs/ts-web-extras.fileapitreeaccessors.md +32 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors._constructor_.md +114 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.fileismutable.md +52 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.fromdirectoryhandle.md +72 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.getdirtypaths.md +17 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.isdirty.md +17 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.md +159 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.savefilecontents.md +66 -0
- package/docs/ts-web-extras.filesystemaccesstreeaccessors.synctodisk.md +17 -0
- package/docs/ts-web-extras.ifilesystemaccesstreeparams.autosync.md +13 -0
- package/docs/ts-web-extras.ifilesystemaccesstreeparams.md +78 -0
- package/docs/ts-web-extras.ifilesystemaccesstreeparams.requirewritepermission.md +13 -0
- package/docs/ts-web-extras.ilocalstoragetreeparams.autosync.md +13 -0
- package/docs/ts-web-extras.ilocalstoragetreeparams.md +97 -0
- package/docs/ts-web-extras.ilocalstoragetreeparams.pathtokeymap.md +13 -0
- package/docs/ts-web-extras.ilocalstoragetreeparams.storage.md +13 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.fileismutable.md +56 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.fromstorage.md +56 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.getdirtypaths.md +19 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.isdirty.md +19 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.md +131 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.savefilecontents.md +72 -0
- package/docs/ts-web-extras.localstoragetreeaccessors.synctodisk.md +19 -0
- package/docs/ts-web-extras.md +50 -4
- package/etc/ts-web-extras.api.md +91 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +25 -2
- package/lib/index.js.map +1 -1
- package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts +77 -0
- package/lib/packlets/crypto-utils/browserCryptoProvider.d.ts.map +1 -0
- package/lib/packlets/crypto-utils/browserCryptoProvider.js +259 -0
- package/lib/packlets/crypto-utils/browserCryptoProvider.js.map +1 -0
- package/lib/packlets/crypto-utils/browserHashProvider.d.ts.map +1 -0
- package/lib/packlets/crypto-utils/browserHashProvider.js.map +1 -0
- package/lib/packlets/{crypto → crypto-utils}/index.d.ts +1 -0
- package/lib/packlets/crypto-utils/index.d.ts.map +1 -0
- package/lib/packlets/{crypto → crypto-utils}/index.js +1 -0
- package/lib/packlets/crypto-utils/index.js.map +1 -0
- package/lib/packlets/file-api-types/index.d.ts.map +1 -1
- package/lib/packlets/file-api-types/index.js +27 -3
- package/lib/packlets/file-api-types/index.js.map +1 -1
- package/lib/packlets/file-tree/directoryHandleStore.d.ts +59 -0
- package/lib/packlets/file-tree/directoryHandleStore.d.ts.map +1 -0
- package/lib/packlets/file-tree/directoryHandleStore.js +128 -0
- package/lib/packlets/file-tree/directoryHandleStore.js.map +1 -0
- package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts +57 -0
- package/lib/packlets/file-tree/fileApiTreeAccessors.d.ts.map +1 -1
- package/lib/packlets/file-tree/fileApiTreeAccessors.js +76 -0
- package/lib/packlets/file-tree/fileApiTreeAccessors.js.map +1 -1
- package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.d.ts +136 -0
- package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js +349 -0
- package/lib/packlets/file-tree/fileSystemAccessTreeAccessors.js.map +1 -0
- package/lib/packlets/file-tree/index.d.ts +3 -0
- package/lib/packlets/file-tree/index.d.ts.map +1 -1
- package/lib/packlets/file-tree/index.js +3 -0
- package/lib/packlets/file-tree/index.js.map +1 -1
- package/lib/packlets/file-tree/localStorageTreeAccessors.d.ts +129 -0
- package/lib/packlets/file-tree/localStorageTreeAccessors.d.ts.map +1 -0
- package/lib/packlets/file-tree/localStorageTreeAccessors.js +312 -0
- package/lib/packlets/file-tree/localStorageTreeAccessors.js.map +1 -0
- package/lib/test/mocks/idb-keyval.d.ts +6 -0
- package/lib/test/mocks/idb-keyval.d.ts.map +1 -0
- package/lib/test/mocks/idb-keyval.js +9 -0
- package/lib/test/mocks/idb-keyval.js.map +1 -0
- package/lib/test/unit/browserHashProvider.test.js +21 -21
- package/lib/test/unit/browserHashProvider.test.js.map +1 -1
- package/lib/test/unit/directoryHandleStore.test.d.ts +2 -0
- package/lib/test/unit/directoryHandleStore.test.d.ts.map +1 -0
- package/lib/test/unit/directoryHandleStore.test.js +192 -0
- package/lib/test/unit/directoryHandleStore.test.js.map +1 -0
- package/lib/test/unit/fileApiTypes.test.js +30 -0
- package/lib/test/unit/fileApiTypes.test.js.map +1 -1
- package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts +2 -0
- package/lib/test/unit/fileSystemAccessTreeAccessors.test.d.ts.map +1 -0
- package/lib/test/unit/fileSystemAccessTreeAccessors.test.js +519 -0
- package/lib/test/unit/fileSystemAccessTreeAccessors.test.js.map +1 -0
- package/lib/test/unit/localStorageTreeAccessors.test.d.ts +2 -0
- package/lib/test/unit/localStorageTreeAccessors.test.d.ts.map +1 -0
- package/lib/test/unit/localStorageTreeAccessors.test.js +597 -0
- package/lib/test/unit/localStorageTreeAccessors.test.js.map +1 -0
- package/lib/test/utils/fileSystemAccessMocks.d.ts +53 -0
- package/lib/test/utils/fileSystemAccessMocks.d.ts.map +1 -0
- package/lib/test/utils/fileSystemAccessMocks.js +277 -0
- package/lib/test/utils/fileSystemAccessMocks.js.map +1 -0
- package/package.json +27 -20
- package/rush-logs/ts-web-extras.build.cache.log +3 -1
- package/rush-logs/ts-web-extras.build.log +35 -25
- package/src/index.ts +2 -2
- package/src/packlets/crypto-utils/browserCryptoProvider.ts +311 -0
- package/src/packlets/{crypto → crypto-utils}/index.ts +1 -0
- package/src/packlets/file-api-types/index.ts +24 -3
- package/src/packlets/file-tree/directoryHandleStore.ts +136 -0
- package/src/packlets/file-tree/fileApiTreeAccessors.ts +90 -0
- package/src/packlets/file-tree/fileSystemAccessTreeAccessors.ts +427 -0
- package/src/packlets/file-tree/index.ts +3 -0
- package/src/packlets/file-tree/localStorageTreeAccessors.ts +377 -0
- package/src/test/mocks/idb-keyval.ts +5 -0
- package/src/test/unit/browserHashProvider.test.ts +1 -1
- package/src/test/unit/directoryHandleStore.test.ts +251 -0
- package/src/test/unit/fileApiTypes.test.ts +36 -0
- package/src/test/unit/fileSystemAccessTreeAccessors.test.ts +732 -0
- package/src/test/unit/localStorageTreeAccessors.test.ts +746 -0
- package/src/test/utils/fileSystemAccessMocks.ts +353 -0
- package/temp/build/typescript/ts_8nwakTlr.json +1 -0
- package/temp/coverage/crypto-utils/browserCryptoProvider.ts.html +1018 -0
- package/temp/coverage/{crypto → crypto-utils}/browserHashProvider.ts.html +3 -3
- package/temp/coverage/{lcov-report/crypto → crypto-utils}/index.html +21 -6
- package/temp/coverage/file-tree/directoryHandleStore.ts.html +493 -0
- package/temp/coverage/file-tree/fileApiTreeAccessors.ts.html +276 -6
- package/temp/coverage/file-tree/fileSystemAccessTreeAccessors.ts.html +1366 -0
- package/temp/coverage/file-tree/index.html +55 -10
- package/temp/coverage/file-tree/localStorageTreeAccessors.ts.html +1216 -0
- package/temp/coverage/helpers/fileTreeHelpers.ts.html +1 -1
- package/temp/coverage/helpers/index.html +1 -1
- package/temp/coverage/index.html +15 -15
- package/temp/coverage/lcov-report/crypto-utils/browserCryptoProvider.ts.html +1018 -0
- package/temp/coverage/lcov-report/{crypto → crypto-utils}/browserHashProvider.ts.html +3 -3
- package/temp/coverage/{crypto → lcov-report/crypto-utils}/index.html +21 -6
- package/temp/coverage/lcov-report/file-tree/directoryHandleStore.ts.html +493 -0
- package/temp/coverage/lcov-report/file-tree/fileApiTreeAccessors.ts.html +276 -6
- package/temp/coverage/lcov-report/file-tree/fileSystemAccessTreeAccessors.ts.html +1366 -0
- package/temp/coverage/lcov-report/file-tree/index.html +55 -10
- package/temp/coverage/lcov-report/file-tree/localStorageTreeAccessors.ts.html +1216 -0
- package/temp/coverage/lcov-report/helpers/fileTreeHelpers.ts.html +1 -1
- package/temp/coverage/lcov-report/helpers/index.html +1 -1
- package/temp/coverage/lcov-report/index.html +15 -15
- package/temp/coverage/lcov-report/url-utils/index.html +1 -1
- package/temp/coverage/lcov-report/url-utils/urlParams.ts.html +1 -1
- package/temp/coverage/lcov.info +2128 -451
- package/temp/coverage/url-utils/index.html +1 -1
- package/temp/coverage/url-utils/urlParams.ts.html +1 -1
- package/temp/test/jest/haste-map-7492f1b44480e0cdd1f220078fb3afd8-c8dd6c3430605adeb2f1cadf4f75e791-8c9336785555d572065b28c111982ba4 +0 -0
- package/temp/test/jest/perf-cache-7492f1b44480e0cdd1f220078fb3afd8-da39a3ee5e6b4b0d3255bfef95601890 +1 -1
- package/temp/ts-web-extras.api.json +3236 -385
- package/temp/ts-web-extras.api.md +91 -1
- package/dist/packlets/crypto/browserHashProvider.js.map +0 -1
- package/dist/packlets/crypto/index.js.map +0 -1
- package/lib/packlets/crypto/browserHashProvider.d.ts.map +0 -1
- package/lib/packlets/crypto/browserHashProvider.js.map +0 -1
- package/lib/packlets/crypto/index.d.ts.map +0 -1
- package/lib/packlets/crypto/index.js.map +0 -1
- package/temp/build/typescript/ts_vnCx6LlY.json +0 -1
- /package/dist/packlets/{crypto → crypto-utils}/browserHashProvider.js +0 -0
- /package/lib/packlets/{crypto → crypto-utils}/browserHashProvider.d.ts +0 -0
- /package/lib/packlets/{crypto → crypto-utils}/browserHashProvider.js +0 -0
- /package/src/packlets/{crypto → crypto-utils}/browserHashProvider.ts +0 -0
- /package/temp/test/jest/jest-transform-cache-7492f1b44480e0cdd1f220078fb3afd8-79ef2876fae7ca75eedb2aa53dc48338/{0e/package_0eb6535f5987849d93ea51ef33a14cf6 → 8d/package_8dcbedef69e4299f0f51fcda8f4f1c8e} +0 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import '@fgv/ts-utils-jest';
|
|
23
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
24
|
+
import { LocalStorageTreeAccessors, FileApiTreeAccessors } from '../../packlets/file-tree';
|
|
25
|
+
/**
|
|
26
|
+
* Mock Storage implementation for testing
|
|
27
|
+
*/
|
|
28
|
+
class MockStorage {
|
|
29
|
+
constructor() {
|
|
30
|
+
this._data = new Map();
|
|
31
|
+
}
|
|
32
|
+
get length() {
|
|
33
|
+
return this._data.size;
|
|
34
|
+
}
|
|
35
|
+
clear() {
|
|
36
|
+
this._data.clear();
|
|
37
|
+
}
|
|
38
|
+
getItem(key) {
|
|
39
|
+
var _a;
|
|
40
|
+
return (_a = this._data.get(key)) !== null && _a !== void 0 ? _a : null;
|
|
41
|
+
}
|
|
42
|
+
key(index) {
|
|
43
|
+
var _a;
|
|
44
|
+
const keys = Array.from(this._data.keys());
|
|
45
|
+
return (_a = keys[index]) !== null && _a !== void 0 ? _a : null;
|
|
46
|
+
}
|
|
47
|
+
removeItem(key) {
|
|
48
|
+
this._data.delete(key);
|
|
49
|
+
}
|
|
50
|
+
setItem(key, value) {
|
|
51
|
+
this._data.set(key, value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
describe('LocalStorageTreeAccessors', () => {
|
|
55
|
+
let mockStorage;
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
mockStorage = new MockStorage();
|
|
58
|
+
});
|
|
59
|
+
describe('fromStorage', () => {
|
|
60
|
+
test('creates accessors from empty storage', () => {
|
|
61
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
62
|
+
pathToKeyMap: {
|
|
63
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
64
|
+
},
|
|
65
|
+
storage: mockStorage
|
|
66
|
+
});
|
|
67
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
68
|
+
expect(accessors).toBeInstanceOf(LocalStorageTreeAccessors);
|
|
69
|
+
expect(accessors.isDirty()).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
test('loads collections from storage', () => {
|
|
73
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({
|
|
74
|
+
collection1: { items: { item1: {} } },
|
|
75
|
+
collection2: { items: { item2: {} } }
|
|
76
|
+
}));
|
|
77
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
78
|
+
pathToKeyMap: {
|
|
79
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
80
|
+
},
|
|
81
|
+
storage: mockStorage
|
|
82
|
+
});
|
|
83
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
84
|
+
expect(accessors.getFileContents('/data/ingredients/collection1.json')).toSucceed();
|
|
85
|
+
expect(accessors.getFileContents('/data/ingredients/collection2.json')).toSucceed();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
test('loads from multiple storage keys', () => {
|
|
89
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ ing1: { items: {} } }));
|
|
90
|
+
mockStorage.setItem('test:fillings:v1', JSON.stringify({ fill1: { items: {} } }));
|
|
91
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
92
|
+
pathToKeyMap: {
|
|
93
|
+
'/data/ingredients': 'test:ingredients:v1',
|
|
94
|
+
'/data/fillings': 'test:fillings:v1'
|
|
95
|
+
},
|
|
96
|
+
storage: mockStorage
|
|
97
|
+
});
|
|
98
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
99
|
+
expect(accessors.getFileContents('/data/ingredients/ing1.json')).toSucceed();
|
|
100
|
+
expect(accessors.getFileContents('/data/fillings/fill1.json')).toSucceed();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
test('skips corrupted JSON data', () => {
|
|
104
|
+
mockStorage.setItem('test:ingredients:v1', 'not valid json');
|
|
105
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
106
|
+
pathToKeyMap: {
|
|
107
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
108
|
+
},
|
|
109
|
+
storage: mockStorage
|
|
110
|
+
});
|
|
111
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
112
|
+
expect(accessors).toBeInstanceOf(LocalStorageTreeAccessors);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
test('skips non-object JSON', () => {
|
|
116
|
+
mockStorage.setItem('test:ingredients:v1', '"string value"');
|
|
117
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
118
|
+
pathToKeyMap: {
|
|
119
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
120
|
+
},
|
|
121
|
+
storage: mockStorage
|
|
122
|
+
});
|
|
123
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
124
|
+
expect(accessors).toBeInstanceOf(LocalStorageTreeAccessors);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
test('loads string values (new format) with inferred extension', () => {
|
|
128
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({
|
|
129
|
+
jsonCollection: '{"items":{}}',
|
|
130
|
+
yamlCollection: 'metadata:\n name: test\nitems: {}'
|
|
131
|
+
}));
|
|
132
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
133
|
+
pathToKeyMap: {
|
|
134
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
135
|
+
},
|
|
136
|
+
storage: mockStorage
|
|
137
|
+
});
|
|
138
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
139
|
+
expect(accessors.getFileContents('/data/ingredients/jsonCollection.json')).toSucceedWith('{"items":{}}');
|
|
140
|
+
expect(accessors.getFileContents('/data/ingredients/yamlCollection.yaml')).toSucceedWith('metadata:\n name: test\nitems: {}');
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
test('skips non-object non-string collection values', () => {
|
|
144
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({
|
|
145
|
+
good: { items: {} },
|
|
146
|
+
bad: 42
|
|
147
|
+
}));
|
|
148
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
149
|
+
pathToKeyMap: {
|
|
150
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
151
|
+
},
|
|
152
|
+
storage: mockStorage
|
|
153
|
+
});
|
|
154
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
155
|
+
expect(accessors.getFileContents('/data/ingredients/good.json')).toSucceed();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
test('applies prefix parameter', () => {
|
|
159
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
160
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
161
|
+
pathToKeyMap: {
|
|
162
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
163
|
+
},
|
|
164
|
+
storage: mockStorage,
|
|
165
|
+
prefix: '/myapp'
|
|
166
|
+
});
|
|
167
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
168
|
+
expect(accessors.getFileContents('/myapp/data/ingredients/collection1.json')).toSucceed();
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
test('succeeds with empty storage when no data present', () => {
|
|
172
|
+
const result = LocalStorageTreeAccessors.fromStorage({
|
|
173
|
+
pathToKeyMap: {
|
|
174
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
175
|
+
},
|
|
176
|
+
storage: mockStorage
|
|
177
|
+
});
|
|
178
|
+
expect(result).toSucceedAndSatisfy((accessors) => {
|
|
179
|
+
expect(accessors).toBeInstanceOf(LocalStorageTreeAccessors);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
describe('dirty tracking', () => {
|
|
184
|
+
test('starts with no dirty files', () => {
|
|
185
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
186
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
187
|
+
pathToKeyMap: {
|
|
188
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
189
|
+
},
|
|
190
|
+
storage: mockStorage,
|
|
191
|
+
mutable: true
|
|
192
|
+
}).orThrow();
|
|
193
|
+
expect(accessors.isDirty()).toBe(false);
|
|
194
|
+
expect(accessors.getDirtyPaths()).toEqual([]);
|
|
195
|
+
});
|
|
196
|
+
test('marks files as dirty after modification', () => {
|
|
197
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
198
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
199
|
+
pathToKeyMap: {
|
|
200
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
201
|
+
},
|
|
202
|
+
storage: mockStorage,
|
|
203
|
+
mutable: true
|
|
204
|
+
}).orThrow();
|
|
205
|
+
const newContent = JSON.stringify({ items: { modified: true } });
|
|
206
|
+
accessors.saveFileContents('/data/ingredients/collection1.json', newContent).orThrow();
|
|
207
|
+
expect(accessors.isDirty()).toBe(true);
|
|
208
|
+
expect(accessors.getDirtyPaths()).toContain('/data/ingredients/collection1.json');
|
|
209
|
+
});
|
|
210
|
+
test('tracks multiple dirty files', () => {
|
|
211
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({
|
|
212
|
+
collection1: { items: {} },
|
|
213
|
+
collection2: { items: {} }
|
|
214
|
+
}));
|
|
215
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
216
|
+
pathToKeyMap: {
|
|
217
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
218
|
+
},
|
|
219
|
+
storage: mockStorage,
|
|
220
|
+
mutable: true
|
|
221
|
+
}).orThrow();
|
|
222
|
+
accessors
|
|
223
|
+
.saveFileContents('/data/ingredients/collection1.json', JSON.stringify({ items: { a: 1 } }))
|
|
224
|
+
.orThrow();
|
|
225
|
+
accessors
|
|
226
|
+
.saveFileContents('/data/ingredients/collection2.json', JSON.stringify({ items: { b: 2 } }))
|
|
227
|
+
.orThrow();
|
|
228
|
+
expect(accessors.isDirty()).toBe(true);
|
|
229
|
+
expect(accessors.getDirtyPaths()).toHaveLength(2);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe('syncToDisk', () => {
|
|
233
|
+
test('syncs modified files to storage', async () => {
|
|
234
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: { original: true } } }));
|
|
235
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
236
|
+
pathToKeyMap: {
|
|
237
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
238
|
+
},
|
|
239
|
+
storage: mockStorage,
|
|
240
|
+
mutable: true
|
|
241
|
+
}).orThrow();
|
|
242
|
+
const newContent = JSON.stringify({ items: { modified: true } });
|
|
243
|
+
accessors.saveFileContents('/data/ingredients/collection1.json', newContent).orThrow();
|
|
244
|
+
const syncResult = await accessors.syncToDisk();
|
|
245
|
+
expect(syncResult).toSucceed();
|
|
246
|
+
const stored = mockStorage.getItem('test:ingredients:v1');
|
|
247
|
+
expect(stored).toBeTruthy();
|
|
248
|
+
const parsed = JSON.parse(stored);
|
|
249
|
+
// New format: values are raw content strings
|
|
250
|
+
expect(parsed.collection1).toBe(newContent);
|
|
251
|
+
});
|
|
252
|
+
test('clears dirty state after successful sync', async () => {
|
|
253
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
254
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
255
|
+
pathToKeyMap: {
|
|
256
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
257
|
+
},
|
|
258
|
+
storage: mockStorage,
|
|
259
|
+
mutable: true
|
|
260
|
+
}).orThrow();
|
|
261
|
+
accessors
|
|
262
|
+
.saveFileContents('/data/ingredients/collection1.json', JSON.stringify({ items: { a: 1 } }))
|
|
263
|
+
.orThrow();
|
|
264
|
+
expect(accessors.isDirty()).toBe(true);
|
|
265
|
+
await accessors.syncToDisk();
|
|
266
|
+
expect(accessors.isDirty()).toBe(false);
|
|
267
|
+
expect(accessors.getDirtyPaths()).toEqual([]);
|
|
268
|
+
});
|
|
269
|
+
test('syncs multiple files to same storage key', async () => {
|
|
270
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({
|
|
271
|
+
collection1: { items: {} },
|
|
272
|
+
collection2: { items: {} }
|
|
273
|
+
}));
|
|
274
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
275
|
+
pathToKeyMap: {
|
|
276
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
277
|
+
},
|
|
278
|
+
storage: mockStorage,
|
|
279
|
+
mutable: true
|
|
280
|
+
}).orThrow();
|
|
281
|
+
const content1 = JSON.stringify({ items: { a: 1 } });
|
|
282
|
+
const content2 = JSON.stringify({ items: { b: 2 } });
|
|
283
|
+
accessors.saveFileContents('/data/ingredients/collection1.json', content1).orThrow();
|
|
284
|
+
accessors.saveFileContents('/data/ingredients/collection2.json', content2).orThrow();
|
|
285
|
+
await accessors.syncToDisk();
|
|
286
|
+
const stored = mockStorage.getItem('test:ingredients:v1');
|
|
287
|
+
const parsed = JSON.parse(stored);
|
|
288
|
+
// New format: values are raw content strings
|
|
289
|
+
expect(parsed.collection1).toBe(content1);
|
|
290
|
+
expect(parsed.collection2).toBe(content2);
|
|
291
|
+
});
|
|
292
|
+
test('syncs files to different storage keys', async () => {
|
|
293
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ ing1: { items: {} } }));
|
|
294
|
+
mockStorage.setItem('test:fillings:v1', JSON.stringify({ fill1: { items: {} } }));
|
|
295
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
296
|
+
pathToKeyMap: {
|
|
297
|
+
'/data/ingredients': 'test:ingredients:v1',
|
|
298
|
+
'/data/fillings': 'test:fillings:v1'
|
|
299
|
+
},
|
|
300
|
+
storage: mockStorage,
|
|
301
|
+
mutable: true
|
|
302
|
+
}).orThrow();
|
|
303
|
+
accessors
|
|
304
|
+
.saveFileContents('/data/ingredients/ing1.json', JSON.stringify({ items: { a: 1 } }))
|
|
305
|
+
.orThrow();
|
|
306
|
+
accessors.saveFileContents('/data/fillings/fill1.json', JSON.stringify({ items: { b: 2 } })).orThrow();
|
|
307
|
+
await accessors.syncToDisk();
|
|
308
|
+
const ingContent = JSON.stringify({ items: { a: 1 } });
|
|
309
|
+
const fillContent = JSON.stringify({ items: { b: 2 } });
|
|
310
|
+
const ingStored = JSON.parse(mockStorage.getItem('test:ingredients:v1'));
|
|
311
|
+
const fillStored = JSON.parse(mockStorage.getItem('test:fillings:v1'));
|
|
312
|
+
// New format: values are raw content strings
|
|
313
|
+
expect(ingStored.ing1).toBe(ingContent);
|
|
314
|
+
expect(fillStored.fill1).toBe(fillContent);
|
|
315
|
+
});
|
|
316
|
+
test('succeeds with no dirty files', async () => {
|
|
317
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
318
|
+
pathToKeyMap: {
|
|
319
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
320
|
+
},
|
|
321
|
+
storage: mockStorage
|
|
322
|
+
}).orThrow();
|
|
323
|
+
const result = await accessors.syncToDisk();
|
|
324
|
+
expect(result).toSucceed();
|
|
325
|
+
});
|
|
326
|
+
test('preserves existing collections when syncing', async () => {
|
|
327
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({
|
|
328
|
+
existing: { items: { keep: true } },
|
|
329
|
+
toModify: { items: { original: true } }
|
|
330
|
+
}));
|
|
331
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
332
|
+
pathToKeyMap: {
|
|
333
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
334
|
+
},
|
|
335
|
+
storage: mockStorage,
|
|
336
|
+
mutable: true
|
|
337
|
+
}).orThrow();
|
|
338
|
+
const modifiedContent = JSON.stringify({ items: { modified: true } });
|
|
339
|
+
accessors.saveFileContents('/data/ingredients/toModify.json', modifiedContent).orThrow();
|
|
340
|
+
await accessors.syncToDisk();
|
|
341
|
+
const stored = JSON.parse(mockStorage.getItem('test:ingredients:v1'));
|
|
342
|
+
// existing was loaded from legacy format and not modified, so it stays as legacy object
|
|
343
|
+
// toModify was saved with new content, so it's now a raw string
|
|
344
|
+
expect(stored.toModify).toBe(modifiedContent);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
describe('autoSync mode', () => {
|
|
348
|
+
test('automatically syncs on save when enabled', () => {
|
|
349
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
350
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
351
|
+
pathToKeyMap: {
|
|
352
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
353
|
+
},
|
|
354
|
+
storage: mockStorage,
|
|
355
|
+
mutable: true,
|
|
356
|
+
autoSync: true
|
|
357
|
+
}).orThrow();
|
|
358
|
+
accessors
|
|
359
|
+
.saveFileContents('/data/ingredients/collection1.json', JSON.stringify({ items: { auto: true } }))
|
|
360
|
+
.orThrow();
|
|
361
|
+
expect(accessors.isDirty()).toBe(false);
|
|
362
|
+
const autoContent = JSON.stringify({ items: { auto: true } });
|
|
363
|
+
const stored = JSON.parse(mockStorage.getItem('test:ingredients:v1'));
|
|
364
|
+
// New format: values are raw content strings
|
|
365
|
+
expect(stored.collection1).toBe(autoContent);
|
|
366
|
+
});
|
|
367
|
+
test('does not auto-sync when disabled', () => {
|
|
368
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
369
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
370
|
+
pathToKeyMap: {
|
|
371
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
372
|
+
},
|
|
373
|
+
storage: mockStorage,
|
|
374
|
+
mutable: true,
|
|
375
|
+
autoSync: false
|
|
376
|
+
}).orThrow();
|
|
377
|
+
accessors
|
|
378
|
+
.saveFileContents('/data/ingredients/collection1.json', JSON.stringify({ items: { manual: true } }))
|
|
379
|
+
.orThrow();
|
|
380
|
+
expect(accessors.isDirty()).toBe(true);
|
|
381
|
+
// Storage should still have the original legacy format (not yet synced)
|
|
382
|
+
const stored = JSON.parse(mockStorage.getItem('test:ingredients:v1'));
|
|
383
|
+
expect(stored.collection1).toEqual({ items: {} });
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
describe('fileIsMutable', () => {
|
|
387
|
+
test('returns persistent detail when mutable', () => {
|
|
388
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
389
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
390
|
+
pathToKeyMap: {
|
|
391
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
392
|
+
},
|
|
393
|
+
storage: mockStorage,
|
|
394
|
+
mutable: true
|
|
395
|
+
}).orThrow();
|
|
396
|
+
expect(accessors.fileIsMutable('/data/ingredients/collection1.json')).toSucceedWithDetail(true, 'persistent');
|
|
397
|
+
});
|
|
398
|
+
test('returns not-mutable when not mutable', () => {
|
|
399
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
400
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
401
|
+
pathToKeyMap: {
|
|
402
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
403
|
+
},
|
|
404
|
+
storage: mockStorage,
|
|
405
|
+
mutable: false
|
|
406
|
+
}).orThrow();
|
|
407
|
+
const result = accessors.fileIsMutable('/data/ingredients/collection1.json');
|
|
408
|
+
expect(result.isFailure()).toBe(true);
|
|
409
|
+
if (result.isFailure()) {
|
|
410
|
+
expect(result.detail).toBe('not-mutable');
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
describe('integration with FileApiTreeAccessors', () => {
|
|
415
|
+
test('createFromLocalStorage creates FileTree with persistent accessors', () => {
|
|
416
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
417
|
+
const result = FileApiTreeAccessors.createFromLocalStorage({
|
|
418
|
+
pathToKeyMap: {
|
|
419
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
420
|
+
},
|
|
421
|
+
storage: mockStorage,
|
|
422
|
+
mutable: true
|
|
423
|
+
});
|
|
424
|
+
expect(result).toSucceed();
|
|
425
|
+
const tree = result.orThrow();
|
|
426
|
+
expect(tree).toBeInstanceOf(FileTree.FileTree);
|
|
427
|
+
expect(FileTree.isPersistentAccessors(tree.hal)).toBe(true);
|
|
428
|
+
const file = tree.getFile('/data/ingredients/collection1.json').orThrow();
|
|
429
|
+
expect(file.getIsMutable()).toSucceedWithDetail(true, 'persistent');
|
|
430
|
+
});
|
|
431
|
+
test('createFromLocalStorage with multiple paths', () => {
|
|
432
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ ing1: { items: {} } }));
|
|
433
|
+
mockStorage.setItem('test:fillings:v1', JSON.stringify({ fill1: { items: {} } }));
|
|
434
|
+
const result = FileApiTreeAccessors.createFromLocalStorage({
|
|
435
|
+
pathToKeyMap: {
|
|
436
|
+
'/data/ingredients': 'test:ingredients:v1',
|
|
437
|
+
'/data/fillings': 'test:fillings:v1'
|
|
438
|
+
},
|
|
439
|
+
storage: mockStorage,
|
|
440
|
+
mutable: true
|
|
441
|
+
});
|
|
442
|
+
expect(result).toSucceedAndSatisfy((tree) => {
|
|
443
|
+
expect(tree.getFile('/data/ingredients/ing1.json')).toSucceed();
|
|
444
|
+
expect(tree.getFile('/data/fillings/fill1.json')).toSucceed();
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
test('createFromLocalStorage with empty storage', () => {
|
|
448
|
+
const result = FileApiTreeAccessors.createFromLocalStorage({
|
|
449
|
+
pathToKeyMap: {
|
|
450
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
451
|
+
},
|
|
452
|
+
storage: mockStorage
|
|
453
|
+
});
|
|
454
|
+
expect(result).toSucceedAndSatisfy((tree) => {
|
|
455
|
+
expect(tree).toBeInstanceOf(FileTree.FileTree);
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
test('createFromLocalStorage fails when localStorage is not available', () => {
|
|
459
|
+
// Remove the global window object to ensure no fallback
|
|
460
|
+
const originalWindow = global.window;
|
|
461
|
+
delete global.window;
|
|
462
|
+
const result = FileApiTreeAccessors.createFromLocalStorage({
|
|
463
|
+
pathToKeyMap: {
|
|
464
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
465
|
+
},
|
|
466
|
+
storage: undefined
|
|
467
|
+
});
|
|
468
|
+
// Restore window
|
|
469
|
+
global.window = originalWindow;
|
|
470
|
+
expect(result).toFailWith(/localStorage is not available/i);
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
describe('path matching and error handling', () => {
|
|
474
|
+
test('handles files outside configured paths', async () => {
|
|
475
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
476
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
477
|
+
pathToKeyMap: {
|
|
478
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
479
|
+
},
|
|
480
|
+
storage: mockStorage,
|
|
481
|
+
mutable: true
|
|
482
|
+
}).orThrow();
|
|
483
|
+
// Try to save a file outside the configured path
|
|
484
|
+
const saveResult = accessors.saveFileContents('/other/path/file.json', JSON.stringify({ test: 1 }));
|
|
485
|
+
expect(saveResult).toSucceed();
|
|
486
|
+
// But syncing should fail because no storage key is configured
|
|
487
|
+
const syncResult = await accessors.syncToDisk();
|
|
488
|
+
expect(syncResult).toFailWith(/No storage key configured for path/i);
|
|
489
|
+
});
|
|
490
|
+
test('handles files inside configured paths', async () => {
|
|
491
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
492
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
493
|
+
pathToKeyMap: {
|
|
494
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
495
|
+
},
|
|
496
|
+
storage: mockStorage,
|
|
497
|
+
mutable: true
|
|
498
|
+
}).orThrow();
|
|
499
|
+
// Save a file inside the configured path
|
|
500
|
+
accessors.saveFileContents('/data/ingredients/newfile.json', JSON.stringify({ test: 1 })).orThrow();
|
|
501
|
+
// Syncing should succeed
|
|
502
|
+
const syncResult = await accessors.syncToDisk();
|
|
503
|
+
expect(syncResult).toSucceed();
|
|
504
|
+
});
|
|
505
|
+
test('handles failed content retrieval during sync', async () => {
|
|
506
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
507
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
508
|
+
pathToKeyMap: {
|
|
509
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
510
|
+
},
|
|
511
|
+
storage: mockStorage,
|
|
512
|
+
mutable: true
|
|
513
|
+
}).orThrow();
|
|
514
|
+
// Modify a file
|
|
515
|
+
accessors.saveFileContents('/data/ingredients/collection1.json', JSON.stringify({ test: 1 })).orThrow();
|
|
516
|
+
// Override getFileContents to fail
|
|
517
|
+
const originalGetFileContents = accessors.getFileContents.bind(accessors);
|
|
518
|
+
accessors.getFileContents = jest.fn((path) => {
|
|
519
|
+
if (path === '/data/ingredients/collection1.json') {
|
|
520
|
+
return { isSuccess: () => false, isFailure: () => true, message: 'File not found' };
|
|
521
|
+
}
|
|
522
|
+
return originalGetFileContents(path);
|
|
523
|
+
});
|
|
524
|
+
const syncResult = await accessors.syncToDisk();
|
|
525
|
+
expect(syncResult).toFailWith(/Failed to get file contents.*File not found/i);
|
|
526
|
+
});
|
|
527
|
+
test('handles corrupted JSON in localStorage during sync', async () => {
|
|
528
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
529
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
530
|
+
pathToKeyMap: {
|
|
531
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
532
|
+
},
|
|
533
|
+
storage: mockStorage,
|
|
534
|
+
mutable: true
|
|
535
|
+
}).orThrow();
|
|
536
|
+
// Modify a file
|
|
537
|
+
accessors.saveFileContents('/data/ingredients/collection1.json', JSON.stringify({ test: 1 })).orThrow();
|
|
538
|
+
// Corrupt the existing storage data
|
|
539
|
+
mockStorage.setItem('test:ingredients:v1', 'not valid json');
|
|
540
|
+
// Sync should still succeed by starting fresh
|
|
541
|
+
const syncResult = await accessors.syncToDisk();
|
|
542
|
+
expect(syncResult).toSucceed();
|
|
543
|
+
// Verify the data was written correctly despite corrupted previous data
|
|
544
|
+
const stored = mockStorage.getItem('test:ingredients:v1');
|
|
545
|
+
const parsed = JSON.parse(stored);
|
|
546
|
+
// New format: values are raw content strings
|
|
547
|
+
expect(parsed.collection1).toBe(JSON.stringify({ test: 1 }));
|
|
548
|
+
});
|
|
549
|
+
test('syncs non-JSON content (YAML) without error', async () => {
|
|
550
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
551
|
+
pathToKeyMap: {
|
|
552
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
553
|
+
},
|
|
554
|
+
storage: mockStorage,
|
|
555
|
+
mutable: true
|
|
556
|
+
}).orThrow();
|
|
557
|
+
// Create and save a YAML file
|
|
558
|
+
const yamlContent = 'metadata:\n name: test\nitems: {}';
|
|
559
|
+
accessors.saveFileContents('/data/ingredients/my-collection.yaml', yamlContent).orThrow();
|
|
560
|
+
const syncResult = await accessors.syncToDisk();
|
|
561
|
+
expect(syncResult).toSucceed();
|
|
562
|
+
const stored = JSON.parse(mockStorage.getItem('test:ingredients:v1'));
|
|
563
|
+
expect(stored['my-collection']).toBe(yamlContent);
|
|
564
|
+
});
|
|
565
|
+
test('handles multiple sync failures with aggregated errors', async () => {
|
|
566
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
567
|
+
pathToKeyMap: {
|
|
568
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
569
|
+
},
|
|
570
|
+
storage: mockStorage,
|
|
571
|
+
mutable: true
|
|
572
|
+
}).orThrow();
|
|
573
|
+
// Add files outside configured paths
|
|
574
|
+
accessors.saveFileContents('/other1/file1.json', JSON.stringify({ test: 1 })).orThrow();
|
|
575
|
+
accessors.saveFileContents('/other2/file2.json', JSON.stringify({ test: 2 })).orThrow();
|
|
576
|
+
const syncResult = await accessors.syncToDisk();
|
|
577
|
+
expect(syncResult).toFailWith(/Failed to sync 2 file\(s\)/i);
|
|
578
|
+
});
|
|
579
|
+
test('handles auto-sync failure', () => {
|
|
580
|
+
mockStorage.setItem('test:ingredients:v1', JSON.stringify({ collection1: { items: {} } }));
|
|
581
|
+
const accessors = LocalStorageTreeAccessors.fromStorage({
|
|
582
|
+
pathToKeyMap: {
|
|
583
|
+
'/data/ingredients': 'test:ingredients:v1'
|
|
584
|
+
},
|
|
585
|
+
storage: mockStorage,
|
|
586
|
+
mutable: true,
|
|
587
|
+
autoSync: true
|
|
588
|
+
}).orThrow();
|
|
589
|
+
// Try to save a file outside configured path with autoSync enabled
|
|
590
|
+
const saveResult = accessors.saveFileContents('/other/path/file.json', JSON.stringify({ test: 1 }));
|
|
591
|
+
expect(saveResult).toFailWith(/No storage key configured for path/i);
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
//# sourceMappingURL=localStorageTreeAccessors.test.js.map
|