@ibgib/core-gib 0.0.5 → 0.0.7

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 (44) hide show
  1. package/README.md +10 -2
  2. package/dist/core-helper.mjs +4 -4
  3. package/dist/core-helper.mjs.map +1 -1
  4. package/dist/witness/app/app-base-v1.d.mts.map +1 -1
  5. package/dist/witness/app/app-base-v1.mjs +1 -1
  6. package/dist/witness/app/app-base-v1.mjs.map +1 -1
  7. package/dist/witness/app/app-helper.d.mts.map +1 -1
  8. package/dist/witness/app/app-helper.mjs +1 -1
  9. package/dist/witness/app/app-helper.mjs.map +1 -1
  10. package/dist/witness/robbot/robbot-base-v1.d.mts.map +1 -1
  11. package/dist/witness/robbot/robbot-base-v1.mjs +1 -1
  12. package/dist/witness/robbot/robbot-base-v1.mjs.map +1 -1
  13. package/dist/witness/robbot/robbot-helper.d.mts.map +1 -1
  14. package/dist/witness/robbot/robbot-helper.mjs +1 -1
  15. package/dist/witness/robbot/robbot-helper.mjs.map +1 -1
  16. package/dist/witness/space/inner-space-v1.d.mts +8 -6
  17. package/dist/witness/space/inner-space-v1.d.mts.map +1 -1
  18. package/dist/witness/space/inner-space-v1.mjs +34 -49
  19. package/dist/witness/space/inner-space-v1.mjs.map +1 -1
  20. package/dist/witness/space/space-base-v1.d.mts.map +1 -1
  21. package/dist/witness/space/space-base-v1.mjs +2 -1
  22. package/dist/witness/space/space-base-v1.mjs.map +1 -1
  23. package/dist/witness/space/space-helper.d.mts +26 -3
  24. package/dist/witness/space/space-helper.d.mts.map +1 -1
  25. package/dist/witness/space/space-helper.mjs +23 -6
  26. package/dist/witness/space/space-helper.mjs.map +1 -1
  27. package/dist/witness/witness-base-v1.d.mts +11 -0
  28. package/dist/witness/witness-base-v1.d.mts.map +1 -1
  29. package/dist/witness/witness-base-v1.mjs +9 -1
  30. package/dist/witness/witness-base-v1.mjs.map +1 -1
  31. package/package.json +2 -2
  32. package/src/core-helper.mts +3 -3
  33. package/src/{helper.spec.mts → core-helper.spec.mts} +7 -7
  34. package/src/witness/app/app-base-v1.mts +1 -1
  35. package/src/witness/app/app-helper.mts +1 -1
  36. package/src/witness/robbot/robbot-base-v1.mts +1 -1
  37. package/src/witness/robbot/robbot-helper.mts +1 -1
  38. package/src/witness/robbot/robbot-helper.spec.mts +112 -113
  39. package/src/witness/space/inner-space-v1.mts +29 -48
  40. package/src/witness/space/inner-space-v1.spec.mts +60 -0
  41. package/src/witness/space/space-base-v1.mts +2 -1
  42. package/src/witness/space/space-helper.mts +41 -9
  43. package/src/witness/space/space-spec-helper.spec.mts +232 -0
  44. package/src/witness/witness-base-v1.mts +17 -0
@@ -0,0 +1,232 @@
1
+ import { IbGibAddr, getIbGibAddr } from "@ibgib/ts-gib";
2
+ import { IbGib_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs";
3
+ import { Factory_V1 as factory } from '@ibgib/ts-gib/dist/V1/factory.mjs';
4
+
5
+ import { deleteFromSpace, getFromSpace, parseSpaceIb, persistTransformResult, putInSpace } from "./space-helper.mjs";
6
+ import { IbGibSpaceAny } from "./space-base-v1.mjs";
7
+
8
+ export interface CommonSpaceSpecOptions {
9
+ /** @optional additional description label for testing */
10
+ desc?: string,
11
+ /**
12
+ * expression to get the initial data when constructing the space
13
+ */
14
+ fnGetInitialData: () => Promise<any | undefined>,
15
+ /**
16
+ * expression to get the initial rel8ns when constructing the space
17
+ */
18
+ fnGetInitialRel8ns: () => Promise<any | undefined>,
19
+ /**
20
+ * expression to get the space, used in a `beforeEach` call
21
+ * @param initialData passed into ctor of space
22
+ * @param initialRel8ns passed into ctor of space
23
+ * @returns space instance
24
+ */
25
+ fnGetSpace: (initialData: any, initialRel8ns: any) => Promise<IbGibSpaceAny>,
26
+ /**
27
+ * if true, will use `fit` to short-circuit testing.
28
+ */
29
+ useFit?: boolean,
30
+ }
31
+
32
+ /**
33
+ * Executes tests related to:
34
+ *
35
+ * space creation
36
+ * `await space.initialized`
37
+ * space ib related (`getSpaceIb` & `parseSpaceIb`)
38
+ *
39
+ * run tests on an ibgib space that are common to all/most spaces.
40
+ *
41
+ * So, we can run these against any space, inner, outer, local ionic/filesystem based, etc.
42
+ */
43
+ export function testSpace_createAndInit({
44
+ desc,
45
+ fnGetInitialData,
46
+ fnGetInitialRel8ns,
47
+ fnGetSpace,
48
+ useFit,
49
+ }: CommonSpaceSpecOptions): Promise<void> {
50
+ const logContext = `[${testSpace_PutGetDelete.name}]${desc ?? ''}`;
51
+
52
+ return new Promise<void>((resolve) => {
53
+ let initialData: any | undefined;
54
+ let initialRel8ns: any | undefined;
55
+ let space: IbGibSpaceAny;
56
+
57
+ beforeEach(async () => {
58
+ initialData = await fnGetInitialData();
59
+ initialRel8ns = await fnGetInitialRel8ns();
60
+ });
61
+
62
+ const fnIt: any = useFit ? fit : it;
63
+
64
+ fnIt(`when created and initialized, space should be initialized`, async () => {
65
+ space = await fnGetSpace(initialData, initialRel8ns);
66
+ await space.initialized;
67
+
68
+ expect(space).withContext('space').toBeTruthy();
69
+ expect(space.ib).withContext('ib').toBeTruthy();
70
+ expect(space.gib).withContext('gib').toBeTruthy();
71
+ if (initialData) {
72
+ expect(space.data).withContext('data, initialData truthy').toBeTruthy();
73
+ } else {
74
+ expect(space.rel8ns).withContext('data, initialData falsy').toBeUndefined();
75
+ }
76
+ if (initialRel8ns) {
77
+ expect(space.rel8ns).withContext('rel8ns, initialRel8ns truthy').toBeTruthy();
78
+ } else {
79
+ expect(space.rel8ns).withContext('rel8ns, initialRel8ns falsy').toBeUndefined();
80
+ }
81
+ });
82
+
83
+ fnIt(`space.ib should contain correct info per initialData`, async () => {
84
+ space = await fnGetSpace(initialData, initialRel8ns);
85
+ await space.initialized;
86
+
87
+ expect(space.ib).toContain(initialData.name);
88
+ expect(space.ib).toContain(initialData.uuid);
89
+
90
+ let { spaceClassname, spaceId, spaceName, } = parseSpaceIb({ spaceIb: space.ib });
91
+ expect(space.data?.classname).withContext('space.data?.classname').toEqual(spaceClassname);
92
+ expect(space.data?.name).withContext('space.data?.name, initial').toEqual(initialData.name);
93
+ expect(space.data?.name).withContext('space.data?.name, spaceName').toEqual(spaceName);
94
+ expect(space.data?.uuid).withContext('space.data?.uuid, initial').toEqual(initialData.uuid);
95
+ expect(space.data?.uuid).withContext('space.data?.uuid, spaceId').toEqual(spaceId);
96
+ });
97
+
98
+ resolve();
99
+ });
100
+ }
101
+
102
+ /**
103
+ * Executes tests related to:
104
+ *
105
+ * `putInSpace`
106
+ * `getFromSpace`
107
+ * `deleteFromSpace`
108
+ *
109
+ * run tests on an ibgib space that are common to all/most spaces.
110
+ *
111
+ * So, we can run these against any space, inner, outer, local ionic/filesystem based, etc.
112
+ */
113
+ export function testSpace_PutGetDelete({
114
+ desc,
115
+ fnGetInitialData,
116
+ fnGetInitialRel8ns,
117
+ fnGetSpace,
118
+ useFit,
119
+ }: CommonSpaceSpecOptions): Promise<void> {
120
+ const logContext = `[${testSpace_PutGetDelete.name}]${desc ?? ''}`;
121
+
122
+ return new Promise<void>((resolve) => {
123
+ let initialData: any | undefined;
124
+ let initialRel8ns: any | undefined;
125
+ let space: IbGibSpaceAny;
126
+
127
+ beforeEach(async () => {
128
+ initialData = await fnGetInitialData();
129
+ initialRel8ns = await fnGetInitialRel8ns();
130
+ space = await fnGetSpace(initialData, initialRel8ns);
131
+ await space.initialized;
132
+ });
133
+
134
+ const fnIt: any = useFit ? fit : it;
135
+
136
+ fnIt(`${logContext}, should put/get/delete`, async () => {
137
+ let newIbGib: IbGib_V1 = {
138
+ ib: 'test ib',
139
+ data: { x: 1 },
140
+ rel8ns: { ancestor: ['test ib^gib'] }
141
+ }
142
+ let newIbGibAddr: IbGibAddr = getIbGibAddr({ ibGib: newIbGib });
143
+
144
+ // put
145
+ let resPut = await putInSpace({ ibGib: newIbGib, space });
146
+ expect(resPut).withContext('resPut').toBeTruthy();
147
+ expect(resPut.success).toBeTrue();
148
+
149
+ // get
150
+ let resGet = await getFromSpace({ addr: newIbGibAddr, space });
151
+ expect(resGet).withContext('resGet').toBeTruthy();
152
+ expect(resGet.success).withContext('resGet').toBeTrue();
153
+ expect(resGet.errorMsg).withContext('resGet').toBeFalsy();
154
+ expect(resGet.ibGibs).withContext('resGet').toBeTruthy();
155
+ expect(resGet.ibGibs!.length).withContext('resGet').toEqual(1);
156
+ expect(resGet.ibGibs![0]).withContext('resGet').toEqual(newIbGib);
157
+
158
+ // delete
159
+ await deleteFromSpace({ addr: newIbGibAddr, space });
160
+ resGet = await getFromSpace({ addr: newIbGibAddr, space });
161
+ expect(resGet).withContext('resGet after delete').toBeTruthy();
162
+ expect(resGet.success).withContext('resGet after delete').toBeFalse();
163
+ expect(resGet.errorMsg).withContext('resGet after delete').toBeTruthy();
164
+ expect(resGet.ibGibs).withContext('resGet after delete').toBeUndefined();
165
+ });
166
+
167
+ resolve();
168
+ });
169
+ }
170
+
171
+ /**
172
+ * Executes tests related to:
173
+ *
174
+ * `persistTransformResults`
175
+ *
176
+ * run tests on an ibgib space that are common to all/most spaces.
177
+ *
178
+ * So, we can run these against any space, inner, outer, local ionic/filesystem based, etc.
179
+ */
180
+ export function testSpace_persistTransformResult({
181
+ desc,
182
+ fnGetInitialData,
183
+ fnGetInitialRel8ns,
184
+ fnGetSpace,
185
+ useFit,
186
+ }: CommonSpaceSpecOptions): Promise<void> {
187
+ const logContext = `[${testSpace_persistTransformResult.name}]${desc ?? ''}`;
188
+
189
+ return new Promise<void>((resolve) => {
190
+ let initialData: any | undefined;
191
+ let initialRel8ns: any | undefined;
192
+ let space: IbGibSpaceAny;
193
+
194
+ beforeEach(async () => {
195
+ initialData = await fnGetInitialData();
196
+ initialRel8ns = await fnGetInitialRel8ns();
197
+ space = await fnGetSpace(initialData, initialRel8ns);
198
+ await space.initialized;
199
+ });
200
+
201
+ const fnIt: any = useFit ? fit : it;
202
+
203
+ fnIt(`${logContext} should persist all ibgibs (new, dna, intermediate)`, async () => {
204
+ let resNew = await factory.firstGen({
205
+ ib: 'test ib',
206
+ parentIbGib: factory.primitive({ ib: 'test ib' }),
207
+ dna: true,
208
+ noTimestamp: true,
209
+ });
210
+ let { newIbGib } = resNew;
211
+
212
+ await persistTransformResult({ resTransform: resNew, space });
213
+
214
+ const allIbGibs = [
215
+ newIbGib,
216
+ ...(resNew.dnas ?? []),
217
+ ...(resNew.intermediateIbGibs ?? []),
218
+ ];
219
+ const allAddrs = allIbGibs.map(x => getIbGibAddr({ ibGib: x }));
220
+
221
+ let resGet = await getFromSpace({ addrs: allAddrs, space });
222
+
223
+ expect(resGet).withContext('resGet').toBeTruthy();
224
+ expect(resGet.success).toBeTrue();
225
+ expect(resGet.ibGibs).toBeTruthy();
226
+ expect(resGet.ibGibs!.length).toEqual(allAddrs.length);
227
+ expect(resGet.ibGibs![0]).toEqual(newIbGib);
228
+ });
229
+
230
+ resolve();
231
+ });
232
+ }
@@ -117,9 +117,25 @@ export abstract class WitnessBase_V1<
117
117
 
118
118
  // #endregion IbGib interface fields: ib, gib, data, rel8ns
119
119
 
120
+ /**
121
+ * await this to ensure the witness is ready.
122
+ *
123
+ * This is called in the base `Witness.witness` function before `witnessImpl`.
124
+ */
125
+ initialized: Promise<void> | undefined;
126
+
120
127
  constructor(initialData?: TData, initialRel8ns?: TRel8ns) {
121
128
  if (initialData) { this.data = initialData; }
122
129
  if (initialRel8ns) { this.rel8ns = initialRel8ns; }
130
+ this.initialized = this.initialize();
131
+ }
132
+
133
+ /**
134
+ * by default, simply returns true atow in base class.
135
+ * override this to perform code before any other code is executed.
136
+ */
137
+ protected initialize(): Promise<void> {
138
+ return Promise.resolve();
123
139
  }
124
140
 
125
141
  /**
@@ -214,6 +230,7 @@ export abstract class WitnessBase_V1<
214
230
  async witness(arg: TOptionsIbGib): Promise<TResultIbGib | undefined> {
215
231
  const lc = `${this.lc}[${this.witness.name}]`;
216
232
  try {
233
+ await this.initialized;
217
234
  if (!this.gib) { this.gib = await sha256v1(this.toIbGibDto()); }
218
235
  const validationErrors_this = await this.validateThis();
219
236
  if (validationErrors_this?.length > 0) {