-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathgraph.test.ts
More file actions
316 lines (299 loc) · 9.22 KB
/
Copy pathgraph.test.ts
File metadata and controls
316 lines (299 loc) · 9.22 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { expect, it } from "@effect/vitest";
import { Effect, Option } from "effect";
import {
parseEntry,
streamOperationBindingsFromStructure,
structuralSplit,
} from "@executor-js/plugin-openapi";
import { microsoftGraphKeepPathItem } from "./graph";
// Mirrors the verbatim shape of the real Microsoft Graph v1.0 spec: every
// success response uses the OpenAPI wildcard status key `2XX` (the real spec
// has zero numeric 200/201 keys), drive content GET is already declared as a
// binary octet-stream, PUT declares a binary octet-stream requestBody, error
// responses are $refs, and path-level shared parameters carry
// `x-ms-docs-key-type`.
const driveContentFixture = `
openapi: 3.0.4
info:
title: Microsoft Graph Fixture
version: v1.0
servers:
- url: https://graph.microsoft.com/v1.0
paths:
/drives/{drive-id}/items/{driveItem-id}/content:
get:
tags:
- drives.driveItem
summary: Get content for the navigation property items from drives
operationId: drives.GetItemsContent
parameters:
- name: $format
in: query
description: Format of the content
style: form
explode: false
schema:
type: string
responses:
2XX:
description: Retrieved media content
content:
application/octet-stream:
schema:
type: string
format: binary
4XX:
$ref: '#/components/responses/error'
5XX:
$ref: '#/components/responses/error'
put:
tags:
- drives.driveItem
summary: Update content for the navigation property items in drives
operationId: drives.UpdateItemsContent
requestBody:
description: New media content.
content:
application/octet-stream:
schema:
type: string
format: binary
required: true
responses:
2XX:
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/microsoft.graph.driveItem'
4XX:
$ref: '#/components/responses/error'
5XX:
$ref: '#/components/responses/error'
delete:
tags:
- drives.driveItem
summary: Delete content for the navigation property items in drives
operationId: drives.DeleteItemsContent
responses:
'204':
description: Success
4XX:
$ref: '#/components/responses/error'
5XX:
$ref: '#/components/responses/error'
parameters:
- name: drive-id
in: path
description: The unique identifier of drive
required: true
schema:
type: string
x-ms-docs-key-type: drive
- name: driveItem-id
in: path
description: The unique identifier of driveItem
required: true
schema:
type: string
x-ms-docs-key-type: driveItem
components:
schemas:
microsoft.graph.driveItem:
type: object
properties:
id:
type: string
microsoft.graph.ODataErrors.ODataError:
type: object
properties:
error:
type: object
responses:
error:
description: error
content:
application/json:
schema:
$ref: '#/components/schemas/microsoft.graph.ODataErrors.ODataError'
`;
// Report-style Graph endpoints declare an octet-stream success response with an
// object schema (a `value` wrapper) instead of a binary string.
const reportContentFixture = `
openapi: 3.0.4
info:
title: Microsoft Graph Fixture
version: v1.0
servers:
- url: https://graph.microsoft.com/v1.0
paths:
/reports/getEmailActivityCounts(period={period}):
get:
tags:
- reports.Functions
summary: Invoke function getEmailActivityCounts
operationId: reports.getEmailActivityCounts
parameters:
- name: period
in: path
required: true
schema:
type: string
responses:
2XX:
description: Success
content:
application/octet-stream:
schema:
type: object
properties:
value:
type: string
format: base64url
4XX:
$ref: '#/components/responses/error'
5XX:
$ref: '#/components/responses/error'
components:
schemas:
microsoft.graph.ODataErrors.ODataError:
type: object
properties:
error:
type: object
responses:
error:
description: error
content:
application/json:
schema:
$ref: '#/components/schemas/microsoft.graph.ODataErrors.ODataError'
`;
const fullGraphSelection = {
coversFullGraph: true,
presetIds: [],
customScopes: [],
exactPaths: [],
pathPrefixes: [],
tagPrefixes: [],
} as const;
const keptPathItem = (fixture: string): Record<string, unknown> => {
const structure = structuralSplit(fixture);
expect(structure).not.toBeNull();
const entry = parseEntry(structure!.text, structure!.pathItems[0]!, 2);
expect(entry).not.toBeNull();
const [path, rawPathItem] = entry!;
const pathItem = microsoftGraphKeepPathItem(fullGraphSelection)(
path,
rawPathItem as Record<string, unknown>,
);
expect(pathItem).not.toBeNull();
return pathItem as Record<string, unknown>;
};
type StreamedBinding = {
readonly binding: {
readonly method: string;
readonly pathTemplate: string;
readonly responseBody: Option.Option<{
readonly fileHint: Option.Option<{
readonly kind: "binaryResponse" | "byteField";
}>;
}>;
};
};
const streamBindings = (fixture: string) =>
Effect.gen(function* () {
const structure = structuralSplit(fixture);
expect(structure).not.toBeNull();
const chunks: StreamedBinding[] = [];
yield* streamOperationBindingsFromStructure(
structure!,
{ chunkSize: 10, keepPathItem: microsoftGraphKeepPathItem(fullGraphSelection) },
(chunk) =>
Effect.sync(() => {
chunks.push(...chunk);
}),
);
return chunks;
});
const responseFileHintKind = (
chunks: readonly StreamedBinding[],
method: string,
pathTemplate: string,
): string | undefined => {
const match = chunks.find(
(chunk) => chunk.binding.method === method && chunk.binding.pathTemplate === pathTemplate,
);
expect(match).toBeDefined();
const hint = Option.flatMap(match!.binding.responseBody, (body) => body.fileHint);
return Option.getOrUndefined(hint)?.kind;
};
it("keeps already-binary drive content responses untouched", () => {
const pathItem = keptPathItem(driveContentFixture);
const get = pathItem.get as Record<string, unknown>;
const getResponses = get.responses as Record<string, unknown>;
expect(getResponses["2XX"]).toEqual({
description: "Retrieved media content",
content: {
"application/octet-stream": {
schema: { type: "string", format: "binary" },
},
},
});
expect(getResponses["4XX"]).toEqual({ $ref: "#/components/responses/error" });
expect(getResponses["5XX"]).toEqual({ $ref: "#/components/responses/error" });
// The real spec already declares the PUT requestBody as binary; the
// normalization must not touch request bodies.
const put = pathItem.put as Record<string, unknown>;
expect(put.requestBody).toEqual({
description: "New media content.",
content: {
"application/octet-stream": {
schema: { type: "string", format: "binary" },
},
},
required: true,
});
const putResponses = put.responses as Record<string, unknown>;
expect(putResponses["2XX"]).toMatchObject({
content: {
"application/json": {
schema: { $ref: "#/components/schemas/microsoft.graph.driveItem" },
},
},
});
// Path-level shared parameters survive the filter.
expect(pathItem.parameters).toMatchObject([
{ name: "drive-id", "x-ms-docs-key-type": "drive" },
{ name: "driveItem-id", "x-ms-docs-key-type": "driveItem" },
]);
});
it("normalizes report-style octet-stream object schemas to binary strings", () => {
const pathItem = keptPathItem(reportContentFixture);
const get = pathItem.get as Record<string, unknown>;
const responses = get.responses as Record<string, unknown>;
const success = responses["2XX"] as Record<string, unknown>;
expect(success.description).toBe("Success");
expect(success.content).toEqual({
"application/octet-stream": {
schema: { type: "string", format: "binary" },
},
});
expect(responses["4XX"]).toEqual({ $ref: "#/components/responses/error" });
});
it.effect("streams drive content download bindings with a binaryResponse file hint", () =>
Effect.gen(function* () {
const chunks = yield* streamBindings(driveContentFixture);
expect(
responseFileHintKind(chunks, "get", "/drives/{drive-id}/items/{driveItem-id}/content"),
).toBe("binaryResponse");
}),
);
it.effect("streams report-style download bindings with a binaryResponse file hint", () =>
Effect.gen(function* () {
const chunks = yield* streamBindings(reportContentFixture);
expect(
responseFileHintKind(chunks, "get", "/reports/getEmailActivityCounts(period={period})"),
).toBe("binaryResponse");
}),
);