@ibgib/ts-gib 0.4.9

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.
Files changed (87) hide show
  1. package/.vscode/launch.json +24 -0
  2. package/.vscode/settings.json +34 -0
  3. package/.vscode/tasks.json +37 -0
  4. package/CHANGELOG.md +159 -0
  5. package/README.md +502 -0
  6. package/dist/V1/constants.d.mts +22 -0
  7. package/dist/V1/constants.d.mts.map +1 -0
  8. package/dist/V1/constants.mjs +21 -0
  9. package/dist/V1/constants.mjs.map +1 -0
  10. package/dist/V1/factory.d.mts +23 -0
  11. package/dist/V1/factory.d.mts.map +1 -0
  12. package/dist/V1/factory.mjs +78 -0
  13. package/dist/V1/factory.mjs.map +1 -0
  14. package/dist/V1/index.d.mts +6 -0
  15. package/dist/V1/index.d.mts.map +1 -0
  16. package/dist/V1/index.mjs +6 -0
  17. package/dist/V1/index.mjs.map +1 -0
  18. package/dist/V1/sha256v1.d.mts +19 -0
  19. package/dist/V1/sha256v1.d.mts.map +1 -0
  20. package/dist/V1/sha256v1.mjs +86 -0
  21. package/dist/V1/sha256v1.mjs.map +1 -0
  22. package/dist/V1/transforms/fork.d.mts +16 -0
  23. package/dist/V1/transforms/fork.d.mts.map +1 -0
  24. package/dist/V1/transforms/fork.mjs +111 -0
  25. package/dist/V1/transforms/fork.mjs.map +1 -0
  26. package/dist/V1/transforms/index.d.mts +5 -0
  27. package/dist/V1/transforms/index.d.mts.map +1 -0
  28. package/dist/V1/transforms/index.mjs +5 -0
  29. package/dist/V1/transforms/index.mjs.map +1 -0
  30. package/dist/V1/transforms/mut8.d.mts +50 -0
  31. package/dist/V1/transforms/mut8.d.mts.map +1 -0
  32. package/dist/V1/transforms/mut8.mjs +246 -0
  33. package/dist/V1/transforms/mut8.mjs.map +1 -0
  34. package/dist/V1/transforms/rel8.d.mts +14 -0
  35. package/dist/V1/transforms/rel8.d.mts.map +1 -0
  36. package/dist/V1/transforms/rel8.mjs +176 -0
  37. package/dist/V1/transforms/rel8.mjs.map +1 -0
  38. package/dist/V1/transforms/transform-helper.d.mts +92 -0
  39. package/dist/V1/transforms/transform-helper.d.mts.map +1 -0
  40. package/dist/V1/transforms/transform-helper.mjs +189 -0
  41. package/dist/V1/transforms/transform-helper.mjs.map +1 -0
  42. package/dist/V1/types.d.mts +79 -0
  43. package/dist/V1/types.d.mts.map +1 -0
  44. package/dist/V1/types.mjs +12 -0
  45. package/dist/V1/types.mjs.map +1 -0
  46. package/dist/helper.d.mts +77 -0
  47. package/dist/helper.d.mts.map +1 -0
  48. package/dist/helper.mjs +179 -0
  49. package/dist/helper.mjs.map +1 -0
  50. package/dist/index.cjs +4 -0
  51. package/dist/index.cjs.map +1 -0
  52. package/dist/index.d.cts +4 -0
  53. package/dist/index.d.cts.map +1 -0
  54. package/dist/index.d.mts +4 -0
  55. package/dist/index.d.mts.map +1 -0
  56. package/dist/index.mjs +4 -0
  57. package/dist/index.mjs.map +1 -0
  58. package/dist/types.d.mts +242 -0
  59. package/dist/types.d.mts.map +1 -0
  60. package/dist/types.mjs +2 -0
  61. package/dist/types.mjs.map +1 -0
  62. package/jasmine-browser.json +18 -0
  63. package/jasmine.json +6 -0
  64. package/package.json +61 -0
  65. package/src/V1/constants.mts +23 -0
  66. package/src/V1/factory.mts +110 -0
  67. package/src/V1/factory.spec.mts +162 -0
  68. package/src/V1/index.mts +5 -0
  69. package/src/V1/sha256v1.mts +85 -0
  70. package/src/V1/sha256v1.spec.mts +221 -0
  71. package/src/V1/transforms/fork.mts +100 -0
  72. package/src/V1/transforms/fork.spec.mts +410 -0
  73. package/src/V1/transforms/index.mts +4 -0
  74. package/src/V1/transforms/mut8.mts +239 -0
  75. package/src/V1/transforms/mut8.spec.mts +656 -0
  76. package/src/V1/transforms/rel8.mts +173 -0
  77. package/src/V1/transforms/rel8.spec.mts +556 -0
  78. package/src/V1/transforms/transform-helper.mts +263 -0
  79. package/src/V1/transforms/transform-helper.spec.mts +45 -0
  80. package/src/V1/types.mts +84 -0
  81. package/src/helper.mts +192 -0
  82. package/src/helper.spec.mts +127 -0
  83. package/src/index.cts +3 -0
  84. package/src/index.mts +3 -0
  85. package/src/types.mts +242 -0
  86. package/tsconfig.json +15 -0
  87. package/tsconfig.test.json +10 -0
@@ -0,0 +1,656 @@
1
+ /**
2
+ * Test basic mut8 transforms.
3
+ *
4
+ * NOTE:
5
+ * This only tests the node implementation, and manual testing
6
+ * is required for browser until I get some kind of browser testing
7
+ * going.
8
+ */
9
+
10
+ import { IbGib_V1, IbGibRel8ns_V1, Rel8n } from '../types.mjs';
11
+ import { TransformOpts_Mut8, IbGibAddr } from '../../types.mjs';
12
+ import { pretty, clone, delay, getIbGibAddr, getIbAndGib, } from '../../helper.mjs';
13
+ import { sha256v1, } from '../sha256v1.mjs';
14
+ import { ROOT, ROOT_ADDR, GIB_DELIMITER } from '../constants.mjs';
15
+ import { fork } from './fork.mjs';
16
+ import { mut8 } from './mut8.mjs';
17
+ import { Factory_V1 as factory } from '../factory.mjs';
18
+ import { getGib, getGibInfo } from './transform-helper.mjs';
19
+
20
+ const PRIMITIVE_IBGIBS = [factory.root()].concat(factory.primitives({
21
+ ibs: [
22
+ 'a', '7', 'tag',
23
+ 'any string/value that isnt hashed with a gib is a primitive',
24
+ // e.g. 6 -> 6^ -> 6^gib are all equivalent ib^gib addresses,
25
+ ]
26
+ }));
27
+
28
+ const STR = 'string here';
29
+ const RENAME_ME_KEY = 'rename me please';
30
+ const RENAMED_KEY = 'renamed successfully';
31
+ const RENAME_VALUE = 'hey heres a value';
32
+ const DATA_SIMPLE_XY = { x: 1, y: 2 };
33
+ const DATA_SIMPLE_XY_STR = { x: 1, y: 2, str: STR };
34
+ const DATA_COMPLEX_XY_NESTED = {
35
+ xyObj: DATA_SIMPLE_XY,
36
+ nested1: { [RENAME_ME_KEY]: RENAME_VALUE },
37
+ };
38
+ const NEW_IB = 'new ib here yo';
39
+
40
+ const DATAS_SIMPLE = [
41
+ DATA_SIMPLE_XY,
42
+ DATA_SIMPLE_XY_STR,
43
+ ];
44
+
45
+ describe(`can't mut8 primitives (including the root)`, () => {
46
+ for (const src of PRIMITIVE_IBGIBS) {
47
+
48
+ it(`should fail to mut8 ib (ib: ${src.ib})`, async () => {
49
+ let errored = false;
50
+ try {
51
+ const _ignored = await mut8({ src, mut8Ib: 'changed ' + src.ib });
52
+ } catch (error) {
53
+ errored = true;
54
+ }
55
+ expect(errored).toBeTrue()
56
+ });
57
+
58
+ it(`should fail to mut8 add or patch data (ib: ${src.ib})`, async () => {
59
+ let errored = false;
60
+ try {
61
+ const _ignored = await mut8({ src, dataToAddOrPatch: { a: "aaaaa" } });
62
+ } catch (error) {
63
+ errored = true;
64
+ }
65
+ expect(errored).toBeTrue()
66
+ });
67
+
68
+ it(`should fail to mut8 rename data (ib: ${src.ib})`, async () => {
69
+ let errored = false;
70
+ try {
71
+ const _ignored = await mut8({ src, dataToRename: { a: "aaaaa" } });
72
+ } catch (error) {
73
+ errored = true;
74
+ }
75
+ expect(errored).toBeTrue()
76
+ });
77
+
78
+ it(`should fail to mut8 remove data (ib: ${src.ib})`, async () => {
79
+ let errored = false;
80
+ try {
81
+ const _ignored = await mut8({ src, dataToRemove: { a: "aaaaa" } });
82
+ } catch (error) {
83
+ errored = true;
84
+ }
85
+ expect(errored).toBeTrue()
86
+ });
87
+ }
88
+ });
89
+
90
+ describe(`when mutating a regular ibgib`, () => {
91
+
92
+ describe(`with simple, 1-level, non-nested data and ib`, () => {
93
+ for (const primitive of PRIMITIVE_IBGIBS) {
94
+ for (const testData of DATAS_SIMPLE) {
95
+ // console.log(`doing testdata: ${primitive.ib}.data ~ ${pretty(testData)}`);
96
+ it(`should add/rename/patch simple data (${primitive.ib}) and ib`, async () => {
97
+
98
+ let { newIbGib: src } = await fork({ src: primitive, noTimestamp: true });
99
+
100
+ // #region add data
101
+
102
+ // we now have a non-primitive source and wish to
103
+ // add some internal data to it.
104
+
105
+ let { newIbGib: dataAddedIbGib } = await mut8({
106
+ src,
107
+ dataToAddOrPatch: testData,
108
+ noTimestamp: true,
109
+ linkedRel8ns: ['past'],
110
+ });
111
+ let dataAddedAddr: IbGibAddr = getIbGibAddr({ ibGib: dataAddedIbGib });
112
+ expect(dataAddedIbGib).toBeTruthy();
113
+ expect(dataAddedIbGib.data).toBeTruthy();
114
+ expect(dataAddedIbGib.data).toBeTruthy();
115
+ expect(dataAddedIbGib.data).toEqual(testData);
116
+ expect(dataAddedIbGib.rel8ns).toBeTruthy();
117
+ expect(dataAddedIbGib.rel8ns!.past).toBeTruthy();
118
+ expect((dataAddedIbGib.rel8ns!.past || []).length)
119
+ .withContext('(dataAddedIbGib.rel8ns!.past || []).length')
120
+ .toEqual(1);
121
+
122
+ // #endregion
123
+
124
+ // #region rename
125
+
126
+ const newNameForX = "new name for x here";
127
+ const valueForX = dataAddedIbGib.data!.x;
128
+ const dataToRename = { x: newNameForX }
129
+
130
+ let { newIbGib: dataRenamedIbGib } =
131
+ await mut8({
132
+ src: dataAddedIbGib,
133
+ dataToRename,
134
+ noTimestamp: true,
135
+ linkedRel8ns: ['past'],
136
+ });
137
+ expect(dataRenamedIbGib).toBeTruthy();
138
+ expect(dataRenamedIbGib.data).toBeTruthy();
139
+ expect(dataRenamedIbGib.data).toBeTruthy();
140
+ expect(dataRenamedIbGib.rel8ns).toBeTruthy();
141
+ expect(dataRenamedIbGib.rel8ns!.past).toBeTruthy();
142
+ expect((dataRenamedIbGib.rel8ns!.past || []).length)
143
+ .withContext('dataRenamedIbGib.rel8ns!.past || []).length')
144
+ .toEqual(1);
145
+
146
+ // the data should have the new key name that we did
147
+ expect(Object.keys(dataRenamedIbGib.data!)).toContain(newNameForX);
148
+
149
+ // the data value for that new key name should be the same as before, since we didn't change it
150
+ expect(dataRenamedIbGib.data![newNameForX]).toEqual(valueForX);
151
+
152
+ // the most recent past of the dataRenamed ibGib should be the src's (dataAddedIbGib) address
153
+ expect(dataRenamedIbGib.rel8ns!.past![dataRenamedIbGib.rel8ns!.past!.length - 1]).toEqual(dataAddedAddr);
154
+
155
+ //#endregion
156
+
157
+ // #region patch
158
+
159
+ const newValueForX = 42;
160
+ const dataToPatch = { x: newValueForX }
161
+
162
+ let { newIbGib: dataPatchedIbGib } =
163
+ await mut8({ src: dataAddedIbGib, dataToAddOrPatch: dataToPatch, noTimestamp: true });
164
+ expect(dataPatchedIbGib).toBeTruthy();
165
+ expect(dataPatchedIbGib.data).toBeTruthy();
166
+ expect(dataPatchedIbGib.data).toBeTruthy();
167
+
168
+ // value should be changed to the new value
169
+ expect(dataPatchedIbGib.data!.x).toEqual(newValueForX);
170
+
171
+ // the most recent past of the dataRenamed ibGib should be the src's (dataAddedIbGib) address
172
+ expect(dataPatchedIbGib.rel8ns!.past![dataPatchedIbGib.rel8ns!.past!.length - 1]).toEqual(dataAddedAddr);
173
+
174
+ //#endregion
175
+
176
+ // #region remove
177
+
178
+ const dataToRemove = { x: '' }; // just want mapping, value is ignored
179
+
180
+ let { newIbGib: dataRemovedIbGib } =
181
+ await mut8({ src: dataAddedIbGib, dataToRemove, noTimestamp: true });
182
+ expect(dataRemovedIbGib).toBeTruthy();
183
+ expect(dataRemovedIbGib.data).toBeTruthy();
184
+ expect(dataRemovedIbGib.data).toBeTruthy();
185
+
186
+ // the data value for that new key name should be the same as before, since we didn't change it
187
+ expect(dataRemovedIbGib.data!.x).toBeUndefined();
188
+
189
+ // the most recent past of the dataRemovedIbGib should be the src's address
190
+ expect(dataRemovedIbGib.rel8ns!.past![dataRemovedIbGib.rel8ns!.past!.length - 1]).toEqual(dataAddedAddr);
191
+
192
+ //#endregion
193
+
194
+ // #region ib
195
+
196
+ let { newIbGib: ibMut8dIbGib } =
197
+ await mut8({ src: dataAddedIbGib, mut8Ib: NEW_IB, noTimestamp: true });
198
+ expect(ibMut8dIbGib).toBeTruthy();
199
+ expect(ibMut8dIbGib.ib).toEqual(NEW_IB);
200
+
201
+ //#endregion
202
+ });
203
+
204
+
205
+ it(`should be rel8d next mut8`, async () => {
206
+ const { newIbGib: tjpIbGib } = await fork({
207
+ src: primitive,
208
+ destIb: 'some ib yo',
209
+ tjp: { timestamp: true, uuid: true },
210
+ });
211
+ const tjpAddr = getIbGibAddr({ ibGib: tjpIbGib });
212
+ const tjpAddrGib = getIbAndGib({ ibGibAddr: tjpAddr }).gib;
213
+ expect(tjpIbGib!.data?.uuid).toBeTruthy();
214
+ expect(tjpIbGib!.data?.isTjp).withContext("isTjp").toBeTrue()
215
+ const { newIbGib: hasRel8dTjp } = await mut8({
216
+ src: tjpIbGib,
217
+ dataToAddOrPatch: { someData: "not intrinsically pertinent to this test" },
218
+ });
219
+ const hasRel8dTjpAddr = getIbGibAddr({ ibGib: hasRel8dTjp });
220
+ const hasRel8dTjpAddrGib = getIbAndGib({ ibGibAddr: hasRel8dTjpAddr }).gib;
221
+ expect(hasRel8dTjp!.data?.uuid).toBeTruthy();
222
+ expect(hasRel8dTjp!.data?.isTjp).withContext("isTjp after additional mut8").toBeUndefined();
223
+ expect(hasRel8dTjp!.rel8ns).withContext("rel8ns").toBeTruthy();
224
+ expect(hasRel8dTjp!.rel8ns!.tjp).withContext("rel8ns.tjp").toBeTruthy();
225
+ expect(hasRel8dTjp!.rel8ns!.tjp!.length).withContext("rel8ns.tjp.length").toEqual(1);
226
+ expect(hasRel8dTjp!.rel8ns!.tjp).withContext("rel8ns.tjp include").toContain(tjpAddr);
227
+
228
+ // the gib should not just be the hash, since it has a tjp timeline
229
+ expect(hasRel8dTjpAddrGib).toContain(tjpAddrGib);
230
+
231
+ // shoe-horning in here some getGibInfo testing...eesh
232
+ let hasRel8dTjpGibInfo = getGibInfo({ ibGibAddr: hasRel8dTjpAddr }); // via addr
233
+ expect(hasRel8dTjpGibInfo.isPrimitive).toBeUndefined();
234
+ expect(hasRel8dTjpGibInfo.piecesCount).toEqual(2, 'pieces count');
235
+ const hasRel8dTjpHash = await sha256v1(hasRel8dTjp, '');
236
+ expect(hasRel8dTjpGibInfo.punctiliarHash).toEqual(hasRel8dTjpHash, 'punctiliarHash');
237
+ expect(hasRel8dTjpGibInfo.tjpGib).toEqual(tjpAddrGib);
238
+ // the info should be the same if we get it straight from the gib
239
+ const hasRel8dTjpGibInfo_viaGib = getGibInfo({ gib: hasRel8dTjpAddrGib }); // via gib
240
+ expect(hasRel8dTjpGibInfo_viaGib).toEqual(hasRel8dTjpGibInfo, 'infos via addr and gib should be equal');
241
+ });
242
+ }
243
+ } // double for..of statement
244
+ })
245
+
246
+ describe(`...and creating dna`, () => {
247
+ var src: IbGib_V1, forkDnas: IbGib_V1[],
248
+ // these are used later when reapplying dnas to test pure functionality
249
+ addDna: IbGib_V1, dataAddedIbGib1: IbGib_V1,
250
+ patchDna: IbGib_V1, dataPatchedIbGib1: IbGib_V1,
251
+ renameDna: IbGib_V1, dataRenamedIbGib1: IbGib_V1,
252
+ removeDna: IbGib_V1, dataRemovedIbGib1: IbGib_V1;
253
+ beforeAll(async () => {
254
+ const resInit = await fork({ src: ROOT, dna: true });
255
+ src = resInit.newIbGib;
256
+ forkDnas = resInit.dnas!;
257
+ })
258
+
259
+ it(`should have well-formed dna...`, async () => {
260
+
261
+ // #region data added
262
+
263
+ const srcAddr = getIbGibAddr({ ibGib: src });
264
+ const dataToAdd = DATA_SIMPLE_XY;
265
+ const { newIbGib: dataAddedIbGib, dnas: Mut8AddDnas } =
266
+ await mut8({ src, dna: true, dataToAddOrPatch: dataToAdd });
267
+ dataAddedIbGib1 = dataAddedIbGib; // for later testing pure functionality
268
+ const dataAddedAddr = getIbGibAddr({ ibGib: dataAddedIbGib });
269
+
270
+ let mut8Dna = Mut8AddDnas![0];
271
+ addDna = mut8Dna;
272
+
273
+ expect(mut8Dna.ib).withContext(`ib should be mut8`).toEqual('mut8');
274
+
275
+ {
276
+ expect(mut8Dna?.rel8ns).withContext(`should descend from mut8^gib primitive`).toBeTruthy();
277
+ const mut8DnaRel8ns: IbGibRel8ns_V1 = mut8Dna?.rel8ns!;
278
+ expect(mut8DnaRel8ns.ancestor).withContext(`should descend from mut8^gib primitive`).toBeTruthy();
279
+ expect(mut8DnaRel8ns.ancestor!.length).withContext(`should descend from mut8^gib primitive`).toEqual(1);
280
+ expect(mut8DnaRel8ns.ancestor![0]).withContext(`should descend from mut8^gib primitive`).toEqual('mut8^gib');
281
+
282
+ const mut8DnaData = (mut8Dna.data as TransformOpts_Mut8<IbGib_V1>)!;
283
+
284
+ expect(mut8DnaData).withContext(`should have well-formed common transform opts`).toBeTruthy();
285
+ expect(mut8DnaData).withContext(`should have well-formed common transform opts`).toBeTruthy();
286
+ expect(mut8DnaData.dna).withContext(`should have well-formed common transform opts`).toBeTrue()
287
+ expect(mut8DnaData.srcAddr).withContext(`should have well-formed common transform opts`).toBeUndefined();
288
+ expect(mut8DnaData.src).withContext(`should have well-formed common transform opts`).toBeUndefined();
289
+
290
+ expect(mut8DnaData.type).withContext(`should have well-formed data specific to type of mut8`).toEqual("mut8");
291
+ expect(mut8DnaData.dataToAddOrPatch).withContext(`should have well-formed data specific to type of mut8`).toEqual(dataToAdd);
292
+ }
293
+ // #endregion
294
+
295
+ // #region data patched
296
+
297
+ {
298
+ const dataToPatch = { z: '3' };
299
+ const { newIbGib: dataPatchedIbGib, dnas: patchedDnas } =
300
+ await mut8({ src: dataAddedIbGib, dna: true, dataToAddOrPatch: dataToPatch });
301
+ dataPatchedIbGib1 = dataPatchedIbGib; // for later testing pure functionality
302
+
303
+ mut8Dna = patchedDnas![0];
304
+ patchDna = mut8Dna;
305
+
306
+ expect(mut8Dna.ib).withContext(`data patched, ib should be mut8`).toEqual('mut8');
307
+ expect(mut8Dna?.rel8ns).withContext(`data patched, should descend from mut8^gib primitive`).toBeTruthy();
308
+ const mut8DnaRel8ns: IbGibRel8ns_V1 = mut8Dna?.rel8ns!;
309
+ expect(mut8DnaRel8ns.ancestor).withContext(`data patched, should descend from mut8^gib primitive`).toBeTruthy();
310
+ expect(mut8DnaRel8ns.ancestor!.length).toEqual(1);
311
+ expect(mut8DnaRel8ns.ancestor![0]).toEqual('mut8^gib');
312
+
313
+ const mut8DnaData = (mut8Dna.data as TransformOpts_Mut8<IbGib_V1>)!;
314
+
315
+ expect(mut8DnaData).withContext(`data patched, should have well-formed common transform opts`).toBeTruthy();
316
+ expect(mut8DnaData).withContext(`data patched, should have well-formed common transform opts`).toBeTruthy();
317
+ expect(mut8DnaData.dna).withContext(`data patched, should have well-formed common transform opts`).toBeTrue()
318
+ expect(mut8DnaData.srcAddr).withContext(`data patched, should have well-formed common transform opts`).toBeUndefined();
319
+ expect(mut8DnaData.src).withContext(`data patched, should have well-formed common transform opts`).toBeUndefined();
320
+ expect(mut8DnaData.type).withContext(`data patched, should have well-formed data specific to type of mut8`).toEqual("mut8");
321
+ expect(mut8DnaData.dataToAddOrPatch).withContext(`data patched, should have well-formed data specific to type of mut8`).toEqual(dataToPatch);
322
+ }
323
+ // #endregion
324
+
325
+ // #region data renamed
326
+ {
327
+ const dataToRename = { x: RENAMED_KEY };
328
+ const { newIbGib: dataRenamedIbGib, dnas: renamedDnas } =
329
+ await mut8({ src: dataAddedIbGib, dna: true, dataToRename });
330
+ dataRenamedIbGib1 = dataRenamedIbGib; // for later testing pure functionality
331
+
332
+ mut8Dna = renamedDnas![0];
333
+ renameDna = mut8Dna;
334
+
335
+ expect(mut8Dna.ib).withContext(`data renamed, ib should be mut8`).toEqual('mut8');
336
+ expect(mut8Dna?.rel8ns).withContext(`data renamed, should descend from mut8^gib primitive`).toBeTruthy();
337
+ const mut8DnaRel8ns: IbGibRel8ns_V1 = mut8Dna?.rel8ns!;
338
+ expect(mut8DnaRel8ns.ancestor).withContext(`data renamed, should descend from mut8^gib primitive`).toBeTruthy();
339
+ expect(mut8DnaRel8ns.ancestor!.length).withContext(`data renamed, should descend from mut8^gib primitive`).toEqual(1);
340
+ expect(mut8DnaRel8ns.ancestor![0]).withContext(`data renamed, should descend from mut8^gib primitive`).toEqual('mut8^gib');
341
+
342
+ const mut8DnaData = (mut8Dna.data as TransformOpts_Mut8<IbGib_V1>)!;
343
+
344
+ expect(mut8DnaData).withContext(`data renamed, should have well-formed common transform opts`).toBeTruthy();
345
+ expect(mut8DnaData).withContext(`data renamed, should have well-formed common transform opts`).toBeTruthy();
346
+ expect(mut8DnaData.dna).withContext(`data renamed, should have well-formed common transform opts`).toBeTrue()
347
+ expect(mut8DnaData.srcAddr).withContext(`data renamed, should have well-formed common transform opts`).toBeUndefined();
348
+ expect(mut8DnaData.src).withContext(`data renamed, should have well-formed common transform opts`).toBeUndefined();
349
+
350
+ expect(mut8DnaData.type).withContext(`data renamed, should have well-formed data specific to type of mut8`).toEqual("mut8");
351
+ expect(mut8DnaData.dataToRename).withContext(`data renamed, should have well-formed data specific to type of mut8`).toEqual(dataToRename);
352
+ }
353
+ // #endregion
354
+
355
+ // #region test remove dna
356
+
357
+ {
358
+ const dataToRemove = { y: '' }; // value is ignored when removing, just want mapping to key
359
+ const { newIbGib: dataRemovedIbGib, dnas: removedDnas } =
360
+ await mut8({ src: dataAddedIbGib, dna: true, dataToRemove });
361
+ dataRemovedIbGib1 = dataRemovedIbGib; // for later testing pure functionality
362
+
363
+ mut8Dna = removedDnas![0];
364
+ removeDna = mut8Dna;
365
+
366
+ expect(mut8Dna.ib).withContext(`data removed, ib should be mut8`).toEqual('mut8');
367
+ expect(mut8Dna?.rel8ns).withContext(`data removed, should descend from mut8^gib primitive`).toBeTruthy();
368
+ const mut8DnaRel8ns: IbGibRel8ns_V1 = mut8Dna?.rel8ns!;
369
+ expect(mut8DnaRel8ns.ancestor).withContext(`data removed, should descend from mut8^gib primitive`).toBeTruthy();
370
+ expect(mut8DnaRel8ns.ancestor!.length).withContext(`data removed, should descend from mut8^gib primitive`).toEqual(1);
371
+ expect(mut8DnaRel8ns.ancestor![0]).withContext(`data removed, should descend from mut8^gib primitive`).toEqual('mut8^gib');
372
+
373
+ const mut8DnaData = (mut8Dna.data as TransformOpts_Mut8<IbGib_V1>)!;
374
+
375
+ expect(mut8DnaData).withContext(`data removed, should have well-formed common transform opts`).toBeTruthy();
376
+ expect(mut8DnaData).withContext(`data removed, should have well-formed common transform opts`).toBeTruthy();
377
+ expect(mut8DnaData.dna).withContext(`data removed, should have well-formed common transform opts`).toBeTrue()
378
+ expect(mut8DnaData.srcAddr).withContext(`data removed, should have well-formed common transform opts`).toBeUndefined();
379
+ expect(mut8DnaData.src).withContext(`data removed, should have well-formed common transform opts`).toBeUndefined();
380
+
381
+ expect(mut8DnaData.type).withContext(`data removed, should have well-formed data specific to type of mut8`).toEqual("mut8");
382
+ expect(mut8DnaData.dataToRemove).withContext(`data removed, should have well-formed data specific to type of mut8`).toEqual(dataToRemove);
383
+ }
384
+
385
+ // #endregion
386
+
387
+ // #region pure dna functionality (placed here for code reuse, but slightly icky)
388
+
389
+ // we're going to repeat our transforms but using the dnas generated in the previous steps.
390
+ // (kinda like translating a foreign language translation back into the
391
+ // original language and making sure it says the same thing)
392
+ // we should produce an entirely new ibGib, because the first newIbGib was timestamped.
393
+ // but the dna "produced" (recreated) should be exactly the same as our initial dna.
394
+
395
+ // timestamp should be different (at least 1 second) making newIbGib2 unique
396
+
397
+ // these are the vars from above.
398
+ // let addDna: IbGib_V1, dataAddedIbGib1: IbGib_V1,
399
+ // patchedDna: IbGib_V1, dataPatchedIbGib1: IbGib_V1,
400
+ // renamedDna: IbGib_V1, dataRenamedIbGib1: IbGib_V1,
401
+ // removedDna: IbGib_V1, dataRemovedIbGib1: IbGib_V1;
402
+ {
403
+ await delay(1010); // delay at least 1 second to guarantee UTC timestamp shows +1 second in timestamp
404
+ const optsClone: TransformOpts_Mut8<IbGib_V1> = clone(addDna!.data);
405
+ optsClone.src = src;
406
+ const { newIbGib: newIbGib2, dnas: dnas2 } = await mut8(optsClone);
407
+ expect(newIbGib2).withContext(`should be pure add mutation (expect delay)`).toBeTruthy();
408
+ expect(dnas2).withContext(`should be pure add mutation (expect delay)`).toBeTruthy();
409
+ const dna2 = dnas2![0];
410
+
411
+ // dna itself should be exactly the same
412
+ expect(dna2).withContext(`should be pure add mutation (expect delay)`).toEqual(addDna);
413
+
414
+ // the ibGibs **created** should NOT be the same because of timestamping
415
+ // (and later on, other factors would change like identity and other rel8ns)
416
+ expect(newIbGib2.gib).withContext(`should be pure add mutation (expect delay)`).not.toEqual(dataAddedIbGib1.gib);
417
+ }
418
+ {
419
+ await delay(500);
420
+ const optsClone: TransformOpts_Mut8<IbGib_V1> = clone(patchDna!.data);
421
+ optsClone.src = dataAddedIbGib1;
422
+ const { newIbGib: newIbGib2, dnas: dnas2 } = await mut8(optsClone);
423
+ expect(newIbGib2).withContext(`should be pure patch mutation (expect delay)`).toBeTruthy();
424
+ expect(dnas2).withContext(`should be pure patch mutation (expect delay)`).toBeTruthy();
425
+ const dna2 = dnas2![0];
426
+
427
+ // dna itself should be exactly the same
428
+ expect(dna2).withContext(`should be pure patch mutation (expect delay)`).toEqual(patchDna);
429
+
430
+ // the ibGibs **created** should NOT be the same because of timestamping
431
+ // (and later on, other factors would change like identity and other rel8ns)
432
+ expect(newIbGib2.gib).withContext(`should be pure patch mutation (expect delay)`).not.toEqual(dataPatchedIbGib1.gib);
433
+ }
434
+ {
435
+ await delay(500);
436
+ const optsClone: TransformOpts_Mut8<IbGib_V1> = clone(renameDna!.data);
437
+ optsClone.src = dataAddedIbGib1;
438
+ const { newIbGib: newIbGib2, dnas: dnas2 } = await mut8(optsClone);
439
+ expect(newIbGib2).withContext(`should be pure rename mutation (expect delay)`).toBeTruthy();
440
+ expect(dnas2).withContext(`should be pure rename mutation (expect delay)`).toBeTruthy();
441
+ const dna2 = dnas2![0];
442
+
443
+ // dna itself should be exactly the same
444
+ expect(dna2).withContext(`should be pure rename mutation (expect delay)`).toEqual(renameDna);
445
+
446
+ // the ibGibs **created** should NOT be the same because of timestamping
447
+ // (and later on, other factors would change like identity and other rel8ns)
448
+ expect(newIbGib2.gib).withContext(`should be pure rename mutation (expect delay)`).not.toEqual(dataRenamedIbGib1.gib);
449
+ }
450
+ {
451
+ await delay(500);
452
+ const optsClone: TransformOpts_Mut8<IbGib_V1> = clone(removeDna!.data);
453
+ optsClone.src = dataAddedIbGib1;
454
+ const { newIbGib: newIbGib2, dnas: dnas2 } = await mut8(optsClone);
455
+ expect(newIbGib2).withContext(`should be pure remove mutation (expect delay)`).toBeTruthy();
456
+ expect(dnas2).withContext(`should be pure remove mutation (expect delay)`).toBeTruthy();
457
+ const dna2 = dnas2![0];
458
+
459
+ // dna itself should be exactly the same
460
+ expect(dna2).withContext(`should be pure remove mutation (expect delay)`).toEqual(removeDna);
461
+
462
+ // the ibGibs **created** should NOT be the same because of timestamping
463
+ // (and later on, other factors would change like identity and other rel8ns)
464
+ expect(newIbGib2.gib).withContext(`should be pure remove mutation (expect delay)`).not.toEqual(dataRemovedIbGib1.gib);
465
+ }
466
+
467
+ // #endregion
468
+ });
469
+
470
+ }); // dna
471
+
472
+ describe(`with more complex data`, () => {
473
+ for (const primitive of PRIMITIVE_IBGIBS) {
474
+ it(`should add/rename/patch (${primitive.ib})`, async () => {
475
+
476
+ let { newIbGib: src } = await fork({ src: primitive, noTimestamp: true });
477
+
478
+ // #region add data
479
+
480
+ // we now have a non-primitive source and wish to
481
+ // add some internal data to it.
482
+
483
+ let { newIbGib: dataAddedIbGib } = await mut8({
484
+ src,
485
+ dataToAddOrPatch: DATA_COMPLEX_XY_NESTED,
486
+ noTimestamp: true
487
+ });
488
+ let dataAddedAddr: IbGibAddr = getIbGibAddr({ ibGib: dataAddedIbGib });
489
+ expect(dataAddedIbGib).toBeTruthy();
490
+ expect(dataAddedIbGib.data).toBeTruthy();
491
+ expect(dataAddedIbGib.data).toBeTruthy();
492
+ expect(dataAddedIbGib.data).toEqual(DATA_COMPLEX_XY_NESTED);
493
+
494
+ // #endregion
495
+
496
+ // #region rename
497
+
498
+ // since it's freezing already, I suppose today I'll have to code offline...or off stream that is.
499
+ const valueForX = dataAddedIbGib.data!.x;
500
+ const dataToRename = {
501
+ nested1: { [RENAME_ME_KEY]: RENAMED_KEY }
502
+ };
503
+
504
+ let { newIbGib: dataRenamedIbGib } =
505
+ await mut8({ src: dataAddedIbGib, dataToRename, noTimestamp: true });
506
+ // let dataRenamedAddr: IbGibAddr = getIbGibAddr({ibGib: dataRenamedIbGib});
507
+ expect(dataRenamedIbGib).toBeTruthy();
508
+ expect(dataRenamedIbGib.data).toBeTruthy();
509
+ expect(dataRenamedIbGib.data).toBeTruthy();
510
+
511
+ // the data should have the new key name that we did
512
+ expect(dataRenamedIbGib.data!.nested1).toBeTruthy();
513
+ expect(dataRenamedIbGib.data!.nested1![RENAME_ME_KEY]).toBeUndefined();
514
+ expect(dataRenamedIbGib.data!.nested1![RENAMED_KEY]).toBeTruthy();
515
+
516
+ // the data value for that new key name should be the same as before, since we didn't change it
517
+ expect(dataRenamedIbGib.data!.nested1![RENAMED_KEY]).toEqual(RENAME_VALUE);
518
+
519
+ // the most recent past of the dataRenamed ibGib should be the src's (dataAddedIbGib) address
520
+ expect(dataRenamedIbGib.rel8ns!.past![dataRenamedIbGib.rel8ns!.past!.length - 1]).toEqual(dataAddedAddr);
521
+
522
+ //#endregion
523
+
524
+ // #region patch
525
+
526
+ const newValueForX = 42;
527
+ const dataToPatch = { xyObj: { x: newValueForX } };
528
+
529
+ let { newIbGib: dataPatchedIbGib } =
530
+ await mut8({ src: dataAddedIbGib, dataToAddOrPatch: dataToPatch, noTimestamp: true });
531
+ expect(dataPatchedIbGib).toBeTruthy();
532
+ expect(dataPatchedIbGib.data).toBeTruthy();
533
+ expect(dataPatchedIbGib.data).toBeTruthy();
534
+ expect(dataPatchedIbGib.data!.xyObj).toBeTruthy();
535
+
536
+ // value should be changed to the new value
537
+ expect(dataPatchedIbGib.data!.xyObj!.x).toEqual(newValueForX);
538
+
539
+ // the most recent past of the dataRenamed ibGib should be the src's (dataAddedIbGib) address
540
+ expect(dataPatchedIbGib.rel8ns!.past![dataPatchedIbGib.rel8ns!.past!.length - 1]).toEqual(dataAddedAddr);
541
+
542
+ //#endregion
543
+
544
+ // #region remove
545
+
546
+ const dataToRemove = { xyObj: { x: '' } }; // just want mapping, value is ignored
547
+
548
+ let { newIbGib: dataRemovedIbGib } =
549
+ await mut8({ src: dataAddedIbGib, dataToRemove, noTimestamp: true });
550
+ expect(dataRemovedIbGib).toBeTruthy();
551
+ expect(dataRemovedIbGib.data).toBeTruthy();
552
+ expect(dataRemovedIbGib.data).toBeTruthy();
553
+
554
+ // the data value for that new key name should be the same as before, since we didn't change it
555
+ expect(dataRemovedIbGib.data!.xyObj).toBeTruthy();
556
+ expect(dataRemovedIbGib.data!.xyObj!.x).toBeUndefined();
557
+
558
+ // the most recent past of the dataRemovedIbGib should be the src's address
559
+ expect(dataRemovedIbGib.rel8ns!.past![dataRemovedIbGib.rel8ns!.past!.length - 1]).toEqual(dataAddedAddr);
560
+
561
+ //#endregion
562
+ });
563
+ }
564
+ })
565
+
566
+ describe(`nCounter`, () => {
567
+ it(`should increment existing n counter`, async () => {
568
+ for (const primitive of PRIMITIVE_IBGIBS) {
569
+ let { newIbGib: src_0 } =
570
+ await fork({ src: primitive, nCounter: true });
571
+
572
+ expect(src_0.data).toBeTruthy();
573
+ expect(src_0.data).toBeTruthy();
574
+ expect(src_0.data!.n).toEqual(0);
575
+
576
+ let { newIbGib: src_1 } =
577
+ await mut8({
578
+ src: src_0,
579
+ dataToAddOrPatch: DATA_SIMPLE_XY,
580
+ nCounter: true
581
+ });
582
+ expect(src_1.data).toBeTruthy();
583
+ expect(src_1.data).toBeTruthy();
584
+ expect(src_1.data!.n).toBeTruthy();
585
+ expect(src_1.data!.n).toBeTruthy();
586
+ expect(src_1.data!.n).toEqual(1);
587
+ }
588
+ });
589
+ it(`should start new n counter, implicit nCounter falsy`, async () => {
590
+ for (const primitive of PRIMITIVE_IBGIBS) {
591
+ let { newIbGib: src_0 } =
592
+ await fork({ src: primitive, noTimestamp: true });
593
+
594
+ expect(src_0.data).toBeUndefined();
595
+
596
+ let { newIbGib: src_1 } =
597
+ await mut8({
598
+ src: src_0,
599
+ dataToAddOrPatch: DATA_SIMPLE_XY,
600
+ nCounter: true
601
+ });
602
+ expect(src_1.data).toBeTruthy();
603
+ expect(src_1.data).toBeTruthy();
604
+ expect(src_1.data!.n).toEqual(0);
605
+ }
606
+ });
607
+ it(`should start new n counter, explicit nCounter falsy`, async () => {
608
+ for (const primitive of PRIMITIVE_IBGIBS) {
609
+ let { newIbGib: src_0 } =
610
+ await fork({ src: primitive, noTimestamp: true, nCounter: false });
611
+
612
+ expect(src_0.data).toBeUndefined();
613
+
614
+ let { newIbGib: src_1 } =
615
+ await mut8({
616
+ src: src_0,
617
+ dataToAddOrPatch: DATA_SIMPLE_XY,
618
+ nCounter: true
619
+ });
620
+ expect(src_1.data).toBeTruthy();
621
+ expect(src_1.data).toBeTruthy();
622
+ expect(src_1.data!.n).toEqual(0);
623
+ }
624
+ });
625
+ });
626
+
627
+ describe(`when mut8 with dna`, () => {
628
+ it(`should have gibs that is corroborated with getGib`, async () => {
629
+ for (const primitive of PRIMITIVE_IBGIBS) {
630
+ let { newIbGib, intermediateIbGibs, dnas } =
631
+ await fork({ src: primitive, nCounter: false, dna: true });
632
+
633
+ const ibGibs_fork = [newIbGib, ...(intermediateIbGibs ?? []), ...(dnas ?? [])];
634
+ // we now have a non-primitive 1st gen source and wish to
635
+ // add rel8ns in it pointing to other ibGibs.
636
+
637
+ let resMut8 =
638
+ await mut8({
639
+ src: newIbGib,
640
+ dataToAddOrPatch: DATA_COMPLEX_XY_NESTED,
641
+ noTimestamp: true,
642
+ nCounter: true,
643
+ dna: true,
644
+ });
645
+
646
+ const ibGibs_mut8 = [resMut8.newIbGib, ...(resMut8.intermediateIbGibs ?? []), ...(resMut8.dnas ?? [])];
647
+ const ibGibs = [...ibGibs_fork, ...ibGibs_mut8];
648
+ for (let i = 0; i < ibGibs.length; i++) {
649
+ const ibGib = ibGibs[i];
650
+ const gottenGib = await getGib({ ibGib });
651
+ expect(ibGib.gib).toEqual(gottenGib);
652
+ }
653
+ }
654
+ });
655
+ });
656
+ });