@livon/schema 0.27.0-rc.1

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 (169) hide show
  1. package/PROMPT.md +21 -0
  2. package/README.md +13 -0
  3. package/SCHEMA.md +13 -0
  4. package/dist/SchemaValidationError.cjs +41 -0
  5. package/dist/SchemaValidationError.d.ts +20 -0
  6. package/dist/SchemaValidationError.js +7 -0
  7. package/dist/SchemaValidationError.spec.cjs +65 -0
  8. package/dist/SchemaValidationError.spec.d.ts +1 -0
  9. package/dist/SchemaValidationError.spec.js +59 -0
  10. package/dist/after.cjs +36 -0
  11. package/dist/after.d.ts +30 -0
  12. package/dist/after.js +2 -0
  13. package/dist/after.spec.cjs +54 -0
  14. package/dist/after.spec.d.ts +1 -0
  15. package/dist/after.spec.js +48 -0
  16. package/dist/and.cjs +36 -0
  17. package/dist/and.d.ts +26 -0
  18. package/dist/and.js +2 -0
  19. package/dist/and.spec.cjs +57 -0
  20. package/dist/and.spec.d.ts +1 -0
  21. package/dist/and.spec.js +51 -0
  22. package/dist/api.cjs +317 -0
  23. package/dist/api.d.ts +107 -0
  24. package/dist/api.js +277 -0
  25. package/dist/api.spec.cjs +512 -0
  26. package/dist/api.spec.d.ts +1 -0
  27. package/dist/api.spec.js +506 -0
  28. package/dist/array.cjs +74 -0
  29. package/dist/array.d.ts +25 -0
  30. package/dist/array.js +40 -0
  31. package/dist/array.spec.cjs +167 -0
  32. package/dist/array.spec.d.ts +1 -0
  33. package/dist/array.spec.js +161 -0
  34. package/dist/before.cjs +36 -0
  35. package/dist/before.d.ts +30 -0
  36. package/dist/before.js +2 -0
  37. package/dist/before.spec.cjs +54 -0
  38. package/dist/before.spec.d.ts +1 -0
  39. package/dist/before.spec.js +48 -0
  40. package/dist/binary.cjs +53 -0
  41. package/dist/binary.d.ts +24 -0
  42. package/dist/binary.js +19 -0
  43. package/dist/binary.spec.cjs +107 -0
  44. package/dist/binary.spec.d.ts +1 -0
  45. package/dist/binary.spec.js +101 -0
  46. package/dist/boolean.cjs +53 -0
  47. package/dist/boolean.d.ts +24 -0
  48. package/dist/boolean.js +19 -0
  49. package/dist/boolean.spec.cjs +96 -0
  50. package/dist/boolean.spec.d.ts +1 -0
  51. package/dist/boolean.spec.js +90 -0
  52. package/dist/context.cjs +125 -0
  53. package/dist/context.d.ts +101 -0
  54. package/dist/context.js +76 -0
  55. package/dist/context.spec.cjs +244 -0
  56. package/dist/context.spec.d.ts +1 -0
  57. package/dist/context.spec.js +238 -0
  58. package/dist/date.cjs +53 -0
  59. package/dist/date.d.ts +24 -0
  60. package/dist/date.js +19 -0
  61. package/dist/date.spec.cjs +97 -0
  62. package/dist/date.spec.d.ts +1 -0
  63. package/dist/date.spec.js +91 -0
  64. package/dist/doc.cjs +54 -0
  65. package/dist/doc.d.ts +25 -0
  66. package/dist/doc.js +17 -0
  67. package/dist/doc.spec.cjs +99 -0
  68. package/dist/doc.spec.d.ts +1 -0
  69. package/dist/doc.spec.js +93 -0
  70. package/dist/enumeration.cjs +74 -0
  71. package/dist/enumeration.d.ts +50 -0
  72. package/dist/enumeration.js +40 -0
  73. package/dist/enumeration.spec.cjs +110 -0
  74. package/dist/enumeration.spec.d.ts +1 -0
  75. package/dist/enumeration.spec.js +104 -0
  76. package/dist/hydrate.cjs +18 -0
  77. package/dist/hydrate.d.ts +1 -0
  78. package/dist/hydrate.js +0 -0
  79. package/dist/index.cjs +145 -0
  80. package/dist/index.d.ts +34 -0
  81. package/dist/index.js +24 -0
  82. package/dist/index.spec.cjs +43 -0
  83. package/dist/index.spec.d.ts +1 -0
  84. package/dist/index.spec.js +37 -0
  85. package/dist/literal.cjs +55 -0
  86. package/dist/literal.d.ts +25 -0
  87. package/dist/literal.js +21 -0
  88. package/dist/literal.spec.cjs +93 -0
  89. package/dist/literal.spec.d.ts +1 -0
  90. package/dist/literal.spec.js +87 -0
  91. package/dist/number.cjs +89 -0
  92. package/dist/number.d.ts +84 -0
  93. package/dist/number.js +55 -0
  94. package/dist/number.spec.cjs +155 -0
  95. package/dist/number.spec.d.ts +1 -0
  96. package/dist/number.spec.js +149 -0
  97. package/dist/object.cjs +66 -0
  98. package/dist/object.d.ts +37 -0
  99. package/dist/object.js +32 -0
  100. package/dist/object.spec.cjs +171 -0
  101. package/dist/object.spec.d.ts +1 -0
  102. package/dist/object.spec.js +165 -0
  103. package/dist/operation.cjs +182 -0
  104. package/dist/operation.d.ts +197 -0
  105. package/dist/operation.js +133 -0
  106. package/dist/operation.spec.cjs +454 -0
  107. package/dist/operation.spec.d.ts +1 -0
  108. package/dist/operation.spec.js +448 -0
  109. package/dist/or.cjs +85 -0
  110. package/dist/or.d.ts +37 -0
  111. package/dist/or.js +51 -0
  112. package/dist/or.spec.cjs +204 -0
  113. package/dist/or.spec.d.ts +1 -0
  114. package/dist/or.spec.js +198 -0
  115. package/dist/schema.cjs +285 -0
  116. package/dist/schema.d.ts +132 -0
  117. package/dist/schema.js +233 -0
  118. package/dist/schema.spec.cjs +587 -0
  119. package/dist/schema.spec.d.ts +1 -0
  120. package/dist/schema.spec.js +581 -0
  121. package/dist/schemaFactory.cjs +125 -0
  122. package/dist/schemaFactory.d.ts +97 -0
  123. package/dist/schemaFactory.js +88 -0
  124. package/dist/schemaFactory.spec.cjs +197 -0
  125. package/dist/schemaFactory.spec.d.ts +1 -0
  126. package/dist/schemaFactory.spec.js +191 -0
  127. package/dist/schemaModule.cjs +280 -0
  128. package/dist/schemaModule.d.ts +97 -0
  129. package/dist/schemaModule.js +243 -0
  130. package/dist/schemaModule.spec.cjs +355 -0
  131. package/dist/schemaModule.spec.d.ts +1 -0
  132. package/dist/schemaModule.spec.js +349 -0
  133. package/dist/string.cjs +93 -0
  134. package/dist/string.d.ts +85 -0
  135. package/dist/string.js +59 -0
  136. package/dist/string.spec.cjs +158 -0
  137. package/dist/string.spec.d.ts +1 -0
  138. package/dist/string.spec.js +152 -0
  139. package/dist/testing/mocks/assertions.mock.cjs +48 -0
  140. package/dist/testing/mocks/assertions.mock.d.ts +5 -0
  141. package/dist/testing/mocks/assertions.mock.js +14 -0
  142. package/dist/testing/mocks/index.cjs +52 -0
  143. package/dist/testing/mocks/index.d.ts +4 -0
  144. package/dist/testing/mocks/index.js +3 -0
  145. package/dist/testing/mocks/schema.mock.cjs +120 -0
  146. package/dist/testing/mocks/schema.mock.d.ts +37 -0
  147. package/dist/testing/mocks/schema.mock.js +74 -0
  148. package/dist/tuple.cjs +58 -0
  149. package/dist/tuple.d.ts +33 -0
  150. package/dist/tuple.js +24 -0
  151. package/dist/tuple.spec.cjs +162 -0
  152. package/dist/tuple.spec.d.ts +1 -0
  153. package/dist/tuple.spec.js +156 -0
  154. package/dist/typeGuards.cjs +60 -0
  155. package/dist/typeGuards.d.ts +93 -0
  156. package/dist/typeGuards.js +8 -0
  157. package/dist/typeGuards.spec.cjs +101 -0
  158. package/dist/typeGuards.spec.d.ts +1 -0
  159. package/dist/typeGuards.spec.js +95 -0
  160. package/dist/types.cjs +18 -0
  161. package/dist/types.d.ts +289 -0
  162. package/dist/types.js +0 -0
  163. package/dist/union.cjs +74 -0
  164. package/dist/union.d.ts +33 -0
  165. package/dist/union.js +40 -0
  166. package/dist/union.spec.cjs +159 -0
  167. package/dist/union.spec.d.ts +1 -0
  168. package/dist/union.spec.js +153 -0
  169. package/package.json +47 -0
@@ -0,0 +1,101 @@
1
+ import { AstBuilder, SchemaBuildContext, SchemaBuildContextInput, SchemaRequestContext, SchemaRequestContextInput, SchemaState, SchemaContext } from './types.js';
2
+ export interface StateSnapshotRecord {
3
+ [key: string]: unknown;
4
+ }
5
+ export interface StateUpdater<T> {
6
+ (current: T | undefined): T;
7
+ }
8
+ export interface CreateBuilder {
9
+ (): AstBuilder;
10
+ }
11
+ export interface CreateState {
12
+ (): SchemaState;
13
+ }
14
+ /**
15
+ * createBuilder is part of the public LIVON API.
16
+ *
17
+ * @remarks
18
+ * Parameter and return types are defined in the TypeScript signature.
19
+ *
20
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/context
21
+ *
22
+ * @example
23
+ * const result = createBuilder(undefined as never);
24
+ */
25
+ export declare const createBuilder: CreateBuilder;
26
+ /**
27
+ * createState is part of the public LIVON API.
28
+ *
29
+ * @remarks
30
+ * Parameter and return types are defined in the TypeScript signature.
31
+ *
32
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/context
33
+ *
34
+ * @example
35
+ * const result = createState(undefined as never);
36
+ */
37
+ export declare const createState: CreateState;
38
+ export interface NormalizeBuildContext {
39
+ (input?: SchemaBuildContextInput): SchemaBuildContext;
40
+ }
41
+ /**
42
+ * normalizeBuildContext is part of the public LIVON API.
43
+ *
44
+ * @remarks
45
+ * Parameter and return types are defined in the TypeScript signature.
46
+ *
47
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/context
48
+ *
49
+ * @example
50
+ * const result = normalizeBuildContext(undefined as never);
51
+ */
52
+ export declare const normalizeBuildContext: NormalizeBuildContext;
53
+ export interface IsNormalizedRequest {
54
+ (input: SchemaRequestContextInput): input is SchemaRequestContext;
55
+ }
56
+ /**
57
+ * isNormalizedRequest is part of the public LIVON API.
58
+ *
59
+ * @remarks
60
+ * Parameter and return types are defined in the TypeScript signature.
61
+ *
62
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/context
63
+ *
64
+ * @example
65
+ * const result = isNormalizedRequest(undefined as never);
66
+ */
67
+ export declare const isNormalizedRequest: (input: SchemaRequestContextInput) => input is SchemaRequestContext;
68
+ export interface NormalizeRequestContext {
69
+ (input?: SchemaRequestContextInput): SchemaRequestContext;
70
+ }
71
+ /**
72
+ * normalizeRequestContext is part of the public LIVON API.
73
+ *
74
+ * @remarks
75
+ * Parameter and return types are defined in the TypeScript signature.
76
+ *
77
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/context
78
+ *
79
+ * @example
80
+ * const result = normalizeRequestContext(undefined as never);
81
+ */
82
+ export declare const normalizeRequestContext: NormalizeRequestContext;
83
+ export interface CreateSchemaContextInput {
84
+ build?: SchemaBuildContextInput;
85
+ request?: SchemaRequestContextInput;
86
+ }
87
+ export interface CreateSchemaContext {
88
+ (input?: CreateSchemaContextInput): SchemaContext;
89
+ }
90
+ /**
91
+ * createSchemaContext is part of the public LIVON API.
92
+ *
93
+ * @remarks
94
+ * Parameter and return types are defined in the TypeScript signature.
95
+ *
96
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/context
97
+ *
98
+ * @example
99
+ * const result = createSchemaContext(undefined as never);
100
+ */
101
+ export declare const createSchemaContext: CreateSchemaContext;
@@ -0,0 +1,76 @@
1
+ const createBuilder = ()=>{
2
+ const nodes = [];
3
+ return {
4
+ add: (node)=>{
5
+ nodes.push(node);
6
+ return node;
7
+ },
8
+ getAll: ()=>nodes
9
+ };
10
+ };
11
+ const createState = ()=>{
12
+ const store = new Map();
13
+ return {
14
+ get: (key)=>store.get(key),
15
+ set: (key, value)=>{
16
+ store.set(key, value);
17
+ },
18
+ update: (key, updater)=>{
19
+ const next = updater(store.get(key));
20
+ store.set(key, next);
21
+ },
22
+ snapshot: ()=>Array.from(store.entries()).reduce((acc, [key, value])=>{
23
+ acc[key] = value;
24
+ return acc;
25
+ }, {})
26
+ };
27
+ };
28
+ const normalizeBuildContext = (input = {})=>({
29
+ buildId: input.buildId ?? `${Date.now()}-${Math.random().toString(16).slice(2)}`,
30
+ builder: input.builder ?? createBuilder(),
31
+ parentNode: input.parentNode,
32
+ schemaPath: input.schemaPath ?? [],
33
+ buildOptions: input.buildOptions ?? {}
34
+ });
35
+ const isNormalizedRequest = (input)=>true === input.normalized;
36
+ const normalizeRequestContext = (input = {})=>{
37
+ if (isNormalizedRequest(input)) return input;
38
+ return {
39
+ normalized: true,
40
+ requestId: input.requestId ?? `${Date.now()}-${Math.random().toString(16).slice(2)}`,
41
+ timestamp: input.timestamp ?? Date.now(),
42
+ correlationId: input.correlationId,
43
+ sourceId: input.sourceId,
44
+ userId: input.userId,
45
+ tenantId: input.tenantId,
46
+ metadata: input.metadata,
47
+ state: createState(),
48
+ publisher: input.publisher,
49
+ onPublishError: input.onPublishError,
50
+ logger: input.logger
51
+ };
52
+ };
53
+ const createSchemaContext = (input = {})=>{
54
+ let buildContext = input.build ? normalizeBuildContext(input.build) : void 0;
55
+ let requestContext = input.request ? normalizeRequestContext(input.request) : void 0;
56
+ const state = requestContext?.state ?? createState();
57
+ return {
58
+ getBuildContext: ()=>buildContext,
59
+ setBuildContext: (next)=>{
60
+ buildContext = next;
61
+ },
62
+ getRequestContext: ()=>requestContext,
63
+ get request () {
64
+ return requestContext;
65
+ },
66
+ setRequestContext: (next)=>{
67
+ requestContext = next ? normalizeRequestContext(next) : void 0;
68
+ if (requestContext && requestContext.state !== state) requestContext = {
69
+ ...requestContext,
70
+ state
71
+ };
72
+ },
73
+ state
74
+ };
75
+ };
76
+ export { createBuilder, createSchemaContext, createState, isNormalizedRequest, normalizeBuildContext, normalizeRequestContext };
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ var __webpack_exports__ = {};
3
+ const external_vitest_namespaceObject = require("vitest");
4
+ const external_context_cjs_namespaceObject = require("./context.cjs");
5
+ (0, external_vitest_namespaceObject.describe)('context utilities', ()=>{
6
+ let dateNowSpy;
7
+ let randomSpy;
8
+ let buildInput;
9
+ let requestInput;
10
+ (0, external_vitest_namespaceObject.beforeAll)(()=>{
11
+ dateNowSpy = external_vitest_namespaceObject.vi.spyOn(Date, 'now');
12
+ randomSpy = external_vitest_namespaceObject.vi.spyOn(Math, 'random');
13
+ buildInput = {};
14
+ requestInput = {};
15
+ });
16
+ (0, external_vitest_namespaceObject.beforeEach)(()=>{
17
+ dateNowSpy.mockReturnValue(1700000000000);
18
+ randomSpy.mockReturnValue(0.25);
19
+ buildInput = {};
20
+ requestInput = {};
21
+ });
22
+ (0, external_vitest_namespaceObject.afterEach)(()=>{
23
+ external_vitest_namespaceObject.vi.clearAllMocks();
24
+ buildInput = {};
25
+ requestInput = {};
26
+ });
27
+ (0, external_vitest_namespaceObject.afterAll)(()=>{
28
+ dateNowSpy.mockRestore();
29
+ randomSpy.mockRestore();
30
+ });
31
+ (0, external_vitest_namespaceObject.describe)('createBuilder()', ()=>{
32
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
33
+ (0, external_vitest_namespaceObject.it)('should collect ast nodes when add is called', ()=>{
34
+ const builder = (0, external_context_cjs_namespaceObject.createBuilder)();
35
+ const node = {
36
+ type: 'string',
37
+ name: 'UserName'
38
+ };
39
+ const addedNode = builder.add(node);
40
+ const allNodes = builder.getAll();
41
+ (0, external_vitest_namespaceObject.expect)(addedNode).toBe(node);
42
+ (0, external_vitest_namespaceObject.expect)(allNodes).toEqual([
43
+ node
44
+ ]);
45
+ });
46
+ });
47
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
48
+ (0, external_vitest_namespaceObject.it)('should return empty list when no nodes are added', ()=>{
49
+ const builder = (0, external_context_cjs_namespaceObject.createBuilder)();
50
+ (0, external_vitest_namespaceObject.expect)(builder.getAll()).toEqual([]);
51
+ });
52
+ });
53
+ });
54
+ (0, external_vitest_namespaceObject.describe)('createState()', ()=>{
55
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
56
+ (0, external_vitest_namespaceObject.it)('should store and read value when set is called before get', ()=>{
57
+ const state = (0, external_context_cjs_namespaceObject.createState)();
58
+ state.set('userId', 'u-1');
59
+ (0, external_vitest_namespaceObject.expect)(state.get('userId')).toBe('u-1');
60
+ });
61
+ (0, external_vitest_namespaceObject.it)('should update value when update is called', ()=>{
62
+ const state = (0, external_context_cjs_namespaceObject.createState)();
63
+ state.set('counter', 1);
64
+ state.update('counter', (current)=>(current ?? 0) + 1);
65
+ (0, external_vitest_namespaceObject.expect)(state.get('counter')).toBe(2);
66
+ });
67
+ (0, external_vitest_namespaceObject.it)('should include all entries when snapshot is called', ()=>{
68
+ const state = (0, external_context_cjs_namespaceObject.createState)();
69
+ state.set('tenantId', 't-1');
70
+ state.set('isAdmin', true);
71
+ (0, external_vitest_namespaceObject.expect)(state.snapshot()).toEqual({
72
+ tenantId: 't-1',
73
+ isAdmin: true
74
+ });
75
+ });
76
+ });
77
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
78
+ (0, external_vitest_namespaceObject.it)('should provide undefined when key was never set', ()=>{
79
+ const state = (0, external_context_cjs_namespaceObject.createState)();
80
+ (0, external_vitest_namespaceObject.expect)(state.get('missing')).toBeUndefined();
81
+ });
82
+ (0, external_vitest_namespaceObject.it)('should pass undefined to updater when value is missing', ()=>{
83
+ const state = (0, external_context_cjs_namespaceObject.createState)();
84
+ const updater = external_vitest_namespaceObject.vi.fn((current)=>(current ?? 0) + 10);
85
+ state.update('counter', updater);
86
+ (0, external_vitest_namespaceObject.expect)(updater).toHaveBeenCalledWith(void 0);
87
+ (0, external_vitest_namespaceObject.expect)(state.get('counter')).toBe(10);
88
+ });
89
+ });
90
+ });
91
+ (0, external_vitest_namespaceObject.describe)('normalizeBuildContext()', ()=>{
92
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
93
+ (0, external_vitest_namespaceObject.it)('should create defaults when input is missing', ()=>{
94
+ const context = (0, external_context_cjs_namespaceObject.normalizeBuildContext)();
95
+ (0, external_vitest_namespaceObject.expect)(context.buildId).toBe('1700000000000-4');
96
+ (0, external_vitest_namespaceObject.expect)(context.schemaPath).toEqual([]);
97
+ (0, external_vitest_namespaceObject.expect)(context.buildOptions).toEqual({});
98
+ (0, external_vitest_namespaceObject.expect)(context.builder.getAll()).toEqual([]);
99
+ });
100
+ (0, external_vitest_namespaceObject.it)('should keep provided values when input provides them', ()=>{
101
+ const builder = (0, external_context_cjs_namespaceObject.createBuilder)();
102
+ buildInput = {
103
+ buildId: 'custom-build',
104
+ builder,
105
+ schemaPath: [
106
+ 'root'
107
+ ],
108
+ buildOptions: {
109
+ explain: true
110
+ }
111
+ };
112
+ const context = (0, external_context_cjs_namespaceObject.normalizeBuildContext)(buildInput);
113
+ (0, external_vitest_namespaceObject.expect)(context.buildId).toBe('custom-build');
114
+ (0, external_vitest_namespaceObject.expect)(context.builder).toBe(builder);
115
+ (0, external_vitest_namespaceObject.expect)(context.schemaPath).toEqual([
116
+ 'root'
117
+ ]);
118
+ (0, external_vitest_namespaceObject.expect)(context.buildOptions).toEqual({
119
+ explain: true
120
+ });
121
+ });
122
+ });
123
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
124
+ (0, external_vitest_namespaceObject.it)('should keep parent node undefined when parent node is omitted', ()=>{
125
+ const context = (0, external_context_cjs_namespaceObject.normalizeBuildContext)();
126
+ (0, external_vitest_namespaceObject.expect)(context.parentNode).toBeUndefined();
127
+ });
128
+ });
129
+ });
130
+ (0, external_vitest_namespaceObject.describe)('isNormalizedRequest()', ()=>{
131
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
132
+ (0, external_vitest_namespaceObject.it)('should return true when normalized flag is true', ()=>{
133
+ const input = {
134
+ normalized: true
135
+ };
136
+ (0, external_vitest_namespaceObject.expect)((0, external_context_cjs_namespaceObject.isNormalizedRequest)(input)).toBe(true);
137
+ });
138
+ });
139
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
140
+ (0, external_vitest_namespaceObject.it)('should return false when normalized flag is missing', ()=>{
141
+ (0, external_vitest_namespaceObject.expect)((0, external_context_cjs_namespaceObject.isNormalizedRequest)({})).toBe(false);
142
+ });
143
+ });
144
+ });
145
+ (0, external_vitest_namespaceObject.describe)('normalizeRequestContext()', ()=>{
146
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
147
+ (0, external_vitest_namespaceObject.it)('should create normalized context when input is plain request input', ()=>{
148
+ requestInput = {
149
+ correlationId: 'corr-1',
150
+ metadata: {
151
+ source: 'test'
152
+ }
153
+ };
154
+ const context = (0, external_context_cjs_namespaceObject.normalizeRequestContext)(requestInput);
155
+ (0, external_vitest_namespaceObject.expect)(context.normalized).toBe(true);
156
+ (0, external_vitest_namespaceObject.expect)(context.requestId).toBe('1700000000000-4');
157
+ (0, external_vitest_namespaceObject.expect)(context.timestamp).toBe(1700000000000);
158
+ (0, external_vitest_namespaceObject.expect)(context.correlationId).toBe('corr-1');
159
+ (0, external_vitest_namespaceObject.expect)(context.metadata).toEqual({
160
+ source: 'test'
161
+ });
162
+ });
163
+ (0, external_vitest_namespaceObject.it)('should return same instance when request is already normalized', ()=>{
164
+ const state = (0, external_context_cjs_namespaceObject.createState)();
165
+ const normalized = (0, external_context_cjs_namespaceObject.normalizeRequestContext)({
166
+ normalized: true,
167
+ requestId: 'already-normalized',
168
+ timestamp: 123,
169
+ state
170
+ });
171
+ const result = (0, external_context_cjs_namespaceObject.normalizeRequestContext)(normalized);
172
+ (0, external_vitest_namespaceObject.expect)(result).toBe(normalized);
173
+ });
174
+ });
175
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
176
+ (0, external_vitest_namespaceObject.it)('should create empty metadata when metadata is not provided', ()=>{
177
+ const context = (0, external_context_cjs_namespaceObject.normalizeRequestContext)({});
178
+ (0, external_vitest_namespaceObject.expect)(context.metadata).toBeUndefined();
179
+ });
180
+ });
181
+ });
182
+ (0, external_vitest_namespaceObject.describe)('createSchemaContext()', ()=>{
183
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
184
+ (0, external_vitest_namespaceObject.it)('should expose normalized build and request contexts when input is provided', ()=>{
185
+ const context = (0, external_context_cjs_namespaceObject.createSchemaContext)({
186
+ build: {
187
+ buildId: 'build-1'
188
+ },
189
+ request: {
190
+ requestId: 'req-1',
191
+ timestamp: 11,
192
+ normalized: true,
193
+ state: (0, external_context_cjs_namespaceObject.createState)()
194
+ }
195
+ });
196
+ (0, external_vitest_namespaceObject.expect)(context.getBuildContext()?.buildId).toBe('build-1');
197
+ (0, external_vitest_namespaceObject.expect)(context.getRequestContext()?.requestId).toBe('req-1');
198
+ });
199
+ (0, external_vitest_namespaceObject.it)('should keep shared state when replacing request context with different state instance', ()=>{
200
+ const initialState = (0, external_context_cjs_namespaceObject.createState)();
201
+ const nextState = (0, external_context_cjs_namespaceObject.createState)();
202
+ const context = (0, external_context_cjs_namespaceObject.createSchemaContext)({
203
+ request: {
204
+ normalized: true,
205
+ requestId: 'req-a',
206
+ timestamp: 1,
207
+ state: initialState
208
+ }
209
+ });
210
+ context.setRequestContext({
211
+ normalized: true,
212
+ requestId: 'req-b',
213
+ timestamp: 2,
214
+ state: nextState
215
+ });
216
+ (0, external_vitest_namespaceObject.expect)(context.getRequestContext()?.requestId).toBe('req-b');
217
+ (0, external_vitest_namespaceObject.expect)(context.getRequestContext()?.state).toBe(initialState);
218
+ });
219
+ (0, external_vitest_namespaceObject.it)('should update build context when setBuildContext is called', ()=>{
220
+ const context = (0, external_context_cjs_namespaceObject.createSchemaContext)();
221
+ context.setBuildContext((0, external_context_cjs_namespaceObject.normalizeBuildContext)({
222
+ buildId: 'updated-build'
223
+ }));
224
+ (0, external_vitest_namespaceObject.expect)(context.getBuildContext()?.buildId).toBe('updated-build');
225
+ });
226
+ });
227
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
228
+ (0, external_vitest_namespaceObject.it)('should clear request context when setRequestContext receives undefined', ()=>{
229
+ const context = (0, external_context_cjs_namespaceObject.createSchemaContext)({
230
+ request: {
231
+ requestId: 'req-1',
232
+ timestamp: 11
233
+ }
234
+ });
235
+ context.setRequestContext(void 0);
236
+ (0, external_vitest_namespaceObject.expect)(context.getRequestContext()).toBeUndefined();
237
+ });
238
+ });
239
+ });
240
+ });
241
+ for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
242
+ Object.defineProperty(exports, '__esModule', {
243
+ value: true
244
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,238 @@
1
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { createBuilder, createSchemaContext, createState, isNormalizedRequest, normalizeBuildContext, normalizeRequestContext } from "./context.js";
3
+ describe('context utilities', ()=>{
4
+ let dateNowSpy;
5
+ let randomSpy;
6
+ let buildInput;
7
+ let requestInput;
8
+ beforeAll(()=>{
9
+ dateNowSpy = vi.spyOn(Date, 'now');
10
+ randomSpy = vi.spyOn(Math, 'random');
11
+ buildInput = {};
12
+ requestInput = {};
13
+ });
14
+ beforeEach(()=>{
15
+ dateNowSpy.mockReturnValue(1700000000000);
16
+ randomSpy.mockReturnValue(0.25);
17
+ buildInput = {};
18
+ requestInput = {};
19
+ });
20
+ afterEach(()=>{
21
+ vi.clearAllMocks();
22
+ buildInput = {};
23
+ requestInput = {};
24
+ });
25
+ afterAll(()=>{
26
+ dateNowSpy.mockRestore();
27
+ randomSpy.mockRestore();
28
+ });
29
+ describe('createBuilder()', ()=>{
30
+ describe('happy', ()=>{
31
+ it('should collect ast nodes when add is called', ()=>{
32
+ const builder = createBuilder();
33
+ const node = {
34
+ type: 'string',
35
+ name: 'UserName'
36
+ };
37
+ const addedNode = builder.add(node);
38
+ const allNodes = builder.getAll();
39
+ expect(addedNode).toBe(node);
40
+ expect(allNodes).toEqual([
41
+ node
42
+ ]);
43
+ });
44
+ });
45
+ describe('sad', ()=>{
46
+ it('should return empty list when no nodes are added', ()=>{
47
+ const builder = createBuilder();
48
+ expect(builder.getAll()).toEqual([]);
49
+ });
50
+ });
51
+ });
52
+ describe('createState()', ()=>{
53
+ describe('happy', ()=>{
54
+ it('should store and read value when set is called before get', ()=>{
55
+ const state = createState();
56
+ state.set('userId', 'u-1');
57
+ expect(state.get('userId')).toBe('u-1');
58
+ });
59
+ it('should update value when update is called', ()=>{
60
+ const state = createState();
61
+ state.set('counter', 1);
62
+ state.update('counter', (current)=>(current ?? 0) + 1);
63
+ expect(state.get('counter')).toBe(2);
64
+ });
65
+ it('should include all entries when snapshot is called', ()=>{
66
+ const state = createState();
67
+ state.set('tenantId', 't-1');
68
+ state.set('isAdmin', true);
69
+ expect(state.snapshot()).toEqual({
70
+ tenantId: 't-1',
71
+ isAdmin: true
72
+ });
73
+ });
74
+ });
75
+ describe('sad', ()=>{
76
+ it('should provide undefined when key was never set', ()=>{
77
+ const state = createState();
78
+ expect(state.get('missing')).toBeUndefined();
79
+ });
80
+ it('should pass undefined to updater when value is missing', ()=>{
81
+ const state = createState();
82
+ const updater = vi.fn((current)=>(current ?? 0) + 10);
83
+ state.update('counter', updater);
84
+ expect(updater).toHaveBeenCalledWith(void 0);
85
+ expect(state.get('counter')).toBe(10);
86
+ });
87
+ });
88
+ });
89
+ describe('normalizeBuildContext()', ()=>{
90
+ describe('happy', ()=>{
91
+ it('should create defaults when input is missing', ()=>{
92
+ const context = normalizeBuildContext();
93
+ expect(context.buildId).toBe('1700000000000-4');
94
+ expect(context.schemaPath).toEqual([]);
95
+ expect(context.buildOptions).toEqual({});
96
+ expect(context.builder.getAll()).toEqual([]);
97
+ });
98
+ it('should keep provided values when input provides them', ()=>{
99
+ const builder = createBuilder();
100
+ buildInput = {
101
+ buildId: 'custom-build',
102
+ builder,
103
+ schemaPath: [
104
+ 'root'
105
+ ],
106
+ buildOptions: {
107
+ explain: true
108
+ }
109
+ };
110
+ const context = normalizeBuildContext(buildInput);
111
+ expect(context.buildId).toBe('custom-build');
112
+ expect(context.builder).toBe(builder);
113
+ expect(context.schemaPath).toEqual([
114
+ 'root'
115
+ ]);
116
+ expect(context.buildOptions).toEqual({
117
+ explain: true
118
+ });
119
+ });
120
+ });
121
+ describe('sad', ()=>{
122
+ it('should keep parent node undefined when parent node is omitted', ()=>{
123
+ const context = normalizeBuildContext();
124
+ expect(context.parentNode).toBeUndefined();
125
+ });
126
+ });
127
+ });
128
+ describe('isNormalizedRequest()', ()=>{
129
+ describe('happy', ()=>{
130
+ it('should return true when normalized flag is true', ()=>{
131
+ const input = {
132
+ normalized: true
133
+ };
134
+ expect(isNormalizedRequest(input)).toBe(true);
135
+ });
136
+ });
137
+ describe('sad', ()=>{
138
+ it('should return false when normalized flag is missing', ()=>{
139
+ expect(isNormalizedRequest({})).toBe(false);
140
+ });
141
+ });
142
+ });
143
+ describe('normalizeRequestContext()', ()=>{
144
+ describe('happy', ()=>{
145
+ it('should create normalized context when input is plain request input', ()=>{
146
+ requestInput = {
147
+ correlationId: 'corr-1',
148
+ metadata: {
149
+ source: 'test'
150
+ }
151
+ };
152
+ const context = normalizeRequestContext(requestInput);
153
+ expect(context.normalized).toBe(true);
154
+ expect(context.requestId).toBe('1700000000000-4');
155
+ expect(context.timestamp).toBe(1700000000000);
156
+ expect(context.correlationId).toBe('corr-1');
157
+ expect(context.metadata).toEqual({
158
+ source: 'test'
159
+ });
160
+ });
161
+ it('should return same instance when request is already normalized', ()=>{
162
+ const state = createState();
163
+ const normalized = normalizeRequestContext({
164
+ normalized: true,
165
+ requestId: 'already-normalized',
166
+ timestamp: 123,
167
+ state
168
+ });
169
+ const result = normalizeRequestContext(normalized);
170
+ expect(result).toBe(normalized);
171
+ });
172
+ });
173
+ describe('sad', ()=>{
174
+ it('should create empty metadata when metadata is not provided', ()=>{
175
+ const context = normalizeRequestContext({});
176
+ expect(context.metadata).toBeUndefined();
177
+ });
178
+ });
179
+ });
180
+ describe('createSchemaContext()', ()=>{
181
+ describe('happy', ()=>{
182
+ it('should expose normalized build and request contexts when input is provided', ()=>{
183
+ const context = createSchemaContext({
184
+ build: {
185
+ buildId: 'build-1'
186
+ },
187
+ request: {
188
+ requestId: 'req-1',
189
+ timestamp: 11,
190
+ normalized: true,
191
+ state: createState()
192
+ }
193
+ });
194
+ expect(context.getBuildContext()?.buildId).toBe('build-1');
195
+ expect(context.getRequestContext()?.requestId).toBe('req-1');
196
+ });
197
+ it('should keep shared state when replacing request context with different state instance', ()=>{
198
+ const initialState = createState();
199
+ const nextState = createState();
200
+ const context = createSchemaContext({
201
+ request: {
202
+ normalized: true,
203
+ requestId: 'req-a',
204
+ timestamp: 1,
205
+ state: initialState
206
+ }
207
+ });
208
+ context.setRequestContext({
209
+ normalized: true,
210
+ requestId: 'req-b',
211
+ timestamp: 2,
212
+ state: nextState
213
+ });
214
+ expect(context.getRequestContext()?.requestId).toBe('req-b');
215
+ expect(context.getRequestContext()?.state).toBe(initialState);
216
+ });
217
+ it('should update build context when setBuildContext is called', ()=>{
218
+ const context = createSchemaContext();
219
+ context.setBuildContext(normalizeBuildContext({
220
+ buildId: 'updated-build'
221
+ }));
222
+ expect(context.getBuildContext()?.buildId).toBe('updated-build');
223
+ });
224
+ });
225
+ describe('sad', ()=>{
226
+ it('should clear request context when setRequestContext receives undefined', ()=>{
227
+ const context = createSchemaContext({
228
+ request: {
229
+ requestId: 'req-1',
230
+ timestamp: 11
231
+ }
232
+ });
233
+ context.setRequestContext(void 0);
234
+ expect(context.getRequestContext()).toBeUndefined();
235
+ });
236
+ });
237
+ });
238
+ });