-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploymentExtracts.test.ts
More file actions
205 lines (191 loc) · 7.23 KB
/
Copy pathdeploymentExtracts.test.ts
File metadata and controls
205 lines (191 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { describe, expect, test } from 'vitest';
import { computeDeploymentExtracts } from '../../configs/camunda-oca/codegen/playwright/roles/deploymentGateway/hook.ts';
import type { OperationNode } from '../../path-analyser/src/types.ts';
function makeOp(
leaves: Array<{ semantic: string; fieldPath: string; status: string; provider: boolean }>,
): OperationNode {
return {
operationId: 'createDeployment',
method: 'POST',
path: '/deployments',
requires: { required: [], optional: [] },
produces: [],
responseSemanticLeaves: leaves,
};
}
describe('computeDeploymentExtracts', () => {
test('returns [] when op is undefined', () => {
expect(computeDeploymentExtracts(undefined)).toEqual([]);
});
test('returns [] when op has no responseSemanticLeaves', () => {
const op = makeOp([]);
expect(computeDeploymentExtracts(op)).toEqual([]);
});
test('keeps leaves where provider === true', () => {
const op = makeOp([
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].processDefinitionKey',
status: '200',
provider: true,
},
]);
const result = computeDeploymentExtracts(op);
expect(result).toHaveLength(1);
expect(result[0].varName).toBe('processDefinitionKeyVar');
expect(result[0].segments).toEqual(['deployments', 0, 'processDefinitionKey']);
});
test('keeps top-level leaves (no . or [ in fieldPath) even when provider === false', () => {
const op = makeOp([
{ semantic: 'DeploymentKey', fieldPath: 'deploymentKey', status: '200', provider: false },
{ semantic: 'TenantId', fieldPath: 'tenantId', status: '200', provider: false },
]);
const result = computeDeploymentExtracts(op);
expect(result.map((r) => r.varName).sort()).toEqual(['deploymentKeyVar', 'tenantIdVar'].sort());
});
test('skips non-top-level leaves where provider === false', () => {
const op = makeOp([
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].processDefinitionKey',
status: '200',
provider: false,
},
]);
expect(computeDeploymentExtracts(op)).toEqual([]);
});
test('skips leaves with .resource. in the fieldPath', () => {
const op = makeOp([
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].resource.processDefinitionKey',
status: '200',
provider: true,
},
]);
expect(computeDeploymentExtracts(op)).toEqual([]);
});
test('skips leaves where status !== "200"', () => {
const op = makeOp([
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].processDefinitionKey',
status: '400',
provider: true,
},
]);
expect(computeDeploymentExtracts(op)).toEqual([]);
});
test('converts [] array markers to index 0 in segments', () => {
const op = makeOp([
{ semantic: 'Foo', fieldPath: 'items[].value', status: '200', provider: true },
]);
const result = computeDeploymentExtracts(op);
expect(result[0].segments).toEqual(['items', 0, 'value']);
});
test('deduplicates by varName, preferring the shorter segments path', () => {
// Two leaves produce the same varName. The pre-dedup sort prefers
// the shorter segments path first, so that one should be kept.
const op = makeOp([
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].processDefinitionKey',
status: '200',
provider: true,
},
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'zzz[].processDefinitionKey',
status: '200',
provider: true,
},
]);
const result = computeDeploymentExtracts(op);
expect(result).toHaveLength(1);
// Same length → lexicographic JSON tie-break picks `deployments` < `zzz`.
expect(result[0].segments[0]).toBe('deployments');
});
test('dedup prefers a strictly shorter path even when JSON.stringify ordering would invert it', () => {
// `["a",0,"b"]` < `["a",0]` lexicographically (because `,` < `]`), so
// an explicit length comparison is required for "shorter path wins"
// to hold. Use leaves whose varName collides but paths differ in
// length: provider-true ensures both survive the filter.
const op = makeOp([
{
semantic: 'ProcessDefinitionKey',
// top-level (after stripping resource. prefix is N/A here)
fieldPath: 'a.processDefinitionKey',
status: '200',
provider: true,
},
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'a[].deeper.processDefinitionKey',
status: '200',
provider: true,
},
]);
const result = computeDeploymentExtracts(op);
expect(result).toHaveLength(1);
// The 2-segment path must win over the 4-segment path even though
// ["a",0,"deeper","processDefinitionKey"] < ["a","processDefinitionKey"]
// lexicographically (because `,` < `]`).
expect(result[0].segments).toEqual(['a', 'processDefinitionKey']);
});
test('final result is sorted by varName for byte-stable output', () => {
const op = makeOp([
{ semantic: 'ZebraVar', fieldPath: 'zebraVar', status: '200', provider: false },
{ semantic: 'AlphaKey', fieldPath: 'alphaKey', status: '200', provider: false },
{ semantic: 'MidField', fieldPath: 'midField', status: '200', provider: false },
]);
const result = computeDeploymentExtracts(op);
const names = result.map((r) => r.varName);
expect(names).toEqual([...names].sort());
});
test('handles op with no responseSemanticLeaves property', () => {
const op: OperationNode = {
operationId: 'createDeployment',
method: 'POST',
path: '/deployments',
requires: { required: [], optional: [] },
produces: [],
};
expect(computeDeploymentExtracts(op)).toEqual([]);
});
test('end-to-end: representative createDeployment leaves produce correct extracts', () => {
const op = makeOp([
{ semantic: 'DeploymentKey', fieldPath: 'deploymentKey', status: '200', provider: false },
{ semantic: 'TenantId', fieldPath: 'tenantId', status: '200', provider: false },
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].processDefinition.processDefinitionKey',
status: '200',
provider: true,
},
// .resource. path — should be skipped
{
semantic: 'ProcessDefinitionKey',
fieldPath: 'deployments[].resource.processDefinitionKey',
status: '200',
provider: true,
},
// non-provider nested — should be skipped
{
semantic: 'InternalId',
fieldPath: 'deployments[].internalId',
status: '200',
provider: false,
},
]);
const result = computeDeploymentExtracts(op);
const varNames = result.map((r) => r.varName);
expect(varNames).toContain('deploymentKeyVar');
expect(varNames).toContain('tenantIdVar');
expect(varNames).toContain('processDefinitionKeyVar');
expect(varNames).not.toContain('internalIdVar');
// resource. path deduplicated/skipped — only one processDefinitionKeyVar
expect(varNames.filter((n) => n === 'processDefinitionKeyVar')).toHaveLength(1);
// Output is sorted by varName
expect(varNames).toEqual([...varNames].sort());
});
});